author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 5506 | 202f599c92aa |
child 20168 | 137788883a22 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.plaf.synth; |
|
27 |
||
28 |
import javax.swing.*; |
|
29 |
import javax.swing.plaf.*; |
|
30 |
import javax.swing.plaf.basic.*; |
|
31 |
import java.awt.Graphics; |
|
32 |
import java.beans.PropertyChangeListener; |
|
33 |
import java.beans.PropertyChangeEvent; |
|
34 |
||
35 |
/** |
|
4394 | 36 |
* Provides the Synth L&F UI delegate for |
37 |
* {@link javax.swing.JPopupMenu}. |
|
2 | 38 |
* |
39 |
* @author Georges Saab |
|
40 |
* @author David Karlton |
|
41 |
* @author Arnaud Weber |
|
4394 | 42 |
* @since 1.7 |
2 | 43 |
*/ |
4394 | 44 |
public class SynthPopupMenuUI extends BasicPopupMenuUI |
45 |
implements PropertyChangeListener, SynthUI { |
|
2 | 46 |
private SynthStyle style; |
47 |
||
4394 | 48 |
/** |
49 |
* Creates a new UI object for the given component. |
|
50 |
* |
|
51 |
* @param x component to create UI object for |
|
52 |
* @return the UI object |
|
53 |
*/ |
|
2 | 54 |
public static ComponentUI createUI(JComponent x) { |
55 |
return new SynthPopupMenuUI(); |
|
56 |
} |
|
57 |
||
4394 | 58 |
/** |
59 |
* @inheritDoc |
|
60 |
*/ |
|
61 |
@Override |
|
2 | 62 |
public void installDefaults() { |
63 |
if (popupMenu.getLayout() == null || |
|
64 |
popupMenu.getLayout() instanceof UIResource) { |
|
4394 | 65 |
popupMenu.setLayout(new SynthMenuLayout(popupMenu, BoxLayout.Y_AXIS)); |
2 | 66 |
} |
67 |
updateStyle(popupMenu); |
|
68 |
} |
|
69 |
||
70 |
private void updateStyle(JComponent c) { |
|
71 |
SynthContext context = getContext(c, ENABLED); |
|
72 |
SynthStyle 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 |
||
4394 | 83 |
/** |
84 |
* @inheritDoc |
|
85 |
*/ |
|
86 |
@Override |
|
2 | 87 |
protected void installListeners() { |
88 |
super.installListeners(); |
|
89 |
popupMenu.addPropertyChangeListener(this); |
|
90 |
} |
|
91 |
||
4394 | 92 |
/** |
93 |
* @inheritDoc |
|
94 |
*/ |
|
95 |
@Override |
|
2 | 96 |
protected void uninstallDefaults() { |
97 |
SynthContext context = getContext(popupMenu, ENABLED); |
|
98 |
||
99 |
style.uninstallDefaults(context); |
|
100 |
context.dispose(); |
|
101 |
style = null; |
|
102 |
||
103 |
if (popupMenu.getLayout() instanceof UIResource) { |
|
104 |
popupMenu.setLayout(null); |
|
105 |
} |
|
106 |
} |
|
107 |
||
4394 | 108 |
/** |
109 |
* @inheritDoc |
|
110 |
*/ |
|
111 |
@Override |
|
2 | 112 |
protected void uninstallListeners() { |
113 |
super.uninstallListeners(); |
|
114 |
popupMenu.removePropertyChangeListener(this); |
|
115 |
} |
|
116 |
||
4394 | 117 |
/** |
118 |
* @inheritDoc |
|
119 |
*/ |
|
120 |
@Override |
|
2 | 121 |
public SynthContext getContext(JComponent c) { |
122 |
return getContext(c, getComponentState(c)); |
|
123 |
} |
|
124 |
||
125 |
private SynthContext getContext(JComponent c, int state) { |
|
126 |
return SynthContext.getContext(SynthContext.class, c, |
|
127 |
SynthLookAndFeel.getRegion(c), style, state); |
|
128 |
} |
|
129 |
||
130 |
private int getComponentState(JComponent c) { |
|
131 |
return SynthLookAndFeel.getComponentState(c); |
|
132 |
} |
|
133 |
||
4394 | 134 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
135 |
* Notifies this UI delegate to repaint the specified component. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
136 |
* This method paints the component background, then calls |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
137 |
* the {@link #paint(SynthContext,Graphics)} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
138 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
139 |
* <p>In general, this method does not need to be overridden by subclasses. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
140 |
* All Look and Feel rendering code should reside in the {@code paint} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
141 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
142 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
143 |
* @param c the component being painted |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
144 |
* @see #paint(SynthContext,Graphics) |
4394 | 145 |
*/ |
146 |
@Override |
|
2 | 147 |
public void update(Graphics g, JComponent c) { |
148 |
SynthContext context = getContext(c); |
|
149 |
||
150 |
SynthLookAndFeel.update(context, g); |
|
151 |
context.getPainter().paintPopupMenuBackground(context, |
|
152 |
g, 0, 0, c.getWidth(), c.getHeight()); |
|
153 |
paint(context, g); |
|
154 |
context.dispose(); |
|
155 |
} |
|
156 |
||
4394 | 157 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
158 |
* Paints the specified component according to the Look and Feel. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
159 |
* <p>This method is not used by Synth Look and Feel. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
160 |
* Painting is handled by the {@link #paint(SynthContext,Graphics)} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
161 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
162 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
163 |
* @param c the component being painted |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
164 |
* @see #paint(SynthContext,Graphics) |
4394 | 165 |
*/ |
166 |
@Override |
|
2 | 167 |
public void paint(Graphics g, JComponent c) { |
168 |
SynthContext context = getContext(c); |
|
169 |
||
170 |
paint(context, g); |
|
171 |
context.dispose(); |
|
172 |
} |
|
173 |
||
4394 | 174 |
/** |
175 |
* Paints the specified component. This implementation does nothing. |
|
176 |
* |
|
177 |
* @param context context for the component being painted |
|
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
178 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
179 |
* @see #update(Graphics,JComponent) |
4394 | 180 |
*/ |
2 | 181 |
protected void paint(SynthContext context, Graphics g) { |
182 |
} |
|
183 |
||
4394 | 184 |
/** |
185 |
* @inheritDoc |
|
186 |
*/ |
|
187 |
@Override |
|
2 | 188 |
public void paintBorder(SynthContext context, Graphics g, int x, |
189 |
int y, int w, int h) { |
|
190 |
context.getPainter().paintPopupMenuBorder(context, g, x, y, w, h); |
|
191 |
} |
|
192 |
||
4394 | 193 |
/** |
194 |
* @inheritDoc |
|
195 |
*/ |
|
196 |
@Override |
|
2 | 197 |
public void propertyChange(PropertyChangeEvent e) { |
198 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
|
199 |
updateStyle(popupMenu); |
|
200 |
} |
|
201 |
} |
|
202 |
} |