jdk/src/solaris/classes/sun/awt/X11/XMenuItemPeer.java
author ant
Fri, 22 Feb 2013 15:13:13 +0400
changeset 15983 26a673dec5b2
parent 13606 91c629b8d59e
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
8006406: lightweight embedding in other Java UI toolkits Reviewed-by: serb, anthony, art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4214
diff changeset
     2
 * Copyright (c) 2002, 2008, 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Rectange occupied by menu item in container's
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final static int SEPARATOR_WIDTH = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final static int SEPARATOR_HEIGHT = 5;
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
     * DEPRECATED:  Replaced by setEnabled(boolean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @see java.awt.peer.MenuItemPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public void enable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        setEnabled( true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * DEPRECATED:  Replaced by setEnabled(boolean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @see java.awt.peer.MenuItemPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void disable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        setEnabled( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Access to target's fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    MenuItem getTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return this.target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    Font getTargetFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (target == null) {
116
9c43d9eb1029 6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents: 2
diff changeset
   196
            return XWindow.getDefaultFont();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   198
        return AWTAccessor.getMenuComponentAccessor().getFont_NoClientCode(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    String getTargetLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
13606
91c629b8d59e 7185678: java/awt/Menu/NullMenuLabelTest/NullMenuLabelTest.java failed with NPE
bagiras
parents: 13604
diff changeset
   205
        String label = AWTAccessor.getMenuItemAccessor().getLabel(target);
91c629b8d59e 7185678: java/awt/Menu/NullMenuLabelTest/NullMenuLabelTest.java failed with NPE
bagiras
parents: 13604
diff changeset
   206
        return (label == null) ? "" : label;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    boolean isTargetEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   213
        return AWTAccessor.getMenuItemAccessor().isEnabled(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Returns true if item and all its parents are enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * This function is used to fix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * 6184485: Popup menu is not disabled on XToolkit even when calling setEnabled (false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    boolean isTargetItemEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   225
        return AWTAccessor.getMenuItemAccessor().isItemEnabled(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    String getTargetActionCommand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   232
        return AWTAccessor.getMenuItemAccessor().getActionCommandImpl(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    MenuShortcut getTargetShortcut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10419
diff changeset
   239
        return AWTAccessor.getMenuItemAccessor().getShortcut(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    String getShortcutText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        //Fix for 6180413: shortcuts should not be displayed for any of the menuitems in a popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (container == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (container.getRootMenuWindow() instanceof XPopupMenuPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        MenuShortcut sc = getTargetShortcut();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        //TODO:This can potentially call user code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return (sc == null) ? null : sc.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
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
     * Basic manipulations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * This function is called when filling item vectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * in XMenuWindow & XMenuBar. We need it because peers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * are created earlier than windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param container the window that this item belongs to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    void setContainer(XBaseMenuWindow container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        synchronized(XBaseMenuWindow.getMenuTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            this.container = container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * returns the window that this item belongs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    XBaseMenuWindow getContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return this.container;
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Overridable behaviour
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * This function should be overriden simply to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * return false in inherited classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    boolean isSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        boolean r = (getTargetLabel().equals("-"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return r;
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Utility functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Returns true if container exists and is showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    boolean isContainerShowing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (container == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return container.isShowing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Repaints item if it is showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    void repaintIfShowing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (isContainerShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            container.postPaintEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * This function is invoked when the user clicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * on menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param when the timestamp of action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    void action(long when) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (!isSeparator() && isTargetItemEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            XWindow.postEventStatic(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                                    getTargetActionCommand(), when,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                                    0));
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Text metrics
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Returns text metrics of menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * This function does not use any locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * and is guaranteed to return some value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * (possibly actual, possibly expired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    TextMetrics getTextMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        TextMetrics textMetrics = this.textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (textMetrics == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            textMetrics = calcTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            this.textMetrics = textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Returns dimensions of item's label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * This function does not use any locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Returns actual or expired  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * or null if error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /*Dimension getTextDimension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        TextMetrics textMetrics = this.textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (textMetrics == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            textMetrics = calcTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            this.textMetrics = textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return (textMetrics != null) ? textMetrics.textDimension : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Returns width of item's shortcut label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * 0 if item has no shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * The height of shortcut can be deternimed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * from text dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * This function does not use any locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * and is guaranteed to return some value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * (possibly actual, possibly expired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /*int getShortcutWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        TextMetrics textMetrics = this.textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (textMetrics == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            textMetrics = calcTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            this.textMetrics = textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return (textMetrics != null) ? textMetrics.shortcutWidth : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    int getTextBaseline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        TextMetrics textMetrics = this.textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (textMetrics == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            textMetrics = calcTextMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            this.textMetrics = textMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return (textMetrics != null) ? textMetrics.textBaseline : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    TextMetrics calcTextMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (container == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (isSeparator()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return new TextMetrics(new Dimension(SEPARATOR_WIDTH, SEPARATOR_HEIGHT), 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        Graphics g = container.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (g == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            g.setFont(getTargetFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            String str = getTargetLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            int width = fm.stringWidth(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            int height = fm.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            Dimension textDimension = new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            int textBaseline = fm.getHeight() - fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            String sc = getShortcutText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            int shortcutWidth = (sc == null) ? 0 : fm.stringWidth(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return new TextMetrics(textDimension, shortcutWidth, textBaseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    void resetTextMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        textMetrics = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if (container != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            container.updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
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
     * Mapping utility functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     ************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Sets mapping of item to window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param bounds bounds of item in container's coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param textOrigin point for drawString in container's coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @see XBaseMenuWindow.map()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    void map(Rectangle bounds, Point textOrigin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        this.bounds = bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        this.textOrigin = textOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * returns bounds of item that were previously set by map() function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * returns origin of item's text that was previously set by map() function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    Point getTextOrigin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return textOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
}