Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/javax/swing/JLayeredPane.java
+++ new/src/share/classes/javax/swing/JLayeredPane.java
1 1 /*
2 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25 package javax.swing;
26 26
27 27 import java.awt.Component;
28 28 import java.util.ArrayList;
29 29 import java.util.Hashtable;
30 30 import java.awt.Color;
31 31 import java.awt.Graphics;
32 32 import java.awt.Rectangle;
33 33 import sun.awt.SunToolkit;
↓ open down ↓ |
33 lines elided |
↑ open up ↑ |
34 34
35 35 import javax.accessibility.*;
36 36
37 37 /**
38 38 * <code>JLayeredPane</code> adds depth to a JFC/Swing container,
39 39 * allowing components to overlap each other when needed.
40 40 * An <code>Integer</code> object specifies each component's depth in the
41 41 * container, where higher-numbered components sit "on top" of other
42 42 * components.
43 43 * For task-oriented documentation and examples of using layered panes see
44 - * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html">How to Use a Layered Pane</a>,
44 + * <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html">How to Use a Layered Pane</a>,
45 45 * a section in <em>The Java Tutorial</em>.
46 46 *
47 47 * <TABLE STYLE="FLOAT:RIGHT" BORDER="0" SUMMARY="layout">
48 48 * <TR>
49 49 * <TD ALIGN="CENTER">
50 50 * <P STYLE="TEXT-ALIGN:CENTER"><IMG SRC="doc-files/JLayeredPane-1.gif"
51 51 * alt="The following text describes this image."
52 52 * WIDTH="269" HEIGHT="264" STYLE="FLOAT:BOTTOM; BORDER=0">
53 53 * </TD>
54 54 * </TR>
55 55 * </TABLE>
56 56 * For convenience, <code>JLayeredPane</code> divides the depth-range
57 57 * into several different layers. Putting a component into one of those
58 58 * layers makes it easy to ensure that components overlap properly,
59 59 * without having to worry about specifying numbers for specific depths:
60 60 * <DL>
61 61 * <DT><FONT SIZE="2">DEFAULT_LAYER</FONT></DT>
62 62 * <DD>The standard layer, where most components go. This the bottommost
63 63 * layer.
64 64 * <DT><FONT SIZE="2">PALETTE_LAYER</FONT></DT>
65 65 * <DD>The palette layer sits over the default layer. Useful for floating
66 66 * toolbars and palettes, so they can be positioned above other components.
67 67 * <DT><FONT SIZE="2">MODAL_LAYER</FONT></DT>
68 68 * <DD>The layer used for modal dialogs. They will appear on top of any
69 69 * toolbars, palettes, or standard components in the container.
70 70 * <DT><FONT SIZE="2">POPUP_LAYER</FONT></DT>
71 71 * <DD>The popup layer displays above dialogs. That way, the popup windows
72 72 * associated with combo boxes, tooltips, and other help text will appear
73 73 * above the component, palette, or dialog that generated them.
74 74 * <DT><FONT SIZE="2">DRAG_LAYER</FONT></DT>
75 75 * <DD>When dragging a component, reassigning it to the drag layer ensures
76 76 * that it is positioned over every other component in the container. When
77 77 * finished dragging, it can be reassigned to its normal layer.
78 78 * </DL>
79 79 * The <code>JLayeredPane</code> methods <code>moveToFront(Component)</code>,
80 80 * <code>moveToBack(Component)</code> and <code>setPosition</code> can be used
81 81 * to reposition a component within its layer. The <code>setLayer</code> method
82 82 * can also be used to change the component's current layer.
83 83 *
84 84 * <h2>Details</h2>
85 85 * <code>JLayeredPane</code> manages its list of children like
86 86 * <code>Container</code>, but allows for the definition of a several
87 87 * layers within itself. Children in the same layer are managed exactly
88 88 * like the normal <code>Container</code> object,
89 89 * with the added feature that when children components overlap, children
90 90 * in higher layers display above the children in lower layers.
91 91 * <p>
92 92 * Each layer is a distinct integer number. The layer attribute can be set
93 93 * on a <code>Component</code> by passing an <code>Integer</code>
94 94 * object during the add call.<br> For example:
95 95 * <PRE>
96 96 * layeredPane.add(child, JLayeredPane.DEFAULT_LAYER);
97 97 * or
98 98 * layeredPane.add(child, new Integer(10));
99 99 * </PRE>
100 100 * The layer attribute can also be set on a Component by calling<PRE>
101 101 * layeredPaneParent.setLayer(child, 10)</PRE>
102 102 * on the <code>JLayeredPane</code> that is the parent of component. The layer
103 103 * should be set <i>before</i> adding the child to the parent.
104 104 * <p>
105 105 * Higher number layers display above lower number layers. So, using
106 106 * numbers for the layers and letters for individual components, a
107 107 * representative list order would look like this:<PRE>
108 108 * 5a, 5b, 5c, 2a, 2b, 2c, 1a </PRE>
109 109 * where the leftmost components are closest to the top of the display.
110 110 * <p>
111 111 * A component can be moved to the top or bottom position within its
112 112 * layer by calling <code>moveToFront</code> or <code>moveToBack</code>.
113 113 * <p>
114 114 * The position of a component within a layer can also be specified directly.
115 115 * Valid positions range from 0 up to one less than the number of
116 116 * components in that layer. A value of -1 indicates the bottommost
117 117 * position. A value of 0 indicates the topmost position. Unlike layer
118 118 * numbers, higher position values are <i>lower</i> in the display.
119 119 * <blockquote>
120 120 * <b>Note:</b> This sequence (defined by java.awt.Container) is the reverse
121 121 * of the layer numbering sequence. Usually though, you will use <code>moveToFront</code>,
122 122 * <code>moveToBack</code>, and <code>setLayer</code>.
123 123 * </blockquote>
124 124 * Here are some examples using the method add(Component, layer, position):
125 125 * Calling add(5x, 5, -1) results in:<PRE>
126 126 * 5a, 5b, 5c, 5x, 2a, 2b, 2c, 1a </PRE>
127 127 *
128 128 * Calling add(5z, 5, 2) results in:<PRE>
129 129 * 5a, 5b, 5z, 5c, 5x, 2a, 2b, 2c, 1a </PRE>
130 130 *
131 131 * Calling add(3a, 3, 7) results in:<PRE>
132 132 * 5a, 5b, 5z, 5c, 5x, 3a, 2a, 2b, 2c, 1a </PRE>
133 133 *
134 134 * Using normal paint/event mechanics results in 1a appearing at the bottom
135 135 * and 5a being above all other components.
136 136 * <p>
137 137 * <b>Note:</b> that these layers are simply a logical construct and LayoutManagers
138 138 * will affect all child components of this container without regard for
139 139 * layer settings.
140 140 * <p>
141 141 * <strong>Warning:</strong> Swing is not thread safe. For more
142 142 * information see <a
143 143 * href="package-summary.html#threading">Swing's Threading
144 144 * Policy</a>.
145 145 * <p>
146 146 * <strong>Warning:</strong>
147 147 * Serialized objects of this class will not be compatible with
148 148 * future Swing releases. The current serialization support is
149 149 * appropriate for short term storage or RMI between applications running
150 150 * the same version of Swing. As of 1.4, support for long term storage
151 151 * of all JavaBeans™
152 152 * has been added to the <code>java.beans</code> package.
153 153 * Please see {@link java.beans.XMLEncoder}.
154 154 *
155 155 * @author David Kloba
156 156 */
157 157 @SuppressWarnings("serial")
158 158 public class JLayeredPane extends JComponent implements Accessible {
159 159 /// Watch the values in getObjectForLayer()
160 160 /** Convenience object defining the Default layer. Equivalent to new Integer(0).*/
161 161 public final static Integer DEFAULT_LAYER = new Integer(0);
162 162 /** Convenience object defining the Palette layer. Equivalent to new Integer(100).*/
163 163 public final static Integer PALETTE_LAYER = new Integer(100);
164 164 /** Convenience object defining the Modal layer. Equivalent to new Integer(200).*/
165 165 public final static Integer MODAL_LAYER = new Integer(200);
166 166 /** Convenience object defining the Popup layer. Equivalent to new Integer(300).*/
167 167 public final static Integer POPUP_LAYER = new Integer(300);
168 168 /** Convenience object defining the Drag layer. Equivalent to new Integer(400).*/
169 169 public final static Integer DRAG_LAYER = new Integer(400);
170 170 /** Convenience object defining the Frame Content layer.
171 171 * This layer is normally only use to position the contentPane and menuBar
172 172 * components of JFrame.
173 173 * Equivalent to new Integer(-30000).
174 174 * @see JFrame
175 175 */
176 176 public final static Integer FRAME_CONTENT_LAYER = new Integer(-30000);
177 177
178 178 /** Bound property */
179 179 public final static String LAYER_PROPERTY = "layeredContainerLayer";
180 180 // Hashtable to store layer values for non-JComponent components
181 181 private Hashtable<Component,Integer> componentToLayer;
182 182 private boolean optimizedDrawingPossible = true;
183 183
184 184
185 185 //////////////////////////////////////////////////////////////////////////////
186 186 //// Container Override methods
187 187 //////////////////////////////////////////////////////////////////////////////
188 188 /** Create a new JLayeredPane */
189 189 public JLayeredPane() {
190 190 setLayout(null);
191 191 }
192 192
193 193 private void validateOptimizedDrawing() {
194 194 boolean layeredComponentFound = false;
195 195 synchronized(getTreeLock()) {
196 196 Integer layer;
197 197
198 198 for (Component c : getComponents()) {
199 199 layer = null;
200 200
201 201 if(SunToolkit.isInstanceOf(c, "javax.swing.JInternalFrame") ||
202 202 (c instanceof JComponent &&
203 203 (layer = (Integer)((JComponent)c).
204 204 getClientProperty(LAYER_PROPERTY)) != null))
205 205 {
206 206 if(layer != null && layer.equals(FRAME_CONTENT_LAYER))
207 207 continue;
208 208 layeredComponentFound = true;
209 209 break;
210 210 }
211 211 }
212 212 }
213 213
214 214 if(layeredComponentFound)
215 215 optimizedDrawingPossible = false;
216 216 else
217 217 optimizedDrawingPossible = true;
218 218 }
219 219
220 220 protected void addImpl(Component comp, Object constraints, int index) {
221 221 int layer;
222 222 int pos;
223 223
224 224 if(constraints instanceof Integer) {
225 225 layer = ((Integer)constraints).intValue();
226 226 setLayer(comp, layer);
227 227 } else
228 228 layer = getLayer(comp);
229 229
230 230 pos = insertIndexForLayer(layer, index);
231 231 super.addImpl(comp, constraints, pos);
232 232 comp.validate();
233 233 comp.repaint();
234 234 validateOptimizedDrawing();
235 235 }
236 236
237 237 /**
238 238 * Remove the indexed component from this pane.
239 239 * This is the absolute index, ignoring layers.
240 240 *
241 241 * @param index an int specifying the component to remove
242 242 * @see #getIndexOf
243 243 */
244 244 public void remove(int index) {
245 245 Component c = getComponent(index);
246 246 super.remove(index);
247 247 if (c != null && !(c instanceof JComponent)) {
248 248 getComponentToLayer().remove(c);
249 249 }
250 250 validateOptimizedDrawing();
251 251 }
252 252
253 253 /**
254 254 * Removes all the components from this container.
255 255 *
256 256 * @since 1.5
257 257 */
258 258 public void removeAll() {
259 259 Component[] children = getComponents();
260 260 Hashtable<Component, Integer> cToL = getComponentToLayer();
261 261 for (int counter = children.length - 1; counter >= 0; counter--) {
262 262 Component c = children[counter];
263 263 if (c != null && !(c instanceof JComponent)) {
264 264 cToL.remove(c);
265 265 }
266 266 }
267 267 super.removeAll();
268 268 }
269 269
270 270 /**
271 271 * Returns false if components in the pane can overlap, which makes
272 272 * optimized drawing impossible. Otherwise, returns true.
273 273 *
274 274 * @return false if components can overlap, else true
275 275 * @see JComponent#isOptimizedDrawingEnabled
276 276 */
277 277 public boolean isOptimizedDrawingEnabled() {
278 278 return optimizedDrawingPossible;
279 279 }
280 280
281 281
282 282 //////////////////////////////////////////////////////////////////////////////
283 283 //// New methods for managing layers
284 284 //////////////////////////////////////////////////////////////////////////////
285 285 /** Sets the layer property on a JComponent. This method does not cause
286 286 * any side effects like setLayer() (painting, add/remove, etc).
287 287 * Normally you should use the instance method setLayer(), in order to
288 288 * get the desired side-effects (like repainting).
289 289 *
290 290 * @param c the JComponent to move
291 291 * @param layer an int specifying the layer to move it to
292 292 * @see #setLayer
293 293 */
294 294 public static void putLayer(JComponent c, int layer) {
295 295 /// MAKE SURE THIS AND setLayer(Component c, int layer, int position) are SYNCED
296 296 Integer layerObj;
297 297
298 298 layerObj = new Integer(layer);
299 299 c.putClientProperty(LAYER_PROPERTY, layerObj);
300 300 }
301 301
302 302 /** Gets the layer property for a JComponent, it
303 303 * does not cause any side effects like setLayer(). (painting, add/remove, etc)
304 304 * Normally you should use the instance method getLayer().
305 305 *
306 306 * @param c the JComponent to check
307 307 * @return an int specifying the component's layer
308 308 */
309 309 public static int getLayer(JComponent c) {
310 310 Integer i;
311 311 if((i = (Integer)c.getClientProperty(LAYER_PROPERTY)) != null)
312 312 return i.intValue();
313 313 return DEFAULT_LAYER.intValue();
314 314 }
315 315
316 316 /** Convenience method that returns the first JLayeredPane which
317 317 * contains the specified component. Note that all JFrames have a
318 318 * JLayeredPane at their root, so any component in a JFrame will
319 319 * have a JLayeredPane parent.
320 320 *
321 321 * @param c the Component to check
322 322 * @return the JLayeredPane that contains the component, or
323 323 * null if no JLayeredPane is found in the component
324 324 * hierarchy
325 325 * @see JFrame
326 326 * @see JRootPane
327 327 */
328 328 public static JLayeredPane getLayeredPaneAbove(Component c) {
329 329 if(c == null) return null;
330 330
331 331 Component parent = c.getParent();
332 332 while(parent != null && !(parent instanceof JLayeredPane))
333 333 parent = parent.getParent();
334 334 return (JLayeredPane)parent;
335 335 }
336 336
337 337 /** Sets the layer attribute on the specified component,
338 338 * making it the bottommost component in that layer.
339 339 * Should be called before adding to parent.
340 340 *
341 341 * @param c the Component to set the layer for
342 342 * @param layer an int specifying the layer to set, where
343 343 * lower numbers are closer to the bottom
344 344 */
345 345 public void setLayer(Component c, int layer) {
346 346 setLayer(c, layer, -1);
347 347 }
348 348
349 349 /** Sets the layer attribute for the specified component and
350 350 * also sets its position within that layer.
351 351 *
352 352 * @param c the Component to set the layer for
353 353 * @param layer an int specifying the layer to set, where
354 354 * lower numbers are closer to the bottom
355 355 * @param position an int specifying the position within the
356 356 * layer, where 0 is the topmost position and -1
357 357 * is the bottommost position
358 358 */
359 359 public void setLayer(Component c, int layer, int position) {
360 360 Integer layerObj;
361 361 layerObj = getObjectForLayer(layer);
362 362
363 363 if(layer == getLayer(c) && position == getPosition(c)) {
364 364 repaint(c.getBounds());
365 365 return;
366 366 }
367 367
368 368 /// MAKE SURE THIS AND putLayer(JComponent c, int layer) are SYNCED
369 369 if(c instanceof JComponent)
370 370 ((JComponent)c).putClientProperty(LAYER_PROPERTY, layerObj);
371 371 else
372 372 getComponentToLayer().put(c, layerObj);
373 373
374 374 if(c.getParent() == null || c.getParent() != this) {
375 375 repaint(c.getBounds());
376 376 return;
377 377 }
378 378
379 379 int index = insertIndexForLayer(c, layer, position);
380 380
381 381 setComponentZOrder(c, index);
382 382 repaint(c.getBounds());
383 383 }
384 384
385 385 /**
386 386 * Returns the layer attribute for the specified Component.
387 387 *
388 388 * @param c the Component to check
389 389 * @return an int specifying the component's current layer
390 390 */
391 391 public int getLayer(Component c) {
392 392 Integer i;
393 393 if(c instanceof JComponent)
394 394 i = (Integer)((JComponent)c).getClientProperty(LAYER_PROPERTY);
395 395 else
396 396 i = getComponentToLayer().get(c);
397 397
398 398 if(i == null)
399 399 return DEFAULT_LAYER.intValue();
400 400 return i.intValue();
401 401 }
402 402
403 403 /**
404 404 * Returns the index of the specified Component.
405 405 * This is the absolute index, ignoring layers.
406 406 * Index numbers, like position numbers, have the topmost component
407 407 * at index zero. Larger numbers are closer to the bottom.
408 408 *
409 409 * @param c the Component to check
410 410 * @return an int specifying the component's index
411 411 */
412 412 public int getIndexOf(Component c) {
413 413 int i, count;
414 414
415 415 count = getComponentCount();
416 416 for(i = 0; i < count; i++) {
417 417 if(c == getComponent(i))
418 418 return i;
419 419 }
420 420 return -1;
421 421 }
422 422 /**
423 423 * Moves the component to the top of the components in its current layer
424 424 * (position 0).
425 425 *
426 426 * @param c the Component to move
427 427 * @see #setPosition(Component, int)
428 428 */
429 429 public void moveToFront(Component c) {
430 430 setPosition(c, 0);
431 431 }
432 432
433 433 /**
434 434 * Moves the component to the bottom of the components in its current layer
435 435 * (position -1).
436 436 *
437 437 * @param c the Component to move
438 438 * @see #setPosition(Component, int)
439 439 */
440 440 public void moveToBack(Component c) {
441 441 setPosition(c, -1);
442 442 }
443 443
444 444 /**
445 445 * Moves the component to <code>position</code> within its current layer,
446 446 * where 0 is the topmost position within the layer and -1 is the bottommost
447 447 * position.
448 448 * <p>
449 449 * <b>Note:</b> Position numbering is defined by java.awt.Container, and
450 450 * is the opposite of layer numbering. Lower position numbers are closer
451 451 * to the top (0 is topmost), and higher position numbers are closer to
452 452 * the bottom.
453 453 *
454 454 * @param c the Component to move
455 455 * @param position an int in the range -1..N-1, where N is the number of
456 456 * components in the component's current layer
457 457 */
458 458 public void setPosition(Component c, int position) {
459 459 setLayer(c, getLayer(c), position);
460 460 }
461 461
462 462 /**
463 463 * Get the relative position of the component within its layer.
464 464 *
465 465 * @param c the Component to check
466 466 * @return an int giving the component's position, where 0 is the
467 467 * topmost position and the highest index value = the count
468 468 * count of components at that layer, minus 1
469 469 *
470 470 * @see #getComponentCountInLayer
471 471 */
472 472 public int getPosition(Component c) {
473 473 int i, startLayer, curLayer, startLocation, pos = 0;
474 474
475 475 getComponentCount();
476 476 startLocation = getIndexOf(c);
477 477
478 478 if(startLocation == -1)
479 479 return -1;
480 480
481 481 startLayer = getLayer(c);
482 482 for(i = startLocation - 1; i >= 0; i--) {
483 483 curLayer = getLayer(getComponent(i));
484 484 if(curLayer == startLayer)
485 485 pos++;
486 486 else
487 487 return pos;
488 488 }
489 489 return pos;
490 490 }
491 491
492 492 /** Returns the highest layer value from all current children.
493 493 * Returns 0 if there are no children.
494 494 *
495 495 * @return an int indicating the layer of the topmost component in the
496 496 * pane, or zero if there are no children
497 497 */
498 498 public int highestLayer() {
499 499 if(getComponentCount() > 0)
500 500 return getLayer(getComponent(0));
501 501 return 0;
502 502 }
503 503
504 504 /** Returns the lowest layer value from all current children.
505 505 * Returns 0 if there are no children.
506 506 *
507 507 * @return an int indicating the layer of the bottommost component in the
508 508 * pane, or zero if there are no children
509 509 */
510 510 public int lowestLayer() {
511 511 int count = getComponentCount();
512 512 if(count > 0)
513 513 return getLayer(getComponent(count-1));
514 514 return 0;
515 515 }
516 516
517 517 /**
518 518 * Returns the number of children currently in the specified layer.
519 519 *
520 520 * @param layer an int specifying the layer to check
521 521 * @return an int specifying the number of components in that layer
522 522 */
523 523 public int getComponentCountInLayer(int layer) {
524 524 int i, count, curLayer;
525 525 int layerCount = 0;
526 526
527 527 count = getComponentCount();
528 528 for(i = 0; i < count; i++) {
529 529 curLayer = getLayer(getComponent(i));
530 530 if(curLayer == layer) {
531 531 layerCount++;
532 532 /// Short circut the counting when we have them all
533 533 } else if(layerCount > 0 || curLayer < layer) {
534 534 break;
535 535 }
536 536 }
537 537
538 538 return layerCount;
539 539 }
540 540
541 541 /**
542 542 * Returns an array of the components in the specified layer.
543 543 *
544 544 * @param layer an int specifying the layer to check
545 545 * @return an array of Components contained in that layer
546 546 */
547 547 public Component[] getComponentsInLayer(int layer) {
548 548 int i, count, curLayer;
549 549 int layerCount = 0;
550 550 Component[] results;
551 551
552 552 results = new Component[getComponentCountInLayer(layer)];
553 553 count = getComponentCount();
554 554 for(i = 0; i < count; i++) {
555 555 curLayer = getLayer(getComponent(i));
556 556 if(curLayer == layer) {
557 557 results[layerCount++] = getComponent(i);
558 558 /// Short circut the counting when we have them all
559 559 } else if(layerCount > 0 || curLayer < layer) {
560 560 break;
561 561 }
562 562 }
563 563
564 564 return results;
565 565 }
566 566
567 567 /**
568 568 * Paints this JLayeredPane within the specified graphics context.
569 569 *
570 570 * @param g the Graphics context within which to paint
571 571 */
572 572 public void paint(Graphics g) {
573 573 if(isOpaque()) {
574 574 Rectangle r = g.getClipBounds();
575 575 Color c = getBackground();
576 576 if(c == null)
577 577 c = Color.lightGray;
578 578 g.setColor(c);
579 579 if (r != null) {
580 580 g.fillRect(r.x, r.y, r.width, r.height);
581 581 }
582 582 else {
583 583 g.fillRect(0, 0, getWidth(), getHeight());
584 584 }
585 585 }
586 586 super.paint(g);
587 587 }
588 588
589 589 //////////////////////////////////////////////////////////////////////////////
590 590 //// Implementation Details
591 591 //////////////////////////////////////////////////////////////////////////////
592 592
593 593 /**
594 594 * Returns the hashtable that maps components to layers.
595 595 *
596 596 * @return the Hashtable used to map components to their layers
597 597 */
598 598 protected Hashtable<Component,Integer> getComponentToLayer() {
599 599 if(componentToLayer == null)
600 600 componentToLayer = new Hashtable<Component,Integer>(4);
601 601 return componentToLayer;
602 602 }
603 603
604 604 /**
605 605 * Returns the Integer object associated with a specified layer.
606 606 *
607 607 * @param layer an int specifying the layer
608 608 * @return an Integer object for that layer
609 609 */
610 610 protected Integer getObjectForLayer(int layer) {
611 611 Integer layerObj;
612 612 switch(layer) {
613 613 case 0:
614 614 layerObj = DEFAULT_LAYER;
615 615 break;
616 616 case 100:
617 617 layerObj = PALETTE_LAYER;
618 618 break;
619 619 case 200:
620 620 layerObj = MODAL_LAYER;
621 621 break;
622 622 case 300:
623 623 layerObj = POPUP_LAYER;
624 624 break;
625 625 case 400:
626 626 layerObj = DRAG_LAYER;
627 627 break;
628 628 default:
629 629 layerObj = new Integer(layer);
630 630 }
631 631 return layerObj;
632 632 }
633 633
634 634 /**
635 635 * Primitive method that determines the proper location to
636 636 * insert a new child based on layer and position requests.
637 637 *
638 638 * @param layer an int specifying the layer
639 639 * @param position an int specifying the position within the layer
640 640 * @return an int giving the (absolute) insertion-index
641 641 *
642 642 * @see #getIndexOf
643 643 */
644 644 protected int insertIndexForLayer(int layer, int position) {
645 645 return insertIndexForLayer(null, layer, position);
646 646 }
647 647
648 648 /**
649 649 * This method is an extended version of insertIndexForLayer()
650 650 * to support setLayer which uses Container.setZOrder which does
651 651 * not remove the component from the containment hierarchy though
652 652 * we need to ignore it when calculating the insertion index.
653 653 *
654 654 * @param comp component to ignore when determining index
655 655 * @param layer an int specifying the layer
656 656 * @param position an int specifying the position within the layer
657 657 * @return an int giving the (absolute) insertion-index
658 658 *
659 659 * @see #getIndexOf
660 660 */
661 661 private int insertIndexForLayer(Component comp, int layer, int position) {
662 662 int i, count, curLayer;
663 663 int layerStart = -1;
664 664 int layerEnd = -1;
665 665 int componentCount = getComponentCount();
666 666
667 667 ArrayList<Component> compList =
668 668 new ArrayList<Component>(componentCount);
669 669 for (int index = 0; index < componentCount; index++) {
670 670 if (getComponent(index) != comp) {
671 671 compList.add(getComponent(index));
672 672 }
673 673 }
674 674
675 675 count = compList.size();
676 676 for (i = 0; i < count; i++) {
677 677 curLayer = getLayer(compList.get(i));
678 678 if (layerStart == -1 && curLayer == layer) {
679 679 layerStart = i;
680 680 }
681 681 if (curLayer < layer) {
682 682 if (i == 0) {
683 683 // layer is greater than any current layer
684 684 // [ ASSERT(layer > highestLayer()) ]
685 685 layerStart = 0;
686 686 layerEnd = 0;
687 687 } else {
688 688 layerEnd = i;
689 689 }
690 690 break;
691 691 }
692 692 }
693 693
694 694 // layer requested is lower than any current layer
695 695 // [ ASSERT(layer < lowestLayer()) ]
696 696 // put it on the bottom of the stack
697 697 if (layerStart == -1 && layerEnd == -1)
698 698 return count;
699 699
700 700 // In the case of a single layer entry handle the degenerative cases
701 701 if (layerStart != -1 && layerEnd == -1)
702 702 layerEnd = count;
703 703
704 704 if (layerEnd != -1 && layerStart == -1)
705 705 layerStart = layerEnd;
706 706
707 707 // If we are adding to the bottom, return the last element
708 708 if (position == -1)
709 709 return layerEnd;
710 710
711 711 // Otherwise make sure the requested position falls in the
712 712 // proper range
713 713 if (position > -1 && layerStart + position <= layerEnd)
714 714 return layerStart + position;
715 715
716 716 // Otherwise return the end of the layer
717 717 return layerEnd;
718 718 }
719 719
720 720 /**
721 721 * Returns a string representation of this JLayeredPane. This method
722 722 * is intended to be used only for debugging purposes, and the
723 723 * content and format of the returned string may vary between
724 724 * implementations. The returned string may be empty but may not
725 725 * be <code>null</code>.
726 726 *
727 727 * @return a string representation of this JLayeredPane.
728 728 */
729 729 protected String paramString() {
730 730 String optimizedDrawingPossibleString = (optimizedDrawingPossible ?
731 731 "true" : "false");
732 732
733 733 return super.paramString() +
734 734 ",optimizedDrawingPossible=" + optimizedDrawingPossibleString;
735 735 }
736 736
737 737 /////////////////
738 738 // Accessibility support
739 739 ////////////////
740 740
741 741 /**
742 742 * Gets the AccessibleContext associated with this JLayeredPane.
743 743 * For layered panes, the AccessibleContext takes the form of an
744 744 * AccessibleJLayeredPane.
745 745 * A new AccessibleJLayeredPane instance is created if necessary.
746 746 *
747 747 * @return an AccessibleJLayeredPane that serves as the
748 748 * AccessibleContext of this JLayeredPane
749 749 */
750 750 public AccessibleContext getAccessibleContext() {
751 751 if (accessibleContext == null) {
752 752 accessibleContext = new AccessibleJLayeredPane();
753 753 }
754 754 return accessibleContext;
755 755 }
756 756
757 757 /**
758 758 * This class implements accessibility support for the
759 759 * <code>JLayeredPane</code> class. It provides an implementation of the
760 760 * Java Accessibility API appropriate to layered pane user-interface
761 761 * elements.
762 762 * <p>
763 763 * <strong>Warning:</strong>
764 764 * Serialized objects of this class will not be compatible with
765 765 * future Swing releases. The current serialization support is
766 766 * appropriate for short term storage or RMI between applications running
767 767 * the same version of Swing. As of 1.4, support for long term storage
768 768 * of all JavaBeans™
769 769 * has been added to the <code>java.beans</code> package.
770 770 * Please see {@link java.beans.XMLEncoder}.
771 771 */
772 772 @SuppressWarnings("serial")
773 773 protected class AccessibleJLayeredPane extends AccessibleJComponent {
774 774
775 775 /**
776 776 * Get the role of this object.
777 777 *
778 778 * @return an instance of AccessibleRole describing the role of the
779 779 * object
780 780 * @see AccessibleRole
781 781 */
782 782 public AccessibleRole getAccessibleRole() {
783 783 return AccessibleRole.LAYERED_PANE;
784 784 }
785 785 }
786 786 }
↓ open down ↓ |
732 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX