1 /* 2 * Copyright (c) 2011, 2015, 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 java.util.logging.Logger; 29 import java.util.logging.Level; 30 31 import javafx.event.ActionEvent; 32 import javafx.event.EventHandler; 33 34 import javafx.scene.control.ContextMenu; 35 import javafx.scene.control.MenuItem; 36 import javafx.scene.web.WebView; 37 import javafx.stage.WindowEvent; 38 39 import com.sun.webkit.Invoker; 40 import com.sun.webkit.graphics.WCFont; 41 import com.sun.webkit.graphics.WCPoint; 42 import com.sun.webkit.WebPage; 43 import com.sun.webkit.WebPageClient; 44 45 public final class PopupMenuImpl extends com.sun.webkit.PopupMenu { 46 47 private final static Logger log = Logger.getLogger(PopupMenuImpl.class.getName()); 48 49 private final ContextMenu popupMenu; 50 51 public PopupMenuImpl() { 52 popupMenu = new ContextMenu(); 53 54 popupMenu.setOnHidden(t1 -> { 55 log.finer("onHidden"); 56 // Postpone notification. This is to let webkit 57 // to process a mouse event first (in case the 58 // event is the trigger of the closing). Otherwise, 59 // if this is a click in a drop down list, webkit 60 // will reopen the popup assuming it is hidden. 61 Invoker.getInvoker().postOnEventThread(() -> { 62 log.finer("onHidden: notifying"); 63 notifyPopupClosed(); 64 }); 65 }); 66 popupMenu.setOnAction(t -> { 67 MenuItem item = (MenuItem) t.getTarget(); 68 log.log(Level.FINE, "onAction: item={0}", item); 69 notifySelectionCommited(popupMenu.getItems().indexOf(item)); 70 }); 71 } 72 73 @Override protected void show(WebPage page, final int x, final int y, final int width) { 74 if (log.isLoggable(Level.FINE)) { 75 log.log(Level.FINE, "show at [{0}, {1}], width={2}", new Object[] {x, y, width}); 76 } 77 // TODO: doesn't work 78 popupMenu.setPrefWidth(width); 79 popupMenu.setPrefHeight(popupMenu.getHeight()); 80 doShow(popupMenu, page, x, y); 81 } 82 83 @Override protected void hide() { 84 log.fine("hiding"); 85 popupMenu.hide(); 86 } 87 88 @Override protected void appendItem(String itemText, boolean isLabel, 89 boolean isSeparator, boolean isEnabled, 90 int bgColor, int fgColor, WCFont font) 91 { 92 if (log.isLoggable(Level.FINEST)) { 93 log.log(Level.FINEST, "itemText={0}, isLabel={1}, isSeparator={2}, isEnabled={3}, " + 94 "bgColor={4}, fgColor={5}, font={6}", new Object[] {itemText, isLabel, 95 isSeparator, isEnabled, bgColor, fgColor, font}); 96 } 97 MenuItem item; 98 99 if (isSeparator) { 100 item = new ContextMenuImpl.SeparatorImpl(null); 101 } else { 102 item = new MenuItem(itemText); 103 item.setDisable(!isEnabled); 104 // TODO: set the rest of properties 105 } 106 107 item.setMnemonicParsing(false); 108 popupMenu.getItems().add(item); 109 } 110 111 @Override protected void setSelectedItem(int index) { 112 log.log(Level.FINEST, "index={0}", index); 113 // TODO requestFocus is not supported currently 114 //popupMenu.getItems().get(index).requestFocus(); 115 } 116 117 static void doShow(final ContextMenu popup, WebPage page, int anchorX, int anchorY) { 118 WebPageClient<WebView> client = page.getPageClient(); 119 assert (client != null); 120 WCPoint pt = client.windowToScreen(new WCPoint(anchorX, anchorY)); 121 popup.show(client.getContainer().getScene().getWindow(), pt.getX(), pt.getY()); 122 } 123 }