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.stage.Stage;
import javafx.util.Pair;
import model.DictionaryModel;
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 Dictionary dico;
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();
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.dico = new Dictionary(this.main_container, this);
this.dico = new Traduction(this.main_container, this);
/* (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);
DictionaryModel.getInstance().addObserver("MainClass", this);
TraductionModel.getInstance().addObserver("MainClass", this);
VBox subMenuContainer = (VBox) this.root_layout.lookup("#submenu");
for(Node n : subMenuContainer.getChildren()) {
FlowPane p = (FlowPane) n;
p.setOnMousePressed(new EventHandler<MouseEvent>() {
@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()) {
@ -159,36 +158,36 @@ public class RootLayout extends Application implements EventObserver {
}
});
}
/* (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.root_layout.lookup("#submenu").setVisible(true);
this.articles.clearContent();
TextField tx = new TextField();
tx.setPromptText("search...");
tx.setId("mag_searchbar");
@ -200,61 +199,61 @@ public class RootLayout extends Application implements EventObserver {
if(arg0.getCode() == KeyCode.ENTER) {
NewsListModel.getInstance().query(tx.textProperty().get());
}
}
});
Platform.runLater(new Runnable() {
@Override
public void run() {
RootLayout.this.main_container.getChildren().add(tx);
}
});
if(NewsListModel.getInstance().getNews().size() != 0) {
// For each news
for( NewsModel news : NewsListModel.getInstance().getNews() ){
try{
this.articles.addItem( news );
}catch(Exception e1){
System.out.println("Cannot fetch article data");
e1.printStackTrace();
}
}
}
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(DictionaryModel.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().getTranslations().size()+" Traductions ont été trouvés");
System.out.println(TraductionModel.getInstance().getUsages().size()+" Exemples ont été trouvés");
this.dico.clearContent();
this.root_layout.lookup("#submenu").setVisible(false);
TextField tx1 = new TextField();
tx1.setPromptText("search...");
tx1.setId("mag_searchbar");
@ -264,71 +263,71 @@ public class RootLayout extends Application implements EventObserver {
@Override
public void handle(KeyEvent arg0) {
if(arg0.getCode() == KeyCode.ENTER) {
DictionaryModel.getInstance().query(tx1.textProperty().get());
TraductionModel.getInstance().query(tx1.textProperty().get());
}
}
});
Platform.runLater(new Runnable() {
@Override
public void run() {
RootLayout.this.main_container.getChildren().add(tx1);
}
});
if(DictionaryModel.getInstance().getTranslations().size() != 0) {
if(TraductionModel.getInstance().getTranslations().size() != 0) {
// For each news
for( Pair<WordTraductionModel, WordTraductionModel> news : DictionaryModel.getInstance().getTranslations() ){
for( Pair<WordTraductionModel, WordTraductionModel> news : TraductionModel.getInstance().getTranslations() ){
try{
this.dico.addItem( news.getKey() );
}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" :
DictionaryModel.getInstance().query("");
container.getChildren().add(new Text("dictionary"));
break;
case "exercises" :
container.getChildren().add(new Text("exercises"));
break;
case "translator" :
container.getChildren().add(new Text("translator"));
TraductionModel.getInstance().query("");
break;
case "magazines" :
NewsListModel.getInstance().query("");
break;
}
}
}

View File

@ -18,7 +18,7 @@ import javafx.scene.text.Text;
import model.NewsModel;
import model.WordTraductionModel;
public class Dictionary{
public class Traduction{
/* Data */
private ArrayList<AnchorPane> items;
@ -27,7 +27,7 @@ public class Dictionary{
/* Constructor */
public Dictionary(FlowPane p_parent, EventObserver observer){
public Traduction(FlowPane p_parent, EventObserver observer){
this.parent = p_parent;
this.items = new ArrayList<AnchorPane>();
this.observer = observer;
@ -76,7 +76,7 @@ public class Dictionary{
/* (11) Add to parent (graphics) */
Platform.runLater(new Runnable(){
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() {
Platform.runLater(new Runnable(){
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>
</ImageView>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Dictionnaire" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Vocabulaire" />
</children>
</FlowPane>
<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;
public class LangModel {
private static LangModel instance;
private Languages fromLang = Languages.fr;
private Languages toLang = Languages.en;
private LangModel() {
}
public static LangModel getInstance() {
if(LangModel.instance == null) {
LangModel.instance = new LangModel();
}
return LangModel.instance;
}
public void setFromLang(Languages lang) {
this.fromLang = lang;
}
public Languages getFromLang() {
return this.fromLang;
}
public void setToLang(Languages lang) {
this.toLang = lang;
}
public Languages getToLang() {
return this.toLang;
}

View File

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