1 /*
   2  * Copyright (c) 2010, 2014, 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.skin;
  27 
  28 import com.sun.javafx.scene.control.ContextMenuContent;
  29 import javafx.scene.Node;
  30 import javafx.scene.control.Control;
  31 import javafx.scene.control.MenuButton;
  32 
  33 import com.sun.javafx.scene.control.behavior.MenuButtonBehavior;
  34 
  35 /**
  36  * Default skin implementation for the {@link MenuButton} control.
  37  *
  38  * @see MenuButton
  39  * @since 9
  40  */
  41 public class MenuButtonSkin extends MenuButtonSkinBase<MenuButton> {
  42 
  43     /***************************************************************************
  44      *                                                                         *
  45      * Static fields                                                           *
  46      *                                                                         *
  47      **************************************************************************/
  48 
  49     static final String AUTOHIDE = "autoHide";
  50 
  51 
  52 
  53     /***************************************************************************
  54      *                                                                         *
  55      * Private fields                                                          *
  56      *                                                                         *
  57      **************************************************************************/
  58 
  59     private final MenuButtonBehavior behavior;
  60 
  61 
  62 
  63     /***************************************************************************
  64      *                                                                         *
  65      * Constructors                                                            *
  66      *                                                                         *
  67      **************************************************************************/
  68 
  69     /**
  70      * Creates a new MenuButtonSkin instance, installing the necessary child
  71      * nodes into the Control {@link Control#getChildren() children} list, as
  72      * well as the necessary input mappings for handling key, mouse, etc events.
  73      *
  74      * @param control The control that this skin should be installed onto.
  75      */
  76     public MenuButtonSkin(final MenuButton control) {
  77         super(control);
  78 
  79         // install default input map for the MenuButton-like controls
  80         this.behavior = new MenuButtonBehavior(control);
  81 //        control.setInputMap(behavior.getInputMap());
  82 
  83         // MenuButton's showing does not get updated when autoHide happens,
  84         // as that hide happens under the covers. So we add to the menuButton's
  85         // properties map to which the MenuButton can react and update accordingly..
  86         popup.setOnAutoHide(e -> {
  87             MenuButton menuButton = getSkinnable();
  88             // work around for the fact autohide happens twice
  89             // remove this check when that is fixed.
  90             if (!menuButton.getProperties().containsKey(AUTOHIDE)) {
  91                 menuButton.getProperties().put(AUTOHIDE, Boolean.TRUE);
  92             }
  93         });
  94 
  95         // request focus on content when the popup is shown
  96         popup.setOnShown(event -> {
  97             ContextMenuContent cmContent = (ContextMenuContent)popup.getSkin().getNode();
  98             if (cmContent != null) cmContent.requestFocus();
  99         });
 100 
 101         if (control.getOnAction() == null) {
 102             control.setOnAction(e -> control.show());
 103         }
 104 
 105         label.setLabelFor(control);
 106     }
 107 
 108 
 109 
 110     /***************************************************************************
 111      *                                                                         *
 112      * Public API                                                              *
 113      *                                                                         *
 114      **************************************************************************/
 115 
 116     /** {@inheritDoc} */
 117     @Override public void dispose() {
 118         super.dispose();
 119 
 120         if (behavior != null) {
 121             behavior.dispose();
 122         }
 123     }
 124 
 125 
 126 
 127     /***************************************************************************
 128      *                                                                         *
 129      * Private implementation                                                  *
 130      *                                                                         *
 131      **************************************************************************/
 132 
 133     @Override MenuButtonBehavior getBehavior() {
 134         return behavior;
 135     }
 136 }