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 javafx.scene.control;
  27 
  28 import javafx.event.ActionEvent;
  29 import javafx.scene.AccessibleAction;
  30 import javafx.scene.AccessibleAttribute;
  31 import javafx.scene.AccessibleRole;
  32 import javafx.scene.control.skin.SplitMenuButtonSkin;
  33 
  34 /**
  35  * The SplitMenuButton, like the {@link MenuButton} is closely associated with
  36  * the concept of selecting a {@link MenuItem} from a menu. Unlike {@link MenuButton},
  37  * the SplitMenuButton is broken into two pieces, the "action" area and the
  38  * "menu open" area.
  39  * <p>
  40  * If the user clicks in the action area, the SplitMenuButton will act similarly
  41  * to a {@link javafx.scene.control.Button Button}, firing whatever is
  42  * associated with the {@link #onAction} property.
  43  * <p>
  44  * The menu open area of the control will show a menu if clicked. When the user
  45  * selects an item from the menu, it is executed.
  46  * <p>
  47  * Note that the SplitMenuButton does not automatically assign whatever was last
  48  * selected in the menu to be the action should the action region be clicked.
  49  *
  50  * <p>Example:</p>
  51  * <pre>
  52  * {@literal
  53  * SplitMenuButton m = new SplitMenuButton();
  54  * m.setText("Shutdown");
  55  * m.getItems().addAll(new MenuItem("Logout"), new MenuItem("Sleep"));
  56  * m.setOnAction(new EventHandler<ActionEvent>() {
  57  *     @Override public void handle(ActionEvent e) {
  58  *         System.out.println("Shutdown");
  59  *     }
  60  * });
  61  * }
  62  * </pre>
  63  *
  64  * <p>
  65  * MnemonicParsing is enabled by default for SplitMenuButton.
  66  * </p>
  67  *
  68  * @see MenuItem
  69  * @see Menu
  70  * @since JavaFX 2.0
  71  */
  72 
  73 public class SplitMenuButton extends MenuButton {
  74 
  75     /***************************************************************************
  76      *                                                                         *
  77      * Constructors                                                            *
  78      *                                                                         *
  79      **************************************************************************/
  80 
  81     /**
  82      * Creates a new empty split menu button. Use {@link #setText(String)},
  83      * {@link #setGraphic(Node)} and {@link #getItems()} to set the content.
  84      */
  85     public SplitMenuButton() {
  86         this((MenuItem[])null);
  87     }
  88 
  89     /**
  90      * Creates a new split menu button with the given list of menu items.
  91      *
  92      * @param items The items to show within this button's menu
  93      */
  94     public SplitMenuButton(MenuItem... items) {
  95         if (items != null) {
  96             getItems().addAll(items);
  97         }
  98 
  99         getStyleClass().setAll(DEFAULT_STYLE_CLASS);
 100         setAccessibleRole(AccessibleRole.SPLIT_MENU_BUTTON);
 101         setMnemonicParsing(true);     // enable mnemonic auto-parsing by default
 102     }
 103 
 104     /***************************************************************************
 105      *                                                                         *
 106      * Properties                                                              *
 107      *                                                                         *
 108      **************************************************************************/
 109     /**
 110      * Call the action when button is pressed.
 111      */
 112     @Override public void fire() {
 113         if (!isDisabled()) {
 114             fireEvent(new ActionEvent());
 115         }
 116     }
 117 
 118     /***************************************************************************
 119      *                                                                         *
 120      * Methods                                                                 *
 121      *                                                                         *
 122      **************************************************************************/
 123 
 124     /** {@inheritDoc} */
 125     @Override protected Skin<?> createDefaultSkin() {
 126         return new SplitMenuButtonSkin(this);
 127     }
 128 
 129     /***************************************************************************
 130      *                                                                         *
 131      * Stylesheet Handling                                                     *
 132      *                                                                         *
 133      **************************************************************************/
 134 
 135     private static final String DEFAULT_STYLE_CLASS = "split-menu-button";
 136 
 137     // SplitMenuButton adds no new CSS keys.
 138 
 139     /***************************************************************************
 140      *                                                                         *
 141      * Accessibility handling                                                  *
 142      *                                                                         *
 143      **************************************************************************/
 144 
 145     @Override
 146     public Object queryAccessibleAttribute(AccessibleAttribute attribute, Object... parameters) {
 147         switch (attribute) {
 148             case EXPANDED: return isShowing();
 149             default: return super.queryAccessibleAttribute(attribute, parameters);
 150         }
 151     }
 152 
 153     @Override
 154     public void executeAccessibleAction(AccessibleAction action, Object... parameters) {
 155         switch (action) {
 156             case FIRE:
 157                 fire();
 158                 break;
 159             case EXPAND:
 160                 show();
 161                 break;
 162             case COLLAPSE:
 163                 hide();
 164                 break;
 165             default: super.executeAccessibleAction(action);
 166         }
 167     }
 168 
 169 }