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