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)
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
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
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. */
41 import java
.awt
.Component
;
43 import java
.awt
.Component
;
44 import javax
.accessibility
.Accessible
;
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,
60 * There is a precise set of words we will use to refer to numbers within
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
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 ();
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 ();
125 ret
[0] += layerSz
.intValue ();
128 // should have found the layer during iteration
129 throw new IllegalArgumentException ();
132 protected void incrLayer(Integer layer
)
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
)
143 if (layers
.containsKey (layer
))
144 sz
= ((Integer
)(layers
.get (layer
))).intValue () - 1;
145 layers
.put (layer
, new Integer(sz
));
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)
165 return ((Integer
)(layers
.lastKey ())).intValue ();
168 public int lowestLayer()
170 if (layers
.size() == 0)
172 return ((Integer
)(layers
.firstKey ())).intValue ();
175 public void moveToFront(Component c
)
180 public void moveToBack(Component c
)
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
)
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);
209 position
= top
- range
[0];
210 int targ
= top
- position
;
213 Component
[] comps
= getComponents();
214 for (int i
= range
[0]; i
< range
[1]; ++i
)
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
];
230 super.add (tmp
, targ
);
236 public Component
[] getComponentsInLayer(int layer
)
238 int[] range
= layerToRange (getObjectForLayer (layer
));
239 if (range
[0] == range
[1])
240 return new Component
[0];
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
];
252 public int getComponentCountInLayer(int layer
)
254 int[] range
= layerToRange (getObjectForLayer (layer
));
255 if (range
[0] == range
[1])
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
)
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
)
294 return FRAME_CONTENT_LAYER
;
297 return DEFAULT_LAYER
;
300 return PALETTE_LAYER
;
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])
324 int bottom
= range
[0];
325 int top
= range
[1] - 1;
327 if (position
== -1 || position
> (top
- bottom
))
330 return top
- position
;
333 public void remove (int index
)
335 Component c
= getComponent (index
);
336 Integer layer
= getLayer (c
);
338 componentToLayer
.remove (c
);
339 super.remove (index
);
342 public void remove (Component comp
)
344 Integer layer
= getLayer (comp
);
346 componentToLayer
.remove (comp
);
350 public void removeAll ()
352 componentToLayer
.clear ();
357 public void setLayer(Component c
, int layer
)
359 componentToLayer
.put (c
, getObjectForLayer (layer
));
362 public void setLayer(Component c
,
366 componentToLayer
.put (c
, getObjectForLayer (layer
));
367 setPosition(c
, position
);
371 protected void addImpl(Component comp
, Object layerConstraint
, int index
)
374 if (layerConstraint
!= null && layerConstraint
instanceof Integer
)
375 layer
= (Integer
) layerConstraint
;
376 else if (componentToLayer
.containsKey (comp
))
377 layer
= (Integer
) componentToLayer
.remove (comp
);
379 layer
= DEFAULT_LAYER
;
381 int newIdx
= insertIndexForLayer(layer
.intValue (), -1);
382 componentToLayer
.put (comp
, layer
);
385 // System.err.println("adding component to layer " + layer);
387 super.addImpl(comp
, null, newIdx
);