1 /*
   2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package hello;
  27 
  28 import javafx.application.Application;
  29 import javafx.beans.InvalidationListener;
  30 import javafx.beans.property.BooleanProperty;
  31 import javafx.event.EventHandler;
  32 import javafx.geometry.Pos;
  33 import javafx.scene.Node;
  34 import javafx.scene.Scene;
  35 import javafx.scene.control.CheckMenuItem;
  36 import javafx.scene.control.Label;
  37 import javafx.scene.control.Menu;
  38 import javafx.scene.control.MenuBar;
  39 import javafx.scene.control.MenuItem;
  40 import javafx.scene.control.RadioMenuItem;
  41 import javafx.scene.control.SeparatorMenuItem;
  42 import javafx.scene.control.ToggleGroup;
  43 import javafx.scene.image.Image;
  44 import javafx.scene.image.ImageView;
  45 import javafx.scene.input.KeyCharacterCombination;
  46 import javafx.scene.input.KeyCode;
  47 import javafx.scene.input.KeyCodeCombination;
  48 import javafx.scene.input.KeyCombination;
  49 import javafx.scene.layout.HBox;
  50 import javafx.scene.layout.VBox;
  51 import javafx.stage.Stage;
  52 
  53 import java.text.SimpleDateFormat;
  54 import java.util.Date;
  55 
  56 public class HelloMenu extends Application {
  57 
  58     //private static final String imageURL = "file:hello/about_16.png";
  59 
  60     private CheckMenuItem showMessagesItem;
  61     private final Label sysMenuLabel = new Label("Using System Menu");
  62     @Override public void start(Stage stage) {
  63         stage.setTitle("Hello Menu");
  64         Scene scene = new Scene(new VBox(20), 400, 350);
  65         final MenuBar menuBar = new MenuBar();
  66         final String os = System.getProperty("os.name");
  67         EventHandler actionHandler = t -> {
  68             if (t.getTarget() instanceof MenuItem) {
  69                 System.out.println(((MenuItem)t.getTarget()).getText() + " - action called");
  70             }
  71         };
  72 
  73         // --- Menu 1
  74         final Menu menu1 = makeMenu("_Debug");
  75 
  76         final Menu menu11 = makeMenu("_New", new ImageView(new Image("hello/about_16.png")));
  77         MenuItem menu12 = new MenuItem("_Open", new ImageView(new Image("hello/folder_16.png")));
  78         menu12.setAccelerator(new KeyCharacterCombination("]", 
  79                 KeyCombination.SHIFT_DOWN, KeyCombination.META_DOWN));
  80         menu12.setOnAction(actionHandler);
  81         Menu menu13 = makeMenu("_Submenu");
  82         showMessagesItem = new CheckMenuItem("Enable onShowing/onHiding _messages", 
  83                                              new ImageView(new Image("hello/about_16.png")));
  84         MenuItem menu15 = new MenuItem("E_xit");
  85         menu15.setOnAction(t -> System.exit(0));
  86         final String change[] = {"Change Text", "Change Back"};
  87         final MenuItem menu16 = new MenuItem(change[0]);
  88         final boolean toggle = false;
  89         menu16.setAccelerator(KeyCombination.keyCombination("Shortcut+C"));
  90         menu16.setOnAction(t -> menu16.setText((menu16.getText().equals(change[0])) ? change[1] : change[0]));
  91         menu1.getItems().addAll(menu11, menu12, menu13, showMessagesItem, new SeparatorMenuItem(), menu15, menu16);
  92 
  93 
  94         // --- Menu 11 submenu
  95         final MenuItem menu111 = new MenuItem("blah");
  96         menu111.setOnAction(actionHandler);
  97         final MenuItem menu112 = new MenuItem("foo");
  98         menu112.setOnAction(actionHandler);
  99         final CheckMenuItem menu113 = new CheckMenuItem("Show \"foo\" item");
 100         menu113.setSelected(true);
 101         menu113.selectedProperty().addListener(valueModel -> {
 102             menu112.setVisible(menu113.isSelected());
 103             System.err.println("MenuItem \"foo\" is now " + (menu112.isVisible() ? "" : "not") + " visible.");
 104         });
 105         menu11.getItems().addAll(menu111, menu112, menu113);
 106 
 107 
 108         // --- Menu 13 submenu
 109         MenuItem menu131 = new MenuItem("Item _1");
 110         menu131.setOnAction(actionHandler);
 111         MenuItem menu132 = new MenuItem("Item _2");
 112         menu132.setOnAction(actionHandler);
 113         menu13.getItems().addAll(menu131, menu132);
 114 
 115 
 116         // --- Menu 2
 117         Menu menu2 = makeMenu("_Edit");
 118         MenuItem menu21 = new MenuItem("_Undo");
 119         menu21.setAccelerator(KeyCombination.keyCombination("shortcut+Z"));
 120         menu21.setOnAction(actionHandler);
 121         MenuItem menu22 = new MenuItem("_Redo");
 122         menu22.setAccelerator(KeyCombination.keyCombination("shortcut+Y"));
 123         menu22.setOnAction(actionHandler);
 124         // menu separator
 125         MenuItem menu23 = new MenuItem("_Disabled");
 126         menu23.setDisable(true);
 127         // menu separator
 128         MenuItem menu24 = new MenuItem("Copy");
 129         menu24.setAccelerator(KeyCombination.keyCombination("shortcut+C"));
 130         menu24.setOnAction(actionHandler);
 131         MenuItem menu25 = new MenuItem("Paste");
 132         menu25.setAccelerator(KeyCombination.keyCombination("shortcut+V"));
 133         menu25.setOnAction(actionHandler);
 134         MenuItem menu26 = new MenuItem("Delete");
 135         menu26.setAccelerator(KeyCombination.keyCombination("shortcut+D"));
 136         MenuItem menu27 = new MenuItem("Help");
 137         menu27.setAccelerator(new KeyCodeCombination(KeyCode.F1));
 138         menu27.setOnAction(actionHandler);
 139         menu27.setDisable(false);
 140         menu2.getItems().addAll(menu21, menu22, new SeparatorMenuItem(), menu23,
 141                  menu24, menu25, menu26, menu27);
 142         
 143 
 144         // --- Menu 3
 145         Menu menu3 = makeMenu("_Radio/CheckBox");
 146         CheckMenuItem checkMI1 = new CheckMenuItem("_1 CheckMenuItem - checked");
 147         checkMI1.setSelected(true);
 148         CheckMenuItem checkMI2 = new CheckMenuItem("_2 CheckMenuItem - not checked");
 149 
 150         RadioMenuItem radioMI1 = new RadioMenuItem("_3 RadioMenuItem - selected");
 151         radioMI1.setSelected(true);
 152         RadioMenuItem radioMI2 = new RadioMenuItem("_4 RadioMenuItem - not selected");
 153         ToggleGroup group = new ToggleGroup();
 154         radioMI1.setToggleGroup(group);
 155         radioMI2.setToggleGroup(group);
 156 
 157         InvalidationListener selectedListener = valueModel -> {
 158             MenuItem mi = (MenuItem)((BooleanProperty)valueModel).getBean();
 159             boolean selected = ((BooleanProperty)valueModel).get();
 160             System.err.println(mi.getText() + " - " + selected);
 161         };
 162 
 163         checkMI1.selectedProperty().addListener(selectedListener);
 164         checkMI2.selectedProperty().addListener(selectedListener);
 165         radioMI1.selectedProperty().addListener(selectedListener);
 166         radioMI2.selectedProperty().addListener(selectedListener);
 167 
 168         menu3.getItems().addAll(checkMI1, checkMI2, radioMI1, radioMI2);
 169 //        ImageView im = new ImageView();
 170 //        im.setImage(new Image(imageURL));
 171 //        cmitem.setGraphic(im);
 172 
 173         menuBar.getMenus().add(menu1);
 174         menuBar.getMenus().add(menu2);
 175         menuBar.getMenus().add(menu3);
 176         
 177         if (os != null && os.startsWith("Mac")) {
 178             Menu systemMenuBarMenu = makeMenu("MenuBar _Options");
 179 
 180             final CheckMenuItem useSystemMenuBarCB = new CheckMenuItem("Use _System Menu Bar");
 181             useSystemMenuBarCB.setSelected(true);
 182             menuBar.useSystemMenuBarProperty().bind(useSystemMenuBarCB.selectedProperty());
 183             systemMenuBarMenu.getItems().add(useSystemMenuBarCB);
 184 
 185             menuBar.getMenus().add(systemMenuBarMenu);
 186         }
 187 
 188         ((VBox)scene.getRoot()).getChildren().add(menuBar);
 189         if (os != null && os.startsWith("Mac")) {
 190             HBox hbox = new HBox();
 191             hbox.setAlignment(Pos.CENTER);
 192             sysMenuLabel.setStyle("-fx-font-size: 24");
 193             hbox.getChildren().add(sysMenuLabel);
 194             ((VBox)scene.getRoot()).getChildren().add(hbox);
 195             sysMenuLabel.setVisible((menuBar.getHeight() == 0) ? true : false);
 196             menuBar.heightProperty().addListener((ov, t, t1) -> sysMenuLabel.setVisible((menuBar.getHeight() == 0) ? true : false));
 197         }
 198         stage.setScene(scene);
 199         stage.show();
 200     }
 201 
 202     private EventHandler showHideHandler = t -> {
 203         Menu menu = (Menu)t.getSource();
 204         if (t.getEventType() == Menu.ON_SHOWING && 
 205                 menu.getText().equals("_Submenu")) {
 206             Date date = new Date();
 207             String time = new SimpleDateFormat("HH:mm:ss").format(date);
 208             menu.getItems().get(0).setText("The time is " + time);
 209         }
 210         if (showMessagesItem.isSelected()) {
 211             System.out.println(((Menu)t.getSource()).getText() + " " + t.getEventType());
 212         }
 213     };
 214 
 215     private Menu makeMenu(String text) {
 216         return makeMenu(text, null);
 217     }
 218 
 219     private Menu makeMenu(String text, Node graphic) {
 220         Menu menu = new Menu(text, graphic);
 221         menu.setOnShowing(showHideHandler);
 222         menu.setOnShown(showHideHandler);
 223         menu.setOnHiding(showHideHandler);
 224         menu.setOnHidden(showHideHandler);
 225         return menu;
 226     }
 227 
 228     /**
 229      * @param args the command line arguments
 230      */
 231     public static void main(String[] args) {
 232         Application.launch(args);
 233     }
 234 }