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<ActionEvent>() { 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<ActionEvent>() { 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 */ 103 public CheckMenuItem(String text) { 104 this(text,null); 105 } 106 107 /** 108 * Constructs a CheckMenuItem and sets the display text with the specified text 109 * and sets the graphic {@link Node} to the given node. 110 */ 111 public CheckMenuItem(String text, Node graphic) { 112 super(text,graphic); 113 getStyleClass().add(DEFAULT_STYLE_CLASS); 114 } 115 116 117 118 /*************************************************************************** 119 * * 120 * Properties * 121 * * 122 **************************************************************************/ 123 /** 124 * Represents the current state of this CheckMenuItem. Bind to this to be 125 * informed whenever the user interacts with the CheckMenuItem (and causes the 126 * selected state to be toggled). 127 * 128 * @defaultValue false 129 */ 130 private BooleanProperty selected; 131 public final void setSelected(boolean value) { 132 selectedProperty().set(value); 133 } 134 135 public final boolean isSelected() { 136 return selected == null ? false : selected.get(); 137 } 138 139 public final BooleanProperty selectedProperty() { 140 if (selected == null) { 141 selected = new BooleanPropertyBase() { 142 @Override protected void invalidated() { 143 // force validation 144 get(); 145 146 // update the styleclass 147 if (isSelected()) { 148 getStyleClass().add(STYLE_CLASS_SELECTED); 149 } else { 150 getStyleClass().remove(STYLE_CLASS_SELECTED); 151 } 152 } 153 154 @Override 155 public Object getBean() { 156 return CheckMenuItem.this; 157 } 158 159 @Override 160 public String getName() { 161 return "selected"; 162 } 163 }; 164 } 165 return selected; 166 } 167 168 /*************************************************************************** 169 * * 170 * Stylesheet Handling * 171 * * 172 **************************************************************************/ 173 174 private static final String DEFAULT_STYLE_CLASS = "check-menu-item"; 175 private static final String STYLE_CLASS_SELECTED = "selected"; 176 }