jdk/src/share/classes/javax/swing/plaf/basic/BasicMenuItemUI.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
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: 4394
diff changeset
     2
 * Copyright (c) 1997, 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: 4394
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: 4394
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: 4394
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
    38
import sun.swing.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * BasicMenuItem implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author David Karlton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Fredrik Lagerblad
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class BasicMenuItemUI extends MenuItemUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected JMenuItem menuItem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected Color selectionBackground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected Color selectionForeground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected Color disabledForeground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected Color acceleratorForeground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected Color acceleratorSelectionForeground;
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    56
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    57
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    58
     * Accelerator delimiter string, such as {@code '+'} in {@code 'Ctrl+C'}.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    59
     * @since 1.7
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    60
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    61
    protected String acceleratorDelimiter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected int defaultTextIconGap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected Font acceleratorFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected MouseInputListener mouseInputListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected MenuDragMouseListener menuDragMouseListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected MenuKeyListener menuKeyListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <code>PropertyChangeListener</code> returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <code>createPropertyChangeListener</code>. You should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * need to access this field, rather if you want to customize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <code>PropertyChangeListener</code> override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <code>createPropertyChangeListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // BasicMenuUI also uses this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected Icon arrowIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected Icon checkIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected boolean oldBorderPainted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /* diagnostic aids -- should be false for production builds. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final boolean TRACE =   false; // trace creates and disposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final boolean VERBOSE = false; // show reuse hits/misses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static final boolean DEBUG =   false;  // show bad params, misc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // NOTE: BasicMenuUI also calls into this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        map.put(new Actions(Actions.CLICK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        BasicLookAndFeel.installAudioActionMap(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return new BasicMenuItemUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        menuItem = (JMenuItem) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        installComponents(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        String prefix = getPropertyPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1299
