JavaFX/controller/RootLayout.java

202 lines
5.0 KiB
Java
Raw Permalink Normal View History

2017-11-14 07:22:26 +00:00
package controller;
import java.io.IOException;
import java.util.ArrayList;
2017-11-14 07:22:26 +00:00
import Classes.Category;
import Classes.SortTypes;
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;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import model.NewsListModel;
2017-11-19 13:44:40 +00:00
import model.NewsModel;
2017-11-14 07:22:26 +00:00
public class RootLayout extends Application implements EventObserver {
2017-11-19 15:30:22 +00:00
public static void main(String[] args){ launch(args); }
/* (1) Attributes
---------------------------------------------------------*/
/* (1) Root elements */
2017-11-14 07:22:26 +00:00
private Stage root_stage;
private Scene root_scene;
private AnchorPane root_layout;
2017-11-19 15:30:22 +00:00
/* (2) Local elements */
private FlowPane main_container;
2017-11-19 13:44:40 +00:00
private Article articles;
2017-11-14 07:22:26 +00:00
2017-11-19 15:30:22 +00:00
/* (2) Builds the stage, scene, ...
*
* @primary_stage<Stage> The primary stage to create in
*
---------------------------------------------------------*/
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-11-19 15:30:22 +00:00
/* (1) Init. stage, scene and context
---------------------------------------------------------*/
2017-11-14 07:22:26 +00:00
/* (1) store primary stage + title it */
this.root_stage = primary_stage;
this.root_stage.setTitle("Inifiny Mail Client");
2017-11-19 15:30:22 +00:00
2017-11-14 07:22:26 +00:00
/* (2) Load the root layout*/
2017-11-19 15:30:22 +00:00
this.loadRootLayout();
2017-11-15 13:23:49 +00:00
/* (3) Load the CSS CONTEXT */
ContextBuilder.createContext();
2017-11-19 15:30:22 +00:00
/* (3) Store container */
this.main_container = (FlowPane) this.root_scene.lookup("#container");
2017-11-15 13:23:49 +00:00
2017-11-19 15:30:22 +00:00
/* (2) Manage static stylesheet
---------------------------------------------------------*/
2017-11-15 13:23:49 +00:00
/* (1) #header */
new HeaderStyleSheet( this.root_scene.lookup("#header") );
2017-11-19 15:30:22 +00:00
2017-11-15 13:23:49 +00:00
/* (2) #menu_container */
new MenuStyleSheet( this.root_scene.lookup("#menu") );
2017-11-19 15:30:22 +00:00
/* (3) #submenu */
new SubMenuStyleSheet( this.root_scene.lookup("#submenu") );
2017-11-19 15:30:22 +00:00
/* (4) #header_icon*/
new HeaderIconStyleSheet( this.root_scene.lookup("#header_icon") );
2017-11-19 15:30:22 +00:00
2017-11-21 07:03:58 +00:00
2017-11-19 15:30:22 +00:00
/* (3) Manage controllers
---------------------------------------------------------*/
/* (1) Create HeaderMenu */
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) Init. articles controller */
this.articles = new Article(this.main_container, this);
2017-11-14 07:22:26 +00:00
}
2017-11-19 15:30:22 +00:00
/* (3) Loads the root layout and init. the scene-stage-pane
*
*
---------------------------------------------------------*/
2017-11-14 07:22:26 +00:00
public void loadRootLayout(){
2017-11-19 15:30:22 +00:00
2017-11-14 07:22:26 +00:00
try{
2017-11-19 15:30:22 +00:00
2017-11-14 07:22:26 +00:00
/* (1) Load the root_disp.fxml */
FXMLLoader loader = new FXMLLoader();
2017-11-19 15:30:22 +00:00
2017-11-14 07:22:26 +00:00
loader.setLocation(getClass().getResource("/fxml/model.fxml"));
2017-11-19 15:30:22 +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-11-19 15:30:22 +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-11-19 15:30:22 +00:00
2017-11-14 07:22:26 +00:00
/* (4) Show the stage */
this.root_stage.show();
2017-11-19 15:30:22 +00:00
2017-11-14 07:22:26 +00:00
}catch(IOException e){
e.printStackTrace();
}
}
2017-11-19 15:30:22 +00:00
/* (4) Event dispatcher
*
* @e<Event> observed event received
*
---------------------------------------------------------*/
@Override
public void handleEvent(Event e) {
/*
HashMap<String,String> headers = new HashMap<String,String>();
headers.put("Referer", "http://www.wordreference.com");
2017-11-19 15:30:22 +00:00
ApiCall call = new ApiCall("http://api.wordreference.com/1/json/enfr/grin","GET",new Callback() {
@Override
public void onSuccess(JSONObject response) {
System.out.println(response.toString());
2017-11-19 15:30:22 +00:00
}
@Override
public void onError() {
System.out.println("APICall error");
2017-11-19 15:30:22 +00:00
}
2017-11-19 15:30:22 +00:00
});
call.addHeaders(headers);
call.send();*/
2017-11-19 15:30:22 +00:00
switch( e.getEventType() ){
/* (1) On HeaderMenu.item.click -> search sources */
case "changeMainLayout":
this.handleMainLayoutChange(e.getObjectId());
break;
2017-11-19 15:30:22 +00:00
/* (2) articles.query.success -> display articles */
case "NewsQuerySuccess":
2017-11-19 15:30:22 +00:00
2017-11-19 13:44:40 +00:00
for( NewsModel news : NewsListModel.getInstance().getNews() ){
2017-11-19 15:30:22 +00:00
2017-11-19 13:44:40 +00:00
try{
this.articles.addItem( news );
}catch(Exception e1){
System.out.println("Cannot fetch article data");
2017-11-28 07:26:58 +00:00
e1.printStackTrace();
2017-11-19 13:44:40 +00:00
}
2017-11-19 15:30:22 +00:00
}
2017-11-28 07:26:58 +00:00
this.articles.display();
break;
2017-11-19 15:30:22 +00:00
/* (3) articles.query.error -> display error ('no result') */
case "NewsQueryFailed":
System.out.println("une erreur est survenue");
break;
}
2017-11-19 15:30:22 +00:00
}
2017-11-19 15:30:22 +00:00
public void handleMainLayoutChange(String layout) {
2017-11-19 15:30:22 +00:00
NewsListModel.getInstance().addObserver("MainClass", this);
2017-11-21 07:03:58 +00:00
NewsListModel.getInstance().setCategory(Category.science);
NewsListModel.getInstance().setSortType(SortTypes.publishedAt);
NewsListModel.getInstance().query("bitcoin");
2017-11-19 15:30:22 +00:00
}
2017-11-14 07:22:26 +00:00
}