001 /*
002 * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.synth;
027
028 import javax.swing.*;
029 import javax.swing.event.*;
030 import javax.swing.plaf.*;
031 import javax.swing.plaf.basic.*;
032 import javax.swing.border.*;
033
034 import java.applet.Applet;
035
036 import java.awt.Component;
037 import java.awt.Container;
038 import java.awt.Dimension;
039 import java.awt.Graphics;
040 import java.awt.KeyboardFocusManager;
041 import java.awt.Window;
042 import java.awt.event.*;
043 import java.awt.AWTEvent;
044 import java.awt.Toolkit;
045
046 import java.beans.PropertyChangeListener;
047 import java.beans.PropertyChangeEvent;
048
049 import java.util.*;
050 import sun.swing.plaf.synth.SynthUI;
051
052 /**
053 * Synth's PopupMenuUI.
054 *
055 * @version 1.30, 05/05/07
056 * @author Georges Saab
057 * @author David Karlton
058 * @author Arnaud Weber
059 */
060 class SynthPopupMenuUI extends BasicPopupMenuUI implements
061 PropertyChangeListener, SynthUI {
062 /**
063 * Maximum size of the text portion of the children menu items.
064 */
065 private int maxTextWidth;
066
067 /**
068 * Maximum size of the icon portion of the children menu items.
069 */
070 private int maxIconWidth;
071
072 /**
073 * Maximum size of the spacing between the text and accelerator
074 * portions of the children menu items.
075 */
076 private int maxAccelSpacingWidth;
077
078 /**
079 * Maximum size of the text for the accelerator portion of the children
080 * menu items.
081 */
082 private int maxAcceleratorWidth;
083
084 /*
085 * Maximum icon and text offsets of the children menu items.
086 */
087 private int maxTextOffset;
088 private int maxIconOffset;
089
090 private SynthStyle style;
091
092 public static ComponentUI createUI(JComponent x) {
093 return new SynthPopupMenuUI();
094 }
095
096 public void installDefaults() {
097 if (popupMenu.getLayout() == null
098 || popupMenu.getLayout() instanceof UIResource) {
099 popupMenu.setLayout(new DefaultMenuLayout(popupMenu,
100 BoxLayout.Y_AXIS));
101 }
102 updateStyle(popupMenu);
103 }
104
105 private void updateStyle(JComponent c) {
106 SynthContext context = getContext(c, ENABLED);
107 SynthStyle oldStyle = style;
108 style = SynthLookAndFeel.updateStyle(context, this );
109 if (style != oldStyle) {
110 if (oldStyle != null) {
111 uninstallKeyboardActions();
112 installKeyboardActions();
113 }
114 }
115 context.dispose();
116 }
117
118 protected void installListeners() {
119 super .installListeners();
120 popupMenu.addPropertyChangeListener(this );
121 }
122
123 protected void uninstallDefaults() {
124 SynthContext context = getContext(popupMenu, ENABLED);
125
126 style.uninstallDefaults(context);
127 context.dispose();
128 style = null;
129
130 if (popupMenu.getLayout() instanceof UIResource) {
131 popupMenu.setLayout(null);
132 }
133 }
134
135 protected void uninstallListeners() {
136 super .uninstallListeners();
137 popupMenu.removePropertyChangeListener(this );
138 }
139
140 public SynthContext getContext(JComponent c) {
141 return getContext(c, getComponentState(c));
142 }
143
144 private SynthContext getContext(JComponent c, int state) {
145 return SynthContext.getContext(SynthContext.class, c,
146 SynthLookAndFeel.getRegion(c), style, state);
147 }
148
149 private Region getRegion(JComponent c) {
150 return SynthLookAndFeel.getRegion(c);
151 }
152
153 private int getComponentState(JComponent c) {
154 return SynthLookAndFeel.getComponentState(c);
155 }
156
157 /**
158 * Resets the max text and accerator widths,
159 * text and icon offsets.
160 */
161 void resetAlignmentHints() {
162 maxTextWidth = maxIconWidth = maxAccelSpacingWidth = maxAcceleratorWidth = maxTextOffset = maxIconOffset = 0;
163 }
164
165 /**
166 * Adjusts the width needed to display the maximum menu item string.
167 *
168 * @param width Text width.
169 * @return max width
170 */
171 int adjustTextWidth(int width) {
172 maxTextWidth = Math.max(maxTextWidth, width);
173 return maxTextWidth;
174 }
175
176 /**
177 * Adjusts the width needed to display the maximum menu item icon.
178 *
179 * @param width Icon width.
180 * @return max width
181 */
182 int adjustIconWidth(int width) {
183 maxIconWidth = Math.max(maxIconWidth, width);
184 return maxIconWidth;
185 }
186
187 /**
188 * Adjusts the width needed to pad between the maximum menu item
189 * text and accelerator.
190 *
191 * @param width Spacing width.
192 * @return max width
193 */
194 int adjustAccelSpacingWidth(int width) {
195 maxAccelSpacingWidth = Math.max(maxAccelSpacingWidth, width);
196 return maxAccelSpacingWidth;
197 }
198
199 /**
200 * Adjusts the width needed to display the maximum accelerator.
201 *
202 * @param width Text width.
203 * @return max width
204 */
205 int adjustAcceleratorWidth(int width) {
206 maxAcceleratorWidth = Math.max(maxAcceleratorWidth, width);
207 return maxAcceleratorWidth;
208 }
209
210 /**
211 * Maximum size needed to display accelerators of children menu items.
212 */
213 int getMaxAcceleratorWidth() {
214 return maxAcceleratorWidth;
215 }
216
217 /**
218 * Adjusts the text offset needed to align text horizontally.
219 *
220 * @param offset Text offset
221 * @return max offset
222 */
223 int adjustTextOffset(int offset) {
224 maxTextOffset = Math.max(maxTextOffset, offset);
225 return maxTextOffset;
226 }
227
228 /**
229 * Adjusts the icon offset needed to align icons horizontally
230 *
231 * @param offset Icon offset
232 * @return max offset
233 */
234 int adjustIconOffset(int offset) {
235 maxIconOffset = Math.max(maxIconOffset, offset);
236 return maxIconOffset;
237 }
238
239 public void update(Graphics g, JComponent c) {
240 SynthContext context = getContext(c);
241
242 SynthLookAndFeel.update(context, g);
243 context.getPainter().paintPopupMenuBackground(context, g, 0, 0,
244 c.getWidth(), c.getHeight());
245 paint(context, g);
246 context.dispose();
247 }
248
249 public void paint(Graphics g, JComponent c) {
250 SynthContext context = getContext(c);
251
252 paint(context, g);
253 context.dispose();
254 }
255
256 protected void paint(SynthContext context, Graphics g) {
257 }
258
259 public void paintBorder(SynthContext context, Graphics g, int x,
260 int y, int w, int h) {
261 context.getPainter().paintPopupMenuBorder(context, g, x, y, w,
262 h);
263 }
264
265 public void propertyChange(PropertyChangeEvent e) {
266 if (SynthLookAndFeel.shouldUpdateStyle(e)) {
267 updateStyle(popupMenu);
268 }
269 }
270 }
|