This commit is contained in:
xdrm-brackets 2017-12-14 14:50:00 +01:00
parent 9f0bc020ec
commit 4e5980c175
5 changed files with 133 additions and 134 deletions

View File

@ -28,127 +28,126 @@ 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 javafx.util.Pair;
import model.DictionaryModel; import model.TraductionModel;
import model.NewsListModel; import model.NewsListModel;
import model.NewsModel; import model.NewsModel;
import model.WordTraductionModel; import model.WordTraductionModel;
public class RootLayout extends Application implements EventObserver { public class RootLayout extends Application implements EventObserver {
private Stage root_stage; private Stage root_stage;
private Scene root_scene; private Scene root_scene;
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; private Traduction dico;
@Override @Override
public void start(Stage primary_stage) throws Exception { public void start(Stage primary_stage) throws Exception {
/* (1) store primary stage + title it */ /* (1) store primary stage + title it */
this.root_stage = primary_stage; this.root_stage = primary_stage;
this.root_stage.setTitle("Language learner pro plus 2000"); this.root_stage.setTitle("Language learner pro plus 2000");
/* (2) Load the root layout*/ /* (2) Load the root layout*/
this.loadRootLayout(); this.loadRootLayout();
/* (3) Load the CSS CONTEXT */ /* (3) Load the CSS CONTEXT */
ContextBuilder.createContext(); ContextBuilder.createContext();
/* (3) Store container */ /* (3) Store container */
this.main_container = (FlowPane) this.root_scene.lookup("#container"); this.main_container = (FlowPane) this.root_scene.lookup("#container");
/* (1) Create controllers' views /* (1) Create controllers' views
-------------------------------------*/ -------------------------------------*/
/* (1) Create header menu */ /* (1) Create header menu */
HeaderMenu hm = new HeaderMenu((FlowPane) this.root_scene.lookup("#header_menu"),this); HeaderMenu hm = new HeaderMenu((FlowPane) this.root_scene.lookup("#header_menu"),this);
hm.addItem("notification", "/src/header-notif.png"); hm.addItem("notification", "/src/header-notif.png");
hm.addItem("mail", "/src/header-mail.png"); hm.addItem("mail", "/src/header-mail.png");
hm.addItem("search", "/src/header-search.png"); hm.addItem("search", "/src/header-search.png");
hm.addItem("menu", "/src/header-menu.png"); hm.addItem("menu", "/src/header-menu.png");
/* (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 Traduction(this.main_container, this);
this.dico = new Dictionary(this.main_container, this);
/* (2) CSS /* (2) CSS
-------------------------------------*/ -------------------------------------*/
/* (1) #header */ /* (1) #header */
new HeaderStyleSheet( this.root_scene.lookup("#header") ); new HeaderStyleSheet( this.root_scene.lookup("#header") );
/* (2) #menu_container */ /* (2) #menu_container */
new MenuStyleSheet( this.root_scene.lookup("#menu") ); new MenuStyleSheet( this.root_scene.lookup("#menu") );
/* (3) #submenu */ /* (3) #submenu */
new SubMenuStyleSheet( this.root_scene.lookup("#submenu") ); new SubMenuStyleSheet( this.root_scene.lookup("#submenu") );
/* (4) #header_icon*/ /* (4) #header_icon*/
new HeaderIconStyleSheet( this.root_scene.lookup("#header_icon") ); new HeaderIconStyleSheet( this.root_scene.lookup("#header_icon") );
/* (4) #scroll_container*/ /* (4) #scroll_container*/
new ContainerScrollStyleSheet( this.root_scene.lookup("#scroll_container") ); new ContainerScrollStyleSheet( this.root_scene.lookup("#scroll_container") );
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); TraductionModel.getInstance().addObserver("MainClass", this);
VBox subMenuContainer = (VBox) this.root_layout.lookup("#submenu"); VBox subMenuContainer = (VBox) this.root_layout.lookup("#submenu");
for(Node n : subMenuContainer.getChildren()) { for(Node n : subMenuContainer.getChildren()) {
FlowPane p = (FlowPane) n; FlowPane p = (FlowPane) n;
p.setOnMousePressed(new EventHandler<MouseEvent>() { p.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override @Override
public void handle(MouseEvent event) { public void handle(MouseEvent event) {
// De-active all items before activating current one // De-active all items before activating current one
for( Node n2 : subMenuContainer.getChildren() ) for( Node n2 : subMenuContainer.getChildren() )
n2.getStyleClass().clear(); n2.getStyleClass().clear();
// Change category // Change category
NewsListModel.getInstance().setCategory(Category.valueOf(p.getChildren().get(0).getId().toLowerCase())); NewsListModel.getInstance().setCategory(Category.valueOf(p.getChildren().get(0).getId().toLowerCase()));
// Active gui category // Active gui category
p.getStyleClass().add("active"); p.getStyleClass().add("active");
// Re-launch request // Re-launch request
NewsListModel.getInstance().query(NewsListModel.getInstance().getQuery()); NewsListModel.getInstance().query(NewsListModel.getInstance().getQuery());
} }
}); });
} }
this.handleMainLayoutChange("magazines"); this.handleMainLayoutChange("magazines");
} }
public void loadRootLayout(){ public void loadRootLayout(){
try{ try{
/* (1) Load the root_disp.fxml */ /* (1) Load the root_disp.fxml */
FXMLLoader loader = new FXMLLoader(); FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/model.fxml")); loader.setLocation(getClass().getResource("/fxml/model.fxml"));
/* (2) Load the layout into the scene */ /* (2) Load the layout into the scene */
this.root_layout = (AnchorPane) loader.load(); this.root_layout = (AnchorPane) loader.load();
this.root_scene = new Scene(this.root_layout); this.root_scene = new Scene(this.root_layout);
/* (3) Add the scene to the stage */ /* (3) Add the scene to the stage */
this.root_stage.setScene(this.root_scene); this.root_stage.setScene(this.root_scene);
//bind events on click fot the menu //bind events on click fot the menu
VBox menuRoot = (VBox) this.root_layout.lookup("#menu"); VBox menuRoot = (VBox) this.root_layout.lookup("#menu");
for (Node c : menuRoot.getChildren()) { for (Node c : menuRoot.getChildren()) {
@ -159,36 +158,36 @@ public class RootLayout extends Application implements EventObserver {
} }
}); });
} }
/* (4) Show the stage */ /* (4) Show the stage */
this.root_stage.show(); this.root_stage.show();
}catch(IOException e){ }catch(IOException e){
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
} }
@Override @Override
public void handleEvent(Event e) { public void handleEvent(Event e) {
switch(e.getEventType()){ switch(e.getEventType()){
case "changeMainLayout": case "changeMainLayout":
this.handleMainLayoutChange(e.getObjectId()); this.handleMainLayoutChange(e.getObjectId());
break; break;
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.root_layout.lookup("#submenu").setVisible(true);
this.articles.clearContent(); this.articles.clearContent();
TextField tx = new TextField(); TextField tx = new TextField();
tx.setPromptText("search..."); tx.setPromptText("search...");
tx.setId("mag_searchbar"); tx.setId("mag_searchbar");
@ -200,61 +199,61 @@ public class RootLayout extends Application implements EventObserver {
if(arg0.getCode() == KeyCode.ENTER) { if(arg0.getCode() == KeyCode.ENTER) {
NewsListModel.getInstance().query(tx.textProperty().get()); NewsListModel.getInstance().query(tx.textProperty().get());
} }
} }
}); });
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@Override @Override
public void run() { public void run() {
RootLayout.this.main_container.getChildren().add(tx); RootLayout.this.main_container.getChildren().add(tx);
} }
}); });
if(NewsListModel.getInstance().getNews().size() != 0) { if(NewsListModel.getInstance().getNews().size() != 0) {
// For each news // For each news
for( NewsModel news : NewsListModel.getInstance().getNews() ){ for( NewsModel news : NewsListModel.getInstance().getNews() ){
try{ try{
this.articles.addItem( news ); this.articles.addItem( news );
}catch(Exception e1){ }catch(Exception e1){
System.out.println("Cannot fetch article data"); System.out.println("Cannot fetch article data");
e1.printStackTrace(); e1.printStackTrace();
} }
} }
} }
tx.setDisable(false); tx.setDisable(false);
break; break;
case "NewsQueryFailed": case "NewsQueryFailed":
System.out.println("une erreur est survenue"); System.out.println("une erreur est survenue");
break; break;
case "WordQueryFailed": case "WordQueryFailed":
System.out.println("une erreur est survenue"); System.out.println("une erreur est survenue");
break; break;
case "WordQuerySuccess": case "WordQuerySuccess":
System.out.println(DictionaryModel.getInstance().getTranslations().size()+" Traductions ont été trouvés"); System.out.println(TraductionModel.getInstance().getTranslations().size()+" Traductions ont été trouvés");
System.out.println(DictionaryModel.getInstance().getUsages().size()+" Exemples ont été trouvés"); System.out.println(TraductionModel.getInstance().getUsages().size()+" Exemples ont été trouvés");
this.dico.clearContent(); this.dico.clearContent();
this.root_layout.lookup("#submenu").setVisible(false); this.root_layout.lookup("#submenu").setVisible(false);
TextField tx1 = new TextField(); TextField tx1 = new TextField();
tx1.setPromptText("search..."); tx1.setPromptText("search...");
tx1.setId("mag_searchbar"); tx1.setId("mag_searchbar");
@ -264,71 +263,71 @@ public class RootLayout extends Application implements EventObserver {
@Override @Override
public void handle(KeyEvent arg0) { public void handle(KeyEvent arg0) {
if(arg0.getCode() == KeyCode.ENTER) { if(arg0.getCode() == KeyCode.ENTER) {
DictionaryModel.getInstance().query(tx1.textProperty().get()); TraductionModel.getInstance().query(tx1.textProperty().get());
} }
} }
}); });
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@Override @Override
public void run() { public void run() {
RootLayout.this.main_container.getChildren().add(tx1); RootLayout.this.main_container.getChildren().add(tx1);
} }
}); });
if(DictionaryModel.getInstance().getTranslations().size() != 0) { if(TraductionModel.getInstance().getTranslations().size() != 0) {
// For each news // For each news
for( Pair<WordTraductionModel, WordTraductionModel> news : DictionaryModel.getInstance().getTranslations() ){ for( Pair<WordTraductionModel, WordTraductionModel> news : TraductionModel.getInstance().getTranslations() ){
try{ try{
this.dico.addItem( news.getKey() ); this.dico.addItem( news.getKey() );
}catch(Exception e1){ }catch(Exception e1){
System.out.println("Cannot fetch dico data"); System.out.println("Cannot fetch dico data");
e1.printStackTrace(); e1.printStackTrace();
} }
} }
} }
tx1.setDisable(false); tx1.setDisable(false);
break; break;
case "changeMagCategory": case "changeMagCategory":
NewsListModel.getInstance().setCategory(Category.valueOf(e.getObjectId().toLowerCase())); NewsListModel.getInstance().setCategory(Category.valueOf(e.getObjectId().toLowerCase()));
break; break;
} }
} }
public void handleMainLayoutChange(String layout) { public void handleMainLayoutChange(String layout) {
FlowPane container = (FlowPane) this.root_layout.lookup("#container"); FlowPane container = (FlowPane) this.root_layout.lookup("#container");
container.getChildren().clear(); container.getChildren().clear();
switch(layout) { switch(layout) {
case "dictionary" : case "dictionary" :
DictionaryModel.getInstance().query(""); container.getChildren().add(new Text("dictionary"));
break; break;
case "exercises" : case "exercises" :
container.getChildren().add(new Text("exercises")); container.getChildren().add(new Text("exercises"));
break; break;
case "translator" : case "translator" :
container.getChildren().add(new Text("translator")); TraductionModel.getInstance().query("");
break; break;
case "magazines" : case "magazines" :
NewsListModel.getInstance().query(""); NewsListModel.getInstance().query("");
break; break;
} }
} }
} }

