1 /*
   2  * Copyright (c) 2011, 2018, 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 com.sun.javafx.webkit.theme;
  27 
  28 import com.sun.javafx.logging.PlatformLogger;
  29 import com.sun.javafx.logging.PlatformLogger.Level;
  30 
  31 import javafx.scene.control.Separator;
  32 import javafx.scene.control.ContextMenu;
  33 import javafx.scene.control.MenuItem;
  34 import javafx.scene.control.Menu;
  35 import javafx.scene.control.CheckMenuItem;
  36 
  37 import javafx.collections.ObservableList;
  38 import javafx.collections.FXCollections;
  39 
  40 import com.sun.webkit.ContextMenuItem;
  41 
  42 public final class ContextMenuImpl extends com.sun.webkit.ContextMenu {
  43 
  44     private final static PlatformLogger log = PlatformLogger.getLogger(ContextMenuImpl.class.getName());
  45 
  46     private final ObservableList<ContextMenuItem> items =
  47             FXCollections.observableArrayList();
  48 
  49     @Override protected void show(final ShowContext showContext, int x, int y) {
  50         if (log.isLoggable(Level.FINE)) {
  51             log.fine("show at [{0}, {1}]", new Object[] {x, y});
  52         }
  53         final ContextMenu popupMenu = new ContextMenu();
  54 
  55         popupMenu.setOnAction(t -> {
  56             MenuItem item = (MenuItem) t.getTarget();
  57             log.fine("onAction: item={0}", item);
  58             showContext.notifyItemSelected(((MenuItemPeer)item).getItemPeer().getAction());
  59         });
  60 
  61         popupMenu.getItems().addAll(fillMenu());
  62         PopupMenuImpl.doShow(popupMenu, showContext.getPage(), x, y);
  63     }
  64 
  65     @Override protected void appendItem(ContextMenuItem item) {
  66         insertItem(item, items.size());
  67     }
  68 
  69     @Override protected void insertItem(ContextMenuItem item, int index) {
  70         if (log.isLoggable(Level.FINE)) {
  71             log.fine("item={0}, index={1}", new Object[] {item, index});
  72         }
  73         if (item == null) {
  74             return;
  75         }
  76         items.remove(item);
  77 
  78         if (items.size() == 0) {
  79             items.add(item);
  80         } else {
  81             items.add(index, item);
  82         }
  83     }
  84 
  85     @Override protected int getItemCount() {
  86         return items.size();
  87     }
  88 
  89     private MenuItem createMenuItem(ContextMenuItem item) {
  90         log.fine("item={0}", item);
  91 
  92         if (item.getType() == ContextMenuItem.SUBMENU_TYPE) {
  93             MenuImpl menu = new MenuImpl(item.getTitle());
  94             if (item.getSubmenu() != null) {
  95                 menu.getItems().addAll(((ContextMenuImpl)item.getSubmenu()).fillMenu());
  96             }
  97             return menu;
  98 
  99         } else if (item.getType() == ContextMenuItem.ACTION_TYPE) {
 100 
 101             MenuItem mi = null;
 102             if (item.isChecked()) {
 103                 mi = new CheckMenuItemImpl(item);
 104             } else {
 105                 mi = new MenuItemImpl(item);
 106             }
 107             mi.setDisable(!item.isEnabled());
 108             return mi;
 109 
 110         } else if (item.getType() == ContextMenuItem.SEPARATOR_TYPE) {
 111             return new SeparatorImpl(item);
 112         }
 113         throw new java.lang.IllegalArgumentException("unexpected item type");
 114     }
 115 
 116     private ObservableList<MenuItem> fillMenu() {
 117         ObservableList<MenuItem> s = FXCollections.observableArrayList();
 118         for (ContextMenuItem item: items) {
 119             s.add(createMenuItem(item));
 120         }
 121         return s;
 122     }
 123 
 124     private interface MenuItemPeer {
 125         public ContextMenuItem getItemPeer();
 126     }
 127 
 128     private static final class MenuItemImpl extends MenuItem implements MenuItemPeer {
 129         private final ContextMenuItem itemPeer;
 130         private MenuItemImpl(ContextMenuItem itemPeer) { super(itemPeer.getTitle()); this.itemPeer = itemPeer; }
 131         @Override public ContextMenuItem getItemPeer() { return itemPeer; }
 132     }
 133 
 134     private static final class CheckMenuItemImpl extends CheckMenuItem implements MenuItemPeer {
 135         private final ContextMenuItem itemPeer;
 136         private CheckMenuItemImpl(ContextMenuItem itemPeer) { this.itemPeer = itemPeer; }
 137         @Override public ContextMenuItem getItemPeer() { return itemPeer; }
 138     }
 139 
 140     private static final class MenuImpl extends Menu {
 141         private MenuImpl(String text) { super(text); }
 142     }
 143 
 144     static final class SeparatorImpl extends MenuItem implements MenuItemPeer {
 145         private final ContextMenuItem itemPeer;
 146         SeparatorImpl(ContextMenuItem itemPeer) {
 147             this.itemPeer = itemPeer;
 148             setGraphic(new Separator());
 149             setDisable(true);
 150         }
 151         @Override public ContextMenuItem getItemPeer() { return itemPeer; }
 152     }
 153 }
 154