KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > common > ExtBasicMenuUI


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.common;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.Graphics JavaDoc;
36 import java.awt.event.MouseAdapter JavaDoc;
37 import java.awt.event.MouseEvent JavaDoc;
38 import java.awt.event.MouseListener JavaDoc;
39
40 import javax.swing.AbstractButton JavaDoc;
41 import javax.swing.Icon JavaDoc;
42 import javax.swing.JComponent JavaDoc;
43 import javax.swing.JMenu JavaDoc;
44 import javax.swing.JMenuItem JavaDoc;
45 import javax.swing.UIManager JavaDoc;
46 import javax.swing.plaf.ComponentUI JavaDoc;
47 import javax.swing.plaf.UIResource JavaDoc;
48 import javax.swing.plaf.basic.BasicMenuUI JavaDoc;
49
50 /**
51  * An implementation of <code>MenuUI</code> used by the JGoodies Windows
52  * and Plastic looks. Unlike it's superclass, it aligns submenu items.
53  *
54  * @author Karsten Lentzsch
55  * @version $Revision: 1.2 $
56  */

57
58 public class ExtBasicMenuUI extends BasicMenuUI JavaDoc {
59
60     private static final String JavaDoc MENU_PROPERTY_PREFIX = "Menu";
61     private static final String JavaDoc SUBMENU_PROPERTY_PREFIX = "MenuItem";
62
63     // May be changed to SUBMENU_PROPERTY_PREFIX later
64
private String JavaDoc propertyPrefix = MENU_PROPERTY_PREFIX;
65
66     private MenuItemRenderer renderer;
67     private MouseListener JavaDoc mouseListener;
68
69
70     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
71         return new ExtBasicMenuUI();
72     }
73
74
75     // Install and Uninstall ************************************************
76

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

159     private void ensureSubMenuInstalled() {
160         if (propertyPrefix.equals(SUBMENU_PROPERTY_PREFIX))
161             return;
162
163         //System.out.println("Sub menu detected.");
164
uninstallRolloverListener();
165         uninstallDefaults();
166         propertyPrefix = SUBMENU_PROPERTY_PREFIX;
167         installDefaults();
168     }
169
170     // Rollover Listener ****************************************************
171

172     protected void installListeners() {
173         super.installListeners();
174         mouseListener = createRolloverListener();
175         menuItem.addMouseListener(mouseListener);
176     }
177
178     protected void uninstallListeners() {
179         super.uninstallListeners();
180         uninstallRolloverListener();
181     }
182     
183     private void uninstallRolloverListener() {
184         if (mouseListener != null) {
185             menuItem.removeMouseListener(mouseListener);
186             mouseListener = null;
187         }
188     }
189
190     protected MouseListener JavaDoc createRolloverListener() {
191         return new MouseAdapter JavaDoc() {
192             public void mouseEntered(MouseEvent JavaDoc e) {
193                 AbstractButton JavaDoc b = (AbstractButton JavaDoc) e.getSource();
194                 b.getModel().setRollover(true);
195             }
196             public void mouseExited(MouseEvent JavaDoc e) {
197                 AbstractButton JavaDoc b = (AbstractButton JavaDoc) e.getSource();
198                 b.getModel().setRollover(false);
199             }
200         };
201     }
202
203
204     // Helper Code **********************************************************
205

206     private boolean isSubMenu(JMenuItem JavaDoc aMenuItem) {
207         return !((JMenu JavaDoc) aMenuItem).isTopLevelMenu();
208     }
209
210 }
Popular Tags