JavaFX/controller/HeaderMenu.java

75 lines
1.8 KiB
Java
Raw Normal View History

2017-11-14 07:22:26 +00:00
package controller;
import java.util.ArrayList;
import Interfaces.EventObserver;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
2017-11-14 07:22:26 +00:00
import javafx.scene.layout.FlowPane;
public class HeaderMenu{
2017-11-19 15:30:22 +00:00
/* (1) Attributes
---------------------------------------------------------*/
private ArrayList<ImageView> items;
2017-11-19 15:30:22 +00:00
private FlowPane parent;
private EventObserver observer;
2017-11-14 07:22:26 +00:00
/* Constructor */
2017-11-19 15:30:22 +00:00
/* (2) Constructor
*
* @p_parent<FlowPane> gParent element
* @observer<EventObserver> Main observer (to send event to)
*
---------------------------------------------------------*/
public HeaderMenu(FlowPane p_parent, EventObserver observer){
2017-11-19 15:30:22 +00:00
this.parent = p_parent;
this.items = new ArrayList<ImageView>();
this.observer = observer;
2017-11-19 15:30:22 +00:00
2017-11-14 07:22:26 +00:00
}
2017-11-19 15:30:22 +00:00
/* (3) Add a new menu item (graphically too)
*
* @p_name<String> The new menu item's name
* @p_image_uri<String> The new menu item's image link
*
---------------------------------------------------------*/
public void addItem(String p_name, String p_image_uri) {
2017-11-19 15:30:22 +00:00
/* (1) Create ImageView */
ImageView menuItem = new ImageView();
2017-11-19 15:30:22 +00:00
/* (2) Set useful attributes */
menuItem.setImage(new Image(p_image_uri)); // image link
menuItem.setId("header_menu_item_"+p_name); // id
menuItem.getStyleClass().add("header_menu_item"); // css .class
/* (3) Bind event -> 'changeMainLayout' */
menuItem.setOnMousePressed(new EventHandler<MouseEvent>() {
2017-11-19 15:30:22 +00:00
@Override
public void handle(MouseEvent event) {
2017-11-19 15:30:22 +00:00
2017-11-19 13:44:40 +00:00
HeaderMenu.this.observer.handleEvent(new Classes.Event(menuItem.getId(), "changeMainLayout"));
2017-11-19 15:30:22 +00:00
}
2017-11-19 15:30:22 +00:00
});
2017-11-19 15:30:22 +00:00
/* (4) Add item to list */
this.items.add(menuItem);
2017-11-19 15:30:22 +00:00
/* (5) Add gitem to gparent */
parent.getChildren().add(menuItem);
2017-11-19 15:30:22 +00:00
2017-11-14 07:22:26 +00:00
}
}