Source Code Cross Referenced for JLayeredPane.java in  » 6.0-JDK-Core » swing » javax » swing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » swing » javax.swing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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