src/java.desktop/unix/classes/sun/awt/X11/XMenuItemPeer.java
author serb
Wed, 18 Apr 2018 14:07:41 -0700
changeset 49998 9070717a16a1
parent 47216 71c04702a3d5
permissions -rw-r--r--
8187392: Deprecated methods in the peers can be removed Reviewed-by: prr, kaddepalli
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49998
9070717a16a1 8187392: Deprecated methods in the peers can be removed
serb
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4214
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4214
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4214
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4214
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4214
diff changeset
    23
 * questions.
2
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
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
    31
import sun.awt.AWTAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class XMenuItemPeer implements MenuItemPeer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     * Data members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Primary members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Window that this item belongs to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private XBaseMenuWindow container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Target MenuItem. Note that 'target' member
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * in XWindow is required for dispatching events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * This member is only used for accessing its fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * and firing ActionEvent & ItemEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private MenuItem target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Mapping to window
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 13606
diff changeset
    63
     * Rectangle occupied by menu item in container's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * coordinates. Filled by map(...) function from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * XBaseMenuWindow.map()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private Rectangle bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Point in container's coordinate system used as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * origin by drawText.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private Point textOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Size constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30948
diff changeset
    78
    private static final int SEPARATOR_WIDTH = 20;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30948
diff changeset
    79
    private static final int SEPARATOR_HEIGHT = 5;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Text Metrics
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
     * Text metrics are filled in calcTextMetrics function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * and reset in resetTextMetrics function. Text metrics
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * contain calculated dimensions of various components of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private TextMetrics textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static class TextMetrics implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * Calculated text size members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        private Dimension textDimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        private int shortcutWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        private int textBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        TextMetrics(Dimension textDimension, int shortcutWidth, int textBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            this.textDimension = textDimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            this.shortcutWidth = shortcutWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            this.textBaseline = textBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } catch (CloneNotSupportedException ex) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   113
                throw new InternalError(ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Dimension getTextDimension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return this.textDimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int getShortcutWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return this.shortcutWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int getTextBaseline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return this.textBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    XMenuItemPeer(MenuItem target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
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
     * Implementaion of interface methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * From MenuComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        //Empty function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void setFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        resetTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        repaintIfShowing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * From MenuItemPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void setLabel(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        resetTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        repaintIfShowing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void setEnabled(boolean enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        repaintIfShowing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Access to target's fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    MenuItem getTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return this.target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    Font getTargetFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (target == null) {
116
9c43d9eb1029 6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents: 2
diff changeset
   180
            return XWindow.getDefaultFont();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   182
        return AWTAccessor.getMenuComponentAccessor().getFont_NoClientCode(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    String getTargetLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
13606
91c629b8d59e 7185678: java/awt/Menu/NullMenuLabelTest/NullMenuLabelTest.java failed with NPE
bagiras
parents: 13604
diff changeset
   189
        String label = AWTAccessor.getMenuItemAccessor().getLabel(target);
91c629b8d59e 7185678: java/awt/Menu/NullMenuLabelTest/NullMenuLabelTest.java failed with NPE
bagiras
parents: 13604
diff changeset
   190
        return (label == null) ? "" : label;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    boolean isTargetEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   197
        return AWTAccessor.getMenuItemAccessor().isEnabled(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Returns true if item and all its parents are enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * This function is used to fix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * 6184485: Popup menu is not disabled on XToolkit even when calling setEnabled (false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    boolean isTargetItemEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   209
        return AWTAccessor.getMenuItemAccessor().isItemEnabled(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    String getTargetActionCommand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   216
        return AWTAccessor.getMenuItemAccessor().getActionCommandImpl(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    MenuShortcut getTargetShortcut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   223
        return AWTAccessor.getMenuItemAccessor().getShortcut(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    String getShortcutText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        //Fix for 6180413: shortcuts should not be displayed for any of the menuitems in a popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (container == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (container.getRootMenuWindow() instanceof XPopupMenuPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        MenuShortcut sc = getTargetShortcut();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        //TODO:This can potentially call user code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return (sc == null) ? null : sc.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Basic manipulations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * This function is called when filling item vectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * in XMenuWindow & XMenuBar. We need it because peers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * are created earlier than windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param container the window that this item belongs to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    void setContainer(XBaseMenuWindow container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        synchronized(XBaseMenuWindow.getMenuTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            this.container = container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * returns the window that this item belongs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    XBaseMenuWindow getContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return this.container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Overridable behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * This function should be overriden simply to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * return false in inherited classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    boolean isSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        boolean r = (getTargetLabel().equals("-"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Utility functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns true if container exists and is showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    boolean isContainerShowing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (container == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return container.isShowing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Repaints item if it is showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    void repaintIfShowing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (isContainerShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            container.postPaintEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * This function is invoked when the user clicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * on menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param when the timestamp of action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
37572
7b0c62fdb5d8 6191390: Action Event triggered by list does not reflect the modifiers properly on win32
arapte
parents: 32865
diff changeset
   310
    void action(long when, int modifiers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (!isSeparator() && isTargetItemEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            XWindow.postEventStatic(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                                    getTargetActionCommand(), when,
37572
7b0c62fdb5d8 6191390: Action Event triggered by list does not reflect the modifiers properly on win32
arapte
parents: 32865
diff changeset
   314
                                                    modifiers));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Text metrics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Returns text metrics of menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * This function does not use any locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * and is guaranteed to return some value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * (possibly actual, possibly expired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    TextMetrics getTextMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        TextMetrics textMetrics = this.textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (textMetrics == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            textMetrics = calcTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            this.textMetrics = textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Returns dimensions of item's label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * This function does not use any locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Returns actual or expired  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * or null if error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /*Dimension getTextDimension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        TextMetrics textMetrics = this.textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (textMetrics == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            textMetrics = calcTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            this.textMetrics = textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return (textMetrics != null) ? textMetrics.textDimension : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Returns width of item's shortcut label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * 0 if item has no shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * The height of shortcut can be deternimed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * from text dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * This function does not use any locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * and is guaranteed to return some value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * (possibly actual, possibly expired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /*int getShortcutWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        TextMetrics textMetrics = this.textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (textMetrics == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            textMetrics = calcTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            this.textMetrics = textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return (textMetrics != null) ? textMetrics.shortcutWidth : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    int getTextBaseline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        TextMetrics textMetrics = this.textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (textMetrics == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            textMetrics = calcTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            this.textMetrics = textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return (textMetrics != null) ? textMetrics.textBaseline : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    TextMetrics calcTextMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (container == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (isSeparator()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return new TextMetrics(new Dimension(SEPARATOR_WIDTH, SEPARATOR_HEIGHT), 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        Graphics g = container.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (g == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            g.setFont(getTargetFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            String str = getTargetLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            int width = fm.stringWidth(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            int height = fm.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            Dimension textDimension = new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            int textBaseline = fm.getHeight() - fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            String sc = getShortcutText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            int shortcutWidth = (sc == null) ? 0 : fm.stringWidth(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return new TextMetrics(textDimension, shortcutWidth, textBaseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    void resetTextMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        textMetrics = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (container != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            container.updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Mapping utility functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Sets mapping of item to window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param bounds bounds of item in container's coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param textOrigin point for drawString in container's coordinates
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 25859
diff changeset
   424
     * @see XBaseMenuWindow#map()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    void map(Rectangle bounds, Point textOrigin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        this.bounds = bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        this.textOrigin = textOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * returns bounds of item that were previously set by map() function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * returns origin of item's text that was previously set by map() function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    Point getTextOrigin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return textOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
}