diff changeset
   118
        // use default if missing so that BasicMenuItemUI can be used in other
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1299
diff changeset
   119
        // LAFs like Nimbus
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1299
diff changeset
   120
        if (acceleratorFont == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1299
diff changeset
   121
            acceleratorFont = UIManager.getFont("MenuItem.font");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1299
diff changeset
   122
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Object opaque = UIManager.get(getPropertyPrefix() + ".opaque");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (opaque != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            LookAndFeel.installProperty(menuItem, "opaque", opaque);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            LookAndFeel.installProperty(menuItem, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if(menuItem.getMargin() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
           (menuItem.getMargin() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            menuItem.setMargin(UIManager.getInsets(prefix + ".margin"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   136
        LookAndFeel.installProperty(menuItem, "iconTextGap", Integer.valueOf(4));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        defaultTextIconGap = menuItem.getIconTextGap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        LookAndFeel.installBorder(menuItem, prefix + ".border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        oldBorderPainted = menuItem.isBorderPainted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        LookAndFeel.installProperty(menuItem, "borderPainted",
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1299
diff changeset
   142
                                    UIManager.getBoolean(prefix + ".borderPainted"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        LookAndFeel.installColorsAndFont(menuItem,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                         prefix + ".background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                         prefix + ".foreground",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                         prefix + ".font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // MenuItem specific defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (selectionBackground == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            selectionBackground instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            selectionBackground =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                UIManager.getColor(prefix + ".selectionBackground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (selectionForeground == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            selectionForeground instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            selectionForeground =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                UIManager.getColor(prefix + ".selectionForeground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (disabledForeground == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            disabledForeground instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            disabledForeground =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                UIManager.getColor(prefix + ".disabledForeground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (acceleratorForeground == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            acceleratorForeground instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            acceleratorForeground =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                UIManager.getColor(prefix + ".acceleratorForeground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (acceleratorSelectionForeground == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            acceleratorSelectionForeground instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            acceleratorSelectionForeground =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                UIManager.getColor(prefix + ".acceleratorSelectionForeground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Get accelerator delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        acceleratorDelimiter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            UIManager.getString("MenuItem.acceleratorDelimiter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (acceleratorDelimiter == null) { acceleratorDelimiter = "+"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // Icons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (arrowIcon == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            arrowIcon instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            arrowIcon = UIManager.getIcon(prefix + ".arrowIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (checkIcon == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            checkIcon instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            checkIcon = UIManager.getIcon(prefix + ".checkIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            //In case of column layout, .checkIconFactory is defined for this UI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            //the icon is compatible with it and useCheckAndArrow() is true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            //then the icon is handled by the checkIcon.
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   189
            boolean isColumnLayout = MenuItemLayoutHelper.isColumnLayout(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    BasicGraphicsUtils.isLeftToRight(menuItem), menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (isColumnLayout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                MenuItemCheckIconFactory iconFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    (MenuItemCheckIconFactory) UIManager.get(prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        + ".checkIconFactory");
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   195
                if (iconFactory != null
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   196
                        && MenuItemLayoutHelper.useCheckAndArrow(menuItem)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        && iconFactory.isCompatible(checkIcon, prefix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    checkIcon = iconFactory.getIcon(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    protected void installComponents(JMenuItem menuItem){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        BasicHTML.updateRenderer(menuItem, menuItem.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return "MenuItem";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if ((mouseInputListener = createMouseInputListener(menuItem)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            menuItem.addMouseListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            menuItem.addMouseMotionListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if ((menuDragMouseListener = createMenuDragMouseListener(menuItem)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            menuItem.addMenuDragMouseListener(menuDragMouseListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if ((menuKeyListener = createMenuKeyListener(menuItem)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            menuItem.addMenuKeyListener(menuKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if ((propertyChangeListener = createPropertyChangeListener(menuItem)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            menuItem.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        installLazyActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        updateAcceleratorBinding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    void installLazyActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        LazyActionMap.installLazyActionMap(menuItem, BasicMenuItemUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                           getPropertyPrefix() + ".actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        menuItem = (JMenuItem)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        uninstallComponents(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        uninstallKeyboardActions();
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   247
        MenuItemLayoutHelper.clearUsedParentClientProperties(menuItem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        menuItem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        LookAndFeel.uninstallBorder(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        LookAndFeel.installProperty(menuItem, "borderPainted", oldBorderPainted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (menuItem.getMargin() instanceof UIResource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            menuItem.setMargin(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (arrowIcon instanceof UIResource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            arrowIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (checkIcon instanceof UIResource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            checkIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    protected void uninstallComponents(JMenuItem menuItem){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        BasicHTML.updateRenderer(menuItem, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (mouseInputListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            menuItem.removeMouseListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            menuItem.removeMouseMotionListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (menuDragMouseListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            menuItem.removeMenuDragMouseListener(menuDragMouseListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (menuKeyListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            menuItem.removeMenuKeyListener(menuKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (propertyChangeListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            menuItem.removePropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        mouseInputListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        menuDragMouseListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        menuKeyListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        propertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        SwingUtilities.replaceUIActionMap(menuItem, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        SwingUtilities.replaceUIInputMap(menuItem, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                         WHEN_IN_FOCUSED_WINDOW, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    protected MouseInputListener createMouseInputListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    protected MenuKeyListener createMenuKeyListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return null;
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
     * Creates a <code>PropertyChangeListener</code> which will be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * the menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * If this method returns null then it will not be added to the menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @return an instance of a <code>PropertyChangeListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    protected PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                  createPropertyChangeListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    InputMap createInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return new ComponentInputMapUIResource(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    void updateAcceleratorBinding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        KeyStroke accelerator = menuItem.getAccelerator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        InputMap windowInputMap = SwingUtilities.getUIInputMap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                       menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (windowInputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            windowInputMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (accelerator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (windowInputMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                windowInputMap = createInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                                WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                SwingUtilities.replaceUIInputMap(menuItem,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                           JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            windowInputMap.put(accelerator, "doClick");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        Dimension d = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return getPreferredMenuItemSize(c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                        checkIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                        arrowIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                        defaultTextIconGap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Dimension d = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    protected Dimension getPreferredMenuItemSize(JComponent c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                                 Icon checkIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                                 Icon arrowIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                                 int defaultTextIconGap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // The method also determines the preferred width of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // parent popup menu (through DefaultMenuLayout class).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // The menu width equals to the maximal width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // among child menu items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // Menu item width will be a sum of the widest check icon, label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        // arrow icon and accelerator text among neighbor menu items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // For the latest menu item we will know the maximal widths exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // It will be the widest menu item and it will determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // the width of the parent popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // There is a conceptual problem: if user sets preferred size manually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        // for a menu item, this method won't be called for it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // (see JComponent.getPreferredSize()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // maximal widths won't be calculated, other menu items won't be able
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // to take them into account and will be layouted in such a way,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // as there is no the item with manual preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // But after the first paint() method call, all maximal widths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // will be correctly calculated and layout of some menu items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        // can be changed. For example, it can cause a shift of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // the icon and text when user points a menu item by mouse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        JMenuItem mi = (JMenuItem) c;
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   412
        MenuItemLayoutHelper lh = new MenuItemLayoutHelper(mi, checkIcon,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   413
                arrowIcon, MenuItemLayoutHelper.createMaxRect(), defaultTextIconGap,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   414
                acceleratorDelimiter, BasicGraphicsUtils.isLeftToRight(mi),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   415
                mi.getFont(), acceleratorFont,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   416
                MenuItemLayoutHelper.useCheckAndArrow(menuItem),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   417
                getPropertyPrefix());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        Dimension result = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        // Calculate the result width
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   422
        result.width = lh.getLeadingGap();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   423
        MenuItemLayoutHelper.addMaxWidth(lh.getCheckSize(),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   424
                lh.getAfterCheckIconGap(), result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // Take into account mimimal text offset.
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   426
        if ((!lh.isTopLevelMenu())
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   427
                && (lh.getMinTextOffset() > 0)
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   428
                && (result.width < lh.getMinTextOffset())) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   429
            result.width = lh.getMinTextOffset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   431
        MenuItemLayoutHelper.addMaxWidth(lh.getLabelSize(), lh.getGap(), result);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   432
        MenuItemLayoutHelper.addMaxWidth(lh.getAccSize(), lh.getGap(), result);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   433
        MenuItemLayoutHelper.addMaxWidth(lh.getArrowSize(), lh.getGap(), result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // Calculate the result height
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   436
        result.height = MenuItemLayoutHelper.max(lh.getCheckSize().getHeight(),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   437
                lh.getLabelSize().getHeight(), lh.getAccSize().getHeight(),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   438
                lh.getArrowSize().getHeight());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        // Take into account menu item insets
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   441
        Insets insets = lh.getMenuItem().getInsets();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if(insets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            result.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            result.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        // if the width is even, bump it up one. This is critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // for the focus dash line to draw properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if(result.width%2 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            result.width++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // if the height is even, bump it up one. This is critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // for the text to center properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if(result.height%2 == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                && Boolean.TRUE !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    UIManager.get(getPropertyPrefix() + ".evenHeight")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            result.height++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * We draw the background in paintMenuItem()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * so override update (which fills the background of opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * components by default) to just call paint().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        paint(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        paintMenuItem(g, c, checkIcon, arrowIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                      selectionBackground, selectionForeground,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                      defaultTextIconGap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    protected void paintMenuItem(Graphics g, JComponent c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                     Icon checkIcon, Icon arrowIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                     Color background, Color foreground,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                     int defaultTextIconGap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        // Save original graphics font and color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        Font holdf = g.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        Color holdc = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        JMenuItem mi = (JMenuItem) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        g.setFont(mi.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        Rectangle viewRect = new Rectangle(0, 0, mi.getWidth(), mi.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        applyInsets(viewRect, mi.getInsets());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   494
        MenuItemLayoutHelper lh = new MenuItemLayoutHelper(mi, checkIcon,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   495
                arrowIcon, viewRect, defaultTextIconGap, acceleratorDelimiter,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   496
                BasicGraphicsUtils.isLeftToRight(mi), mi.getFont(),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   497
                acceleratorFont, MenuItemLayoutHelper.useCheckAndArrow(menuItem),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   498
                getPropertyPrefix());
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   499
        MenuItemLayoutHelper.LayoutResult lr = lh.layoutMenuItem();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        paintBackground(g, mi, background);
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   502
        paintCheckIcon(g, lh, lr, holdc, foreground);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   503
        paintIcon(g, lh, lr, holdc);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   504
        paintText(g, lh, lr);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   505
        paintAccText(g, lh, lr);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   506
        paintArrowIcon(g, lh, lr, foreground);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        // Restore original graphics font and color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        g.setColor(holdc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        g.setFont(holdf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   513
    private void paintIcon(Graphics g, MenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   514
                           MenuItemLayoutHelper.LayoutResult lr, Color holdc) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   515
        if (lh.getIcon() != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            Icon icon;
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   517
            ButtonModel model = lh.getMenuItem().getModel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            if (!model.isEnabled()) {
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   519
                icon = lh.getMenuItem().getDisabledIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            } else if (model.isPressed() && model.isArmed()) {
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   521
                icon = lh.getMenuItem().getPressedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                if (icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    // Use default icon
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   524
                    icon = lh.getMenuItem().getIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            } else {
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   527
                icon = lh.getMenuItem().getIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (icon != null) {
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   531
                icon.paintIcon(lh.getMenuItem(), g, lr.getIconRect().x,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   532
                        lr.getIconRect().y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                g.setColor(holdc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   538
    private void paintCheckIcon(Graphics g, MenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   539
                                MenuItemLayoutHelper.LayoutResult lr,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                                Color holdc, Color foreground) {
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   541
        if (lh.getCheckIcon() != null) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   542
            ButtonModel model = lh.getMenuItem().getModel();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   543
            if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   544
                    && model.isSelected())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                g.setColor(foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                g.setColor(holdc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   549
            if (lh.useCheckAndArrow()) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   550
                lh.getCheckIcon().paintIcon(lh.getMenuItem(), g,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   551
                        lr.getCheckRect().x, lr.getCheckRect().y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            g.setColor(holdc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   557
    private void paintAccText(Graphics g, MenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   558
                              MenuItemLayoutHelper.LayoutResult lr) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   559
        if (!lh.getAccText().equals("")) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   560
            ButtonModel model = lh.getMenuItem().getModel();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   561
            g.setFont(lh.getAccFontMetrics().getFont());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            if (!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                // *** paint the accText disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                if (disabledForeground != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    g.setColor(disabledForeground);
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   566
                    SwingUtilities2.drawString(lh.getMenuItem(), g,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   567
                        lh.getAccText(), lr.getAccRect().x,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   568
                        lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                } else {
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   570
                    g.setColor(lh.getMenuItem().getBackground().brighter());
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   571
                    SwingUtilities2.drawString(lh.getMenuItem(), g,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   572
                        lh.getAccText(), lr.getAccRect().x,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   573
                        lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   574
                    g.setColor(lh.getMenuItem().getBackground().darker());
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   575
                    SwingUtilities2.drawString(lh.getMenuItem(), g,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   576
                        lh.getAccText(), lr.getAccRect().x - 1,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   577
                        lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                // *** paint the accText normally
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   581
                if (model.isArmed()
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   582
                        || (lh.getMenuItem() instanceof JMenu
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   583
                        && model.isSelected())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    g.setColor(acceleratorSelectionForeground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    g.setColor(acceleratorForeground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   588
                SwingUtilities2.drawString(lh.getMenuItem(), g, lh.getAccText(),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   589
                        lr.getAccRect().x, lr.getAccRect().y +
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   590
                        lh.getAccFontMetrics().getAscent());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   595
    private void paintText(Graphics g, MenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   596
                           MenuItemLayoutHelper.LayoutResult lr) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   597
        if (!lh.getText().equals("")) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   598
            if (lh.getHtmlView() != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                // Text is HTML
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   600
                lh.getHtmlView().paint(g, lr.getTextRect());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                // Text isn't HTML
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   603
                paintText(g, lh.getMenuItem(), lr.getTextRect(), lh.getText());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   608
    private void paintArrowIcon(Graphics g, MenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   609
                                MenuItemLayoutHelper.LayoutResult lr,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   610
                                Color foreground) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   611
        if (lh.getArrowIcon() != null) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   612
            ButtonModel model = lh.getMenuItem().getModel();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   613
            if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   614
                                && model.isSelected())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                g.setColor(foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            }
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   617
            if (lh.useCheckAndArrow()) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   618
                lh.getArrowIcon().paintIcon(lh.getMenuItem(), g,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   619
                        lr.getArrowRect().x, lr.getArrowRect().y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    private void applyInsets(Rectangle rect, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if(insets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            rect.x += insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            rect.y += insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            rect.width -= (insets.right + rect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            rect.height -= (insets.bottom + rect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Draws the background of the menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @param g the paint graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param menuItem menu item to be painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @param bgColor selection background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        ButtonModel model = menuItem.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        Color oldColor = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        int menuWidth = menuItem.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        int menuHeight = menuItem.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if(menuItem.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                g.setColor(bgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                g.fillRect(0,0, menuWidth, menuHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                g.setColor(menuItem.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                g.fillRect(0,0, menuWidth, menuHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        else if (model.isArmed() || (menuItem instanceof JMenu &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                     model.isSelected())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            g.setColor(bgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            g.fillRect(0,0, menuWidth, menuHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * Renders the text of the current menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @param g graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @param menuItem menu item to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @param textRect bounding rectangle for rendering the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param text string to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        ButtonModel model = menuItem.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        FontMetrics fm = SwingUtilities2.getFontMetrics(menuItem, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        int mnemIndex = menuItem.getDisplayedMnemonicIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if(!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            // *** paint the text disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if ( UIManager.get("MenuItem.disabledForeground") instanceof Color ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                g.setColor( UIManager.getColor("MenuItem.disabledForeground") );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                          mnemIndex, textRect.x,  textRect.y + fm.getAscent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                g.setColor(menuItem.getBackground().brighter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                SwingUtilities2.drawStringUnderlineCharAt(menuItem, g, text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                           mnemIndex, textRect.x, textRect.y + fm.getAscent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                g.setColor(menuItem.getBackground().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                           mnemIndex,  textRect.x - 1, textRect.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                           fm.getAscent() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            // *** paint the text normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                g.setColor(selectionForeground); // Uses protected field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                           mnemIndex, textRect.x, textRect.y + fm.getAscent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    public MenuElement[] getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        MenuSelectionManager m = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        MenuElement oldPath[] = m.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        MenuElement newPath[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        int i = oldPath.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (i == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return new MenuElement[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        Component parent = menuItem.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (oldPath[i-1].getComponent() == parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            // The parent popup menu is the last so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            newPath = new MenuElement[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            System.arraycopy(oldPath, 0, newPath, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            newPath[i] = menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            // A sibling menuitem is the current selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            //  This probably needs to handle 'exit submenu into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            // a menu item.  Search backwards along the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            // selection until you find the parent popup menu,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            // then copy up to that and add yourself...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            for (j = oldPath.length-1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                if (oldPath[j].getComponent() == parent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            newPath = new MenuElement[j+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            System.arraycopy(oldPath, 0, newPath, 0, j+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            newPath[j+1] = menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            System.out.println("Sibling condition -- ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            System.out.println("Old array : ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            printMenuElementArray(oldPath, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            System.out.println("New array : ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            printMenuElementArray(newPath, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        return newPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    void printMenuElementArray(MenuElement path[], boolean dumpStack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        System.out.println("Path is(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        for(i=0,j=path.length; i<j ;i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            for (int k=0; k<=i; k++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                System.out.print("  ");
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   749
            MenuElement me = path[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            if(me instanceof JMenuItem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                System.out.println(((JMenuItem)me).getText() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            else if (me == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                System.out.println("NULL , ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                System.out.println("" + me + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        System.out.println(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (dumpStack == true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    protected class MouseInputHandler implements MouseInputListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   763
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            getHandler().mouseClicked(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            getHandler().mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            getHandler().mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            getHandler().mouseEntered(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            getHandler().mouseExited(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            getHandler().mouseDragged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            getHandler().mouseMoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        private static final String CLICK = "doClick";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        Actions(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            super(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            JMenuItem mi = (JMenuItem)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            MenuSelectionManager.defaultManager().clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            mi.doClick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Call this method when a menu item is to be activated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * This method handles some of the details of menu item activation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * such as clearing the selected path and messaging the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * JMenuItem's doClick() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @param msm  A MenuSelectionManager. The visual feedback and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *             internal bookkeeping tasks are delegated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *             this MenuSelectionManager. If <code>null</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *             passed as this argument, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *             <code>MenuSelectionManager.defaultManager</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *             used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @see MenuSelectionManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @see JMenuItem#doClick(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    protected void doClick(MenuSelectionManager msm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        // Auditory cue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (! isInternalFrameSystemMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            BasicLookAndFeel.playSound(menuItem, getPropertyPrefix() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                       ".commandSound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        // Visual feedback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (msm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        msm.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        menuItem.doClick(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * This is to see if the menu item in question is part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * system menu on an internal frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * The Strings that are being checked can be found in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * MetalInternalFrameTitlePaneUI.java,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * WindowsInternalFrameTitlePaneUI.java, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * MotifInternalFrameTitlePaneUI.java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    private boolean isInternalFrameSystemMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        String actionCommand = menuItem.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        if ((actionCommand == "Close") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            (actionCommand == "Minimize") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            (actionCommand == "Restore") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            (actionCommand == "Maximize")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
          return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
          return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    // BasicMenuUI subclasses this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    class Handler implements MenuDragMouseListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                          MouseInputListener, PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        // MouseInputListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        public void mouseClicked(MouseEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            if (!menuItem.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            MenuSelectionManager manager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            Point p = e.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            if(p.x >= 0 && p.x < menuItem.getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
               p.y >= 0 && p.y < menuItem.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                doClick(manager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                manager.processMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            MenuSelectionManager manager = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            int modifiers = e.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            if ((modifiers & (InputEvent.BUTTON1_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                              InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                MenuSelectionManager.defaultManager().processMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            manager.setSelectedPath(getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            MenuSelectionManager manager = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            int modifiers = e.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            if ((modifiers & (InputEvent.BUTTON1_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                              InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                MenuSelectionManager.defaultManager().processMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                MenuElement path[] = manager.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                if (path.length > 1 && path[path.length-1] == menuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    MenuElement newPath[] = new MenuElement[path.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    int i,c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    for(i=0,c=path.length-1;i<c;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        newPath[i] = path[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    manager.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            MenuSelectionManager.defaultManager().processMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        // MenuDragListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        public void menuDragMouseEntered(MenuDragMouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            MenuSelectionManager manager = e.getMenuSelectionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            MenuElement path[] = e.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            manager.setSelectedPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        public void menuDragMouseDragged(MenuDragMouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            MenuSelectionManager manager = e.getMenuSelectionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            MenuElement path[] = e.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            manager.setSelectedPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        public void menuDragMouseExited(MenuDragMouseEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        public void menuDragMouseReleased(MenuDragMouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            if (!menuItem.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            MenuSelectionManager manager = e.getMenuSelectionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            MenuElement path[] = e.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            Point p = e.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            if (p.x >= 0 && p.x < menuItem.getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    p.y >= 0 && p.y < menuItem.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                doClick(manager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                manager.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            String name = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            if (name == "labelFor" || name == "displayedMnemonic" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                name == "accelerator") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                updateAcceleratorBinding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            } else if (name == "text" || "font" == name ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                       "foreground" == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                // remove the old html view client property if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                // existed, and install a new one if the text installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                // into the JLabel is html source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                JMenuItem lbl = ((JMenuItem) e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                String text = lbl.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                BasicHTML.updateRenderer(lbl, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            } else if (name  == "iconTextGap") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                defaultTextIconGap = ((Number)e.getNewValue()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
}