1 /*
   2  * Copyright (c) 2010, 2013, 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.beans.property.BooleanProperty;
  29 import javafx.beans.property.BooleanPropertyBase;
  30 import javafx.scene.Node;
  31 
  32 /**
  33  * <p>
  34  * A {@link MenuItem} that can be toggled between selected and unselected states.
  35  * It is intended that CheckMenuItem be used in conjunction with the
  36  * {@link Menu} or {@link ContextMenu} controls.
  37  * <p>
  38  * Creating and inserting a CheckMenuItem into a Menu is shown below.
  39 <pre><code>
  40 final subsystem1 = new CheckMenuItem("Enabled");
  41 subsystem1.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  42     public void handle(ActionEvent e) {
  43         System.out.println("subsystem1 #1 Enabled!");
  44     }
  45 });
  46 
  47 Menu subsystemsMenu = new Menu("Subsystems");
  48 subsystemsMenu.add(subsystem1);
  49 </code></pre>
  50  * <p>
  51  * Of course, the approach shown above separates out the definition of the
  52  * CheckMenuItem from the Menu, but this needn't be so.
  53  * <p>
  54  * To ascertain the current state of the CheckMenuItem, you should refer to the
  55  * {@link #selectedProperty() selected} boolean. An example use case may be the following example:
  56 <pre><code>
  57 final checkMenuItem = new CheckMenuItem("Show Widget");
  58 subsystem1.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  59     public void handle(ActionEvent e) {
  60         System.out.println("Show the widget!");
  61     }
  62 });
  63 private final BooleanProperty widgetShowing();
  64 public final boolean isWidgetShowing() { return widgetShowing.get(); )
  65 public final void setWidgetShowing(boolean value) {
  66     widgetShowingProperty().set(value);
  67 }
  68 public final BooleanProperty widgetShowingProperty() {
  69     if (widgetShowing == null) {
  70         widgetShowing = new SimpleBooleanProperty(this, "widgetShowing", true);
  71     }
  72     return widgetShowing;
  73 }
  74 
  75 widgetShowing.bind(checkMenuItem.selected);
  76 </code></pre>
  77  * <p>
  78  * Typically a CheckMenuItem will be rendered such that, when selected, it shows
  79  * a check (or tick) mark in the area normally reserved for the MenuItem
  80  * graphic. Of course, this will vary depending on the skin and styling specified.
  81  *
  82  * @see Menu
  83  * @see MenuItem
  84  * @see RadioMenuItem
  85  *
  86  * @since JavaFX 2.0
  87  */
  88 public class CheckMenuItem extends MenuItem {
  89 
  90     /***************************************************************************
  91      *                                                                         *
  92      * Constructors                                                            *
  93      *                                                                         *
  94      **************************************************************************/
  95 
  96     public CheckMenuItem() {
  97         this(null,null);
  98     }
  99 
 100     /**
 101      * Constructs a CheckMenuItem and sets the display text with the specified text.
 102      * @param text the display text
 103      */
 104     public CheckMenuItem(String text) {
 105         this(text,null);
 106     }
 107 
 108     /**
 109      * Constructs a CheckMenuItem and sets the display text with the specified text
 110      * and sets the graphic {@link Node} to the given node.
 111      * @param text the display text
 112      * @param graphic the graphic Node
 113      */
 114     public CheckMenuItem(String text, Node graphic) {
 115         super(text,graphic);
 116         getStyleClass().add(DEFAULT_STYLE_CLASS);
 117     }
 118 
 119 
 120 
 121     /***************************************************************************
 122      *                                                                         *
 123      * Properties                                                              *
 124      *                                                                         *
 125      **************************************************************************/
 126     /**
 127      * Represents the current state of this CheckMenuItem. Bind to this to be
 128      * informed whenever the user interacts with the CheckMenuItem (and causes the
 129      * selected state to be toggled).
 130      *
 131      * @defaultValue false
 132      */
 133     private BooleanProperty selected;
 134     public final void setSelected(boolean value) {
 135         selectedProperty().set(value);
 136     }
 137 
 138     public final boolean isSelected() {
 139         return selected == null ? false : selected.get();
 140     }
 141 
 142     public final BooleanProperty selectedProperty() {
 143         if (selected == null) {
 144             selected = new BooleanPropertyBase() {
 145                 @Override protected void invalidated() {
 146                     // force validation
 147                     get();
 148 
 149                     // update the styleclass
 150                     if (isSelected()) {
 151                         getStyleClass().add(STYLE_CLASS_SELECTED);
 152                     } else {
 153                         getStyleClass().remove(STYLE_CLASS_SELECTED);
 154                     }
 155                 }
 156 
 157                 @Override
 158                 public Object getBean() {
 159                     return CheckMenuItem.this;
 160                 }
 161 
 162                 @Override
 163                 public String getName() {
 164                     return "selected";
 165                 }
 166             };
 167         }
 168         return selected;
 169     }
 170 
 171     /***************************************************************************
 172      *                                                                         *
 173      * Stylesheet Handling                                                     *
 174      *                                                                         *
 175      **************************************************************************/
 176 
 177     private static final String DEFAULT_STYLE_CLASS = "check-menu-item";
 178     private static final String STYLE_CLASS_SELECTED = "selected";
 179 }