From 0d2847170fb41a6b12eb18b7ca95fa15cf528da0 Mon Sep 17 00:00:00 2001 From: MASCARO LUCAS Date: Tue, 14 Nov 2017 09:09:25 +0100 Subject: [PATCH] Ajout du systeme d'event + refactoring des items du menu --- Classes/Event.java | 22 ++++++++++++ Interfaces/Event.java | 8 +++++ Interfaces/EventObserver.java | 7 ++++ controller/HeaderMenu.java | 63 +++-------------------------------- controller/RootLayout.java | 38 ++++++++++----------- model/HeaderMenuItem.java | 41 +++++++++++++++-------- 6 files changed, 87 insertions(+), 92 deletions(-) create mode 100644 Classes/Event.java create mode 100644 Interfaces/Event.java create mode 100644 Interfaces/EventObserver.java diff --git a/Classes/Event.java b/Classes/Event.java new file mode 100644 index 0000000..b716fc6 --- /dev/null +++ b/Classes/Event.java @@ -0,0 +1,22 @@ +package Classes; + +public class Event implements Interfaces.Event { + + private String objectId; + private String eventType; + + public Event(String objectId, String eventType) { + this.objectId = objectId; + this.eventType = eventType; + + } + + public String getObjectId() { + return objectId; + } + + public String getEventType() { + return eventType; + } + +} diff --git a/Interfaces/Event.java b/Interfaces/Event.java new file mode 100644 index 0000000..db910ce --- /dev/null +++ b/Interfaces/Event.java @@ -0,0 +1,8 @@ +package Interfaces; + +public interface Event { + + public String getObjectId(); + + public String getEventType(); +} diff --git a/Interfaces/EventObserver.java b/Interfaces/EventObserver.java new file mode 100644 index 0000000..a6b599d --- /dev/null +++ b/Interfaces/EventObserver.java @@ -0,0 +1,7 @@ +package Interfaces; + +public interface EventObserver { + + public void handleEvent(Event e); + +} diff --git a/controller/HeaderMenu.java b/controller/HeaderMenu.java index 2910346..07f6ccf 100644 --- a/controller/HeaderMenu.java +++ b/controller/HeaderMenu.java @@ -2,10 +2,6 @@ package controller; import java.util.ArrayList; -import javafx.event.ActionEvent; -import javafx.scene.image.Image; -import javafx.scene.image.ImageView; -import javafx.scene.input.MouseEvent; import javafx.scene.layout.FlowPane; import model.HeaderMenuItem; @@ -13,69 +9,20 @@ public class HeaderMenu{ /* Data */ private ArrayList items; + private FlowPane parent; /* Constructor */ - public HeaderMenu(){ + public HeaderMenu(FlowPane p_parent){ + this.parent = p_parent; this.items = new ArrayList(); } public void addItem(HeaderMenuItem p_item) { this.items.add(p_item); - } - - - - - /* Content Builder */ - public void render(FlowPane p_parent) { - - /* (1) Initialize variables */ - int i, il; - - /* (2) Create each item */ - for( i = 0, il = this.items.size() ; i < il ; i++ ) { - - // Create ImageView - ImageView v_view = new ImageView(); - - // Get image - Image v_img = new Image( this.items.get(i).getImageURI() ); - - // Set image on button - v_view.setImage(v_img); - - // Set default props. - v_view.setId("header_menu_item_"+this.items.get(i).getName()); - v_view.getStyleClass().add("header_menu_item"); - - // HOVER - v_view.addEventHandler(MouseEvent.MOUSE_MOVED, e -> { - System.out.println("hover"); - ImageView view = (ImageView) e.getTarget(); - System.out.println( view.getId() ); - e.consume(); - }); - - // CLICK - v_view.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> { - System.out.println("clicked"); - ImageView view = (ImageView) e.getTarget(); - e.consume(); - }); - - // Add to parent - p_parent.getChildren().add(v_view); - - } - - } - - - - public void onClick(ActionEvent p_event){ - System.out.println("You clicked the button"); + // Add to parent + parent.getChildren().add(p_item); } } diff --git a/controller/RootLayout.java b/controller/RootLayout.java index dce7b44..459d539 100644 --- a/controller/RootLayout.java +++ b/controller/RootLayout.java @@ -2,6 +2,8 @@ package controller; import java.io.IOException; +import Interfaces.Event; +import Interfaces.EventObserver; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -10,7 +12,7 @@ import javafx.scene.layout.FlowPane; import javafx.stage.Stage; import model.HeaderMenuItem; -public class RootLayout extends Application { +public class RootLayout extends Application implements EventObserver { private Stage root_stage; private Scene root_scene; @@ -26,22 +28,17 @@ public class RootLayout extends Application { this.root_stage.setTitle("Inifiny Mail Client"); /* (2) Load the root layout*/ - this.loadRootLayout(); - - - + this.loadRootLayout(); /* (1) HEADER -------------------------------------*/ /* (1) Create header menu */ - HeaderMenu hm = new HeaderMenu(); + HeaderMenu hm = new HeaderMenu((FlowPane) this.root_scene.lookup("#header_menu")); - hm.addItem( new HeaderMenuItem("notification", "/src/header-notif.png") ); - hm.addItem( new HeaderMenuItem("mail", "/src/header-mail.png") ); - hm.addItem( new HeaderMenuItem("search", "/src/header-search.png") ); - hm.addItem( new HeaderMenuItem("menu", "/src/header-menu.png") ); - - hm.render( (FlowPane) this.root_scene.lookup("#header_menu") ); + hm.addItem( new HeaderMenuItem("notification", "/src/header-notif.png",this) ); + hm.addItem( new HeaderMenuItem("mail", "/src/header-mail.png",this) ); + hm.addItem( new HeaderMenuItem("search", "/src/header-search.png",this) ); + hm.addItem( new HeaderMenuItem("menu", "/src/header-menu.png",this) ); } @@ -75,15 +72,16 @@ public class RootLayout extends Application { } - - - - - - - - public static void main(String[] args) { launch(args); } + + + + + @Override + public void handleEvent(Event e) { + System.out.println("Object "+e.getObjectId()+" fired a "+e.getEventType()+" event"); + + } } diff --git a/model/HeaderMenuItem.java b/model/HeaderMenuItem.java index fdaebeb..93b10a1 100644 --- a/model/HeaderMenuItem.java +++ b/model/HeaderMenuItem.java @@ -1,12 +1,16 @@ package model; -import javafx.beans.property.SimpleStringProperty; -import javafx.beans.property.StringProperty; +import Interfaces.Event; +import Interfaces.EventObserver; +import javafx.event.EventHandler; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.input.MouseEvent; -public class HeaderMenuItem{ +public class HeaderMenuItem extends ImageView{ - private final StringProperty name; - private final String image_uri; + private final String name; + private EventObserver parentHandler; /* Constructor @@ -15,19 +19,28 @@ public class HeaderMenuItem{ * @param p_image_uri The URI of the image * */ - public HeaderMenuItem(String p_name, String p_image_uri){ - this.name = new SimpleStringProperty(p_name); - this.image_uri = p_image_uri; + public HeaderMenuItem(String p_name, String p_image_uri, EventObserver parent){ + this.name = p_name; + this.parentHandler = parent; + this.setImage(new Image(p_image_uri)); + this.setId("header_menu_item_"+p_name); + this.getStyleClass().add("header_menu_item"); + + this.setOnMousePressed(new EventHandler() { + @Override + public void handle(MouseEvent event) { + HeaderMenuItem.this.sendEvent(new Classes.Event(HeaderMenuItem.this.getId(),"click")); + } + }); + } + public void sendEvent(Event e) { + this.parentHandler.handleEvent(e); + } - - public StringProperty getName(){ + public String getName(){ return this.name; } - - public String getImageURI(){ - return this.image_uri; - } }