2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / javax / swing / JLayeredPane.java
blob68512a6a8db634a6a8f728c2d05d64a3315e7095
1 /* JLayeredPane.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing;
41 import java.awt.Component;
42 import java.util.*;
43 import java.awt.Component;
44 import javax.accessibility.Accessible;
47 /**
48 * The "Layered Pane" is a container which divides its children into 6 (or
49 * more) disjoint sets. the pre-defined sets are:
51 * "Frame Content", "Default", "Palette", "Modal", "Popup", and "Drag".
53 * A child is in exactly one of these layers at any time, though there may
54 * be other layers if someone creates them.
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.
60 * There is a precise set of words we will use to refer to numbers within
61 * this class:
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.
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.
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.
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").
81 public class JLayeredPane extends JComponent implements Accessible
84 public static String LAYER_PROPERTY = "LAYER_PROPERTY";
86 public static Integer FRAME_CONTENT_LAYER = new Integer (-30000);
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);
94 TreeMap layers; // Layer Number (Integer) -> Layer Size (Integer)
95 Hashtable componentToLayer; // Component -> Layer Number (Integer)
97 protected Integer getLayer (Component c)
99 if (! componentToLayer.containsKey (c))
100 throw new IllegalArgumentException ();
101 return (Integer) componentToLayer.get (c);
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].
109 protected int[] layerToRange (Integer layer)
111 int[] ret = new int[2];
112 Iterator i = layers.entrySet ().iterator ();
113 while (i.hasNext())
115 Map.Entry pair = (Map.Entry) i.next();
116 Integer layerNum = (Integer) pair.getKey ();
117 Integer layerSz = (Integer) pair.getValue ();
118 if (layerNum == layer)
120 ret[1] = ret[0] + layerSz.intValue ();
121 return ret;
123 else
125 ret[0] += layerSz.intValue ();
128 // should have found the layer during iteration
129 throw new IllegalArgumentException ();
132 protected void incrLayer(Integer layer)
134 int sz = 1;
135 if (layers.containsKey (layer))
136 sz += ((Integer)(layers.get (layer))).intValue ();
137 layers.put (layer, new Integer(sz));
140 protected void decrLayer(Integer layer)
142 int sz = 0;
143 if (layers.containsKey (layer))
144 sz = ((Integer)(layers.get (layer))).intValue () - 1;
145 layers.put (layer, new Integer(sz));
148 JLayeredPane()
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));
158 componentToLayer = new Hashtable ();
161 public int highestLayer()
163 if (layers.size() == 0)
164 return 0;
165 return ((Integer)(layers.lastKey ())).intValue ();
168 public int lowestLayer()
170 if (layers.size() == 0)
171 return 0;
172 return ((Integer)(layers.firstKey ())).intValue ();
175 public void moveToFront(Component c)
177 setPosition (c, 0);
180 public void moveToBack(Component c)
182 setPosition (c, -1);
185 public int getPosition(Component c)
187 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)
193 if (comps[i] == c)
194 return top - i;
196 // should have found it
197 throw new IllegalArgumentException ();
200 public void setPosition(Component c, int position)
202 Integer layer = getLayer (c);
203 int[] range = layerToRange (layer);
204 if (range[0] == range[1])
205 throw new IllegalArgumentException ();
207 int top = (range[1] - 1);
208 if (position == -1)
209 position = top - range[0];
210 int targ = top - position;
211 int curr = -1;
213 Component[] comps = getComponents();
214 for (int i = range[0]; i < range[1]; ++i)
216 if (comps[i] == c)
218 curr = i;
219 break;
222 if (curr == -1)
223 // should have found it
224 throw new IllegalArgumentException ();
226 // System.err.println("set component position to " + position + " in layer " + layer);
228 Component tmp = comps[curr];
229 super.remove (curr);
230 super.add (tmp, targ);
231 super.validate ();
236 public Component[] getComponentsInLayer(int layer)
238 int[] range = layerToRange (getObjectForLayer (layer));
239 if (range[0] == range[1])
240 return new Component[0];
241 else
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;
252 public int getComponentCountInLayer(int layer)
254 int[] range = layerToRange (getObjectForLayer (layer));
255 if (range[0] == range[1])
256 return 0;
257 else
258 return ((range[1] - 1) - range[0]);
261 protected Hashtable getComponentToLayer()
263 return componentToLayer;
266 protected int getInternalIndexOf(Component c)
268 Integer layer = getLayer (c);
269 int[] range = layerToRange (layer);
270 Component[] comps = getComponents();
271 for (int i = range[0]; i < range[1]; ++i)
273 if (comps[i] == c)
274 return i;
276 // should have found the component during iteration
277 throw new IllegalArgumentException ();
281 public int getIndexOf(Component c)
283 // returns the *external* index of the component.
284 int top = getComponentCount() - 1;
285 return top - getIndexOf (c);
289 protected Integer getObjectForLayer(int layer)
291 switch (layer)
293 case -30000:
294 return FRAME_CONTENT_LAYER;
296 case 0:
297 return DEFAULT_LAYER;
299 case 100:
300 return PALETTE_LAYER;
302 case 200:
303 return MODAL_LAYER;
305 case 300:
306 return POPUP_LAYER;
308 case 400:
309 return DRAG_LAYER;
311 default:
312 break;
315 return new Integer(layer);
318 protected int insertIndexForLayer(int layer, int position)
320 int[] range = layerToRange (getObjectForLayer (layer));
321 if (range[0] == range[1])
322 return range[0];
324 int bottom = range[0];
325 int top = range[1] - 1;
327 if (position == -1 || position > (top - bottom))
328 return bottom;
329 else
330 return top - position;
333 public void remove (int index)
335 Component c = getComponent (index);
336 Integer layer = getLayer (c);
337 decrLayer (layer);
338 componentToLayer.remove (c);
339 super.remove (index);
342 public void remove (Component comp)
344 Integer layer = getLayer (comp);
345 decrLayer (layer);
346 componentToLayer.remove (comp);
347 super.remove (comp);
350 public void removeAll ()
352 componentToLayer.clear ();
353 layers.clear ();
354 super.removeAll ();
357 public void setLayer(Component c, int layer)
359 componentToLayer.put (c, getObjectForLayer (layer));
362 public void setLayer(Component c,
363 int layer,
364 int position)
366 componentToLayer.put (c, getObjectForLayer (layer));
367 setPosition(c, position);
368 repaint();
371 protected void addImpl(Component comp, Object layerConstraint, int index)
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;
381 int newIdx = insertIndexForLayer(layer.intValue (), -1);
382 componentToLayer.put (comp, layer);
383 incrLayer (layer);
385 // System.err.println("adding component to layer " + layer);
387 super.addImpl(comp, null, newIdx);
388 validate();
389 repaint();