Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.control;
027
028import javafx.beans.property.BooleanProperty;
029import javafx.beans.property.BooleanPropertyBase;
030import javafx.scene.Node;
031
032/**
033 * <p>
034 * A {@link MenuItem} that can be toggled between selected and unselected states.
035 * It is intended that CheckMenuItem be used in conjunction with the
036 * {@link Menu} or {@link ContextMenu} controls.
037 * <p>
038 * Creating and inserting a CheckMenuItem into a Menu is shown below.
039<pre><code>
040final subsystem1 = new CheckMenuItem("Enabled");
041subsystem1.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
042    public void handle(ActionEvent e) {
043        System.out.println("subsystem1 #1 Enabled!");
044    }
045});
046
047Menu subsystemsMenu = new Menu("Subsystems");
048subsystemsMenu.add(subsystem1);
049</code></pre>
050 * <p>
051 * Of course, the approach shown above separates out the definition of the
052 * CheckMenuItem from the Menu, but this needn't be so.
053 * <p>
054 * To ascertain the current state of the CheckMenuItem, you should refer to the
055 * {@link #selectedProperty() selected} boolean. An example use case may be the following example:
056<pre><code>
057final checkMenuItem = new CheckMenuItem("Show Widget");
058subsystem1.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
059    public void handle(ActionEvent e) {
060        System.out.println("Show the widget!");
061    }
062});
063private final BooleanProperty widgetShowing();
064public final boolean isWidgetShowing() { return widgetShowing.get(); )
065public final void setWidgetShowing(boolean value) { 
066    widgetShowingProperty().set(value);
067}
068public final BooleanProperty widgetShowingProperty() {
069    if (widgetShowing == null) {
070        widgetShowing = new SimpleBooleanProperty(this, "widgetShowing", true);
071    }
072    return widgetShowing;
073}
074
075widgetShowing.bind(checkMenuItem.selected);
076</code></pre>
077 * <p>
078 * Typically a CheckMenuItem will be rendered such that, when selected, it shows
079 * a check (or tick) mark in the area normally reserved for the MenuItem
080 * graphic. Of course, this will vary depending on the skin and styling specified.
081 *
082 * @see Menu
083 * @see MenuItem
084 * @see RadioMenuItem
085 *
086 */
087public class CheckMenuItem extends MenuItem {
088
089    /***************************************************************************
090     *                                                                         *
091     * Constructors                                                            *
092     *                                                                         *
093     **************************************************************************/
094
095    public CheckMenuItem() {
096        this(null,null);
097    }
098
099    /**
100     * Constructs a CheckMenuItem and sets the display text with the specified text.
101     */
102    public CheckMenuItem(String text) {
103        this(text,null);
104    }
105
106    /**
107     * Constructs a CheckMenuItem and sets the display text with the specified text
108     * and sets the graphic {@link Node} to the given node.
109     */
110    public CheckMenuItem(String text, Node graphic) {
111        super(text,graphic);
112        getStyleClass().add(DEFAULT_STYLE_CLASS);
113    }
114
115
116
117    /***************************************************************************
118     *                                                                         *
119     * Properties                                                              *
120     *                                                                         *
121     **************************************************************************/
122    /**
123     * Represents the current state of this CheckMenuItem. Bind to this to be
124     * informed whenever the user interacts with the CheckMenuItem (and causes the
125     * selected state to be toggled).
126     *
127     * @defaultValue false
128     */
129    private BooleanProperty selected;
130    public final void setSelected(boolean value) {
131        selectedProperty().set(value);
132    }
133
134    public final boolean isSelected() {
135        return selected == null ? false : selected.get();
136    }
137
138    public final BooleanProperty selectedProperty() {
139        if (selected == null) {
140            selected = new BooleanPropertyBase() {
141                @Override protected void invalidated() {
142                    // force validation
143                    get();
144                    
145                    // update the styleclass
146                    if (isSelected()) {
147                        getStyleClass().add(STYLE_CLASS_SELECTED);
148                    } else {
149                        getStyleClass().remove(STYLE_CLASS_SELECTED);
150                    }
151                }
152
153                @Override
154                public Object getBean() {
155                    return CheckMenuItem.this;
156                }
157
158                @Override
159                public String getName() {
160                    return "selected";
161                }
162            };
163        }
164        return selected;
165    }
166
167    /***************************************************************************
168     *                                                                         *
169     * Stylesheet Handling                                                     *
170     *                                                                         *
171     **************************************************************************/
172
173    private static final String DEFAULT_STYLE_CLASS = "check-menu-item";
174    private static final String STYLE_CLASS_SELECTED = "selected";
175}