/[classpath]/classpath/javax/swing/JLayeredPane.java
ViewVC logotype

Diff of /classpath/javax/swing/JLayeredPane.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by mkoch, Sun Jun 8 11:43:14 2003 UTC revision 1.5 by graydon, Fri Nov 21 22:38:41 2003 UTC
# Line 39  exception statement from your version. * Line 39  exception statement from your version. *
39  package javax.swing;  package javax.swing;
40    
41  import java.awt.Component;  import java.awt.Component;
42    import java.util.*;
43    import java.awt.Component;
44  import javax.accessibility.Accessible;  import javax.accessibility.Accessible;
45    
46    
47    /**
48     * The "Layered Pane" is a container which divides its children into 6 (or
49     * more) disjoint sets. the pre-defined sets are:
50     *
51     *  "Frame Content", "Default", "Palette", "Modal", "Popup", and "Drag".
52     *
53     * A child is in exactly one of these layers at any time, though there may
54     * be other layers if someone creates them.
55     *
56     * The purpose of this class is to translate this view of "layers" into a
57     * contiguous array of components: the one held in our ancestor,
58     * java.awt.Container.
59     *
60     * There is a precise set of words we will use to refer to numbers within
61     * this class:
62     *
63     * Internal Component Index: an offset into the "component" array held in
64     * our ancestor, java.awt.Container, from [0 .. component.length). The
65     * drawing rule with internal indices is that 0 is drawn first.
66     *
67     * External Component Index: an offset into the "logical drawing order" of
68     * this container. If I is the internal index of a component, the external
69     * index E = component.length - I. The rule with external indices is that 0
70     * is drawn last.
71     *
72     * Layer Number: a general int specifying a layer within this component.
73     * Negative numbers are drawn first, then layer 0, then positive numbered
74     * layers, in ascending order.
75     *
76     * Position: an offset into a layer's "logical drawing order". Layer
77     * position 0 is drawn last. Layer position -1 is a synonym for the first
78     * layer position (the logical "bottom").
79     */
80    
81  public class JLayeredPane extends JComponent implements Accessible  public class JLayeredPane extends JComponent implements Accessible
82  {  {
83    
84        public static String LAYER_PROPERTY = "LAYER_PROPERTY";
85    
86        public static Integer FRAME_CONTENT_LAYER = new Integer (-30000);
87    
88        public static Integer DEFAULT_LAYER = new Integer (0);
89        public static Integer PALETTE_LAYER = new Integer (100);
90        public static Integer MODAL_LAYER   = new Integer (200);
91        public static Integer POPUP_LAYER   = new Integer (300);
92        public static Integer DRAG_LAYER    = new Integer (400);
93    
94        TreeMap layers;               // Layer Number (Integer) -> Layer Size (Integer)
95        Hashtable componentToLayer;   // Component -> Layer Number (Integer)
96    
97        protected Integer getLayer (Component c)
98        {
99            if (! componentToLayer.containsKey (c))
100                throw new IllegalArgumentException ();
101            return (Integer) componentToLayer.get (c);
102        }
103    
104        // this returns a half-open range [bottom, top), which is the range of
105        // internal component indices this layer number corresponds to.  note
106        // that top is *not* included in the range of component indices in this
107        // layer: a layer with 0 elements in it has ret[0] == ret[1].
108    
109        protected int[] layerToRange (Integer layer)
110        {
111            int[] ret = new int[2];
112            Iterator i = layers.entrySet ().iterator ();
113            while (i.hasNext())
114                {
115                    Map.Entry pair = (Map.Entry) i.next();
116                    Integer layerNum = (Integer) pair.getKey ();
117                    Integer layerSz = (Integer) pair.getValue ();
118                    if (layerNum == layer)
119                        {
120                            ret[1] = ret[0] + layerSz.intValue ();
121                            return ret;
122                        }
123                    else
124                        {
125                            ret[0] += layerSz.intValue ();
126                        }
127                }
128            // should have found the layer during iteration
129            throw new IllegalArgumentException ();
130        }
131    
132        protected void incrLayer(Integer layer)
133        {
134            int sz = 1;
135            if (layers.containsKey (layer))
136                sz += ((Integer)(layers.get (layer))).intValue ();
137            layers.put (layer, new Integer(sz));
138        }
139    
140        protected void decrLayer(Integer layer)
141        {
142            int sz = 0;
143            if (layers.containsKey (layer))
144                sz = ((Integer)(layers.get (layer))).intValue () - 1;
145            layers.put (layer, new Integer(sz));
146        }
147    
148      JLayeredPane()      JLayeredPane()
149      {      {
150            layers = new TreeMap ();
151            layers.put (FRAME_CONTENT_LAYER, new Integer (0));
152            layers.put (DEFAULT_LAYER, new Integer (0));
153            layers.put (PALETTE_LAYER, new Integer (0));
154            layers.put (MODAL_LAYER, new Integer (0));
155            layers.put (POPUP_LAYER, new Integer (0));
156            layers.put (DRAG_LAYER, new Integer (0));      
157    
158            componentToLayer = new Hashtable ();
159        }
160    
161        public int highestLayer()
162        {
163            if (layers.size() == 0)
164                return 0;
165            return ((Integer)(layers.lastKey ())).intValue ();
166        }
167        
168        public int lowestLayer()
169        {
170            if (layers.size() == 0)
171                return 0;
172            return ((Integer)(layers.firstKey ())).intValue ();
173        }
174    
175        public void moveToFront(Component c)
176        {
177            setPosition (c, 0);
178      }      }
179    
180        public void moveToBack(Component c)
181        {
182            setPosition (c, -1);
183        }
184            
185      protected void addImpl(Component comp, Object constraints, int index)      public int getPosition(Component c)
186      {              {
187          super.addImpl(comp, constraints, index);          Integer layer = getLayer (c);
188            int[] range = layerToRange (layer);
189            int top = (range[1] - 1);
190            Component[] comps = getComponents ();
191            for (int i = range[0]; i < range[1]; ++i)
192                {
193                    if (comps[i] == c)
194                        return top - i;
195                }
196            // should have found it
197            throw new IllegalArgumentException ();
198        }
199    
200        public void setPosition(Component c, int position)
201        {
202            Integer layer = getLayer (c);
203            int[] range = layerToRange (layer);
204            if (range[0] == range[1])
205                throw new IllegalArgumentException ();
206    
207            int top = (range[1] - 1);
208            if (position == -1)
209                position = top - range[0];
210            int targ = top - position;
211            int curr = -1;
212    
213            Component[] comps = getComponents();
214            for (int i = range[0]; i < range[1]; ++i)
215                {
216                    if (comps[i] == c)
217                        {
218                            curr = i;
219                            break;
220                        }
221                }
222            if (curr == -1)
223                // should have found it
224                throw new IllegalArgumentException ();
225    
226            // System.err.println("set component position to " + position + " in layer " + layer);
227    
228            Component tmp = comps[curr];
229            super.remove (curr);
230            super.add (tmp, targ);
231            super.validate ();
232        }
233        
234    
235    
236        public Component[] getComponentsInLayer(int layer)
237        {
238            int[] range = layerToRange (getObjectForLayer (layer));
239            if (range[0] == range[1])
240                return new Component[0];
241            else
242                {
243                    Component[] comps = getComponents ();
244                    int sz = (range[1] - 1) - range[0];
245                    Component[] nc = new Component[sz];
246                    for (int i = 0; i < sz; ++i)
247                        nc[i] = comps[range[0] + i];
248                    return nc;
249                }
250        }
251    
252        public int getComponentCountInLayer(int layer)
253        {
254            int[] range = layerToRange (getObjectForLayer (layer));
255            if (range[0] == range[1])
256                return 0;
257            else
258                return ((range[1] - 1) - range[0]);
259        }
260    
261        protected Hashtable getComponentToLayer()
262        {
263            return componentToLayer;
264        }
265    
266        protected int getInternalIndexOf(Component c)
267        {
268            Integer layer = getLayer (c);
269            int[] range = layerToRange (layer);
270            Component[] comps = getComponents();
271            for (int i = range[0]; i < range[1]; ++i)
272                {
273                    if (comps[i] == c)
274                        return i;
275                }
276            // should have found the component during iteration
277            throw new IllegalArgumentException ();
278        }
279    
280    
281        public int getIndexOf(Component c)
282        {
283            // returns the *external* index of the component.
284            int top = getComponentCount() - 1;
285            return top - getIndexOf (c);
286        }    
287    
288    
289        protected Integer getObjectForLayer(int layer)
290        {
291            switch (layer)
292                {
293                case -30000:
294                    return FRAME_CONTENT_LAYER;
295    
296                case 0:
297                    return DEFAULT_LAYER;
298    
299                case 100:
300                    return PALETTE_LAYER;
301    
302                case 200:
303                    return MODAL_LAYER;
304    
305                case 300:
306                    return POPUP_LAYER;
307    
308                case 400:
309                    return DRAG_LAYER;
310    
311                default:
312                    break;
313                }
314    
315            return new Integer(layer);
316        }
317        
318        protected int insertIndexForLayer(int layer, int position)
319        {
320            int[] range = layerToRange (getObjectForLayer (layer));
321            if (range[0] == range[1])
322                return range[0];
323                    
324          comp.validate();          int bottom = range[0];
325          comp.repaint();          int top = range[1] - 1;
326            
327            if (position == -1 || position > (top - bottom))
328                return bottom;
329            else
330                return top - position;
331      }      }
332            
333          public void remove (int index)
334        {
335            Component c = getComponent (index);
336            Integer layer = getLayer (c);
337            decrLayer (layer);
338            componentToLayer.remove (c);
339            super.remove (index);
340        }
341            
342        public void remove (Component comp)
343        {
344            Integer layer = getLayer (comp);
345            decrLayer (layer);
346            componentToLayer.remove (comp);
347            super.remove (comp);
348        }
349    
350        public void removeAll ()
351        {
352            componentToLayer.clear ();
353            layers.clear ();
354            super.removeAll ();
355        }
356    
357        public void setLayer(Component c, int layer)
358        {
359            componentToLayer.put (c, getObjectForLayer (layer));
360        }
361    
362        public void setLayer(Component c,
363                             int layer,
364                             int position)
365        {
366            componentToLayer.put (c, getObjectForLayer (layer));
367            setPosition(c, position);
368            repaint();
369        }
370    
371        protected void addImpl(Component comp, Object layerConstraint, int index)
372        {          
373            Integer layer;
374            if (layerConstraint != null && layerConstraint instanceof Integer)
375                    layer = (Integer) layerConstraint;
376            else if (componentToLayer.containsKey (comp))
377                layer = (Integer) componentToLayer.remove (comp);
378            else
379                layer = DEFAULT_LAYER;
380    
381            int newIdx = insertIndexForLayer(layer.intValue (), -1);
382            componentToLayer.put (comp, layer);
383            incrLayer (layer);
384    
385            // System.err.println("adding component to layer " + layer);
386            
387            super.addImpl(comp, null, newIdx);      
388            validate();
389            repaint();
390        }    
391  }  }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26