KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthPopupMenuUI


1 /*
2  * @(#)SynthPopupMenuUI.java 1.21 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.synth;
9
10 import javax.swing.*;
11 import javax.swing.event.*;
12 import javax.swing.plaf.*;
13 import javax.swing.plaf.basic.*;
14 import javax.swing.border.*;
15
16 import java.applet.Applet JavaDoc;
17
18 import java.awt.Component JavaDoc;
19 import java.awt.Container JavaDoc;
20 import java.awt.Dimension JavaDoc;
21 import java.awt.Graphics JavaDoc;
22 import java.awt.KeyboardFocusManager JavaDoc;
23 import java.awt.Window JavaDoc;
24 import java.awt.event.*;
25 import java.awt.AWTEvent JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30
31 import java.util.*;
32 import sun.swing.plaf.synth.SynthUI;
33
34 /**
35  * Synth's PopupMenuUI.
36  *
37  * @version 1.21, 12/19/03
38  * @author Georges Saab
39  * @author David Karlton
40  * @author Arnaud Weber
41  */

42 class SynthPopupMenuUI extends BasicPopupMenuUI implements
43                 PropertyChangeListener JavaDoc, SynthUI {
44     /**
45      * Maximum size of the text portion of the children menu items.
46      */

47     private int maxTextWidth;
48
49     /**
50      * Maximum size of the text for the acclerator portion of the children
51      * menu items.
52      */

53     private int maxAcceleratorWidth;
54
55     private SynthStyle JavaDoc style;
56
57     public static ComponentUI createUI(JComponent x) {
58     return new SynthPopupMenuUI JavaDoc();
59     }
60
61     public void installDefaults() {
62     if (popupMenu.getLayout() == null ||
63         popupMenu.getLayout() instanceof UIResource) {
64         popupMenu.setLayout(new DefaultMenuLayout JavaDoc(
65                                     popupMenu, BoxLayout.Y_AXIS));
66         }
67         updateStyle(popupMenu);
68     }
69
70     private void updateStyle(JComponent c) {
71         SynthContext JavaDoc context = getContext(c, ENABLED);
72         SynthStyle JavaDoc oldStyle = style;
73         style = SynthLookAndFeel.updateStyle(context, this);
74         if (style != oldStyle) {
75             if (oldStyle != null) {
76                 uninstallKeyboardActions();
77                 installKeyboardActions();
78             }
79         }
80         context.dispose();
81     }
82
83     protected void installListeners() {
84         super.installListeners();
85         popupMenu.addPropertyChangeListener(this);
86     }
87
88     protected void uninstallDefaults() {
89         SynthContext JavaDoc context = getContext(popupMenu, ENABLED);
90
91         style.uninstallDefaults(context);
92         context.dispose();
93         style = null;
94
95         if (popupMenu.getLayout() instanceof UIResource) {
96             popupMenu.setLayout(null);
97         }
98     }
99
100     protected void uninstallListeners() {
101         super.uninstallListeners();
102         popupMenu.removePropertyChangeListener(this);
103     }
104
105     public SynthContext JavaDoc getContext(JComponent c) {
106         return getContext(c, getComponentState(c));
107     }
108
109     private SynthContext JavaDoc getContext(JComponent c, int state) {
110         return SynthContext.getContext(SynthContext JavaDoc.class, c,
111                     SynthLookAndFeel.getRegion(c), style, state);
112     }
113
114     private Region JavaDoc getRegion(JComponent c) {
115         return SynthLookAndFeel.getRegion(c);
116     }
117
118     private int getComponentState(JComponent c) {
119         return SynthLookAndFeel.getComponentState(c);
120     }
121
122     /**
123      * Resets the max text and accerator widths.
124      */

125     void resetAcceleratorWidths() {
126         maxTextWidth = maxAcceleratorWidth = 0;
127     }
128
129     /**
130      * Adjusts the width needed to display the maximum menu item string.
131      *
132      * @param width Text width.
133      * @return max width
134      */

135     int adjustTextWidth(int width) {
136         maxTextWidth = Math.max(maxTextWidth, width);
137         return maxTextWidth;
138     }
139
140     /**
141      * Adjusts the width needed to display the maximum accelerator.
142      *
143      * @param width Text width.
144      * @return max width
145      */

146     int adjustAcceleratorWidth(int width) {
147         maxAcceleratorWidth = Math.max(maxAcceleratorWidth, width);
148         return maxAcceleratorWidth;
149     }
150
151     /**
152      * Maximum size to display text of children menu items.
153      */

154     int getMaxTextWidth() {
155         return maxTextWidth;
156     }
157
158     /**
159      * Maximum size needed to display accelerators of children menu items.
160      */

161     int getMaxAcceleratorWidth() {
162         return maxAcceleratorWidth;
163     }
164
165     public void update(Graphics JavaDoc g, JComponent c) {
166         SynthContext JavaDoc context = getContext(c);
167
168         SynthLookAndFeel.update(context, g);
169         context.getPainter().paintPopupMenuBackground(context,
170                           g, 0, 0, c.getWidth(), c.getHeight());
171         paint(context, g);
172         context.dispose();
173     }
174
175     public void paint(Graphics JavaDoc g, JComponent c) {
176         SynthContext JavaDoc context = getContext(c);
177
178         paint(context, g);
179         context.dispose();
180     }
181
182     protected void paint(SynthContext JavaDoc context, Graphics JavaDoc g) {
183     }
184
185     public void paintBorder(SynthContext JavaDoc context, Graphics JavaDoc g, int x,
186                             int y, int w, int h) {
187         context.getPainter().paintPopupMenuBorder(context, g, x, y, w, h);
188     }
189
190     public void propertyChange(PropertyChangeEvent JavaDoc e) {
191         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
192             updateStyle(popupMenu);
193         }
194     }
195 }
196
Popular Tags