From c0668ab41eb3d085bb3172303a9159d600735937 Mon Sep 17 00:00:00 2001 From: SeekDaSky Date: Wed, 13 Dec 2017 16:55:34 +0100 Subject: [PATCH] implement dictionary --- controller/Dictionary.java | 153 +++++++++++++++++++++++++++++++++++++ controller/RootLayout.java | 65 +++++++++++++++- model/DictionaryModel.java | 72 ++++++++++------- 3 files changed, 260 insertions(+), 30 deletions(-) create mode 100644 controller/Dictionary.java diff --git a/controller/Dictionary.java b/controller/Dictionary.java new file mode 100644 index 0000000..517bb8a --- /dev/null +++ b/controller/Dictionary.java @@ -0,0 +1,153 @@ +package controller; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; + +import Classes.Category; +import Interfaces.EventObserver; +import javafx.application.Platform; +import javafx.fxml.FXMLLoader; +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 model.NewsModel; +import model.WordTraductionModel; + +public class Dictionary{ + + /* Data */ + private ArrayList items; + private FlowPane parent; + private EventObserver observer; + + + /* Constructor */ + public Dictionary(FlowPane p_parent, EventObserver observer){ + this.parent = p_parent; + this.items = new ArrayList(); + this.observer = observer; + } + + public void addItem(WordTraductionModel model) throws IOException { + + /* (1) Load the article_disp.fxml */ + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(getClass().getResource("/fxml/article_disp.fxml")); + + /* (2) Get the loaded item*/ + AnchorPane item = (AnchorPane) loader.load(); + + /* (3) Set content */ + this.gsetContent( model.getSense(), item ); + + /* (4) Set date */ + //this.gsetDate( model.getDate(), item ); + + /* (5) Set title */ + HBox headerContainer = (HBox) item.getChildren().get(3); + this.gsetTitle( model.getUsage(), headerContainer ); + + /* (6) Set tags */ + this.gsetTags( model.getPOS(), headerContainer ); + + /* (7) Set image */ + //this.gsetImage( model.getImageURL(), item ); + + /* (8) Bind event */ +// item.setOnMousePressed(new EventHandler() { +// @Override +// public void handle(MouseEvent event) { +// Article.this.observer.handleEvent(new Classes.Event(item.getId(),"changeMainLayout")); +// } +// }); + + /* (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(){ + Dictionary.this.parent.getChildren().add(item); + } + }); + } + + public void clearContent() { + Platform.runLater(new Runnable(){ + public void run(){ + Dictionary.this.parent.getChildren().clear(); + } + }); + } + + + + public void gsetContent(String p_content, AnchorPane p_parent){ + /* (1) Get node */ + Text g_content = (Text) p_parent.getChildren().get(1); + + /* (2) Update content */ + g_content.setText(p_content); + } + + + public void gsetDate(Date p_date, AnchorPane p_parent){ + /* (1) Get node */ + Text g_date = (Text) p_parent.getChildren().get(2); + + /* (2) Update content */ + g_date.setText(new SimpleDateFormat("dd-MM-yyyy HH:mm").format(p_date)); + + } + + + public void gsetTitle(String p_title, HBox p_parent){ + /* (1) Get node */ + Text g_title = (Text) p_parent.getChildren().get(0); + + /* (2) Update title */ + g_title.setText(p_title); + } + + + public void gsetImage(String p_uri, AnchorPane p_parent){ + /* (1) Get node */ + ImageView g_image = (ImageView) p_parent.getChildren().get(0); + + /* (2) Update title */ + try { + g_image.setImage(new Image(p_uri)); + }catch(Exception e) { + g_image.setImage(new Image("http://lorempicsum.com/futurama/255/200/2")); + } + } + + + public void gsetTags(String p_tags, HBox p_parent) throws IOException{ + + /* (1.1) Create the container */ + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(getClass().getResource("/fxml/article_tag_disp.fxml")); + + /* (1.2) Load the tag elements */ + FlowPane g_tag = (FlowPane) loader.load(); + Text g_tagText = (Text) g_tag.getChildren().get(0); + + /* (1.3) Update the tag name */ + g_tagText.setText(p_tags); + + /* (1.5) Ajout au parent*/ + p_parent.getChildren().add(g_tag); + + } + +} diff --git a/controller/RootLayout.java b/controller/RootLayout.java index 19e4497..4e92163 100644 --- a/controller/RootLayout.java +++ b/controller/RootLayout.java @@ -27,9 +27,11 @@ import javafx.scene.layout.FlowPane; import javafx.scene.layout.VBox; import javafx.scene.text.Text; import javafx.stage.Stage; +import javafx.util.Pair; import model.DictionaryModel; import model.NewsListModel; import model.NewsModel; +import model.WordTraductionModel; public class RootLayout extends Application implements EventObserver { @@ -38,6 +40,7 @@ public class RootLayout extends Application implements EventObserver { private AnchorPane root_layout; private FlowPane main_container; private Article articles; + private Dictionary dico; @Override @@ -72,6 +75,8 @@ public class RootLayout extends Application implements EventObserver { /* (2) Create container */ this.articles = new Article(this.main_container, this); + this.dico = new Dictionary(this.main_container, this); + /* (2) CSS -------------------------------------*/ @@ -96,6 +101,8 @@ public class RootLayout extends Application implements EventObserver { NewsListModel.getInstance().addObserver("MainClass", this); NewsListModel.getInstance().setCategory(Category.all); NewsListModel.getInstance().setSortType(SortTypes.relevancy); + + DictionaryModel.getInstance().addObserver("MainClass", this); this.handleMainLayoutChange("magazines"); } @@ -208,9 +215,63 @@ public class RootLayout extends Application implements EventObserver { System.out.println("une erreur est survenue"); break; + case "WordQueryFailed": + System.out.println("une erreur est survenue"); + break; + case "WordQuerySuccess": + System.out.println(DictionaryModel.getInstance().getTranslations().size()+" Traductions ont été trouvés"); System.out.println(DictionaryModel.getInstance().getUsages().size()+" Exemples ont été trouvés"); + + this.dico.clearContent(); + + TextField tx1 = new TextField(); + tx1.setPromptText("search..."); + tx1.setId("mag_searchbar"); + tx1.setDisable(true); + tx1.setOnKeyPressed(new EventHandler() { + + @Override + public void handle(KeyEvent arg0) { + if(arg0.getCode() == KeyCode.ENTER) { + DictionaryModel.getInstance().query(tx1.textProperty().get()); + } + + } + + }); + + Platform.runLater(new Runnable() { + + @Override + public void run() { + RootLayout.this.main_container.getChildren().add(tx1); + + } + + }); + + + if(DictionaryModel.getInstance().getTranslations().size() != 0) { + + // For each news + for( Pair news : DictionaryModel.getInstance().getTranslations() ){ + + try{ + + this.dico.addItem( news.getKey() ); + + }catch(Exception e1){ + + System.out.println("Cannot fetch dico data"); + e1.printStackTrace(); + + } + } + } + + tx1.setDisable(false); break; case "changeMagCategory": @@ -226,9 +287,7 @@ public class RootLayout extends Application implements EventObserver { switch(layout) { case "dictionary" : - DictionaryModel.getInstance().addObserver("MainClass", this); - DictionaryModel.getInstance().query("maison"); - container.getChildren().add(new Text("dictionnaire")); + DictionaryModel.getInstance().query(""); break; case "exercises" : container.getChildren().add(new Text("exercises")); diff --git a/model/DictionaryModel.java b/model/DictionaryModel.java index 8c7804e..4d3d6b4 100644 --- a/model/DictionaryModel.java +++ b/model/DictionaryModel.java @@ -34,11 +34,19 @@ public class DictionaryModel implements Observable { public void query(String search) { + //si recherche vide + if(search.isEmpty()) { + this.translations = new ArrayList>(); + this.usages = new ArrayList>(); + this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess")); + return; + } + final String encodedQuery; try { encodedQuery = java.net.URLEncoder.encode(search,"UTF-8"); } catch (UnsupportedEncodingException e1) { - this.notifyObservers(new Event("NewsModel","NewsQueryFailed")); + this.notifyObservers(new Event("DictionaryModel","WordQueryFailed")); return; } @@ -52,35 +60,45 @@ public class DictionaryModel implements Observable { @Override public void onSuccess(JSONObject response) { - //on reset le contenu du modele - DictionaryModel.this.translations = new ArrayList>(); - DictionaryModel.this.usages = new ArrayList>(); - if(response.has("Error")) { - //pas de résultat - DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQueryNoResults")); - }else{ - //Traitement des traductions principales - DictionaryModel.this.translations.addAll( - DictionaryModel.this.computeJSON( - response.getJSONObject("term0").getJSONObject("PrincipalTranslations") - ) - ); - //Traitement des traductions secondaires - DictionaryModel.this.translations.addAll( - DictionaryModel.this.computeJSON( - response.getJSONObject("term0").getJSONObject("AdditionalTranslations") - ) - ); - //traitement des usages - DictionaryModel.this.usages.addAll( - DictionaryModel.this.computeJSON( - response.getJSONObject("original").getJSONObject("Compounds") - ) - ); + try { - DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess")); + //on reset le contenu du modele + DictionaryModel.this.translations = new ArrayList>(); + DictionaryModel.this.usages = new ArrayList>(); + if(response.has("Error")) { + //pas de résultat + DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed")); + }else{ + //Traitement des traductions principales + DictionaryModel.this.translations.addAll( + DictionaryModel.this.computeJSON( + response.getJSONObject("term0").getJSONObject("PrincipalTranslations") + ) + ); + //Traitement des traductions secondaires + if(response.getJSONObject("term0").has("AdditionalTranslations")) { + DictionaryModel.this.translations.addAll( + DictionaryModel.this.computeJSON( + response.getJSONObject("term0").getJSONObject("AdditionalTranslations") + ) + ); + } + + //traitement des usages + DictionaryModel.this.usages.addAll( + DictionaryModel.this.computeJSON( + response.getJSONObject("original").getJSONObject("Compounds") + ) + ); + + DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess")); + + } + + }catch(Exception e) { + e.printStackTrace(); } }