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 com.sun.javafx.css.converters.BooleanConverter;
029import java.util.ArrayList;
030import java.util.Collections;
031import java.util.List;
032
033import javafx.beans.DefaultProperty;
034import javafx.beans.property.BooleanProperty;
035import javafx.collections.FXCollections;
036import javafx.collections.ObservableList;
037
038import javafx.css.CssMetaData;
039import javafx.css.StyleableBooleanProperty;
040import com.sun.javafx.scene.control.skin.MenuBarSkin;
041import javafx.css.Styleable;
042import javafx.css.StyleableProperty;
043import javafx.scene.Node;
044
045/**
046 * <p>
047 * A MenuBar control traditionally is placed at the very top of the user
048 * interface, and embedded within it are {@link Menu Menus}. To add a menu to
049 * a menubar, you add it to the {@link #getMenus() menus} ObservableList.
050 * By default, for each menu added to the menubar, it will be
051 * represented as a button with the Menu {@link MenuItem#textProperty() text} value displayed.
052 * <p>
053 * MenuBar sets focusTraversable to false.
054 * </p>
055 *
056 * To create and populate a {@code MenuBar}, you may do what is shown below.
057 * Please refer to the {@link Menu} API page for more information on how to
058 * configure it.
059 * <pre><code>
060 * final Menu menu1 = new Menu("File");
061 * final Menu menu2 = new Menu("Options");
062 * final Menu menu3 = new Menu("Help");
063 * 
064 * MenuBar menuBar = new MenuBar();
065 * menuBar.getMenus().addAll(menu1, menu2, menu3);
066 * </code></pre>
067 *
068 * @see Menu
069 * @see MenuItem
070 */
071@DefaultProperty("menus")
072public class MenuBar extends Control {
073
074    /***************************************************************************
075     *                                                                         *
076     * Constructors                                                            *
077     *                                                                         *
078     **************************************************************************/
079
080    public MenuBar() {
081        getStyleClass().setAll(DEFAULT_STYLE_CLASS);
082        // focusTraversable is styleable through css. Calling setFocusTraversable
083        // makes it look to css like the user set the value and css will not 
084        // override. Initializing focusTraversable by calling applyStyle with null
085        // StyleOrigin ensures that css will be able to override the value.
086        ((StyleableProperty)focusTraversableProperty()).applyStyle(null, Boolean.FALSE);
087    }
088
089
090
091    /***************************************************************************
092     *                                                                         *
093     * Instance variables                                                      *
094     *                                                                         *
095     **************************************************************************/
096    private ObservableList<Menu> menus = FXCollections.<Menu>observableArrayList();
097
098
099    /***************************************************************************
100     *                                                                         *
101     * Properties                                                              *
102     *                                                                         *
103     **************************************************************************/
104
105    /**
106     * Use the system menu bar if the current platform supports it.
107     */
108    public final BooleanProperty useSystemMenuBarProperty() {
109        if (useSystemMenuBar == null) {
110            useSystemMenuBar = new StyleableBooleanProperty() {
111
112                @Override
113                public CssMetaData<MenuBar,Boolean> getCssMetaData() {
114                    return StyleableProperties.USE_SYSTEM_MENU_BAR;
115                }
116
117                @Override
118                public Object getBean() {
119                    return MenuBar.this;
120                }
121
122                @Override
123                public String getName() {
124                    return "useSystemMenuBar";
125                }
126            };
127        }
128        return useSystemMenuBar;
129    }
130    private BooleanProperty useSystemMenuBar;
131    public final void setUseSystemMenuBar(boolean value) {
132        useSystemMenuBarProperty().setValue(value);
133    }
134    public final boolean isUseSystemMenuBar() {
135        return useSystemMenuBar == null ? false : useSystemMenuBar.getValue();
136    }
137
138
139    /***************************************************************************
140     *                                                                         *
141     * Public API                                                              *
142     *                                                                         *
143     **************************************************************************/
144
145    /**
146     * The menus to show within this MenuBar. If this ObservableList is modified at
147     * runtime, the MenuBar will update as expected.
148     * @see Menu
149     */
150    public final ObservableList<Menu> getMenus() {
151        return menus;
152    }
153
154    /** {@inheritDoc} */
155    @Override protected Skin<?> createDefaultSkin() {
156        return new MenuBarSkin(this);
157    }
158    
159    /***************************************************************************
160     *                                                                         *
161     * Stylesheet Handling                                                     *
162     *                                                                         *
163     **************************************************************************/
164
165    private static final String DEFAULT_STYLE_CLASS = "menu-bar";
166
167    private static class StyleableProperties {
168        private static final CssMetaData<MenuBar, Boolean> USE_SYSTEM_MENU_BAR =
169                new CssMetaData<MenuBar, Boolean>("-fx-use-system-menu-bar",
170                                                        BooleanConverter.getInstance(),
171                                                        false) {
172            @Override public boolean isSettable(MenuBar n) {
173                return n.useSystemMenuBar == null || !n.useSystemMenuBar.isBound();
174            }
175
176            @Override public StyleableProperty<Boolean> getStyleableProperty(MenuBar n) {
177                return (StyleableProperty<Boolean>)n.useSystemMenuBarProperty();
178            }
179        };
180
181        private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
182        static {
183            final List<CssMetaData<? extends Styleable, ?>> styleables =
184                new ArrayList<CssMetaData<? extends Styleable, ?>>(Control.getClassCssMetaData());
185            styleables.add(USE_SYSTEM_MENU_BAR);
186            STYLEABLES = Collections.unmodifiableList(styleables);
187        }
188    }
189
190    /**
191     * @return The CssMetaData associated with this class, which may include the
192     * CssMetaData of its super classes.
193     */
194    public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
195        return StyleableProperties.STYLEABLES;
196    }
197
198    /**
199     * {@inheritDoc}
200     */
201    @Override
202    public List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData() {
203        return getClassCssMetaData();
204    }
205
206    /**
207      * Most Controls return true for focusTraversable, so Control overrides
208      * this method to return true, but MenuBar returns false for
209      * focusTraversable's initial value; hence the override of the override. 
210      * This method is called from CSS code to get the correct initial value.
211      * @treatAsPrivate implementation detail
212      */
213    @Deprecated @Override
214    protected /*do not make final*/ Boolean impl_cssGetFocusTraversableInitialValue() {
215        return Boolean.FALSE;
216    }
217}
218