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; import javafx.scene.layout.FlowPane; public class HeaderMenu{ /* (1) Attributes ---------------------------------------------------------*/ private ArrayList items; private FlowPane parent; private EventObserver observer; /* Constructor */ /* (2) Constructor * * @p_parent gParent element * @observer Main observer (to send event to) * ---------------------------------------------------------*/ public HeaderMenu(FlowPane p_parent, EventObserver observer){ this.parent = p_parent; this.items = new ArrayList(); this.observer = observer; } /* (3) Add a new menu item (graphically too) * * @p_name The new menu item's name * @p_image_uri The new menu item's image link * ---------------------------------------------------------*/ public void addItem(String p_name, String p_image_uri) { /* (1) Create ImageView */ ImageView menuItem = new ImageView(); /* (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() { @Override public void handle(MouseEvent event) { HeaderMenu.this.observer.handleEvent(new Classes.Event(menuItem.getId(), "changeMainLayout")); } }); /* (4) Add item to list */ this.items.add(menuItem); /* (5) Add gitem to gparent */ parent.getChildren().add(menuItem); } }