~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/looks-2.2.2/src/core/com/jgoodies/looks/windows/WindowsXPMenuUI.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2001-2009 JGoodies Karsten Lentzsch. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 *  o Redistributions of source code must retain the above copyright notice,
 
8
 *    this list of conditions and the following disclaimer.
 
9
 *
 
10
 *  o Redistributions in binary form must reproduce the above copyright notice,
 
11
 *    this list of conditions and the following disclaimer in the documentation
 
12
 *    and/or other materials provided with the distribution.
 
13
 *
 
14
 *  o Neither the name of JGoodies Karsten Lentzsch nor the names of
 
15
 *    its contributors may be used to endorse or promote products derived
 
16
 *    from this software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
20
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
22
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
25
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
26
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 
27
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
package com.jgoodies.looks.windows;
 
32
 
 
33
import java.awt.Color;
 
34
import java.awt.Dimension;
 
35
import java.awt.Graphics;
 
36
 
 
37
import javax.swing.*;
 
38
import javax.swing.plaf.ComponentUI;
 
39
import javax.swing.plaf.UIResource;
 
40
 
 
41
import com.jgoodies.looks.common.MenuItemRenderer;
 
42
 
 
43
/**
 
44
 * The JGoodies Windows XP look&amp;feel implementation of <code>MenuUI</code>.<p>
 
45
 *
 
46
 * It differs from the superclass in that it uses an overhauled menu
 
47
 * rendering an aligmnent system. Furthermore, you can set a client property
 
48
 * <tt>Options.NO_ICONS_KEY</tt> to indicate that this menu has no icons.
 
49
 *
 
50
 * @author  Karsten Lentzsch
 
51
 * @version $Revision: 1.2 $
 
52
 *
 
53
 * @see com.jgoodies.looks.Options
 
54
 */
 
55
 
 
56
public final class WindowsXPMenuUI extends com.sun.java.swing.plaf.windows.WindowsMenuUI {
 
57
 
 
58
    private static final String MENU_PROPERTY_PREFIX    = "Menu";
 
59
    private static final String SUBMENU_PROPERTY_PREFIX = "MenuItem";
 
60
 
 
61
    // May be changed to SUBMENU_PROPERTY_PREFIX later
 
62
    private String propertyPrefix = MENU_PROPERTY_PREFIX;
 
63
 
 
64
    private MenuItemRenderer renderer;
 
65
 
 
66
    public static ComponentUI createUI(JComponent b) {
 
67
        return new WindowsXPMenuUI();
 
68
    }
 
69
 
 
70
    // Install and Uninstall **************************************************
 
71
 
 
72
    protected void installDefaults() {
 
73
        super.installDefaults();
 
74
        if (arrowIcon == null || arrowIcon instanceof UIResource) {
 
75
            arrowIcon = UIManager.getIcon("Menu.arrowIcon");
 
76
        }
 
77
        renderer =
 
78
            new MenuItemRenderer(
 
79
                menuItem,
 
80
                false,
 
81
                acceleratorFont,
 
82
                selectionForeground,
 
83
                disabledForeground,
 
84
                acceleratorForeground,
 
85
                acceleratorSelectionForeground);
 
86
        Integer gap =
 
87
            (Integer) UIManager.get(getPropertyPrefix() + ".textIconGap");
 
88
        defaultTextIconGap = gap != null ? gap.intValue() : 2;
 
89
    }
 
90
 
 
91
    protected void uninstallDefaults() {
 
92
        super.uninstallDefaults();
 
93
        renderer = null;
 
94
    }
 
95
 
 
96
    protected String getPropertyPrefix() {
 
97
        return propertyPrefix;
 
98
    }
 
99
 
 
100
    protected Dimension getPreferredMenuItemSize(
 
101
        JComponent c,
 
102
        Icon aCheckIcon,
 
103
        Icon anArrowIcon,
 
104
        int textIconGap) {
 
105
 
 
106
        if (isSubMenu(menuItem)) {
 
107
            ensureSubMenuInstalled();
 
108
            return renderer.getPreferredMenuItemSize(
 
109
                c,
 
110
                aCheckIcon,
 
111
                anArrowIcon,
 
112
                textIconGap);
 
113
        } else {
 
114
            Dimension size =
 
115
                super.getPreferredMenuItemSize(
 
116
                    c,
 
117
                    aCheckIcon,
 
118
                    anArrowIcon,
 
119
                    textIconGap);
 
120
            int width = size.width;
 
121
            int height = size.height;
 
122
            if (height % 2 == 1)
 
123
                height--;
 
124
            return new Dimension(width, height);
 
125
        }
 
126
    }
 
127
 
 
128
    protected void paintMenuItem(
 
129
        Graphics g,
 
130
        JComponent c,
 
131
        Icon aCheckIcon,
 
132
        Icon anArrowIcon,
 
133
        Color background,
 
134
        Color foreground,
 
135
        int textIconGap) {
 
136
        if (isSubMenu(menuItem)) {
 
137
            renderer.paintMenuItem(
 
138
                g,
 
139
                c,
 
140
                aCheckIcon,
 
141
                anArrowIcon,
 
142
                background,
 
143
                foreground,
 
144
                textIconGap);
 
145
        } else {
 
146
            super.paintMenuItem(
 
147
                g,
 
148
                c,
 
149
                aCheckIcon,
 
150
                anArrowIcon,
 
151
                background,
 
152
                foreground,
 
153
                textIconGap);
 
154
        }
 
155
    }
 
156
 
 
157
    /**
 
158
     * Checks if we have already detected the correct menu type,
 
159
     * menu in menu bar vs. sub menu; reinstalls if necessary.
 
160
     */
 
161
    private void ensureSubMenuInstalled() {
 
162
        if (propertyPrefix.equals(SUBMENU_PROPERTY_PREFIX))
 
163
            return;
 
164
 
 
165
        ButtonModel model = menuItem.getModel();
 
166
 
 
167
        //save values of armed and selected properties.
 
168
        //they will be resetted in #ununinstallDefaults().
 
169
        boolean oldArmed = model.isArmed();
 
170
        boolean oldSelected = model.isSelected();
 
171
 
 
172
        uninstallDefaults();
 
173
        propertyPrefix = SUBMENU_PROPERTY_PREFIX;
 
174
        installDefaults();
 
175
 
 
176
        //restore values of armed and selected properties
 
177
        model.setArmed(oldArmed);
 
178
        model.setSelected(oldSelected);
 
179
    }
 
180
 
 
181
 
 
182
    // Helper Code **********************************************************
 
183
 
 
184
    private boolean isSubMenu(JMenuItem aMenuItem) {
 
185
        return !((JMenu) aMenuItem).isTopLevelMenu();
 
186
    }
 
187
 
 
188
 
 
189
}
 
 
b'\\ No newline at end of file'