implement dictionary

This commit is contained in:
SeekDaSky 2017-12-13 16:55:34 +01:00
parent b08e6d1cb4
commit c0668ab41e
3 changed files with 260 additions and 30 deletions

153
controller/Dictionary.java Normal file
View File

@ -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<AnchorPane> items;
private FlowPane parent;
private EventObserver observer;
/* Constructor */
public Dictionary(FlowPane p_parent, EventObserver observer){
this.parent = p_parent;
this.items = new ArrayList<AnchorPane>();
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<MouseEvent>() {
// @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);
}
}

View File

@ -27,9 +27,11 @@ import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Pair;
import model.DictionaryModel; import model.DictionaryModel;
import model.NewsListModel; import model.NewsListModel;
import model.NewsModel; import model.NewsModel;
import model.WordTraductionModel;
public class RootLayout extends Application implements EventObserver { public class RootLayout extends Application implements EventObserver {
@ -38,6 +40,7 @@ public class RootLayout extends Application implements EventObserver {
private AnchorPane root_layout; private AnchorPane root_layout;
private FlowPane main_container; private FlowPane main_container;
private Article articles; private Article articles;
private Dictionary dico;
@Override @Override
@ -72,6 +75,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.dico = new Dictionary(this.main_container, this);
/* (2) CSS /* (2) CSS
-------------------------------------*/ -------------------------------------*/
@ -96,6 +101,8 @@ public class RootLayout extends Application implements EventObserver {
NewsListModel.getInstance().addObserver("MainClass", this); NewsListModel.getInstance().addObserver("MainClass", this);
NewsListModel.getInstance().setCategory(Category.all); NewsListModel.getInstance().setCategory(Category.all);
NewsListModel.getInstance().setSortType(SortTypes.relevancy); NewsListModel.getInstance().setSortType(SortTypes.relevancy);
DictionaryModel.getInstance().addObserver("MainClass", this);
this.handleMainLayoutChange("magazines"); this.handleMainLayoutChange("magazines");
} }
@ -208,9 +215,63 @@ public class RootLayout extends Application implements EventObserver {
System.out.println("une erreur est survenue"); System.out.println("une erreur est survenue");
break; break;
case "WordQueryFailed":
System.out.println("une erreur est survenue");
break;
case "WordQuerySuccess": case "WordQuerySuccess":
System.out.println(DictionaryModel.getInstance().getTranslations().size()+" Traductions ont été trouvés"); System.out.println(DictionaryModel.getInstance().getTranslations().size()+" Traductions ont été trouvés");
System.out.println(DictionaryModel.getInstance().getUsages().size()+" Exemples 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<KeyEvent>() {
@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<WordTraductionModel, WordTraductionModel> 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; break;
case "changeMagCategory": case "changeMagCategory":
@ -226,9 +287,7 @@ public class RootLayout extends Application implements EventObserver {
switch(layout) { switch(layout) {
case "dictionary" : case "dictionary" :
DictionaryModel.getInstance().addObserver("MainClass", this); DictionaryModel.getInstance().query("");
DictionaryModel.getInstance().query("maison");
container.getChildren().add(new Text("dictionnaire"));
break; break;
case "exercises" : case "exercises" :
container.getChildren().add(new Text("exercises")); container.getChildren().add(new Text("exercises"));

View File

@ -34,11 +34,19 @@ public class DictionaryModel implements Observable {
public void query(String search) { public void query(String search) {
//si recherche vide
if(search.isEmpty()) {
this.translations = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
this.usages = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess"));
return;
}
final String encodedQuery; final String encodedQuery;
try { try {
encodedQuery = java.net.URLEncoder.encode(search,"UTF-8"); encodedQuery = java.net.URLEncoder.encode(search,"UTF-8");
} catch (UnsupportedEncodingException e1) { } catch (UnsupportedEncodingException e1) {
this.notifyObservers(new Event("NewsModel","NewsQueryFailed")); this.notifyObservers(new Event("DictionaryModel","WordQueryFailed"));
return; return;
} }
@ -52,35 +60,45 @@ public class DictionaryModel implements Observable {
@Override @Override
public void onSuccess(JSONObject response) { public void onSuccess(JSONObject response) {
//on reset le contenu du modele
DictionaryModel.this.translations = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
DictionaryModel.this.usages = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
if(response.has("Error")) { try {
//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")
)
);
DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess")); //on reset le contenu du modele
DictionaryModel.this.translations = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
DictionaryModel.this.usages = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
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();
} }
} }