1 /*
   2  * Copyright (c) 2010, 2014, 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 com.sun.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  * SplitMenuButton m = new SplitMenuButton();
  53  * m.setText("Shutdown");
  54  * m.getItems().addAll(new MenuItem("Logout"), new MenuItem("Sleep"));
  55  * m.setOnAction(new EventHandler<ActionEvent>() {
  56  *     &#064;Override public void handle(ActionEvent e) {
  57  *         System.out.println("Shutdown");
  58  *     }
  59  * });
  60  * </pre>
  61  * <p>
  62  * 
  63  * <p>
  64  * MnemonicParsing is enabled by default for SplitMenuButton.
  65  * </p>
  66  *
  67  * @see MenuItem
  68  * @see Menu
  69  * @since JavaFX 2.0
  70  */
  71 
  72 public class SplitMenuButton extends MenuButton {
  73 
  74     /***************************************************************************
  75      *                                                                         *
  76      * Constructors                                                            *
  77      *                                                                         *
  78      **************************************************************************/
  79 
  80     /**
  81      * Creates a new empty split menu button. Use {@link #setText(String)},
  82      * {@link #setGraphic(Node)} and {@link #getItems()} to set the content.
  83      */
  84     public SplitMenuButton() {
  85         this((MenuItem[])null);
  86     }
  87 
  88     /**
  89      * Creates a new split menu button with the given list of menu items.
  90      *
  91      * @param items The items to show within this button's menu
  92      */
  93     public SplitMenuButton(MenuItem... items) {
  94         if (items != null) {
  95             getItems().addAll(items);
  96         }
  97 
  98         getStyleClass().setAll(DEFAULT_STYLE_CLASS);
  99         setAccessibleRole(AccessibleRole.SPLIT_MENU_BUTTON);
 100         setMnemonicParsing(true);     // enable mnemonic auto-parsing by default
 101     }
 102 
 103     /***************************************************************************
 104      *                                                                         *
 105      * Properties                                                              *
 106      *                                                                         *
 107      **************************************************************************/
 108     /**
 109      * Call the action when button is pressed.
 110      */
 111     @Override public void fire() {
 112         if (!isDisabled()) {
 113             fireEvent(new ActionEvent());
 114         }
 115     }
 116 
 117     /***************************************************************************
 118      *                                                                         *
 119      * Methods                                                                 *
 120      *                                                                         *
 121      **************************************************************************/
 122 
 123     /** {@inheritDoc} */
 124     @Override protected Skin<?> createDefaultSkin() {
 125         return new SplitMenuButtonSkin(this);
 126     }
 127 
 128     /***************************************************************************
 129      *                                                                         *
 130      * Stylesheet Handling                                                     *
 131      *                                                                         *
 132      **************************************************************************/
 133 
 134     private static final String DEFAULT_STYLE_CLASS = "split-menu-button";
 135 
 136     // SplitMenuButton adds no new CSS keys.
 137 
 138     /***************************************************************************
 139      *                                                                         *
 140      * Accessibility handling                                                  *
 141      *                                                                         *
 142      **************************************************************************/
 143 
 144     @Override
 145     public Object queryAccessibleAttribute(AccessibleAttribute attribute, Object... parameters) {
 146         switch (attribute) {
 147             case EXPANDED: return isShowing();
 148             default: return super.queryAccessibleAttribute(attribute, parameters);
 149         }
 150     }
 151 
 152     @Override
 153     public void executeAccessibleAction(AccessibleAction action, Object... parameters) {
 154         switch (action) {
 155             case FIRE:
 156                 fire();
 157                 break;
 158             case EXPAND:
 159                 show();
 160                 break;
 161             case COLLAPSE:
 162                 hide();
 163                 break;
 164             default: super.executeAccessibleAction(action);
 165         }
 166     }
 167 
 168 }