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.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import Classes.Category;
import Interfaces.EventObserver;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
@ -23,16 +26,44 @@ public class Article{
private ArrayList<AnchorPane> items;
private FlowPane parent;
private EventObserver observer;
private AnchorPane root;
public Boolean activated = false;
/* Constructor */
public Article(FlowPane p_parent, EventObserver observer){
public Article(FlowPane p_parent, EventObserver observer, AnchorPane root_layout){
this.parent = p_parent;
this.items = new ArrayList<AnchorPane>();
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 */
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 */
item.prefWidthProperty().bind(this.parent.widthProperty());
item.maxWidthProperty().bind(this.parent.widthProperty());
/* (11) Add to parent (graphics) */
Platform.runLater(new Runnable(){
public void run(){
Article.this.parent.getChildren().add(item);
}
});
return item;
}
public void clearContent() {
public synchronized void clearContent() {
this.items.clear();
Platform.runLater(new Runnable(){
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 */
this.articles = new Article(this.main_container, this);
this.dico = new Traduction(this.main_container, this);
this.articles = new Article(this.main_container, this, this.root_layout);
this.dico = new Traduction(this.main_container, this, this.root_layout);
/* (2) CSS
@ -107,6 +107,10 @@ public class RootLayout extends Application implements EventObserver {
for(Node n : subMenuContainer.getChildren()) {
FlowPane p = (FlowPane) n;
if(p.getChildren().get(0).getId().toLowerCase().equals("all") ) {
p.getStyleClass().add("active");
}
p.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
@ -185,44 +189,16 @@ public class RootLayout extends Application implements EventObserver {
case "NewsQuerySuccess":
System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé");
this.root_layout.lookup("#submenu").setVisible(true);
this.articles.clearContent();
TextField tx = new TextField();
tx.setPromptText("search...");
tx.setId("mag_searchbar");
TextField tx = (TextField) this.root_layout.lookup("#mag_searchbar");
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) {
// For each news
for( NewsModel news : NewsListModel.getInstance().getNews() ){
try{
this.articles.addItem( news );
this.articles.addAllItem( NewsListModel.getInstance().getNews() );
}catch(Exception e1){
@ -230,7 +206,7 @@ public class RootLayout extends Application implements EventObserver {
e1.printStackTrace();
}
}
tx.setDisable(false);
}
tx.setDisable(false);
@ -252,43 +228,14 @@ public class RootLayout extends Application implements EventObserver {
this.dico.clearContent();
this.root_layout.lookup("#submenu").setVisible(false);
TextField tx1 = new TextField();
tx1.setPromptText("search...");
tx1.setId("mag_searchbar");
TextField tx1 = (TextField) this.root_layout.lookup("#mag_searchbar");
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) {
// For each news
for( Pair<WordTraductionModel, WordTraductionModel> news : TraductionModel.getInstance().getTranslations() ){
try{
this.dico.addItem( news.getKey() );
this.dico.addAll( TraductionModel.getInstance().getTranslations() );
}catch(Exception e1){
@ -297,7 +244,6 @@ public class RootLayout extends Application implements EventObserver {
}
}
}
tx1.setDisable(false);
break;
@ -321,10 +267,49 @@ public class RootLayout extends Application implements EventObserver {
container.getChildren().add(new Text("exercises"));
break;
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;
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;
}

View File

@ -4,17 +4,21 @@ import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import Classes.Category;
import Interfaces.EventObserver;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.util.Pair;
import model.NewsModel;
import model.WordTraductionModel;
@ -24,16 +28,44 @@ public class Traduction{
private ArrayList<AnchorPane> items;
private FlowPane parent;
private EventObserver observer;
private AnchorPane root;
public Boolean activated = false;
/* Constructor */
public Traduction(FlowPane p_parent, EventObserver observer){
public Traduction(FlowPane p_parent, EventObserver observer, AnchorPane root_layout){
this.parent = p_parent;
this.items = new ArrayList<AnchorPane>();
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 */
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 */
item.prefWidthProperty().bind(this.parent.widthProperty());
item.maxWidthProperty().bind(this.parent.widthProperty());
/* (11) Add to parent (graphics) */
Platform.runLater(new Runnable(){
public void run(){
Traduction.this.parent.getChildren().add(item);
}
});
return item;
}
public void clearContent() {
this.items.clear();
Platform.runLater(new Runnable(){
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;
}
this.query = q;
final String encodedQuery;
try {
encodedQuery = java.net.URLEncoder.encode(q,"UTF-8");
@ -180,13 +182,20 @@ public class NewsListModel implements Observable{
news.setDate(new Date());
}
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
NewsListModel.this.removeObserver("newsApiCall");
NewsListModel.this.notifyObservers(new Event("NewsModel","NewsQuerySuccess"));
}else {
return;
}
}