View File

@ -18,7 +18,7 @@ import javafx.scene.text.Text;
import model.NewsModel; import model.NewsModel;
import model.WordTraductionModel; import model.WordTraductionModel;
public class Dictionary{ public class Traduction{
/* Data */ /* Data */
private ArrayList<AnchorPane> items; private ArrayList<AnchorPane> items;
@ -27,7 +27,7 @@ public class Dictionary{
/* Constructor */ /* Constructor */
public Dictionary(FlowPane p_parent, EventObserver observer){ public Traduction(FlowPane p_parent, EventObserver observer){
this.parent = p_parent; this.parent = p_parent;
this.items = new ArrayList<AnchorPane>(); this.items = new ArrayList<AnchorPane>();
this.observer = observer; this.observer = observer;
@ -76,7 +76,7 @@ public class Dictionary{
/* (11) Add to parent (graphics) */ /* (11) Add to parent (graphics) */
Platform.runLater(new Runnable(){ Platform.runLater(new Runnable(){
public void run(){ public void run(){
Dictionary.this.parent.getChildren().add(item); Traduction.this.parent.getChildren().add(item);
} }
}); });
} }
@ -84,7 +84,7 @@ public class Dictionary{
public void clearContent() { public void clearContent() {
Platform.runLater(new Runnable(){ Platform.runLater(new Runnable(){
public void run(){ public void run(){
Dictionary.this.parent.getChildren().clear(); Traduction.this.parent.getChildren().clear();
} }
}); });
} }

View File

@ -69,7 +69,7 @@
<Image url="@../src/menu/dictionary.png" /> <Image url="@../src/menu/dictionary.png" />
</image> </image>
</ImageView> </ImageView>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Dictionnaire" /> <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Vocabulaire" />
</children> </children>
</FlowPane> </FlowPane>
<FlowPane id="exercises" alignment="CENTER" columnHalignment="CENTER" layoutX="10.0" layoutY="182.0" prefHeight="58.0" prefWidth="234.0" styleClass="menu_item"> <FlowPane id="exercises" alignment="CENTER" columnHalignment="CENTER" layoutX="10.0" layoutY="182.0" prefHeight="58.0" prefWidth="234.0" styleClass="menu_item">

View File

@ -3,36 +3,36 @@ package model;
import Classes.Languages; import Classes.Languages;
public class LangModel { public class LangModel {
private static LangModel instance; private static LangModel instance;
private Languages fromLang = Languages.fr; private Languages fromLang = Languages.fr;
private Languages toLang = Languages.en; private Languages toLang = Languages.en;
private LangModel() { private LangModel() {
} }
public static LangModel getInstance() { public static LangModel getInstance() {
if(LangModel.instance == null) { if(LangModel.instance == null) {
LangModel.instance = new LangModel(); LangModel.instance = new LangModel();
} }
return LangModel.instance; return LangModel.instance;
} }
public void setFromLang(Languages lang) { public void setFromLang(Languages lang) {
this.fromLang = lang; this.fromLang = lang;
} }
public Languages getFromLang() { public Languages getFromLang() {
return this.fromLang; return this.fromLang;
} }
public void setToLang(Languages lang) { public void setToLang(Languages lang) {
this.toLang = lang; this.toLang = lang;
} }
public Languages getToLang() { public Languages getToLang() {
return this.toLang; return this.toLang;
} }

View File

@ -13,23 +13,23 @@ import Interfaces.EventObserver;
import Interfaces.Observable; import Interfaces.Observable;
import javafx.util.Pair; import javafx.util.Pair;
public class DictionaryModel implements Observable { public class TraductionModel implements Observable {
private HashMap<String,EventObserver> observers; private HashMap<String,EventObserver> observers;
private static DictionaryModel instance; private static TraductionModel instance;
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> translations; private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> translations;
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> usages; private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> usages;
private DictionaryModel() { private TraductionModel() {
this.observers = new HashMap<String,EventObserver>(); this.observers = new HashMap<String,EventObserver>();
} }
public static DictionaryModel getInstance() { public static TraductionModel getInstance() {
if(DictionaryModel.instance == null) { if(TraductionModel.instance == null) {
DictionaryModel.instance = new DictionaryModel(); TraductionModel.instance = new TraductionModel();
} }
return DictionaryModel.instance; return TraductionModel.instance;
} }
public void query(String search) { public void query(String search) {
@ -64,36 +64,36 @@ public class DictionaryModel implements Observable {
try { try {
//on reset le contenu du modele //on reset le contenu du modele
DictionaryModel.this.translations = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>(); TraductionModel.this.translations = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
DictionaryModel.this.usages = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>(); TraductionModel.this.usages = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
if(response.has("Error")) { if(response.has("Error")) {
//pas de résultat //pas de résultat
DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed")); TraductionModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed"));
}else{ }else{
//Traitement des traductions principales //Traitement des traductions principales
DictionaryModel.this.translations.addAll( TraductionModel.this.translations.addAll(
DictionaryModel.this.computeJSON( TraductionModel.this.computeJSON(
response.getJSONObject("term0").getJSONObject("PrincipalTranslations") response.getJSONObject("term0").getJSONObject("PrincipalTranslations")
) )
); );
//Traitement des traductions secondaires //Traitement des traductions secondaires
if(response.getJSONObject("term0").has("AdditionalTranslations")) { if(response.getJSONObject("term0").has("AdditionalTranslations")) {
DictionaryModel.this.translations.addAll( TraductionModel.this.translations.addAll(
DictionaryModel.this.computeJSON( TraductionModel.this.computeJSON(
response.getJSONObject("term0").getJSONObject("AdditionalTranslations") response.getJSONObject("term0").getJSONObject("AdditionalTranslations")
) )
); );
} }
//traitement des usages //traitement des usages
DictionaryModel.this.usages.addAll( TraductionModel.this.usages.addAll(
DictionaryModel.this.computeJSON( TraductionModel.this.computeJSON(
response.getJSONObject("original").getJSONObject("Compounds") response.getJSONObject("original").getJSONObject("Compounds")
) )
); );
DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess")); TraductionModel.this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess"));
} }
@ -105,7 +105,7 @@ public class DictionaryModel implements Observable {
@Override @Override
public void onError() { public void onError() {
DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed")); TraductionModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed"));
} }
}); });