KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JLayeredPane


1 /*
2  * @(#)JLayeredPane.java 1.54 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.Component JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.awt.Color JavaDoc;
13 import java.awt.Graphics JavaDoc;
14 import java.awt.Rectangle JavaDoc;
15
16 import javax.accessibility.*;
17
18 /**
19  * <code>JLayeredPane</code> adds depth to a JFC/Swing container,
20  * allowing components to overlap each other when needed.
21  * An <code>Integer</code> object specifies each component's depth in the
22  * container, where higher-numbered components sit &quot;on top&quot; of other
23  * components.
24  * For task-oriented documentation and examples of using layered panes see
25  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/layeredpane.html">How to Use a Layered Pane</a>,
26  * a section in <em>The Java Tutorial</em>.
27  * <P>
28  * <TABLE ALIGN="RIGHT" BORDER="0" SUMMARY="layout">
29  * <TR>
30  * <TD ALIGN="CENTER">
31  * <P ALIGN="CENTER"><IMG SRC="doc-files/JLayeredPane-1.gif"
32  * alt="The following text describes this image."
33  * WIDTH="269" HEIGHT="264" ALIGN="BOTTOM" BORDER="0">
34  * </TD>
35  * </TR>
36  * </TABLE>
37  * For convenience, <code>JLayeredPane</code> divides the depth-range
38  * into several different layers. Putting a component into one of those
39  * layers makes it easy to ensure that components overlap properly,
40  * without having to worry about specifying numbers for specific depths:
41  * <DL>
42  * <DT><FONT SIZE="2">DEFAULT_LAYER</FONT></DT>
43  * <DD>The standard layer, where most components go. This the bottommost
44  * layer.
45  * <DT><FONT SIZE="2">PALETTE_LAYER</FONT></DT>
46  * <DD>The palette layer sits over the default layer. Useful for floating
47  * toolbars and palettes, so they can be positioned above other components.
48  * <DT><FONT SIZE="2">MODAL_LAYER</FONT></DT>
49  * <DD>The layer used for modal dialogs. They will appear on top of any
50  * toolbars, palettes, or standard components in the container.
51  * <DT><FONT SIZE="2">POPUP_LAYER</FONT></DT>
52  * <DD>The popup layer displays above dialogs. That way, the popup windows
53  * associated with combo boxes, tooltips, and other help text will appear
54  * above the component, palette, or dialog that generated them.
55  * <DT><FONT SIZE="2">DRAG_LAYER</FONT></DT>
56  * <DD>When dragging a component, reassigning it to the drag layer ensures
57  * that it is positioned over every other component in the container. When
58  * finished dragging, it can be reassigned to its normal layer.
59  * </DL>
60  * The <code>JLayeredPane</code> methods <code>moveToFront(Component)</code>,
61  * <code>moveToBack(Component)</code> and <code>setPosition</code> can be used
62  * to reposition a component within its layer. The <code>setLayer</code> method
63  * can also be used to change the component's current layer.
64  *
65  * <h2>Details</h2>
66  * <code>JLayeredPane</code> manages its list of children like
67  * <code>Container</code>, but allows for the definition of a several
68  * layers within itself. Children in the same layer are managed exactly
69  * like the normal <code>Container</code> object,
70  * with the added feature that when children components overlap, children
71  * in higher layers display above the children in lower layers.
72  * <p>
73  * Each layer is a distinct integer number. The layer attribute can be set
74  * on a <code>Component</code> by passing an <code>Integer</code>
75  * object during the add call.<br> For example:
76  * <PRE>
77  * layeredPane.add(child, JLayeredPane.DEFAULT_LAYER);
78  * or
79  * layeredPane.add(child, new Integer(10));
80  * </PRE>
81  * The layer attribute can also be set on a Component by calling<PRE>
82  * layeredPaneParent.setLayer(child, 10)</PRE>
83  * on the <code>JLayeredPane</code> that is the parent of component. The layer
84  * should be set <i>before</i> adding the child to the parent.
85  * <p>
86  * Higher number layers display above lower number layers. So, using
87  * numbers for the layers and letters for individual components, a
88  * representative list order would look like this:<PRE>
89  * 5a, 5b, 5c, 2a, 2b, 2c, 1a </PRE>
90  * where the leftmost components are closest to the top of the display.
91  * <p>
92  * A component can be moved to the top or bottom position within its
93  * layer by calling <code>moveToFront</code> or <code>moveToBack</code>.
94  * <p>
95  * The position of a component within a layer can also be specified directly.
96  * Valid positions range from 0 up to one less than the number of
97  * components in that layer. A value of -1 indicates the bottommost
98  * position. A value of 0 indicates the topmost position. Unlike layer
99  * numbers, higher position values are <i>lower</i> in the display.
100  * <blockquote>
101  * <b>Note:</b> This sequence (defined by java.awt.Container) is the reverse
102  * of the layer numbering sequence. Usually though, you will use <code>moveToFront</code>,
103  * <code>moveToBack</code>, and <code>setLayer</code>.
104  * </blockquote>
105  * Here are some examples using the method add(Component, layer, position):
106  * Calling add(5x, 5, -1) results in:<PRE>
107  * 5a, 5b, 5c, 5x, 2a, 2b, 2c, 1a </PRE>
108  *
109  * Calling add(5z, 5, 2) results in:<PRE>
110  * 5a, 5b, 5z, 5c, 5x, 2a, 2b, 2c, 1a </PRE>
111  *
112  * Calling add(3a, 3, 7) results in:<PRE>
113  * 5a, 5b, 5z, 5c, 5x, 3a, 2a, 2b, 2c, 1a </PRE>
114  *
115  * Using normal paint/event mechanics results in 1a appearing at the bottom
116  * and 5a being above all other components.
117  * <p>
118  * <b>Note:</b> that these layers are simply a logical construct and LayoutManagers
119  * will affect all child components of this container without regard for
120  * layer settings.
121  * <p>
122  * <strong>Warning:</strong>
123  * Serialized objects of this class will not be compatible with
124  * future Swing releases. The current serialization support is
125  * appropriate for short term storage or RMI between applications running
126  * the same version of Swing. As of 1.4, support for long term storage
127  * of all JavaBeans<sup><font size="-2">TM</font></sup>
128  * has been added to the <code>java.beans</code> package.
129  * Please see {@link java.beans.XMLEncoder}.
130  *
131  * @version 1.36 02/02/00
132  * @author David Kloba
133  */

