JavaFX/controller/RootLayout.java

334 lines
8.2 KiB
Java
Raw Permalink Normal View History

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