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