author | alexp |
Tue, 01 Sep 2009 18:51:15 +0400 | |
changeset 3748 | b88af44d77a8 |
parent 3737 | 83fb4621a129 |
child 4276 | ff9daeaa1e21 |
permissions | -rw-r--r-- |
3508 | 1 |
/* |
3733 | 2 |
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. |
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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
3508 | 24 |
*/ |
25 |
||
26 |
package javax.swing.plaf; |
|
27 |
||
28 |
import javax.accessibility.Accessible; |
|
29 |
import javax.swing.*; |
|
30 |
import javax.swing.plaf.ComponentUI; |
|
31 |
import java.awt.*; |
|
32 |
import java.awt.event.*; |
|
33 |
import java.beans.PropertyChangeEvent; |
|
34 |
import java.beans.PropertyChangeSupport; |
|
35 |
import java.beans.PropertyChangeListener; |
|
36 |
import java.io.Serializable; |
|
37 |
||
38 |
/** |
|
39 |
* The base class for all {@link javax.swing.JLayer}'s UI delegates. |
|
40 |
* <p/> |
|
41 |
* {@link #paint(java.awt.Graphics, javax.swing.JComponent)} method performes the |
|
42 |
* painting of the {@code JLayer} |
|
43 |
* and {@link #eventDispatched(AWTEvent, JLayer)} method is notified |
|
44 |
* about any {@code AWTEvent}s which have been generated by a {@code JLayer} |
|
45 |
* or any of its subcomponents. |
|
46 |
* <p/> |
|
47 |
* The {@code LayerUI} differs from the UI delegates of the other components, |
|
48 |
* because it is LookAndFeel independent and is not updated by default when |
|
49 |
* the system LookAndFeel is changed. |
|
50 |
* <p/> |
|
51 |
* The subclasses of {@code LayerUI} can either be stateless and shareable |
|
52 |
* by multiple {@code JLayer}s or not shareable. |
|
53 |
* |
|
54 |
* @param <V> one of the super types of {@code JLayer}'s view component |
|
55 |
* |
|
56 |
* @see JLayer#setUI(LayerUI) |
|
57 |
* @see JLayer#setView(Component) |
|
58 |
* @see JLayer#getView() |
|
59 |
* @since 1.7 |
|
60 |
* |
|
61 |
* @author Alexander Potochkin |
|
62 |
*/ |
|
63 |
public class LayerUI<V extends Component> |
|
64 |
extends ComponentUI implements Serializable { |
|
65 |
||
66 |
private final PropertyChangeSupport propertyChangeSupport = |
|
67 |
new PropertyChangeSupport(this); |
|
68 |
||
69 |
/** |
|
70 |
* Paints the specified component. |
|
71 |
* Subclasses should override this method and use |
|
72 |
* the specified {@code Graphics} object to |
|
73 |
* render the content of the component. |
|
74 |
* |
|
75 |
* @param g the {@code Graphics} context in which to paint; |
|
76 |
* @param c the component being painted; |
|
77 |
* it can be safely cast to the {@code JLayer<V>} |
|
78 |
*/ |
|
79 |
@Override |
|
80 |
public void paint(Graphics g, JComponent c) { |
|
81 |
c.paint(g); |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Dispatches {@code AWTEvent}s for {@code JLayer} |
|
86 |
* and <b>all it subcomponents</b> to this {@code LayerUI} instance. |
|
87 |
* <p> |
|
88 |
* To enable the {@code AWTEvent} of the particular type, |
|
89 |
* you call {@link javax.swing.JLayer#setLayerEventMask} |
|
90 |
* in {@link #installUI(javax.swing.JComponent)} |
|
91 |
* and set the layer event mask to {@code 0} |
|
92 |
* in {@link #uninstallUI(javax.swing.JComponent)} after that |
|
93 |
* |
|
94 |
* @param e the event to be dispatched |
|
95 |
* @param l the layer this LayerUI is set to |
|
96 |
* |
|
97 |
* @see JLayer#setLayerEventMask(long) |
|
98 |
* @see javax.swing.JLayer#getLayerEventMask() |
|
99 |
*/ |
|
100 |
public void eventDispatched(AWTEvent e, JLayer<? extends V> l){ |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Invoked when {@link javax.swing.JLayer#updateUI()} is called |
|
105 |
* by the {@code JLayer} this {@code LayerUI} is set to. |
|
106 |
* |
|
107 |
* @param l the {@code JLayer} which UI is updated |
|
108 |
*/ |
|
109 |
public void updateUI(JLayer<? extends V> l){ |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Configures the {@code JLayer} this {@code LayerUI} is set to. |
|
114 |
* The default implementation registers the {@code LayerUI} |
|
115 |
* as a property change listener for the passed {@code JLayer} component. |
|
116 |
* |
|
117 |
* @param c the {@code JLayer} component where this UI delegate is being installed |
|
118 |
*/ |
|
119 |
public void installUI(JComponent c) { |
|
120 |
addPropertyChangeListener((JLayer) c); |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* Reverses the configuration which was previously set |
|
125 |
* in the {@link #installUI(JComponent)} method. |
|
126 |
* The default implementation unregisters the property change listener |
|
127 |
* for the passed JLayer component. |
|
128 |
* |
|
129 |
* @param c the component from which this UI delegate is being removed. |
|
130 |
*/ |
|
131 |
public void uninstallUI(JComponent c) { |
|
132 |
removePropertyChangeListener((JLayer) c); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Adds a PropertyChangeListener to the listener list. The listener is |
|
137 |
* registered for all bound properties of this class. |
|
138 |
* <p/> |
|
139 |
* If {@code listener} is {@code null}, |
|
140 |
* no exception is thrown and no action is performed. |
|
141 |
* |
|
142 |
* @param listener the property change listener to be added |
|
143 |
* @see #removePropertyChangeListener |
|
144 |
* @see #getPropertyChangeListeners |
|
145 |
* @see #addPropertyChangeListener(String, java.beans.PropertyChangeListener) |
|
146 |
*/ |
|
147 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
|
148 |
propertyChangeSupport.addPropertyChangeListener(listener); |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* Removes a PropertyChangeListener from the listener list. This method |
|
153 |
* should be used to remove PropertyChangeListeners that were registered |
|
154 |
* for all bound properties of this class. |
|
155 |
* <p/> |
|
156 |
* If {@code listener} is {@code null}, |
|
157 |
* no exception is thrown and no action is performed. |
|
158 |
* |
|
159 |
* @param listener the PropertyChangeListener to be removed |
|
160 |
* @see #addPropertyChangeListener |
|
161 |
* @see #getPropertyChangeListeners |
|
162 |
* @see #removePropertyChangeListener(String, PropertyChangeListener) |
|
163 |
*/ |
|
164 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
|
165 |
propertyChangeSupport.removePropertyChangeListener(listener); |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Returns an array of all the property change listeners |
|
170 |
* registered on this component. |
|
171 |
* |
|
172 |
* @return all of this ui's {@code PropertyChangeListener}s |
|
173 |
* or an empty array if no property change |
|
174 |
* listeners are currently registered |
|
175 |
* @see #addPropertyChangeListener |
|
176 |
* @see #removePropertyChangeListener |
|
177 |
* @see #getPropertyChangeListeners(String) |
|
178 |
*/ |
|
179 |
public PropertyChangeListener[] getPropertyChangeListeners() { |
|
180 |
return propertyChangeSupport.getPropertyChangeListeners(); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* Adds a PropertyChangeListener to the listener list for a specific |
|
185 |
* property. |
|
186 |
* <p/> |
|
187 |
* If {@code propertyName} or {@code listener} is {@code null}, |
|
188 |
* no exception is thrown and no action is taken. |
|
189 |
* |
|
190 |
* @param propertyName one of the property names listed above |
|
191 |
* @param listener the property change listener to be added |
|
192 |
* @see #removePropertyChangeListener(String, PropertyChangeListener) |
|
193 |
* @see #getPropertyChangeListeners(String) |
|
194 |
* @see #addPropertyChangeListener(String, PropertyChangeListener) |
|
195 |
*/ |
|
196 |
public void addPropertyChangeListener(String propertyName, |
|
197 |
PropertyChangeListener listener) { |
|
198 |
propertyChangeSupport.addPropertyChangeListener(propertyName, listener); |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* Removes a {@code PropertyChangeListener} from the listener |
|
203 |
* list for a specific property. This method should be used to remove |
|
204 |
* {@code PropertyChangeListener}s |
|
205 |
* that were registered for a specific bound property. |
|
206 |
* <p/> |
|
207 |
* If {@code propertyName} or {@code listener} is {@code null}, |
|
208 |
* no exception is thrown and no action is taken. |
|
209 |
* |
|
210 |
* @param propertyName a valid property name |
|
211 |
* @param listener the PropertyChangeListener to be removed |
|
212 |
* @see #addPropertyChangeListener(String, PropertyChangeListener) |
|
213 |
* @see #getPropertyChangeListeners(String) |
|
214 |
* @see #removePropertyChangeListener(PropertyChangeListener) |
|
215 |
*/ |
|
216 |
public void removePropertyChangeListener(String propertyName, |
|
217 |
PropertyChangeListener listener) { |
|
218 |
propertyChangeSupport.removePropertyChangeListener(propertyName, listener); |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Returns an array of all the listeners which have been associated |
|
223 |
* with the named property. |
|
224 |
* |
|
3748
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
225 |
* @param propertyName The name of the property being listened to |
3508 | 226 |
* @return all of the {@code PropertyChangeListener}s associated with |
227 |
* the named property; if no such listeners have been added or |
|
228 |
* if {@code propertyName} is {@code null}, an empty |
|
229 |
* array is returned |
|
230 |
* @see #addPropertyChangeListener(String, PropertyChangeListener) |
|
231 |
* @see #removePropertyChangeListener(String, PropertyChangeListener) |
|
232 |
* @see #getPropertyChangeListeners |
|
233 |
*/ |
|
234 |
public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) { |
|
235 |
return propertyChangeSupport.getPropertyChangeListeners(propertyName); |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Support for reporting bound property changes for Object properties. |
|
240 |
* This method can be called when a bound property has changed and it will |
|
241 |
* send the appropriate PropertyChangeEvent to any registered |
|
242 |
* PropertyChangeListeners. |
|
243 |
* |
|
244 |
* @param propertyName the property whose value has changed |
|
245 |
* @param oldValue the property's previous value |
|
246 |
* @param newValue the property's new value |
|
247 |
*/ |
|
248 |
protected void firePropertyChange(String propertyName, |
|
249 |
Object oldValue, Object newValue) { |
|
250 |
propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Notifies the {@code LayerUI} when any of its property are changed |
|
255 |
* and enables updating every {@code JLayer} this {@code LayerUI} instance is set to. |
|
256 |
* |
|
257 |
* @param evt the PropertyChangeEvent generated by this {@code LayerUI} |
|
258 |
* @param l the {@code JLayer} this LayerUI is set to |
|
259 |
*/ |
|
260 |
public void applyPropertyChange(PropertyChangeEvent evt, JLayer<? extends V> l) { |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Returns the preferred size of the viewport for a view component. |
|
265 |
* |
|
3748
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
266 |
* @param l the {@code JLayer} component where this UI delegate is being installed |
3508 | 267 |
* @return the preferred size of the viewport for a view component |
268 |
* @see Scrollable#getPreferredScrollableViewportSize() |
|
269 |
*/ |
|
270 |
public Dimension getPreferredScrollableViewportSize(JLayer<? extends V> l) { |
|
271 |
if (l.getView() instanceof Scrollable) { |
|
272 |
return ((Scrollable)l.getView()).getPreferredScrollableViewportSize(); |
|
273 |
} |
|
274 |
return l.getPreferredSize(); |
|
275 |
} |
|
276 |
||
277 |
/** |
|
278 |
* Returns a scroll increment, which is required for components |
|
279 |
* that display logical rows or columns in order to completely expose |
|
280 |
* one block of rows or columns, depending on the value of orientation. |
|
281 |
* |
|
3748
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
282 |
* @param l the {@code JLayer} component where this UI delegate is being installed |
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
283 |
* @param visibleRect The view area visible within the viewport |
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
284 |
* @param orientation Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL. |
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
285 |
* @param direction Less than zero to scroll up/left, greater than zero for down/right. |
3508 | 286 |
* @return the "block" increment for scrolling in the specified direction |
287 |
* @see Scrollable#getScrollableBlockIncrement(Rectangle, int, int) |
|
288 |
*/ |
|
289 |
public int getScrollableBlockIncrement(JLayer<? extends V> l, |
|
290 |
Rectangle visibleRect, |
|
291 |
int orientation, int direction) { |
|
292 |
if (l.getView() instanceof Scrollable) { |
|
293 |
return ((Scrollable)l.getView()).getScrollableBlockIncrement( |
|
294 |
visibleRect,orientation, direction); |
|
295 |
} |
|
296 |
return (orientation == SwingConstants.VERTICAL) ? visibleRect.height : |
|
297 |
visibleRect.width; |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* Returns {@code false} to indicate that the height of the viewport does not |
|
302 |
* determine the height of the layer, unless the preferred height |
|
303 |
* of the layer is smaller than the height of the viewport. |
|
304 |
* |
|
3748
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
305 |
* @param l the {@code JLayer} component where this UI delegate is being installed |
3508 | 306 |
* @return whether the layer should track the height of the viewport |
307 |
* @see Scrollable#getScrollableTracksViewportHeight() |
|
308 |
*/ |
|
309 |
public boolean getScrollableTracksViewportHeight(JLayer<? extends V> l) { |
|
310 |
if (l.getView() instanceof Scrollable) { |
|
311 |
return ((Scrollable)l.getView()).getScrollableTracksViewportHeight(); |
|
312 |
} |
|
313 |
return false; |
|
314 |
} |
|
315 |
||
316 |
/** |
|
317 |
* Returns {@code false} to indicate that the width of the viewport does not |
|
318 |
* determine the width of the layer, unless the preferred width |
|
319 |
* of the layer is smaller than the width of the viewport. |
|
320 |
* |
|
3748
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
321 |
* @param l the {@code JLayer} component where this UI delegate is being installed |
3508 | 322 |
* @return whether the layer should track the width of the viewport |
323 |
* @see Scrollable |
|
324 |
* @see LayerUI#getScrollableTracksViewportWidth(JLayer) |
|
325 |
*/ |
|
326 |
public boolean getScrollableTracksViewportWidth(JLayer<? extends V> l) { |
|
327 |
if (l.getView() instanceof Scrollable) { |
|
328 |
return ((Scrollable)l.getView()).getScrollableTracksViewportWidth(); |
|
329 |
} |
|
330 |
return false; |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* Returns a scroll increment, which is required for components |
|
335 |
* that display logical rows or columns in order to completely expose |
|
336 |
* one new row or column, depending on the value of orientation. |
|
337 |
* Ideally, components should handle a partially exposed row or column |
|
338 |
* by returning the distance required to completely expose the item. |
|
339 |
* <p> |
|
340 |
* Scrolling containers, like JScrollPane, will use this method |
|
341 |
* each time the user requests a unit scroll. |
|
342 |
* |
|
3748
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
343 |
* @param l the {@code JLayer} component where this UI delegate is being installed |
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
344 |
* @param visibleRect The view area visible within the viewport |
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
345 |
* @param orientation Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL. |
b88af44d77a8
6875153: JLayer.isOptimizedDrawingEnabled() throws NPE for null glass pane set
alexp
parents:
3737
diff
changeset
|
346 |
* @param direction Less than zero to scroll up/left, greater than zero for down/right. |
3508 | 347 |
* @return The "unit" increment for scrolling in the specified direction. |
348 |
* This value should always be positive. |
|
349 |
* @see Scrollable#getScrollableUnitIncrement(Rectangle, int, int) |
|
350 |
*/ |
|
351 |
public int getScrollableUnitIncrement(JLayer<? extends V> l, |
|
352 |
Rectangle visibleRect, |
|
353 |
int orientation, int direction) { |
|
354 |
if (l.getView() instanceof Scrollable) { |
|
355 |
return ((Scrollable)l.getView()).getScrollableUnitIncrement( |
|
356 |
visibleRect, orientation, direction); |
|
357 |
} |
|
358 |
return 1; |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* If the {@code JLayer}'s view component is not {@code null}, |
|
363 |
* this calls the view's {@code getBaseline()} method. |
|
364 |
* Otherwise, the default implementation is called. |
|
365 |
* |
|
366 |
* @param c {@code JLayer} to return baseline resize behavior for |
|
367 |
* @param width the width to get the baseline for |
|
368 |
* @param height the height to get the baseline for |
|
369 |
* @return baseline or a value < 0 indicating there is no reasonable |
|
370 |
* baseline |
|
371 |
*/ |
|
372 |
public int getBaseline(JComponent c, int width, int height) { |
|
373 |
JLayer l = (JLayer) c; |
|
374 |
if (l.getView() != null) { |
|
375 |
return l.getView().getBaseline(width, height); |
|
376 |
} |
|
377 |
return super.getBaseline(c, width, height); |
|
378 |
} |
|
379 |
||
380 |
/** |
|
381 |
* If the {@code JLayer}'s view component is not {@code null}, |
|
382 |
* this calls the view's {@code getBaselineResizeBehavior()} method. |
|
383 |
* Otherwise, the default implementation is called. |
|
384 |
* |
|
385 |
* @param c {@code JLayer} to return baseline resize behavior for |
|
386 |
* @return an enum indicating how the baseline changes as the component |
|
387 |
* size changes |
|
388 |
*/ |
|
389 |
public Component.BaselineResizeBehavior getBaselineResizeBehavior(JComponent c) { |
|
390 |
JLayer l = (JLayer) c; |
|
391 |
if (l.getView() != null) { |
|
392 |
return l.getView().getBaselineResizeBehavior(); |
|
393 |
} |
|
394 |
return super.getBaselineResizeBehavior(c); |
|
395 |
} |
|
3733 | 396 |
} |