jdk/src/solaris/classes/sun/awt/X11/XPopupMenuPeer.java
author mchung
Tue, 29 Sep 2009 16:03:03 -0700
changeset 3938 ef327bd847c0
parent 116 9c43d9eb1029
child 5506 202f599c92aa
permissions -rw-r--r--
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes Summary: Replace calls to Logger with sun.util.logging.PlatformLogger Reviewed-by: prr, art, alexp, dcherepanov, igor, dav, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
116
9c43d9eb1029 6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents: 2
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.peer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Vector;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 116
diff changeset
    36
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class XPopupMenuPeer extends XMenuWindow implements PopupMenuPeer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Data members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     ************************************************/
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 116
diff changeset
    47
    private static PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XBaseMenuWindow");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Primary members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private XComponentPeer componentPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private PopupMenu popupMenuTarget;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * If mouse button is clicked on item showing submenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * we have to hide its submenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * This member saves the submenu under cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Only if it's showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private XMenuPeer showingMousePressedSubmenu = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Painting constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final static int CAPTION_MARGIN_TOP = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final static int CAPTION_SEPARATOR_HEIGHT = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Menu's fields & methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    //Fix for 6184485: Popup menu is not disabled on XToolkit even when calling setEnabled (false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final static Field f_enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    //Fix for 6267144: PIT: Popup menu label is not shown, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private final static Field f_label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final static Method m_getFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private final static Field f_items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        f_enabled = SunToolkit.getField(MenuItem.class, "enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        f_label = SunToolkit.getField(MenuItem.class, "label");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        f_items = SunToolkit.getField(Menu.class, "items");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        m_getFont = SunToolkit.getMethod(MenuComponent.class, "getFont_NoClientCode", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    XPopupMenuPeer(PopupMenu target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.popupMenuTarget = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Implementaion of interface methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * From MenuComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public void setFont(Font f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        resetMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        setItemsFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        postPaintEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * From MenuItemPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void setLabel(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        resetMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        postPaintEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void setEnabled(boolean enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        postPaintEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * DEPRECATED:  Replaced by setEnabled(boolean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see java.awt.peer.MenuItemPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public void enable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        setEnabled( true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * DEPRECATED:  Replaced by setEnabled(boolean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see java.awt.peer.MenuItemPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void disable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        setEnabled( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * From MenuPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * addSeparator routines are not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * in peers. Shared code invokes addItem("-")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * for adding separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public void addSeparator() {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 116
diff changeset
   149
        if (log.isLoggable(PlatformLogger.FINER)) log.finer("addSeparator is not implemented");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * From PopupMenuPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void show(Event e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        target = (Component)e.target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // Get menus from the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        Vector targetItemVector = getMenuTargetItems();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (targetItemVector != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            reloadItems(targetItemVector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            //Fix for 6287092: JCK15a: api/java_awt/interactive/event/EventTests.html#EventTest0015 fails, mustang
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Point tl = target.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            Point pt = new Point(tl.x + e.x, tl.y + e.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            //Fixed 6266513: Incorrect key handling in XAWT popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            //No item should be selected when showing popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (!ensureCreated()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            Dimension dim = getDesiredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            //Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            //near the periphery of the screen, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            Rectangle bounds = getWindowBounds(pt, dim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            reshape(bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            xSetVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            toFront();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            selectItem(null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            grabInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Access to target's fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    //Fix for 6267144: PIT: Popup menu label is not shown, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    Font getTargetFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (popupMenuTarget == null) {
116
9c43d9eb1029 6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents: 2
diff changeset
   190
            return XWindow.getDefaultFont();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return (Font)m_getFont.invoke(popupMenuTarget, new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
116
9c43d9eb1029 6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents: 2
diff changeset
   199
        return XWindow.getDefaultFont();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    String getTargetLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            String label = (String)f_label.get(popupMenuTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return (label == null) ? "" : label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    //Fix for 6184485: Popup menu is not disabled on XToolkit even when calling setEnabled (false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    boolean isTargetEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (popupMenuTarget == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return f_enabled.getBoolean(popupMenuTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    Vector getMenuTargetItems() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return (Vector)f_items.get(popupMenuTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            iae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Utility functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    //Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    //near the periphery of the screen, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Calculates placement of popup menu window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * given origin in global coordinates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * size of menu window. Returns suggested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * rectangle for menu window in global coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param origin the origin point specified in show()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * function converted to global coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param windowSize the desired size of menu's window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    protected Rectangle getWindowBounds(Point origin, Dimension windowSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        Rectangle globalBounds = new Rectangle(origin.x, origin.y, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        Rectangle res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        res = fitWindowRight(globalBounds, windowSize, screenSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        res = fitWindowLeft(globalBounds, windowSize, screenSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        res = fitWindowBelow(globalBounds, windowSize, screenSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        res = fitWindowAbove(globalBounds, windowSize, screenSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return fitWindowToScreen(windowSize, screenSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Overriden XMenuWindow caption-painting functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Necessary to fix 6267144: PIT: Popup menu label is not shown, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Returns height of menu window's caption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Can be overriden for popup menus and tear-off menus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    protected Dimension getCaptionSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        String s = getTargetLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (s.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        Graphics g = getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (g == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            g.setFont(getTargetFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            String str = getTargetLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int width = fm.stringWidth(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            int height = CAPTION_MARGIN_TOP + fm.getHeight() + CAPTION_SEPARATOR_HEIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            Dimension textDimension = new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return textDimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Paints menu window's caption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Can be overriden for popup menus and tear-off menus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Default implementation does nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    protected void paintCaption(Graphics g, Rectangle rect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        String s = getTargetLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (s.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        g.setFont(getTargetFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        String str = getTargetLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        int width = fm.stringWidth(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        int textx = rect.x + (rect.width - width) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int texty = rect.y + CAPTION_MARGIN_TOP + fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int sepy = rect.y + rect.height - CAPTION_SEPARATOR_HEIGHT / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        g.setColor(isTargetEnabled() ? getForegroundColor() : getDisabledColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        g.drawString(s, textx, texty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        draw3DRect(g, rect.x, sepy,  rect.width, 2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Overriden XBaseMenuWindow functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    protected void doDispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        super.doDispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        XToolkit.targetDisposedPeer(popupMenuTarget, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    protected void handleEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        switch(event.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        case MouseEvent.MOUSE_CLICKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        case MouseEvent.MOUSE_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        case MouseEvent.MOUSE_ENTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        case MouseEvent.MOUSE_EXITED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            doHandleJavaMouseEvent((MouseEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        case KeyEvent.KEY_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        case KeyEvent.KEY_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            doHandleJavaKeyEvent((KeyEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            super.handleEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Overriden XWindow general-purpose functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    void ungrabInputImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Overriden XWindow keyboard processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * In previous version keys were handled in handleKeyPress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Now we override this function do disable F10 explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * processing. All processing is done using KeyEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public void handleKeyPress(XEvent xev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        XKeyEvent xkey = xev.get_xkey();
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 116
diff changeset
   385
        if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            log.fine(xkey.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (isEventDisabled(xev)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        final Component currentSource = (Component)getEventSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        handleKeyPress(xkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}