Ajout du systeme d'event + refactoring des items du menu

This commit is contained in:
MASCARO LUCAS 2017-11-14 09:09:25 +01:00
parent 61e97d3bbd
commit 0d2847170f
6 changed files with 87 additions and 92 deletions

22
Classes/Event.java Normal file
View File

@ -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;
}
}

8
Interfaces/Event.java Normal file
View File

@ -0,0 +1,8 @@
package Interfaces;
public interface Event {
public String getObjectId();
public String getEventType();
}

View File

@ -0,0 +1,7 @@
package Interfaces;
public interface EventObserver {
public void handleEvent(Event e);
}

View File

@ -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<HeaderMenuItem> items;
private FlowPane parent;
/* Constructor */
public HeaderMenu(){
public HeaderMenu(FlowPane p_parent){
this.parent = p_parent;
this.items = new ArrayList<HeaderMenuItem>();
}
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");
parent.getChildren().add(p_item);
}
}

View File

@ -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;
@ -28,20 +30,15 @@ public class RootLayout extends Application {
/* (2) Load the root layout*/
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");
}
}

View File

@ -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<String> 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<MouseEvent>() {
@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;
}
}