JavaFX/controller/RootLayout.java

351 lines
9.1 KiB
Java
Raw 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-12-19 17:09:03 +00:00
private VBox main_menu;
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-19 16:31:27 +00:00
setUserAgentStylesheet(STYLESHEET_CASPIAN);
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 */
this.articles = new Article(this.main_container, this, this.root_layout,this.root_stage);
this.dico = new Traduction(this.main_container, this, this.root_layout);
2017-12-14 13:50:00 +00:00
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-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;
if(p.getChildren().get(0).getId().toLowerCase().equals("all") ) {
p.getStyleClass().add("active");
}
2017-12-13 16:02:15 +00:00
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-19 16:09:52 +00:00
NewsListModel.getInstance().addObserver("MainClass", this);
NewsListModel.getInstance().setCategory(Category.all);
NewsListModel.getInstance().setSortType(SortTypes.relevancy);
TraductionModel.getInstance().addObserver("MainClass", this);
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
2017-12-19 17:09:03 +00:00
this.main_menu = (VBox) this.root_layout.lookup("#menu");
for (Node c : this.main_menu.getChildren()) {
2017-12-12 12:40:22 +00:00
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
this.articles.clearContent();
2017-12-14 13:50:00 +00:00
TextField tx = (TextField) this.root_layout.lookup("#mag_searchbar");
tx.setDisable(true);
if(NewsListModel.getInstance().getNews().size() != 0) {
2017-12-14 13:50:00 +00:00
try{
2017-12-14 13:50:00 +00:00
this.articles.addAllItem( NewsListModel.getInstance().getNews() );
2017-12-14 13:50:00 +00:00
}catch(Exception e1){
2017-12-14 13:50:00 +00:00
System.out.println("Cannot fetch article data");
e1.printStackTrace();
2017-12-14 13:50:00 +00:00
2017-11-19 13:44:40 +00:00
}
tx.setDisable(false);
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
TextField tx1 = (TextField) this.root_layout.lookup("#mag_searchbar");
2017-12-13 15:55:34 +00:00
tx1.setDisable(true);
2017-12-14 13:50:00 +00:00
if(TraductionModel.getInstance().getTranslations().size() != 0) {
try{
2017-12-14 13:50:00 +00:00
this.dico.addAll( TraductionModel.getInstance().getTranslations() );
2017-12-14 13:50:00 +00:00
}catch(Exception e1){
2017-12-14 13:50:00 +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-19 16:31:27 +00:00
this.main_container.getChildren().clear();
2017-12-19 17:09:03 +00:00
FXMLLoader loader;
/* Un-select all menu items but re-select the chosen one */
for( Node n : this.main_menu.getChildren() ){
n.getStyleClass().clear();
n.getStyleClass().add("menu_item");
if( n.getId() == layout )
n.getStyleClass().add("active");
}
2017-12-14 13:50:00 +00:00
2017-12-12 12:40:22 +00:00
switch(layout) {
case "dictionary" :
2017-12-19 17:09:03 +00:00
/* (1) Load the root_disp.fxml */
loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/vocabulary_disp.fxml"));
/* (2) Load the layout into the scene */
try {
this.main_container.getChildren().add((AnchorPane) loader.load());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2017-12-12 12:40:22 +00:00
break;
case "exercises" :
2017-12-19 16:31:27 +00:00
/* (1) Load the root_disp.fxml */
2017-12-19 17:09:03 +00:00
loader = new FXMLLoader();
2017-12-19 16:31:27 +00:00
2017-12-19 17:09:03 +00:00
loader.setLocation(getClass().getResource("/fxml/exercise_disp.fxml"));
2017-12-19 16:31:27 +00:00
/* (2) Load the layout into the scene */
2017-12-19 17:09:03 +00:00
try {
this.main_container.getChildren().add((AnchorPane) loader.load());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2017-12-12 12:40:22 +00:00
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<KeyEvent>() {
@Override
public void handle(KeyEvent arg0) {
if(arg0.getCode() == KeyCode.ENTER) {
TraductionModel.getInstance().query(tx1.textProperty().get());
}
}
});
RootLayout.this.main_container.getChildren().add(tx1);
2017-12-12 12:40:22 +00:00
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<KeyEvent>() {
@Override
public void handle(KeyEvent arg0) {
if(arg0.getCode() == KeyCode.ENTER) {
NewsListModel.getInstance().query(tx.textProperty().get());
}
}
});
this.main_container.getChildren().add(tx);
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
}