KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > windows > WindowsMenuUI


1 /*
2  * Copyright (c) 2001-2005 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 JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.Graphics JavaDoc;
36
37 import javax.swing.Icon JavaDoc;
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.JMenu JavaDoc;
40 import javax.swing.JMenuItem JavaDoc;
41 import javax.swing.UIManager JavaDoc;
42 import javax.swing.plaf.ComponentUI JavaDoc;
43 import javax.swing.plaf.UIResource JavaDoc;
44
45 import com.jgoodies.looks.common.MenuItemRenderer;
46
47 /**
48  * The JGoodies Windows look&amp;feel implementation of <code>MenuUI</code>.<p>
49  *
50  * It differs from the superclass in that it uses an overhauled menu
51  * rendering an aligmnent system. Furthermore, you can set a client property
52  * <tt>Options.NO_ICONS_KEY</tt> to indicate that this menu has no icons.
53  *
54  * @author Karsten Lentzsch
55  * @version $Revision: 1.2 $
56  *
57  * @see com.jgoodies.looks.Options
58  */

59
60 public final class WindowsMenuUI extends com.sun.java.swing.plaf.windows.WindowsMenuUI {
61
62     private static final String JavaDoc MENU_PROPERTY_PREFIX = "Menu";
63     private static final String JavaDoc SUBMENU_PROPERTY_PREFIX = "MenuItem";
64
65     // May be changed to SUBMENU_PROPERTY_PREFIX later
66
private String JavaDoc propertyPrefix = MENU_PROPERTY_PREFIX;
67
68     private MenuItemRenderer renderer;
69
70     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
71         return new WindowsMenuUI();
72     }
73
74     // Install and Uninstall **************************************************
75

76     protected void installDefaults() {
77         super.installDefaults();
78         if (arrowIcon == null || arrowIcon instanceof UIResource JavaDoc) {
79             arrowIcon = UIManager.getIcon("Menu.arrowIcon");
80         }
81         renderer =
82             new MenuItemRenderer(
83                 menuItem,
84                 false,
85                 acceleratorFont,
86                 selectionForeground,
87                 disabledForeground,
88                 acceleratorForeground,
89                 acceleratorSelectionForeground);
90         Integer JavaDoc gap =
91             (Integer JavaDoc) UIManager.get(getPropertyPrefix() + ".textIconGap");
92         defaultTextIconGap = gap != null ? gap.intValue() : 2;
93     }
94
95     protected void uninstallDefaults() {
96         super.uninstallDefaults();
97         renderer = null;
98     }
99
100     protected String JavaDoc getPropertyPrefix() {
101         return propertyPrefix;
102     }
103
104     protected Dimension JavaDoc getPreferredMenuItemSize(
105         JComponent JavaDoc c,
106         Icon JavaDoc aCheckIcon,
107         Icon JavaDoc anArrowIcon,
108         int textIconGap) {
109
110         if (isSubMenu(menuItem)) {
111             ensureSubMenuInstalled();
112             return renderer.getPreferredMenuItemSize(
113                 c,
114                 aCheckIcon,
115                 anArrowIcon,
116                 textIconGap);
117         } else {
118             Dimension JavaDoc size =
119                 super.getPreferredMenuItemSize(
120                     c,
121                     aCheckIcon,
122                     anArrowIcon,
123                     textIconGap);
124             int width = size.width;
125             int height = size.height;
126             if (height % 2 == 1)
127                 height--;
128             return new Dimension JavaDoc(width, height);
129         }
130     }
131
132     protected void paintMenuItem(
133         Graphics JavaDoc g,
134         JComponent JavaDoc c,
135         Icon JavaDoc aCheckIcon,
136         Icon JavaDoc anArrowIcon,
137         Color JavaDoc background,
138         Color JavaDoc foreground,
139         int textIconGap) {
140         if (isSubMenu(menuItem)) {
141             renderer.paintMenuItem(
142                 g,
143                 c,
144                 aCheckIcon,
145                 anArrowIcon,
146                 background,
147                 foreground,
148                 textIconGap);
149         } else {
150             super.paintMenuItem(
151                 g,
152                 c,
153                 aCheckIcon,
154                 anArrowIcon,
155                 background,
156                 foreground,
157                 textIconGap);
158         }
159     }
160
161     /**
162      * Checks if we have already detected the correct menu type,
163      * menu in menu bar vs. sub menu; reinstalls if necessary.
164      */

165     private void ensureSubMenuInstalled() {
166         if (propertyPrefix.equals(SUBMENU_PROPERTY_PREFIX))
167             return;
168         uninstallDefaults();
169         propertyPrefix = SUBMENU_PROPERTY_PREFIX;
170         installDefaults();
171     }
172
173
174     // Helper Code **********************************************************
175

176     private boolean isSubMenu(JMenuItem JavaDoc aMenuItem) {
177         return !((JMenu JavaDoc) aMenuItem).isTopLevelMenu();
178     }
179
180
181 }
Popular Tags