KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SynthMenuUI.java 1.11 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 package javax.swing.plaf.synth;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.beans.*;
12 import javax.swing.*;
13 import javax.swing.event.*;
14 import javax.swing.plaf.*;
15 import javax.swing.plaf.basic.*;
16 import javax.swing.border.*;
17 import java.util.Arrays JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import sun.swing.plaf.synth.SynthUI;
20
21
22 /**
23  * Synth's MenuUI.
24  *
25  * @version 1.11, 12/19/03
26  * @author Georges Saab
27  * @author David Karlton
28  * @author Arnaud Weber
29  */

30 class SynthMenuUI extends BasicMenuUI implements PropertyChangeListener,
31                                SynthUI {
32     private SynthStyle JavaDoc style;
33     private SynthStyle JavaDoc accStyle;
34
35     private String JavaDoc acceleratorDelimiter;
36
37     public static ComponentUI createUI(JComponent x) {
38     return new SynthMenuUI JavaDoc();
39     }
40
41     protected void installDefaults() {
42         updateStyle(menuItem);
43     }
44
45     protected void installListeners() {
46         super.installListeners();
47         menuItem.addPropertyChangeListener(this);
48     }
49
50     private void updateStyle(JMenuItem mi) {
51         SynthStyle JavaDoc oldStyle = style;
52         SynthContext JavaDoc context = getContext(mi, ENABLED);
53
54         style = SynthLookAndFeel.updateStyle(context, this);
55         if (oldStyle != style) {
56             String JavaDoc prefix = getPropertyPrefix();
57             defaultTextIconGap = style.getInt(
58                            context, prefix + ".textIconGap", 4);
59             if (menuItem.getMargin() == null ||
60                          (menuItem.getMargin() instanceof UIResource)) {
61                 Insets insets = (Insets)style.get(context, prefix + ".margin");
62
63                 if (insets == null) {
64                     // Some places assume margins are non-null.
65
insets = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
66                 }
67                 menuItem.setMargin(insets);
68             }
69             acceleratorDelimiter = style.getString(context, prefix +
70                                             ".acceleratorDelimiter", "+");
71
72             arrowIcon = style.getIcon(context, prefix + ".arrowIcon");
73
74             checkIcon = style.getIcon(context, prefix + ".checkIcon");
75
76             
77             ((JMenu)menuItem).setDelay(style.getInt(context, prefix +
78                                                     ".delay", 200));
79             if (oldStyle != null) {
80                 uninstallKeyboardActions();
81                 installKeyboardActions();
82             }
83         }
84         context.dispose();
85
86         SynthContext JavaDoc accContext = getContext(mi, Region.MENU_ITEM_ACCELERATOR,
87                                              ENABLED);
88
89         accStyle = SynthLookAndFeel.updateStyle(accContext, this);
90         accContext.dispose();
91     }
92
93     protected void uninstallDefaults() {
94         SynthContext JavaDoc context = getContext(menuItem, ENABLED);
95         style.uninstallDefaults(context);
96         context.dispose();
97         style = null;
98
99         SynthContext JavaDoc accContext = getContext(menuItem,
100                                      Region.MENU_ITEM_ACCELERATOR, ENABLED);
101         accStyle.uninstallDefaults(accContext);
102         accContext.dispose();
103         accStyle = null;
104
105         super.uninstallDefaults();
106     }
107
108     protected void uninstallListeners() {
109     super.uninstallListeners();
110         menuItem.removePropertyChangeListener(this);
111     }
112
113     public SynthContext JavaDoc getContext(JComponent c) {
114         return getContext(c, getComponentState(c));
115     }
116
117     SynthContext JavaDoc getContext(JComponent c, int state) {
118         Region JavaDoc region = getRegion(c);
119         return SynthContext.getContext(SynthContext JavaDoc.class, c, region,
120                                        style, state);
121     }
122
123     public SynthContext JavaDoc getContext(JComponent c, Region JavaDoc region) {
124         return getContext(c, region, getComponentState(c, region));
125     }
126
127     private SynthContext JavaDoc getContext(JComponent c, Region JavaDoc region, int state) {
128         return SynthContext.getContext(SynthContext JavaDoc.class, c,
129                                        region, accStyle, state);
130     }
131
132     private Region JavaDoc getRegion(JComponent c) {
133         return SynthLookAndFeel.getRegion(c);
134     }
135
136     private int getComponentState(JComponent c) {
137         int state;
138
139         if (!c.isEnabled()) {
140             return DISABLED;
141         }
142         if (menuItem.isArmed()) {
143             state = MOUSE_OVER;
144         }
145         else {
146             state = SynthLookAndFeel.getComponentState(c);
147         }
148         if (menuItem.isSelected()) {
149             state |= SELECTED;
150         }
151         return state;
152     }
153
154     private int getComponentState(JComponent c, Region JavaDoc region) {
155         return getComponentState(c);
156     }
157
158     protected Dimension getPreferredMenuItemSize(JComponent c,
159                                                      Icon checkIcon,
160                                                      Icon arrowIcon,
161                                                      int defaultTextIconGap) {
162         SynthContext JavaDoc context = getContext(c);
163         SynthContext JavaDoc accContext = getContext(c, Region.MENU_ITEM_ACCELERATOR);
164         Dimension value = SynthMenuItemUI.getPreferredMenuItemSize(
165                   context, accContext, useCheckAndArrow(), c, checkIcon,
166                   arrowIcon, defaultTextIconGap, acceleratorDelimiter);
167         context.dispose();
168         accContext.dispose();
169         return value;
170     }
171
172
173     public void update(Graphics g, JComponent c) {
174         SynthContext JavaDoc context = getContext(c);
175
176         SynthLookAndFeel.update(context, g);
177         context.getPainter().paintMenuBackground(context,
178                           g, 0, 0, c.getWidth(), c.getHeight());
179         paint(context, g);
180         context.dispose();
181     }
182
183     public void paint(Graphics g, JComponent c) {
184         SynthContext JavaDoc context = getContext(c);
185
186         paint(context, g);
187         context.dispose();
188     }
189
190     protected void paint(SynthContext JavaDoc context, Graphics g) {
191         SynthContext JavaDoc accContext = getContext(menuItem,
192                                              Region.MENU_ITEM_ACCELERATOR);
193         SynthMenuItemUI.paint(context, accContext, g, checkIcon, arrowIcon,
194                         useCheckAndArrow(), acceleratorDelimiter,
195                         defaultTextIconGap);
196         accContext.dispose();
197     }
198
199     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
200                             int y, int w, int h) {
201         context.getPainter().paintMenuBorder(context, g, x, y, w, h);
202     }
203
204     public void propertyChange(PropertyChangeEvent e) {
205         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
206             updateStyle((JMenu)e.getSource());
207         }
208     }
209
210     private boolean useCheckAndArrow() {
211         return !((JMenu)menuItem).isTopLevelMenu();
212     }
213 }
214
Popular Tags