1 /*
   2  * Copyright (c) 2014, 2016, 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 package com.sun.javafx.scene.control;
  26 
  27 import javafx.collections.ObservableList;
  28 import javafx.scene.Node;
  29 import javafx.scene.control.ContextMenu;
  30 import javafx.scene.control.Menu;
  31 import javafx.scene.control.MenuItem;
  32 import javafx.scene.control.skin.ContextMenuSkin;
  33 
  34 import java.util.Optional;
  35 import javafx.scene.layout.Region;
  36 
  37 public class ContextMenuContentShim {
  38 
  39     private ContextMenuContentShim() {
  40         // no-op
  41     }
  42 
  43     public static Region get_selectedBackground(ContextMenuContent menu) {
  44         return menu.selectedBackground;
  45     }
  46 
  47     public static Menu getOpenSubMenu(ContextMenu menu) {
  48         ContextMenuContent content = getMenuContent(menu);
  49         return content.getOpenSubMenu();
  50     }
  51 
  52     public static Menu getShowingSubMenu(ContextMenu menu) {
  53         ContextMenuContent content = getMenuContent(menu);
  54         Menu showingSubMenu = content.getOpenSubMenu();
  55         ContextMenu subContextMenu = content.getSubMenu();
  56 
  57         while (showingSubMenu != null) {
  58             content = getMenuContent(subContextMenu);
  59 
  60             Menu newShowingMenu = content == null ? null : content.getOpenSubMenu();
  61             subContextMenu = content == null ? null : content.getSubMenu();
  62 
  63             if (newShowingMenu == null) {
  64                 break;
  65             }
  66         }
  67         return showingSubMenu;
  68     }
  69 
  70     public static ObservableList<MenuItem> getShowingMenuItems(ContextMenu menu) {
  71         ContextMenuContent content = getMenuContent(menu);
  72         Menu showingSubMenu = content.getOpenSubMenu();
  73         ContextMenu subContextMenu = content.getSubMenu();
  74 
  75         if (showingSubMenu == null || subContextMenu == null) {
  76             return menu.getItems();
  77         }
  78 
  79         while (showingSubMenu != null) {
  80             content = getMenuContent(subContextMenu);
  81 
  82             Menu newShowingMenu = content == null ? null : content.getOpenSubMenu();
  83             subContextMenu = content == null ? null : content.getSubMenu();
  84 
  85             if (newShowingMenu == null) {
  86                 break;
  87             }
  88         }
  89         return showingSubMenu.getItems();
  90     }
  91 
  92     public static Optional<ContextMenuContent> getShowingMenuContent(ContextMenu menu) {
  93         ContextMenuContent content = getMenuContent(menu);
  94         Menu showingSubMenu = content.getOpenSubMenu();
  95         ContextMenu subContextMenu = content.getSubMenu();
  96         return showingSubMenu != null &&
  97                subContextMenu != null &&
  98                subContextMenu.isShowing() ? getShowingMenuContent(subContextMenu) : Optional.of(content);
  99     }
 100 
 101     private static ContextMenuContent getMenuContent(ContextMenu menu) {
 102         ContextMenuSkin skin = (ContextMenuSkin) menu.getSkin();
 103         Node node = skin.getNode();
 104         if (node instanceof ContextMenuContent) {
 105             return (ContextMenuContent) node;
 106         }
 107         return null;
 108     }
 109 
 110     public static int getCurrentFocusedIndex(ContextMenu menu) {
 111 //        Optional<Integer> index = getShowingMenuContent(menu).flatMap(content -> Optional.of(content.getCurrentFocusIndex()));
 112 //        return index.orElse(-1);
 113 
 114         Optional<ContextMenuContent> showingMenuContent = getShowingMenuContent(menu);
 115         if (showingMenuContent.isPresent()) {
 116             ContextMenuContent content = showingMenuContent.get();
 117             return content.getCurrentFocusIndex();
 118         }
 119 
 120         return -1;
 121     }
 122 
 123     public static MenuItem getCurrentFocusedItem(ContextMenu menu) {
 124         ObservableList<MenuItem> showingMenuItems = getShowingMenuItems(menu);
 125 
 126 //        Optional<MenuItem> item = getShowingMenuContent(menu)
 127 //                .flatMap(content -> Optional.of(content.getCurrentFocusIndex()))
 128 //                .flatMap(index   -> Optional.of(showingMenuItems.get(index)));
 129 //        return item.orElse(null);
 130 
 131         Optional<ContextMenuContent> showingMenuContent = getShowingMenuContent(menu);
 132         if (showingMenuContent.isPresent()) {
 133             ContextMenuContent content = showingMenuContent.get();
 134             int currentFocusIndex = content.getCurrentFocusIndex();
 135             return currentFocusIndex == -1 ? null : showingMenuItems.get(currentFocusIndex);
 136         }
 137 
 138         return null;
 139     }
 140 
 141 }