134 public class JLayeredPane extends JComponent JavaDoc implements Accessible {
135     /// Watch the values in getObjectForLayer()
136
/** Convenience object defining the Default layer. Equivalent to new Integer(0).*/
137     public final static Integer JavaDoc DEFAULT_LAYER = new Integer JavaDoc(0);
138     /** Convenience object defining the Palette layer. Equivalent to new Integer(100).*/
139     public final static Integer JavaDoc PALETTE_LAYER = new Integer JavaDoc(100);
140     /** Convenience object defining the Modal layer. Equivalent to new Integer(200).*/
141     public final static Integer JavaDoc MODAL_LAYER = new Integer JavaDoc(200);
142     /** Convenience object defining the Popup layer. Equivalent to new Integer(300).*/
143     public final static Integer JavaDoc POPUP_LAYER = new Integer JavaDoc(300);
144     /** Convenience object defining the Drag layer. Equivalent to new Integer(400).*/
145     public final static Integer JavaDoc DRAG_LAYER = new Integer JavaDoc(400);
146     /** Convenience object defining the Frame Content layer.
147       * This layer is normally only use to positon the contentPane and menuBar
148       * components of JFrame.
149       * Equivalent to new Integer(-30000).
150       * @see JFrame
151       */

152     public final static Integer JavaDoc FRAME_CONTENT_LAYER = new Integer JavaDoc(-30000);
153
154     /** Bound property */
155     public final static String JavaDoc LAYER_PROPERTY = "layeredContainerLayer";
156     // Hashtable to store layer values for non-JComponent components
157
private Hashtable JavaDoc<Component JavaDoc,Integer JavaDoc> componentToLayer;
158     private boolean optimizedDrawingPossible = true;
159
160
161 //////////////////////////////////////////////////////////////////////////////
162
//// Container Override methods
163
//////////////////////////////////////////////////////////////////////////////
164
/** Create a new JLayeredPane */
165     public JLayeredPane() {
166         setLayout(null);
167     }
168
169     private void validateOptimizedDrawing() {
170         boolean layeredComponentFound = false;
171         synchronized(getTreeLock()) {
172             Integer JavaDoc layer = null;
173
174             for (Component JavaDoc c : getComponents()) {
175                 layer = null;
176                 if(c instanceof JInternalFrame JavaDoc || (c instanceof JComponent JavaDoc &&
177                          (layer = (Integer JavaDoc)((JComponent JavaDoc)c).getClientProperty(
178                           LAYER_PROPERTY)) != null)) {
179                     if(layer != null && layer.equals(FRAME_CONTENT_LAYER))
180                         continue;
181                     layeredComponentFound = true;
182                     break;
183                 }
184             }
185         }
186         
187         if(layeredComponentFound)
188             optimizedDrawingPossible = false;
189         else
190             optimizedDrawingPossible = true;
191     }
192     
193     protected void addImpl(Component JavaDoc comp, Object JavaDoc constraints, int index) {
194         int layer = DEFAULT_LAYER.intValue();
195         int pos;
196
197         if(constraints instanceof Integer JavaDoc) {
198             layer = ((Integer JavaDoc)constraints).intValue();
199             setLayer(comp, layer);
200         } else
201             layer = getLayer(comp);
202
203         pos = insertIndexForLayer(layer, index);
204         super.addImpl(comp, constraints, pos);
205         comp.validate();
206         comp.repaint();
207         validateOptimizedDrawing();
208     }
209
210     /**
211      * Remove the indexed component from this pane.
212      * This is the absolute index, ignoring layers.
213      *
214      * @param index an int specifying the component to remove
215      * @see #getIndexOf
216      */

217     public void remove(int index) {
218         Component JavaDoc c = getComponent(index);
219         super.remove(index);
220         if (c != null && !(c instanceof JComponent JavaDoc)) {
221             getComponentToLayer().remove(c);
222         }
223         validateOptimizedDrawing();
224     }
225
226     /**
227      * Removes all the components from this container.
228      *
229      * @since 1.5
230      */

231     public void removeAll() {
232         Component JavaDoc[] children = getComponents();
233         Hashtable JavaDoc cToL = getComponentToLayer();
234         for (int counter = children.length - 1; counter >= 0; counter--) {
235             Component JavaDoc c = children[counter];
236             if (c != null && !(c instanceof JComponent JavaDoc)) {
237                 cToL.remove(c);
238             }
239         }
240         super.removeAll();
241     }
242
243     /**
244      * Returns false if components in the pane can overlap, which makes
245      * optimized drawing impossible. Otherwise, returns true.
246      *
247      * @return false if components can overlap, else true
248      * @see JComponent#isOptimizedDrawingEnabled
249      */

250     public boolean isOptimizedDrawingEnabled() {
251         return optimizedDrawingPossible;
252     }
253
254
255 //////////////////////////////////////////////////////////////////////////////
256
//// New methods for managing layers
257
//////////////////////////////////////////////////////////////////////////////
258
/** Sets the layer property on a JComponent. This method does not cause
259       * any side effects like setLayer() (painting, add/remove, etc).
260       * Normally you should use the instance method setLayer(), in order to
261       * get the desired side-effects (like repainting).
262       *
263       * @param c the JComponent to move
264       * @param layer an int specifying the layer to move it to
265       * @see #setLayer
266       */

267     public static void putLayer(JComponent JavaDoc c, int layer) {
268         /// MAKE SURE THIS AND setLayer(Component c, int layer, int position) are SYNCED
269
Integer JavaDoc layerObj;
270
271         layerObj = new Integer JavaDoc(layer);
272         c.putClientProperty(LAYER_PROPERTY, layerObj);
273     }
274
275     /** Gets the layer property for a JComponent, it
276       * does not cause any side effects like setLayer(). (painting, add/remove, etc)
277       * Normally you should use the instance method getLayer().
278       *
279       * @param c the JComponent to check
280       * @return an int specifying the component's layer
281       */

282     public static int getLayer(JComponent JavaDoc c) {
283         Integer JavaDoc i;
284         if((i = (Integer JavaDoc)c.getClientProperty(LAYER_PROPERTY)) != null)
285             return i.intValue();
286         return DEFAULT_LAYER.intValue();
287     }
288
289     /** Convenience method that returns the first JLayeredPane which
290       * contains the specified component. Note that all JFrames have a
291       * JLayeredPane at their root, so any component in a JFrame will
292       * have a JLayeredPane parent.
293       *
294       * @param c the Component to check
295       * @return the JLayeredPane that contains the component, or
296       * null if no JLayeredPane is found in the component
297       * hierarchy
298       * @see JFrame
299       * @see JRootPane
300       */

301     public static JLayeredPane JavaDoc getLayeredPaneAbove(Component JavaDoc c) {
302         if(c == null) return null;
303         
304         Component JavaDoc parent = c.getParent();
305         while(parent != null && !(parent instanceof JLayeredPane JavaDoc))
306             parent = parent.getParent();
307         return (JLayeredPane JavaDoc)parent;
308     }
309
310     /** Sets the layer attribute on the specified component,
311       * making it the bottommost component in that layer.
312       * Should be called before adding to parent.
313       *
314       * @param c the Component to set the layer for
315       * @param layer an int specifying the layer to set, where
316       * lower numbers are closer to the bottom
317       */

318     public void setLayer(Component JavaDoc c, int layer) {
319         setLayer(c, layer, -1);
320     }
321
322     /** Sets the layer attribute for the specified component and
323       * also sets its position within that layer.
324       *
325       * @param c the Component to set the layer for
326       * @param layer an int specifying the layer to set, where
327       * lower numbers are closer to the bottom
328       * @param position an int specifying the position within the
329       * layer, where 0 is the topmost position and -1
330       * is the bottommost position
331       */

332     public void setLayer(Component JavaDoc c, int layer, int position) {
333         Integer JavaDoc layerObj;
334         layerObj = getObjectForLayer(layer);
335
336         if(layer == getLayer(c) && position == getPosition(c)) {
337                 repaint(c.getBounds());
338             return;
339         }
340         
341         /// MAKE SURE THIS AND putLayer(JComponent c, int layer) are SYNCED
342
if(c instanceof JComponent JavaDoc)
343             ((JComponent JavaDoc)c).putClientProperty(LAYER_PROPERTY, layerObj);
344         else
345             getComponentToLayer().put((Component JavaDoc)c, layerObj);
346         
347         if(c.getParent() == null || c.getParent() != this) {
348             repaint(c.getBounds());
349             return;
350         }
351
352         int index = insertIndexForLayer(c, layer, position);
353
354         setComponentZOrder(c, index);
355         repaint(c.getBounds());
356     }
357
358     /**
359      * Returns the layer attribute for the specified Component.
360      *
361      * @param c the Component to check
362      * @return an int specifying the component's current layer
363      */

364     public int getLayer(Component JavaDoc c) {
365         Integer JavaDoc i;
366         if(c instanceof JComponent JavaDoc)
367             i = (Integer JavaDoc)((JComponent JavaDoc)c).getClientProperty(LAYER_PROPERTY);
368         else
369             i = (Integer JavaDoc)getComponentToLayer().get((Component JavaDoc)c);
370
371         if(i == null)
372             return DEFAULT_LAYER.intValue();
373         return i.intValue();
374     }
375
376     /**
377      * Returns the index of the specified Component.
378      * This is the absolute index, ignoring layers.
379      * Index numbers, like position numbers, have the topmost component
380      * at index zero. Larger numbers are closer to the bottom.
381      *
382      * @param c the Component to check
383      * @return an int specifying the component's index
384      */

385     public int getIndexOf(Component JavaDoc c) {
386         int i, count;
387         
388         count = getComponentCount();
389         for(i = 0; i < count; i++) {
390             if(c == getComponent(i))
391                 return i;
392         }
393         return -1;
394     }
395     /**
396      * Moves the component to the top of the components in its current layer
397      * (position 0).
398      *
399      * @param c the Component to move
400      * @see #setPosition(Component, int)
401      */

402     public void moveToFront(Component JavaDoc c) {
403         setPosition(c, 0);
404     }
405
406     /**
407      * Moves the component to the bottom of the components in its current layer
408      * (position -1).
409      *
410      * @param c the Component to move
411      * @see #setPosition(Component, int)
412      */

413     public void moveToBack(Component JavaDoc c) {
414         setPosition(c, -1);
415     }
416
417     /**
418      * Moves the component to <code>position</code> within its current layer,
419      * where 0 is the topmost position within the layer and -1 is the bottommost
420      * position.
421      * <p>
422      * <b>Note:</b> Position numbering is defined by java.awt.Container, and
423      * is the opposite of layer numbering. Lower position numbers are closer
424      * to the top (0 is topmost), and higher position numbers are closer to
425      * the bottom.
426      *
427      * @param c the Component to move
428      * @param position an int in the range -1..N-1, where N is the number of
429      * components in the component's current layer
430      */

431     public void setPosition(Component JavaDoc c, int position) {
432         setLayer(c, getLayer(c), position);
433     }
434
435     /**
436      * Get the relative position of the component within its layer.
437      *
438      * @param c the Component to check
439      * @return an int giving the component's position, where 0 is the
440      * topmost position and the highest index value = the count
441      * count of components at that layer, minus 1
442      *
443      * @see #getComponentCountInLayer
444      */

445     public int getPosition(Component JavaDoc c) {
446         int i, count, startLayer, curLayer, startLocation, pos = 0;
447         
448         count = getComponentCount();
449         startLocation = getIndexOf(c);
450
451         if(startLocation == -1)
452             return -1;
453         
454         startLayer = getLayer(c);
455         for(i = startLocation - 1; i >= 0; i--) {
456             curLayer = getLayer(getComponent(i));
457             if(curLayer == startLayer)
458                 pos++;
459             else
460                 return pos;
461         }
462         return pos;
463     }
464
465     /** Returns the highest layer value from all current children.
466       * Returns 0 if there are no children.
467       *
468       * @return an int indicating the layer of the topmost component in the
469       * pane, or zero if there are no children
470       */

471     public int highestLayer() {
472         if(getComponentCount() > 0)
473             return getLayer(getComponent(0));
474         return 0;
475     }
476
477     /** Returns the lowest layer value from all current children.
478       * Returns 0 if there are no children.
479       *
480       * @return an int indicating the layer of the bottommost component in the
481       * pane, or zero if there are no children
482       */

483     public int lowestLayer() {
484         int count = getComponentCount();
485         if(count > 0)
486             return getLayer(getComponent(count-1));
487         return 0;
488     }
489
490     /**
491      * Returns the number of children currently in the specified layer.
492      *
493      * @param layer an int specifying the layer to check
494      * @return an int specifying the number of components in that layer
495      */

496     public int getComponentCountInLayer(int layer) {
497         int i, count, curLayer;
498         int layerCount = 0;
499         
500         count = getComponentCount();
501         for(i = 0; i < count; i++) {
502             curLayer = getLayer(getComponent(i));
503             if(curLayer == layer) {
504                 layerCount++;
505             /// Short circut the counting when we have them all
506
} else if(layerCount > 0 || curLayer < layer) {
507                 break;
508             }
509         }
510         
511         return layerCount;
512     }
513
514     /**
515      * Returns an array of the components in the specified layer.
516      *
517      * @param layer an int specifying the layer to check
518      * @return an array of Components contained in that layer
519      */

520     public Component JavaDoc[] getComponentsInLayer(int layer) {
521         int i, count, curLayer;
522         int layerCount = 0;
523         Component JavaDoc[] results;
524         
525         results = new Component JavaDoc[getComponentCountInLayer(layer)];
526         count = getComponentCount();
527         for(i = 0; i < count; i++) {
528             curLayer = getLayer(getComponent(i));
529             if(curLayer == layer) {
530                 results[layerCount++] = getComponent(i);
531             /// Short circut the counting when we have them all
532
} else if(layerCount > 0 || curLayer < layer) {
533                 break;
534             }
535         }
536         
537         return results;
538     }
539
540     /**
541      * Paints this JLayeredPane within the specified graphics context.
542      *
543      * @param g the Graphics context within which to paint
544      */

545     public void paint(Graphics JavaDoc g) {
546         if(isOpaque()) {
547             Rectangle JavaDoc r = g.getClipBounds();
548             Color JavaDoc c = getBackground();
549             if(c == null)
550                 c = Color.lightGray;
551             g.setColor(c);
552             if (r != null) {
553                 g.fillRect(r.x, r.y, r.width, r.height);
554             }
555             else {
556                 g.fillRect(0, 0, getWidth(), getHeight());
557             }
558         }
559         super.paint(g);
560     }
561
562 //////////////////////////////////////////////////////////////////////////////
563
//// Implementation Details
564
//////////////////////////////////////////////////////////////////////////////
565

566     /**
567      * Returns the hashtable that maps components to layers.
568      *
569      * @return the Hashtable used to map components to their layers
570      */

571     protected Hashtable JavaDoc<Component JavaDoc,Integer JavaDoc> getComponentToLayer() {
572         if(componentToLayer == null)
573             componentToLayer = new Hashtable JavaDoc<Component JavaDoc,Integer JavaDoc>(4);
574         return componentToLayer;
575     }
576     
577     /**
578      * Returns the Integer object associated with a specified layer.
579      *
580      * @param layer an int specifying the layer
581      * @return an Integer object for that layer
582      */

583     protected Integer JavaDoc getObjectForLayer(int layer) {
584         Integer JavaDoc layerObj;
585         switch(layer) {
586         case 0:
587             layerObj = DEFAULT_LAYER;
588             break;
589         case 100:
590             layerObj = PALETTE_LAYER;
591             break;
592         case 200:
593             layerObj = MODAL_LAYER;
594             break;
595         case 300:
596             layerObj = POPUP_LAYER;
597             break;
598         case 400:
599             layerObj = DRAG_LAYER;
600             break;
601         default:
602             layerObj = new Integer JavaDoc(layer);
603         }
604         return layerObj;
605     }
606
607     /**
608      * Primitive method that determines the proper location to
609      * insert a new child based on layer and position requests.
610      *
611      * @param layer an int specifying the layer
612      * @param position an int specifying the position within the layer
613      * @return an int giving the (absolute) insertion-index
614      *
615      * @see #getIndexOf
616      */

617     protected int insertIndexForLayer(int layer, int position) {
618         return insertIndexForLayer(null, layer, position);
619     }
620
621     /**
622      * This method is an extended version of insertIndexForLayer()
623      * to support setLayer which uses Container.setZOrder which does
624      * not remove the component from the containment heirarchy though
625      * we need to ignore it when calculating the insertion index.
626      *
627      * @param comp component to ignore when determining index
628      * @param layer an int specifying the layer
629      * @param position an int specifying the position within the layer
630      * @return an int giving the (absolute) insertion-index
631      *
632      * @see #getIndexOf
633      */

634     private int insertIndexForLayer(Component JavaDoc comp, int layer, int position) {
635         int i, count, curLayer;
636         int layerStart = -1;
637         int layerEnd = -1;
638         int componentCount = getComponentCount();
639         
640         ArrayList JavaDoc<Component JavaDoc> compList =
641             new ArrayList JavaDoc<Component JavaDoc>(componentCount);
642         for (int index = 0; index < componentCount; index++) {
643             if (getComponent(index) != comp) {
644                 compList.add(getComponent(index));
645             }
646         }
647
648         count = compList.size();
649         for (i = 0; i < count; i++) {
650             curLayer = getLayer(compList.get(i));
651             if (layerStart == -1 && curLayer == layer) {
652                 layerStart = i;
653             }
654             if (curLayer < layer) {
655                 if (i == 0) {
656                     // layer is greater than any current layer
657
// [ ASSERT(layer > highestLayer()) ]
658
layerStart = 0;
659                     layerEnd = 0;
660                 } else {
661                     layerEnd = i;
662                 }
663                 break;
664             }
665         }
666
667         // layer requested is lower than any current layer
668
// [ ASSERT(layer < lowestLayer()) ]
669
// put it on the bottom of the stack
670
if (layerStart == -1 && layerEnd == -1)
671             return count;
672
673         // In the case of a single layer entry handle the degenerative cases
674
if (layerStart != -1 && layerEnd == -1)
675             layerEnd = count;
676         
677         if (layerEnd != -1 && layerStart == -1)
678             layerStart = layerEnd;
679         
680         // If we are adding to the bottom, return the last element
681
if (position == -1)
682             return layerEnd;
683         
684         // Otherwise make sure the requested position falls in the
685
// proper range
686
if (position > -1 && layerStart + position <= layerEnd)
687             return layerStart + position;
688         
689         // Otherwise return the end of the layer
690
return layerEnd;
691     }
692
693     /**
694      * Returns a string representation of this JLayeredPane. This method
695      * is intended to be used only for debugging purposes, and the
696      * content and format of the returned string may vary between
697      * implementations. The returned string may be empty but may not
698      * be <code>null</code>.
699      *
700      * @return a string representation of this JLayeredPane.
701      */

702     protected String JavaDoc paramString() {
703         String JavaDoc optimizedDrawingPossibleString = (optimizedDrawingPossible ?
704                          "true" : "false");
705
706     return super.paramString() +
707         ",optimizedDrawingPossible=" + optimizedDrawingPossibleString;
708     }
709
710 /////////////////
711
// Accessibility support
712
////////////////
713

714     /**
715      * Gets the AccessibleContext associated with this JLayeredPane.
716      * For layered panes, the AccessibleContext takes the form of an
717      * AccessibleJLayeredPane.
718      * A new AccessibleJLayeredPane instance is created if necessary.
719      *
720      * @return an AccessibleJLayeredPane that serves as the
721      * AccessibleContext of this JLayeredPane
722      */

723     public AccessibleContext getAccessibleContext() {
724         if (accessibleContext == null) {
725             accessibleContext = new AccessibleJLayeredPane();
726         }
727         return accessibleContext;
728     }
729
730     /**
731      * This class implements accessibility support for the
732      * <code>JLayeredPane</code> class. It provides an implementation of the
733      * Java Accessibility API appropriate to layered pane user-interface
734      * elements.
735      * <p>
736      * <strong>Warning:</strong>
737      * Serialized objects of this class will not be compatible with
738      * future Swing releases. The current serialization support is
739      * appropriate for short term storage or RMI between applications running
740      * the same version of Swing. As of 1.4, support for long term storage
741      * of all JavaBeans<sup><font size="-2">TM</font></sup>
742      * has been added to the <code>java.beans</code> package.
743      * Please see {@link java.beans.XMLEncoder}.
744      */

745     protected class AccessibleJLayeredPane extends AccessibleJComponent {
746
747         /**
748          * Get the role of this object.
749          *
750          * @return an instance of AccessibleRole describing the role of the
751          * object
752          * @see AccessibleRole
753          */

754         public AccessibleRole getAccessibleRole() {
755             return AccessibleRole.LAYERED_PANE;
756         }
757     }
758 }
759
760
761
Popular Tags