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.ObjectProperty;
030import javafx.beans.property.SimpleBooleanProperty;
031import javafx.beans.property.SimpleObjectProperty;
032import javafx.scene.Node;
033
034/**
035 * <p>
036 * A {@link MenuItem} that allows for arbitrary nodes to be embedded within it,
037 * by assigning a {@link Node} to the {@link #contentProperty() content} property.
038 * CustomMenuItem attempts to make arbitrary nodes
039 * appear more natural in the Menu. For
040 * example, you automatically get hover highlighting when the user hovers their
041 * mouse over the node (or navigates to it using keyboard or other alternate means).
042 * <p>
043 * A useful property of this class is
044 * {@link #hideOnClickProperty() hideOnClick}. This boolean specifies whether the CustomMenuItem
045 * will be automatically hidden should the user click within the bounds of the
046 * CustomMenuItem.
047 * This is of particular use when the node inside this CustomMenuItem is a
048 * control that requires user interaction, as by setting the hideOnClick
049 * property to false means the user can interact with it without the menu hiding.
050 * Note that by default hideOnClick is true.
051 * <p>
052 * An example of how to use CustomMenuItem is shown below.
053<pre><code>
054CustomMenuItem customMenuItem = new CustomMenuItem(new Slider());
055customMenuItem.setHideOnClick(false);
056</code></pre>
057 * <p>
058 * If this CustomMenuItem, with the {@link Slider} within it, is placed in a
059 * menu, you'll be presented with a slider that is horizontally centered when
060 * the menu is visible, and interacting with the slider will not result in the
061 * menu disappearing.
062 * <p>
063 *
064 * @see MenuItem
065 * @see Menu
066 */
067public class CustomMenuItem extends MenuItem {
068    
069    /***************************************************************************
070     *                                                                         *
071     * Constructors                                                            *
072     *                                                                         *
073     **************************************************************************/
074    
075    /**
076     * 
077     */
078    public CustomMenuItem() {
079        this(null, true);
080    }
081    
082    /**
083     * Constructs a CustomMenuItem and initializes its content with the node specified.
084     * @param node to be embedded inside this CustomMenuItem
085     */
086    public CustomMenuItem(Node node) {
087        this(node, true);
088    }
089
090    /**
091     * Constructs a CustomMenuItem and sets the content to the node specified.
092     * @param node to be embedded inside this CustomMenuItem
093     * @param hideOnClick if false the menu will not hide when the user interacts with the node.
094     */
095    public CustomMenuItem(Node node, boolean hideOnClick) {
096        getStyleClass().add(DEFAULT_STYLE_CLASS);
097        
098        setContent(node);
099        setHideOnClick(hideOnClick);
100    }
101    
102    
103    
104    /***************************************************************************
105     *                                                                         *
106     * Properties                                                              *
107     *                                                                         *
108     **************************************************************************/    
109    
110    /**
111     * The node to display within this CustomMenuItem.
112     */
113    private ObjectProperty<Node> content;
114
115    public final void setContent(Node value) {
116        contentProperty().set(value);
117    }
118
119    public final Node getContent() {
120        return content == null ? null : content.get();
121    }
122
123    public final ObjectProperty<Node> contentProperty() {
124        if (content == null) {
125            content = new SimpleObjectProperty<Node>(this, "content");
126        }
127        return content;
128    }
129
130    
131    /**
132     * If true, this menu item, and all visible menus, will be hidden when this
133     * menu item is clicked on.
134     *
135     * @defaultValue true
136     */
137    private BooleanProperty hideOnClick;
138
139    public final void setHideOnClick(boolean value) {
140        hideOnClickProperty().set(value);
141    }
142
143    public final boolean isHideOnClick() {
144        return hideOnClick == null ? true : hideOnClick.get();
145    }
146
147    public final BooleanProperty hideOnClickProperty() {
148        if (hideOnClick == null) {
149            hideOnClick = new SimpleBooleanProperty(this, "hideOnClick", true);
150        }
151        return hideOnClick;
152    }
153 
154    
155    
156    /***************************************************************************
157     *                                                                         *
158     * Stylesheet Handling                                                     *
159     *                                                                         *
160     **************************************************************************/
161
162    private static final String DEFAULT_STYLE_CLASS = "custom-menu-item";
163}