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;
|
|
27 |
|
|
28 |
import javax.swing.plaf.LayerUI;
|
|
29 |
import java.awt.*;
|
|
30 |
import java.awt.event.*;
|
|
31 |
import java.beans.PropertyChangeEvent;
|
|
32 |
import java.beans.PropertyChangeListener;
|
|
33 |
import java.io.IOException;
|
|
34 |
import java.io.ObjectInputStream;
|
|
35 |
import java.io.Serializable;
|
|
36 |
import java.lang.ref.WeakReference;
|
|
37 |
import java.util.ArrayList;
|
|
38 |
import java.util.Iterator;
|
|
39 |
import java.security.AccessController;
|
|
40 |
import java.security.PrivilegedAction;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* {@code JLayer} is a universal decorator for Swing components
|
|
44 |
* which enables you to implement various advanced painting effects as well as
|
|
45 |
* receive notifications of all {@code AWTEvent}s generated within its borders.
|
|
46 |
* <p/>
|
|
47 |
* {@code JLayer} delegates the handling of painting and input events to a
|
|
48 |
* {@link javax.swing.plaf.LayerUI} object, which performs the actual decoration.
|
|
49 |
* <p/>
|
|
50 |
* The custom painting implemented in the {@code LayerUI} and events notification
|
|
51 |
* work for the JLayer itself and all its subcomponents.
|
|
52 |
* This combination enables you to enrich existing components
|
|
53 |
* by adding new advanced functionality such as temporary locking of a hierarchy,
|
|
54 |
* data tips for compound components, enhanced mouse scrolling etc and so on.
|
|
55 |
* <p/>
|
|
56 |
* {@code JLayer} is a good solution if you only need to do custom painting
|
|
57 |
* over compound component or catch input events from its subcomponents.
|
|
58 |
* <pre>
|
|
59 |
* // create a component to be decorated with the layer
|
|
60 |
* JPanel panel = new JPanel();
|
|
61 |
* panel.add(new JButton("JButton"));
|
|
62 |
* // This custom layerUI will fill the layer with translucent green
|
|
63 |
* // and print out all mouseMotion events generated within its borders
|
|
64 |
* LayerUI<JPanel> layerUI = new LayerUI<JPanel>() {
|
|
65 |
* public void paint(Graphics g, JCompo nent c) {
|
|
66 |
* // paint the layer as is
|
|
67 |
* super.paint(g, c);
|
|
68 |
* // fill it with the translucent green
|
|
69 |
* g.setColor(new Color(0, 128, 0, 128));
|
|
70 |
* g.fillRect(0, 0, c.getWidth(), c.getHeight());
|
|
71 |
* }
|
|
72 |
* // overridden method which catches MouseMotion events
|
|
73 |
* public void eventDispatched(AWTEvent e, JLayer<JPanel> l) {
|
|
74 |
* System.out.println("AWTEvent detected: " + e);
|
|
75 |
* }
|
|
76 |
* };
|
|
77 |
* // create the layer for the panel using our custom layerUI
|
|
78 |
* JLayer<JPanel> layer = new JLayer<JPanel>(panel, layerUI);
|
|
79 |
* // work with the layer as with any other Swing component
|
|
80 |
* frame.add(layer);
|
|
81 |
* </pre>
|
|
82 |
*
|
|
83 |
* <b>Note:</b> {@code JLayer} doesn't support the following methods:
|
|
84 |
* <ul>
|
|
85 |
* <li>{@link Container#add(java.awt.Component)}</li>
|
|
86 |
* <li>{@link Container#add(String, java.awt.Component)}</li>
|
|
87 |
* <li>{@link Container#add(java.awt.Component, int)}</li>
|
|
88 |
* <li>{@link Container#add(java.awt.Component, Object)}</li>
|
|
89 |
* <li>{@link Container#add(java.awt.Component, Object, int)}</li>
|
|
90 |
* </ul>
|
|
91 |
* using any of of them will cause {@code UnsupportedOperationException} to be thrown,
|
|
92 |
* to add a component to {@code JLayer}
|
|
93 |
* use {@link #setView(Component)} or {@link #setGlassPane(JPanel)}.
|
|
94 |
*
|
|
95 |
* @param <V> the type of {@code JLayer}'s view component
|
|
96 |
*
|
|
97 |
* @see #JLayer(Component)
|
|
98 |
* @see #setView(Component)
|
|
99 |
* @see #getView()
|
|
100 |
* @see javax.swing.plaf.LayerUI
|
|
101 |
* @see #JLayer(Component, LayerUI)
|
|
102 |
* @see #setUI(javax.swing.plaf.LayerUI)
|
|
103 |
* @see #getUI()
|
|
104 |
* @since 1.7
|
|
105 |
*
|
|
106 |
* @author Alexander Potochkin
|
|
107 |
*/
|
|
108 |
public final class JLayer<V extends Component>
|
|
109 |
extends JComponent
|
|
110 |
implements Scrollable, PropertyChangeListener {
|
|
111 |
private V view;
|
|
112 |
// this field is necessary because JComponent.ui is transient
|
|
113 |
// when layerUI is serializable
|
|
114 |
private LayerUI<? super V> layerUI;
|
|
115 |
private JPanel glassPane;
|
|
116 |
private boolean isPainting;
|
|
117 |
private static final DefaultLayerLayout sharedLayoutInstance =
|
|
118 |
new DefaultLayerLayout();
|
|
119 |
private long eventMask;
|
|
120 |
|
|
121 |
private static final LayerEventController eventController =
|
|
122 |
new LayerEventController();
|
|
123 |
|
|
124 |
private static final long ACCEPTED_EVENTS =
|
|
125 |
AWTEvent.COMPONENT_EVENT_MASK |
|
|
126 |
AWTEvent.CONTAINER_EVENT_MASK |
|
|
127 |
AWTEvent.FOCUS_EVENT_MASK |
|
|
128 |
AWTEvent.KEY_EVENT_MASK |
|
|
129 |
AWTEvent.MOUSE_WHEEL_EVENT_MASK |
|
|
130 |
AWTEvent.MOUSE_MOTION_EVENT_MASK |
|
|
131 |
AWTEvent.MOUSE_EVENT_MASK |
|
|
132 |
AWTEvent.INPUT_METHOD_EVENT_MASK |
|
|
133 |
AWTEvent.HIERARCHY_EVENT_MASK |
|
|
134 |
AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK;
|
|
135 |
|
|
136 |
/**
|
|
137 |
* Creates a new {@code JLayer} object with a {@code null} view component
|
|
138 |
* and {@code null} {@link javax.swing.plaf.LayerUI}.
|
|
139 |
*
|
|
140 |
* @see #setView
|
|
141 |
* @see #setUI
|
|
142 |
*/
|
|
143 |
public JLayer() {
|
|
144 |
this(null);
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Creates a new {@code JLayer} object
|
|
149 |
* with {@code null} {@link javax.swing.plaf.LayerUI}.
|
|
150 |
*
|
|
151 |
* @param view the component to be decorated by this {@code JLayer}
|
|
152 |
*
|
|
153 |
* @see #setUI
|
|
154 |
*/
|
|
155 |
public JLayer(V view) {
|
|
156 |
this(view, null);
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Creates a new {@code JLayer} object with the specified view component
|
|
161 |
* and {@link javax.swing.plaf.LayerUI} object.
|
|
162 |
*
|
|
163 |
* @param view the component to be decorated
|
|
164 |
* @param ui the {@link javax.swing.plaf.LayerUI} delegate
|
|
165 |
* to be used by this {@code JLayer}
|
|
166 |
*/
|
|
167 |
public JLayer(V view, LayerUI<V> ui) {
|
|
168 |
setLayout(sharedLayoutInstance);
|
|
169 |
setGlassPane(createGlassPane());
|
|
170 |
setView(view);
|
|
171 |
setUI(ui);
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Returns the {@code JLayer}'s view component or {@code null}.
|
|
176 |
* <br/>This is a bound property.
|
|
177 |
*
|
|
178 |
* @return the {@code JLayer}'s view component
|
|
179 |
* or {@code null} if none exists
|
|
180 |
*
|
|
181 |
* @see #setView(V)
|
|
182 |
*/
|
|
183 |
public V getView() {
|
|
184 |
return view;
|
|
185 |
}
|
|
186 |
|
|
187 |
/**
|
|
188 |
* Sets the {@code JLayer}'s view component, which can be {@code null}.
|
|
189 |
* <br/>This is a bound property.
|
|
190 |
*
|
|
191 |
* @param view the view component for this {@code JLayer}
|
|
192 |
*
|
|
193 |
* @see #getView()
|
|
194 |
*/
|
|
195 |
public void setView(V view) {
|
|
196 |
Component oldView = getView();
|
|
197 |
if (oldView != null) {
|
|
198 |
super.remove(oldView);
|
|
199 |
}
|
|
200 |
if (view != null) {
|
|
201 |
super.addImpl(view, null, getComponentCount());
|
|
202 |
}
|
|
203 |
this.view = view;
|
|
204 |
firePropertyChange("view", oldView, view);
|
|
205 |
revalidate();
|
|
206 |
repaint();
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Sets the {@link javax.swing.plaf.LayerUI} which will perform painting
|
|
211 |
* and receive input events for this {@code JLayer}.
|
|
212 |
*
|
|
213 |
* @param ui the {@link javax.swing.plaf.LayerUI} for this {@code JLayer}
|
|
214 |
*/
|
|
215 |
public void setUI(LayerUI<? super V> ui) {
|
|
216 |
this.layerUI = ui;
|
|
217 |
super.setUI(ui);
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* Returns the {@link javax.swing.plaf.LayerUI} for this {@code JLayer}.
|
|
222 |
*
|
|
223 |
* @return the {@code LayerUI} for this {@code JLayer}
|
|
224 |
*/
|
|
225 |
public LayerUI<? super V> getUI() {
|
|
226 |
return layerUI;
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Returns the {@code JLayer}'s glassPane component or {@code null}.
|
|
231 |
* <br/>This is a bound property.
|
|
232 |
*
|
|
233 |
* @return the {@code JLayer}'s glassPane component
|
|
234 |
* or {@code null} if none exists
|
|
235 |
*
|
|
236 |
* @see #setGlassPane(JPanel)
|
|
237 |
*/
|
|
238 |
public JPanel getGlassPane() {
|
|
239 |
return glassPane;
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Sets the {@code JLayer}'s glassPane component, which can be {@code null}.
|
|
244 |
* <br/>This is a bound property.
|
|
245 |
*
|
|
246 |
* @param glassPane the glassPane component of this {@code JLayer}
|
|
247 |
*
|
|
248 |
* @see #getGlassPane()
|
|
249 |
*/
|
|
250 |
public void setGlassPane(JPanel glassPane) {
|
|
251 |
Component oldGlassPane = getGlassPane();
|
|
252 |
if (oldGlassPane != null) {
|
|
253 |
super.remove(oldGlassPane);
|
|
254 |
}
|
|
255 |
if (glassPane != null) {
|
|
256 |
super.addImpl(glassPane, null, 0);
|
|
257 |
}
|
|
258 |
this.glassPane = glassPane;
|
|
259 |
firePropertyChange("glassPane", oldGlassPane, glassPane);
|
|
260 |
revalidate();
|
|
261 |
repaint();
|
|
262 |
}
|
|
263 |
|
|
264 |
/**
|
|
265 |
* Called by the constructor methods to create a default {@code glassPane}.
|
|
266 |
* By default this method creates a new JPanel with visibility set to true
|
|
267 |
* and opacity set to false.
|
|
268 |
*
|
|
269 |
* @return the default {@code glassPane}
|
|
270 |
*/
|
|
271 |
public JPanel createGlassPane() {
|
|
272 |
return new DefaultLayerGlassPane();
|
|
273 |
}
|
|
274 |
|
|
275 |
/**
|
|
276 |
* This method is not supported by {@code JLayer}
|
|
277 |
* and always throws {@code UnsupportedOperationException}
|
|
278 |
*
|
|
279 |
* @throws UnsupportedOperationException this method is not supported
|
|
280 |
*
|
|
281 |
* @see #setView(Component)
|
|
282 |
* @see #setGlassPane(Component)
|
|
283 |
*/
|
|
284 |
protected void addImpl(Component comp, Object constraints, int index) {
|
|
285 |
throw new UnsupportedOperationException(
|
|
286 |
"Adding components to JLayer is not supported, " +
|
|
287 |
"use setView() or setGlassPane() instead");
|
|
288 |
}
|
|
289 |
|
|
290 |
/**
|
|
291 |
* {@inheritDoc}
|
|
292 |
*/
|
|
293 |
public void remove(Component comp) {
|
|
294 |
if (comp == getView()) {
|
|
295 |
setView(null);
|
|
296 |
} else if (comp == getGlassPane()) {
|
|
297 |
setGlassPane(null);
|
|
298 |
} else {
|
|
299 |
super.remove(comp);
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
|
303 |
/**
|
|
304 |
* {@inheritDoc}
|
|
305 |
*/
|
|
306 |
public void removeAll() {
|
|
307 |
setView(null);
|
|
308 |
setGlassPane(null);
|
|
309 |
}
|
|
310 |
|
|
311 |
/**
|
|
312 |
* Delegates all painting to the {@link javax.swing.plaf.LayerUI} object.
|
|
313 |
*
|
|
314 |
* @param g the {@code Graphics} to render to
|
|
315 |
*/
|
|
316 |
public void paint(Graphics g) {
|
|
317 |
if (!isPainting) {
|
|
318 |
isPainting = true;
|
|
319 |
super.paintComponent(g);
|
|
320 |
isPainting = false;
|
|
321 |
} else {
|
|
322 |
super.paint(g);
|
|
323 |
}
|
|
324 |
}
|
|
325 |
|
|
326 |
/**
|
|
327 |
* This method is empty, because all painting is done by
|
|
328 |
* {@link #paint(Graphics)} and
|
|
329 |
* {@link javax.swing.plaf.LayerUI#update(Graphics, JComponent)} methods
|
|
330 |
*/
|
|
331 |
protected void paintComponent(Graphics g) {
|
|
332 |
}
|
|
333 |
|
|
334 |
/**
|
|
335 |
* To enable the correct painting of the {@code glassPane} and view component,
|
|
336 |
* the {@code JLayer} overrides the default implementation of
|
|
337 |
* this method to return {@code false} when the {@code glassPane} is visible.
|
|
338 |
*
|
|
339 |
* @return false if {@code JLayer}'s {@code glassPane} is visible
|
|
340 |
*/
|
|
341 |
public boolean isOptimizedDrawingEnabled() {
|
|
342 |
return !glassPane.isVisible();
|
|
343 |
}
|
|
344 |
|
|
345 |
/**
|
|
346 |
* {@inheritDoc}
|
|
347 |
*/
|
|
348 |
public void propertyChange(PropertyChangeEvent evt) {
|
|
349 |
if (getUI() != null) {
|
|
350 |
getUI().applyPropertyChange(evt, this);
|
|
351 |
}
|
|
352 |
}
|
|
353 |
|
|
354 |
/**
|
|
355 |
* Sets the bitmask of event types to receive by this {@code JLayer}.
|
|
356 |
* Here is the list of the supported event types:
|
|
357 |
* <ul>
|
|
358 |
* <li>AWTEvent.COMPONENT_EVENT_MASK</li>
|
|
359 |
* <li>AWTEvent.CONTAINER_EVENT_MASK</li>
|
|
360 |
* <li>AWTEvent.FOCUS_EVENT_MASK</li>
|
|
361 |
* <li>AWTEvent.KEY_EVENT_MASK</li>
|
|
362 |
* <li>AWTEvent.MOUSE_WHEEL_EVENT_MASK</li>
|
|
363 |
* <li>AWTEvent.MOUSE_MOTION_EVENT_MASK</li>
|
|
364 |
* <li>AWTEvent.MOUSE_EVENT_MASK</li>
|
|
365 |
* <li>AWTEvent.INPUT_METHOD_EVENT_MASK</li>
|
|
366 |
* <li>AWTEvent.HIERARCHY_EVENT_MASK</li>
|
|
367 |
* <li>AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK</li>
|
|
368 |
* </ul>
|
|
369 |
* <p/>
|
|
370 |
* If {@code LayerUI} is installed,
|
|
371 |
* {@link javax.swing.plaf.LayerUI#eventDispatched(AWTEvent, JLayer)} method
|
|
372 |
* will only receive events that match the event mask.
|
|
373 |
* <p/>
|
|
374 |
* The following example shows how to correclty use this method
|
|
375 |
* in the {@code LayerUI} implementations:
|
|
376 |
* <pre>
|
|
377 |
* public void installUI(JComponent c) {
|
|
378 |
* super.installUI(c);
|
|
379 |
* JLayer l = (JLayer) c;
|
|
380 |
* // this LayerUI will receive only key and focus events
|
|
381 |
* l.setLayerEventMask(AWTEvent.KEY_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
|
|
382 |
* }
|
|
383 |
*
|
|
384 |
* public void uninstallUI(JComponent c) {
|
|
385 |
* super.uninstallUI(c);
|
|
386 |
* JLayer l = (JLayer) c;
|
|
387 |
* // JLayer must be returned to its initial state
|
|
388 |
* l.setLayerEventMask(0);
|
|
389 |
* }
|
|
390 |
* </pre>
|
|
391 |
*
|
|
392 |
* By default {@code JLayer} receives no events.
|
|
393 |
*
|
|
394 |
* @param layerEventMask the bitmask of event types to receive
|
|
395 |
*
|
|
396 |
* @throws IllegalArgumentException if the {@code layerEventMask} parameter
|
|
397 |
* contains unsupported event types
|
|
398 |
* @see #getLayerEventMask()
|
|
399 |
*/
|
|
400 |
public void setLayerEventMask(long layerEventMask) {
|
|
401 |
if (layerEventMask != (layerEventMask & ACCEPTED_EVENTS)) {
|
|
402 |
throw new IllegalArgumentException(
|
|
403 |
"The event bitmask contains unsupported event types");
|
|
404 |
}
|
|
405 |
long oldEventMask = getLayerEventMask();
|
|
406 |
this.eventMask = layerEventMask;
|
|
407 |
firePropertyChange("layerEventMask", oldEventMask, layerEventMask);
|
|
408 |
if (layerEventMask != oldEventMask) {
|
|
409 |
disableEvents(oldEventMask);
|
|
410 |
enableEvents(eventMask);
|
3743
|
411 |
if (isDisplayable()) {
|
|
412 |
eventController.updateAWTEventListener(
|
|
413 |
oldEventMask, layerEventMask);
|
|
414 |
}
|
3508
|
415 |
}
|
|
416 |
}
|
|
417 |
|
|
418 |
/**
|
|
419 |
* Returns the bitmap of event mask to receive by this {@code JLayer}
|
|
420 |
* and its {@code LayerUI}.
|
|
421 |
* <p/>
|
|
422 |
* It means that {@link javax.swing.plaf.LayerUI#eventDispatched(AWTEvent, JLayer)} method
|
|
423 |
* will only receive events that match the event mask.
|
|
424 |
* <p/>
|
|
425 |
* By default {@code JLayer} receives no events.
|
|
426 |
*
|
|
427 |
* @return the bitmask of event types to receive for this {@code JLayer}
|
|
428 |
*/
|
|
429 |
public long getLayerEventMask() {
|
|
430 |
return eventMask;
|
|
431 |
}
|
|
432 |
|
|
433 |
/**
|
|
434 |
* Delegates its functionality to the {@link javax.swing.plaf.LayerUI#updateUI(JLayer)} method,
|
|
435 |
* if {@code LayerUI} is set.
|
|
436 |
*/
|
|
437 |
public void updateUI() {
|
|
438 |
if (getUI() != null) {
|
|
439 |
getUI().updateUI(this);
|
|
440 |
}
|
|
441 |
}
|
|
442 |
|
|
443 |
/**
|
|
444 |
* Returns the preferred size of the viewport for a view component.
|
|
445 |
* <p/>
|
|
446 |
* If the ui delegate of this layer is not {@code null}, this method delegates its
|
|
447 |
* implementation to the {@code LayerUI.getPreferredScrollableViewportSize(JLayer)}
|
|
448 |
*
|
|
449 |
* @return the preferred size of the viewport for a view component
|
|
450 |
*
|
|
451 |
* @see Scrollable
|
|
452 |
* @see LayerUI#getPreferredScrollableViewportSize(JLayer)
|
|
453 |
*/
|
|
454 |
public Dimension getPreferredScrollableViewportSize() {
|
|
455 |
if (getUI() != null) {
|
|
456 |
return getUI().getPreferredScrollableViewportSize(this);
|
|
457 |
}
|
|
458 |
return getPreferredSize();
|
|
459 |
}
|
|
460 |
|
|
461 |
/**
|
|
462 |
* Returns a scroll increment, which is required for components
|
|
463 |
* that display logical rows or columns in order to completely expose
|
|
464 |
* one block of rows or columns, depending on the value of orientation.
|
|
465 |
* <p/>
|
|
466 |
* If the ui delegate of this layer is not {@code null}, this method delegates its
|
|
467 |
* implementation to the {@code LayerUI.getScrollableBlockIncrement(JLayer,Rectangle,int,int)}
|
|
468 |
*
|
|
469 |
* @return the "block" increment for scrolling in the specified direction
|
|
470 |
*
|
|
471 |
* @see Scrollable
|
|
472 |
* @see LayerUI#getScrollableBlockIncrement(JLayer, Rectangle, int, int)
|
|
473 |
*/
|
|
474 |
public int getScrollableBlockIncrement(Rectangle visibleRect,
|
|
475 |
int orientation, int direction) {
|
|
476 |
if (getUI() != null) {
|
|
477 |
return getUI().getScrollableBlockIncrement(this, visibleRect,
|
|
478 |
orientation, direction);
|
|
479 |
}
|
|
480 |
return (orientation == SwingConstants.VERTICAL) ? visibleRect.height :
|
|
481 |
visibleRect.width;
|
|
482 |
}
|
|
483 |
|
|
484 |
/**
|
|
485 |
* Returns {@code false} to indicate that the height of the viewport does not
|
|
486 |
* determine the height of the layer, unless the preferred height
|
|
487 |
* of the layer is smaller than the height of the viewport.
|
|
488 |
* <p/>
|
|
489 |
* If the ui delegate of this layer is not null, this method delegates its
|
|
490 |
* implementation to the {@code LayerUI.getScrollableTracksViewportHeight(JLayer)}
|
|
491 |
*
|
|
492 |
* @return whether the layer should track the height of the viewport
|
|
493 |
*
|
|
494 |
* @see Scrollable
|
|
495 |
* @see LayerUI#getScrollableTracksViewportHeight(JLayer)
|
|
496 |
*/
|
|
497 |
public boolean getScrollableTracksViewportHeight() {
|
|
498 |
if (getUI() != null) {
|
|
499 |
return getUI().getScrollableTracksViewportHeight(this);
|
|
500 |
}
|
|
501 |
return false;
|
|
502 |
}
|
|
503 |
|
|
504 |
/**
|
|
505 |
* Returns {@code false} to indicate that the width of the viewport does not
|
|
506 |
* determine the width of the layer, unless the preferred width
|
|
507 |
* of the layer is smaller than the width of the viewport.
|
|
508 |
* <p/>
|
|
509 |
* If the ui delegate of this layer is not null, this method delegates its
|
|
510 |
* implementation to the {@code LayerUI.getScrollableTracksViewportWidth(JLayer)}
|
|
511 |
*
|
|
512 |
* @return whether the layer should track the width of the viewport
|
|
513 |
*
|
|
514 |
* @see Scrollable
|
|
515 |
* @see LayerUI#getScrollableTracksViewportWidth(JLayer)
|
|
516 |
*/
|
|
517 |
public boolean getScrollableTracksViewportWidth() {
|
|
518 |
if (getUI() != null) {
|
|
519 |
return getUI().getScrollableTracksViewportWidth(this);
|
|
520 |
}
|
|
521 |
return false;
|
|
522 |
}
|
|
523 |
|
|
524 |
/**
|
|
525 |
* Returns a scroll increment, which is required for components
|
|
526 |
* that display logical rows or columns in order to completely expose
|
|
527 |
* one new row or column, depending on the value of orientation.
|
|
528 |
* Ideally, components should handle a partially exposed row or column
|
|
529 |
* by returning the distance required to completely expose the item.
|
|
530 |
* <p/>
|
|
531 |
* Scrolling containers, like {@code JScrollPane}, will use this method
|
|
532 |
* each time the user requests a unit scroll.
|
|
533 |
* <p/>
|
|
534 |
* If the ui delegate of this layer is not {@code null}, this method delegates its
|
|
535 |
* implementation to the {@code LayerUI.getScrollableUnitIncrement(JLayer,Rectangle,int,int)}
|
|
536 |
*
|
|
537 |
* @return The "unit" increment for scrolling in the specified direction.
|
|
538 |
* This value should always be positive.
|
|
539 |
*
|
|
540 |
* @see Scrollable
|
|
541 |
* @see LayerUI#getScrollableUnitIncrement(JLayer, Rectangle, int, int)
|
|
542 |
*/
|
|
543 |
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
|
|
544 |
int direction) {
|
|
545 |
if (getUI() != null) {
|
|
546 |
return getUI().getScrollableUnitIncrement(
|
|
547 |
this, visibleRect, orientation, direction);
|
|
548 |
}
|
|
549 |
return 1;
|
|
550 |
}
|
|
551 |
|
|
552 |
private void readObject(ObjectInputStream s)
|
|
553 |
throws IOException, ClassNotFoundException {
|
|
554 |
s.defaultReadObject();
|
3743
|
555 |
if (layerUI != null) {
|
|
556 |
setUI(layerUI);
|
|
557 |
}
|
|
558 |
if (eventMask != 0) {
|
|
559 |
eventController.updateAWTEventListener(0, eventMask);
|
3508
|
560 |
}
|
3743
|
561 |
}
|
|
562 |
|
|
563 |
public void addNotify() {
|
|
564 |
eventController.updateAWTEventListener(0, eventMask);
|
|
565 |
super.addNotify();
|
|
566 |
}
|
|
567 |
|
|
568 |
public void removeNotify() {
|
|
569 |
eventController.updateAWTEventListener(eventMask, 0);
|
|
570 |
super.removeNotify();
|
3508
|
571 |
}
|
|
572 |
|
|
573 |
/**
|
|
574 |
* static AWTEventListener to be shared with all AbstractLayerUIs
|
|
575 |
*/
|
|
576 |
private static class LayerEventController implements AWTEventListener {
|
3743
|
577 |
private ArrayList<Long> layerMaskList =
|
|
578 |
new ArrayList<Long>();
|
3508
|
579 |
|
|
580 |
private long currentEventMask;
|
|
581 |
|
|
582 |
@SuppressWarnings("unchecked")
|
|
583 |
public void eventDispatched(AWTEvent event) {
|
|
584 |
Object source = event.getSource();
|
|
585 |
if (source instanceof Component) {
|
|
586 |
Component component = (Component) source;
|
|
587 |
while (component != null) {
|
|
588 |
if (component instanceof JLayer) {
|
|
589 |
JLayer l = (JLayer) component;
|
|
590 |
LayerUI ui = l.getUI();
|
|
591 |
if (ui != null &&
|
|
592 |
isEventEnabled(l.getLayerEventMask(),
|
|
593 |
event.getID())) {
|
|
594 |
ui.eventDispatched(event, l);
|
|
595 |
}
|
|
596 |
}
|
|
597 |
component = component.getParent();
|
|
598 |
}
|
|
599 |
}
|
|
600 |
}
|
|
601 |
|
3743
|
602 |
private void updateAWTEventListener(long oldEventMask, long newEventMask) {
|
|
603 |
if (oldEventMask != 0) {
|
|
604 |
layerMaskList.remove(oldEventMask);
|
3508
|
605 |
}
|
3743
|
606 |
if (newEventMask != 0) {
|
|
607 |
layerMaskList.add(newEventMask);
|
3508
|
608 |
}
|
|
609 |
long combinedMask = 0;
|
3743
|
610 |
for (Long mask : layerMaskList) {
|
|
611 |
combinedMask |= mask;
|
3508
|
612 |
}
|
|
613 |
if (combinedMask == 0) {
|
|
614 |
removeAWTEventListener();
|
|
615 |
} else if (getCurrentEventMask() != combinedMask) {
|
|
616 |
removeAWTEventListener();
|
|
617 |
addAWTEventListener(combinedMask);
|
|
618 |
}
|
3743
|
619 |
currentEventMask = combinedMask;
|
3508
|
620 |
}
|
|
621 |
|
|
622 |
private long getCurrentEventMask() {
|
|
623 |
return currentEventMask;
|
|
624 |
}
|
|
625 |
|
|
626 |
private void addAWTEventListener(final long eventMask) {
|
|
627 |
AccessController.doPrivileged(new PrivilegedAction<Void>() {
|
|
628 |
public Void run() {
|
|
629 |
Toolkit.getDefaultToolkit().
|
|
630 |
addAWTEventListener(LayerEventController.this, eventMask);
|
|
631 |
return null;
|
|
632 |
}
|
|
633 |
});
|
3743
|
634 |
|
3508
|
635 |
}
|
|
636 |
|
|
637 |
private void removeAWTEventListener() {
|
|
638 |
AccessController.doPrivileged(new PrivilegedAction<Void>() {
|
|
639 |
public Void run() {
|
|
640 |
Toolkit.getDefaultToolkit().
|
|
641 |
removeAWTEventListener(LayerEventController.this);
|
|
642 |
return null;
|
|
643 |
}
|
|
644 |
});
|
|
645 |
}
|
|
646 |
|
|
647 |
private boolean isEventEnabled(long eventMask, int id) {
|
|
648 |
return (((eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 &&
|
|
649 |
id >= ComponentEvent.COMPONENT_FIRST &&
|
|
650 |
id <= ComponentEvent.COMPONENT_LAST)
|
|
651 |
|| ((eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 &&
|
|
652 |
id >= ContainerEvent.CONTAINER_FIRST &&
|
|
653 |
id <= ContainerEvent.CONTAINER_LAST)
|
|
654 |
|| ((eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0 &&
|
|
655 |
id >= FocusEvent.FOCUS_FIRST &&
|
|
656 |
id <= FocusEvent.FOCUS_LAST)
|
|
657 |
|| ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 &&
|
|
658 |
id >= KeyEvent.KEY_FIRST &&
|
|
659 |
id <= KeyEvent.KEY_LAST)
|
|
660 |
|| ((eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0 &&
|
|
661 |
id == MouseEvent.MOUSE_WHEEL)
|
|
662 |
|| ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 &&
|
|
663 |
(id == MouseEvent.MOUSE_MOVED ||
|
|
664 |
id == MouseEvent.MOUSE_DRAGGED))
|
|
665 |
|| ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0 &&
|
|
666 |
id != MouseEvent.MOUSE_MOVED &&
|
|
667 |
id != MouseEvent.MOUSE_DRAGGED &&
|
|
668 |
id != MouseEvent.MOUSE_WHEEL &&
|
|
669 |
id >= MouseEvent.MOUSE_FIRST &&
|
|
670 |
id <= MouseEvent.MOUSE_LAST)
|
|
671 |
|| ((eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0 &&
|
|
672 |
id >= InputMethodEvent.INPUT_METHOD_FIRST &&
|
|
673 |
id <= InputMethodEvent.INPUT_METHOD_LAST)
|
|
674 |
|| ((eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
|
|
675 |
id == HierarchyEvent.HIERARCHY_CHANGED)
|
|
676 |
|| ((eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 &&
|
|
677 |
(id == HierarchyEvent.ANCESTOR_MOVED ||
|
|
678 |
id == HierarchyEvent.ANCESTOR_RESIZED)));
|
|
679 |
}
|
|
680 |
}
|
|
681 |
|
|
682 |
/**
|
|
683 |
* The default glassPane for the {@link javax.swing.JLayer}.
|
|
684 |
* It is a subclass of {@code JPanel} which is non opaque by default.
|
|
685 |
*/
|
|
686 |
private static class DefaultLayerGlassPane extends JPanel {
|
|
687 |
/**
|
|
688 |
* Creates a new {@link DefaultLayerGlassPane}
|
|
689 |
*/
|
|
690 |
public DefaultLayerGlassPane() {
|
|
691 |
setOpaque(false);
|
|
692 |
}
|
|
693 |
|
|
694 |
/**
|
|
695 |
* First, implementatation of this method iterates through
|
|
696 |
* glassPane's child components and returns {@code true}
|
|
697 |
* if any of them is visible and contains passed x,y point.
|
|
698 |
* After that it checks if no mouseListeners is attached to this component
|
|
699 |
* and no mouse cursor is set, then it returns {@code false},
|
|
700 |
* otherwise calls the super implementation of this method.
|
|
701 |
*
|
|
702 |
* @param x the <i>x</i> coordinate of the point
|
|
703 |
* @param y the <i>y</i> coordinate of the point
|
|
704 |
* @return true if this component logically contains x,y
|
|
705 |
*/
|
|
706 |
public boolean contains(int x, int y) {
|
|
707 |
for (int i = 0; i < getComponentCount(); i++) {
|
|
708 |
Component c = getComponent(i);
|
|
709 |
Point point = SwingUtilities.convertPoint(this, new Point(x, y), c);
|
|
710 |
if(c.isVisible() && c.contains(point)){
|
|
711 |
return true;
|
|
712 |
}
|
|
713 |
}
|
|
714 |
if (getMouseListeners().length == 0
|
|
715 |
&& getMouseMotionListeners().length == 0
|
|
716 |
&& getMouseWheelListeners().length == 0
|
|
717 |
&& !isCursorSet()) {
|
|
718 |
return false;
|
|
719 |
}
|
|
720 |
return super.contains(x, y);
|
|
721 |
}
|
|
722 |
}
|
|
723 |
|
|
724 |
/**
|
|
725 |
* The default layout manager for the {@link javax.swing.JLayer}.<br/>
|
|
726 |
* It places the glassPane on top of the view component
|
|
727 |
* and makes it the same size as {@code JLayer},
|
|
728 |
* it also makes the view component the same size but minus layer's insets<br/>
|
|
729 |
*/
|
|
730 |
private static class DefaultLayerLayout implements LayoutManager, Serializable {
|
|
731 |
/**
|
|
732 |
* {@inheritDoc}
|
|
733 |
*/
|
|
734 |
public void layoutContainer(Container parent) {
|
|
735 |
JLayer layer = (JLayer) parent;
|
|
736 |
Component view = layer.getView();
|
|
737 |
Component glassPane = layer.getGlassPane();
|
|
738 |
if (view != null) {
|
|
739 |
Insets insets = layer.getInsets();
|
|
740 |
view.setLocation(insets.left, insets.top);
|
|
741 |
view.setSize(layer.getWidth() - insets.left - insets.right,
|
|
742 |
layer.getHeight() - insets.top - insets.bottom);
|
|
743 |
}
|
|
744 |
if (glassPane != null) {
|
|
745 |
glassPane.setLocation(0, 0);
|
|
746 |
glassPane.setSize(layer.getWidth(), layer.getHeight());
|
|
747 |
}
|
|
748 |
}
|
|
749 |
|
|
750 |
/**
|
|
751 |
* {@inheritDoc}
|
|
752 |
*/
|
|
753 |
public Dimension minimumLayoutSize(Container parent) {
|
|
754 |
JLayer layer = (JLayer) parent;
|
|
755 |
Insets insets = layer.getInsets();
|
|
756 |
Dimension ret = new Dimension(insets.left + insets.right,
|
|
757 |
insets.top + insets.bottom);
|
|
758 |
Component view = layer.getView();
|
|
759 |
if (view != null) {
|
|
760 |
Dimension size = view.getMinimumSize();
|
|
761 |
ret.width += size.width;
|
|
762 |
ret.height += size.height;
|
|
763 |
}
|
|
764 |
if (ret.width == 0 || ret.height == 0) {
|
|
765 |
ret.width = ret.height = 4;
|
|
766 |
}
|
|
767 |
return ret;
|
|
768 |
}
|
|
769 |
|
|
770 |
/**
|
|
771 |
* {@inheritDoc}
|
|
772 |
*/
|
|
773 |
public Dimension preferredLayoutSize(Container parent) {
|
|
774 |
JLayer layer = (JLayer) parent;
|
|
775 |
Insets insets = layer.getInsets();
|
|
776 |
Dimension ret = new Dimension(insets.left + insets.right,
|
|
777 |
insets.top + insets.bottom);
|
|
778 |
Component view = layer.getView();
|
|
779 |
if (view != null) {
|
|
780 |
Dimension size = view.getPreferredSize();
|
|
781 |
if (size.width > 0 && size.height > 0) {
|
|
782 |
ret.width += size.width;
|
|
783 |
ret.height += size.height;
|
|
784 |
}
|
|
785 |
}
|
|
786 |
return ret;
|
|
787 |
}
|
|
788 |
|
|
789 |
/**
|
|
790 |
* {@inheritDoc}
|
|
791 |
*/
|
|
792 |
public void addLayoutComponent(String name, Component comp) {
|
|
793 |
}
|
|
794 |
|
|
795 |
/**
|
|
796 |
* {@inheritDoc}
|
|
797 |
*/
|
|
798 |
public void removeLayoutComponent(Component comp) {
|
|
799 |
}
|
|
800 |
}
|
3733
|
801 |
}
|