implémentation du loader de contenu + bugfix

This commit is contained in:
SeekDaSky 2017-12-19 17:06:15 +01:00
parent 4e5980c175
commit 7e2f272e27
4 changed files with 162 additions and 107 deletions

View File

@ -4,11 +4,14 @@ import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator;
import Classes.Category; import Classes.Category;
import Interfaces.EventObserver; import Interfaces.EventObserver;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
@ -23,16 +26,44 @@ public class Article{
private ArrayList<AnchorPane> items; private ArrayList<AnchorPane> items;
private FlowPane parent; private FlowPane parent;
private EventObserver observer; private EventObserver observer;
private AnchorPane root;
public Boolean activated = false;
/* Constructor */ /* Constructor */
public Article(FlowPane p_parent, EventObserver observer){ public Article(FlowPane p_parent, EventObserver observer, AnchorPane root_layout){
this.parent = p_parent; this.parent = p_parent;
this.items = new ArrayList<AnchorPane>(); this.items = new ArrayList<AnchorPane>();
this.observer = observer; this.observer = observer;
this.root = root_layout;
} }
public void addItem(NewsModel model) throws IOException { public synchronized void addAllItem(ArrayList<NewsModel> models) throws IOException {
/* (9) Add to the controller local */
TextField tx = (TextField) this.root.lookup("#mag_searchbar");
int i = 0;
for(NewsModel news : models) {
tx.textProperty().set("Chargement... "+i+"/"+models.size());
this.items.add(this.buildItem(news));
i++;
}
/* (11) Add to parent (graphics) */
Platform.runLater(new Runnable(){
public void run(){
if(Article.this.activated) {
Article.this.parent.getChildren().addAll(Article.this.items);
tx.textProperty().set("");
}
}
});
}
public AnchorPane buildItem(NewsModel model) throws IOException {
/* (1) Load the article_disp.fxml */ /* (1) Load the article_disp.fxml */
FXMLLoader loader = new FXMLLoader(); FXMLLoader loader = new FXMLLoader();
@ -65,25 +96,24 @@ public class Article{
// } // }
// }); // });
/* (9) Add to the controller local */
this.items.add(item);
/* (10) On bind au width du parent */ /* (10) On bind au width du parent */
item.prefWidthProperty().bind(this.parent.widthProperty()); item.prefWidthProperty().bind(this.parent.widthProperty());
item.maxWidthProperty().bind(this.parent.widthProperty()); item.maxWidthProperty().bind(this.parent.widthProperty());
/* (11) Add to parent (graphics) */ return item;
Platform.runLater(new Runnable(){
public void run(){
Article.this.parent.getChildren().add(item);
}
});
} }
public void clearContent() { public synchronized void clearContent() {
this.items.clear();
Platform.runLater(new Runnable(){ Platform.runLater(new Runnable(){
public void run(){ public void run(){
Article.this.parent.getChildren().clear(); Iterator<Node> it = Article.this.parent.getChildren().iterator();
while(it.hasNext()) {
Node n = it.next();
if(n.getId() != "mag_searchbar") {
it.remove();
}
}
} }
}); });
} }

View File

@ -73,8 +73,8 @@ public class RootLayout extends Application implements EventObserver {
/* (2) Create container */ /* (2) Create container */
this.articles = new Article(this.main_container, this); this.articles = new Article(this.main_container, this, this.root_layout);
this.dico = new Traduction(this.main_container, this); this.dico = new Traduction(this.main_container, this, this.root_layout);
/* (2) CSS /* (2) CSS
@ -107,6 +107,10 @@ public class RootLayout extends Application implements EventObserver {
for(Node n : subMenuContainer.getChildren()) { for(Node n : subMenuContainer.getChildren()) {
FlowPane p = (FlowPane) n; FlowPane p = (FlowPane) n;
if(p.getChildren().get(0).getId().toLowerCase().equals("all") ) {
p.getStyleClass().add("active");
}
p.setOnMousePressed(new EventHandler<MouseEvent>() { p.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override @Override
public void handle(MouseEvent event) { public void handle(MouseEvent event) {
@ -185,52 +189,24 @@ public class RootLayout extends Application implements EventObserver {
case "NewsQuerySuccess": case "NewsQuerySuccess":
System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé"); System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé");
this.root_layout.lookup("#submenu").setVisible(true);
this.articles.clearContent(); this.articles.clearContent();
TextField tx = new TextField(); TextField tx = (TextField) this.root_layout.lookup("#mag_searchbar");
tx.setPromptText("search...");
tx.setId("mag_searchbar");
tx.setDisable(true); tx.setDisable(true);
tx.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent arg0) {
if(arg0.getCode() == KeyCode.ENTER) {
NewsListModel.getInstance().query(tx.textProperty().get());
}
}
});
Platform.runLater(new Runnable() {
@Override
public void run() {
RootLayout.this.main_container.getChildren().add(tx);
}
});
if(NewsListModel.getInstance().getNews().size() != 0) { if(NewsListModel.getInstance().getNews().size() != 0) {
// For each news try{
for( NewsModel news : NewsListModel.getInstance().getNews() ){
try{ this.articles.addAllItem( NewsListModel.getInstance().getNews() );
this.articles.addItem( news ); }catch(Exception e1){
}catch(Exception e1){ System.out.println("Cannot fetch article data");
e1.printStackTrace();
System.out.println("Cannot fetch article data");
e1.printStackTrace();
}
} }
tx.setDisable(false);
} }
tx.setDisable(false); tx.setDisable(false);
@ -252,52 +228,22 @@ public class RootLayout extends Application implements EventObserver {
this.dico.clearContent(); this.dico.clearContent();
this.root_layout.lookup("#submenu").setVisible(false); TextField tx1 = (TextField) this.root_layout.lookup("#mag_searchbar");
TextField tx1 = new TextField();
tx1.setPromptText("search...");
tx1.setId("mag_searchbar");
tx1.setDisable(true); tx1.setDisable(true);
tx1.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent arg0) {
if(arg0.getCode() == KeyCode.ENTER) {
TraductionModel.getInstance().query(tx1.textProperty().get());
}
}
});
Platform.runLater(new Runnable() {
@Override
public void run() {
RootLayout.this.main_container.getChildren().add(tx1);
}
});
if(TraductionModel.getInstance().getTranslations().size() != 0) { if(TraductionModel.getInstance().getTranslations().size() != 0) {
// For each news try{
for( Pair<WordTraductionModel, WordTraductionModel> news : TraductionModel.getInstance().getTranslations() ){
try{ this.dico.addAll( TraductionModel.getInstance().getTranslations() );
this.dico.addItem( news.getKey() ); }catch(Exception e1){
}catch(Exception e1){ System.out.println("Cannot fetch dico data");
e1.printStackTrace();
System.out.println("Cannot fetch dico data");
e1.printStackTrace();
}
} }
} }
tx1.setDisable(false); tx1.setDisable(false);
break; break;
@ -321,10 +267,49 @@ public class RootLayout extends Application implements EventObserver {
container.getChildren().add(new Text("exercises")); container.getChildren().add(new Text("exercises"));
break; break;
case "translator" : case "translator" :
TraductionModel.getInstance().query(""); this.root_layout.lookup("#submenu").setVisible(false);
this.articles.activated = false;
this.dico.activated = true;
TextField tx1 = new TextField();
tx1.setPromptText("search...");
tx1.setId("mag_searchbar");
tx1.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent arg0) {
if(arg0.getCode() == KeyCode.ENTER) {
TraductionModel.getInstance().query(tx1.textProperty().get());
}
}
});
RootLayout.this.main_container.getChildren().add(tx1);
break; break;
case "magazines" : case "magazines" :
NewsListModel.getInstance().query(""); this.root_layout.lookup("#submenu").setVisible(true);
this.articles.activated = true;
this.dico.activated = false;
TextField tx = new TextField();
tx.setPromptText("search...");
tx.setId("mag_searchbar");
tx.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent arg0) {
if(arg0.getCode() == KeyCode.ENTER) {
NewsListModel.getInstance().query(tx.textProperty().get());
}
}
});
this.main_container.getChildren().add(tx);
break; break;
} }

View File

@ -4,17 +4,21 @@ import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator;
import Classes.Category; import Classes.Category;
import Interfaces.EventObserver; import Interfaces.EventObserver;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane; import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import javafx.util.Pair;
import model.NewsModel; import model.NewsModel;
import model.WordTraductionModel; import model.WordTraductionModel;
@ -24,16 +28,44 @@ public class Traduction{
private ArrayList<AnchorPane> items; private ArrayList<AnchorPane> items;
private FlowPane parent; private FlowPane parent;
private EventObserver observer; private EventObserver observer;
private AnchorPane root;
public Boolean activated = false;
/* Constructor */ /* Constructor */
public Traduction(FlowPane p_parent, EventObserver observer){ public Traduction(FlowPane p_parent, EventObserver observer, AnchorPane root_layout){
this.parent = p_parent; this.parent = p_parent;
this.items = new ArrayList<AnchorPane>(); this.items = new ArrayList<AnchorPane>();
this.observer = observer; this.observer = observer;
this.root = root_layout;
} }
public void addItem(WordTraductionModel model) throws IOException { public synchronized void addAll(ArrayList<Pair<WordTraductionModel, WordTraductionModel>> models) throws IOException {
/* (9) Add to the controller local */
TextField tx = (TextField) this.root.lookup("#mag_searchbar");
int i = 0;
for(Pair<WordTraductionModel, WordTraductionModel> news : models) {
tx.textProperty().set("Chargement... "+i+"/"+models.size());
this.items.add(this.buildItem(news.getKey()));
i++;
}
/* (11) Add to parent (graphics) */
Platform.runLater(new Runnable(){
public void run(){
if(Traduction.this.activated) {
Traduction.this.parent.getChildren().addAll(Traduction.this.items);
tx.textProperty().set("");
}
}
});
}
public AnchorPane buildItem(WordTraductionModel model) throws IOException {
/* (1) Load the article_disp.fxml */ /* (1) Load the article_disp.fxml */
FXMLLoader loader = new FXMLLoader(); FXMLLoader loader = new FXMLLoader();
@ -66,25 +98,24 @@ public class Traduction{
// } // }
// }); // });
/* (9) Add to the controller local */
this.items.add(item);
/* (10) On bind au width du parent */ /* (10) On bind au width du parent */
item.prefWidthProperty().bind(this.parent.widthProperty()); item.prefWidthProperty().bind(this.parent.widthProperty());
item.maxWidthProperty().bind(this.parent.widthProperty()); item.maxWidthProperty().bind(this.parent.widthProperty());
/* (11) Add to parent (graphics) */ return item;
Platform.runLater(new Runnable(){
public void run(){
Traduction.this.parent.getChildren().add(item);
}
});
} }
public void clearContent() { public void clearContent() {
this.items.clear();
Platform.runLater(new Runnable(){ Platform.runLater(new Runnable(){
public void run(){ public void run(){
Traduction.this.parent.getChildren().clear(); Iterator<Node> it = Traduction.this.parent.getChildren().iterator();
while(it.hasNext()) {
Node n = it.next();
if(n.getId() != "mag_searchbar") {
it.remove();
}
}
} }
}); });
} }

View File

@ -133,6 +133,8 @@ public class NewsListModel implements Observable{
return; return;
} }
this.query = q;
final String encodedQuery; final String encodedQuery;
try { try {
encodedQuery = java.net.URLEncoder.encode(q,"UTF-8"); encodedQuery = java.net.URLEncoder.encode(q,"UTF-8");
@ -180,13 +182,20 @@ public class NewsListModel implements Observable{
news.setDate(new Date()); news.setDate(new Date());
} }
NewsListModel.this.news.add(news); if(NewsListModel.this.query == q) {
NewsListModel.this.news.add(news);
}else {
return;
}
} }
NewsListModel.this.query = q; if(NewsListModel.this.query == q) {
//ne pas oublier d'enlever l'api des observer, sinon il y a des risques de récurrence //ne pas oublier d'enlever l'api des observer, sinon il y a des risques de récurrence
NewsListModel.this.removeObserver("newsApiCall"); NewsListModel.this.removeObserver("newsApiCall");
NewsListModel.this.notifyObservers(new Event("NewsModel","NewsQuerySuccess")); NewsListModel.this.notifyObservers(new Event("NewsModel","NewsQuerySuccess"));
}else {
return;
}
} }