KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > CardLayout


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4  
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7  
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9  
10    $Log: CardLayout.java,v $
11    Revision 1.10 2004/04/21 10:43:57 bobintetley
12    Code cleanup and native build script fix
13
14    Revision 1.9 2004/04/20 16:35:46 bobintetley
15    Code cleanup
16
17    Revision 1.8 2004/04/16 10:19:05 dannaab
18    Misc bug fixes, InputMap implementation, preliminary undo support
19
20    Revision 1.7 2004/01/06 08:28:02 bobintetley
21    Header fixes
22
23  
24  */

25
26 package swingwt.awt;
27
28 import java.util.Vector JavaDoc;
29
30 public class CardLayout implements LayoutManager2 {
31     
32     int currentCard = 0;
33     int hgap;
34     int vgap;
35         
36     public CardLayout() {
37         this(0, 0);
38     }
39
40     public CardLayout(int hgap, int vgap) {
41         this.hgap = hgap;
42         this.vgap = vgap;
43     }
44
45     public int getHgap() {
46         return hgap;
47     }
48
49     public void setHgap(int hgap) {
50         this.hgap = hgap;
51     }
52
53     public int getVgap() {
54         return vgap;
55     }
56
57     public void setVgap(int vgap) {
58         this.vgap = vgap;
59     }
60
61     public void addLayoutComponent(Component comp, Object JavaDoc constraints) {
62         if (constraints instanceof String JavaDoc) {
63             addLayoutComponent((String JavaDoc) constraints, comp);
64         } else {
65             System.out.println(comp.toString());
66         }
67     }
68
69     public void addLayoutComponent(String JavaDoc name, Component comp) {
70         if (!cards.isEmpty()) {
71             comp.setVisible(false);
72         }
73         for (int i = 0; i < cards.size(); i++) {
74             if (((Card) cards.get(i)).name.equals(name)) {
75                 ((Card) cards.get(i)).comp = comp;
76                 return;
77             }
78         }
79         cards.add(new Card(name, comp));
80     }
81
82     public void removeLayoutComponent(Component comp) {
83         for (int i = 0; i < cards.size(); i++) {
84             if (((Card) cards.get(i)).comp == comp) {
85                 if (comp.isVisible() && (comp.getParent() != null)) {
86                     next(comp.getParent());
87                 }
88
89                 cards.remove(i);
90                 if (currentCard > i) {
91                     currentCard--;
92                 }
93                 break;
94             }
95         }
96     }
97
98     public Dimension preferredLayoutSize(Container parent) {
99         Insets insets = parent.getInsets();
100         int ncomponents = parent.getComponentCount();
101         int w = 0;
102         int h = 0;
103
104         for (int i = 0; i < ncomponents; i++) {
105             Component comp = parent.getComponent(i);
106             Dimension d = comp.getPreferredSize();
107             if (d.width > w) {
108                 w = d.width;
109             }
110             if (d.height > h) {
111                 h = d.height;
112             }
113         }
114         return new Dimension(
115         insets.left + insets.right + w + hgap * 2,
116         insets.top + insets.bottom + h + vgap * 2);
117     }
118
119     public Dimension minimumLayoutSize(Container parent) {
120         Insets insets = parent.getInsets();
121         int ncomponents = parent.getComponentCount();
122         int w = 0;
123         int h = 0;
124
125         for (int i = 0; i < ncomponents; i++) {
126             Component comp = parent.getComponent(i);
127             Dimension d = comp.getMinimumSize();
128             if (d.width > w) {
129                 w = d.width;
130             }
131             if (d.height > h) {
132                 h = d.height;
133             }
134         }
135         return new Dimension(
136         insets.left + insets.right + w + hgap * 2,
137         insets.top + insets.bottom + h + vgap * 2);
138     }
139
140     public Dimension maximumLayoutSize(Container target) {
141         return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
142     }
143
144     public float getLayoutAlignmentX(Container parent) {
145         return (float) 0.5;
146     }
147
148     public float getLayoutAlignmentY(Container parent) {
149         return (float) 0.5;
150     }
151
152     public void invalidateLayout(Container target) {
153     }
154
155     public void layoutContainer(Container parent) {
156         Insets insets = parent.getInsets();
157         int ncomponents = parent.getComponentCount();
158         Component comp = null;
159         boolean currentFound = false;
160
161         for (int i = 0; i < ncomponents; i++) {
162             comp = parent.getComponent(i);
163             comp.setBounds(
164             hgap + insets.left,
165             vgap + insets.top,
166             parent.getWidth() - (hgap * 2 + insets.left + insets.right),
167             parent.getHeight() - (vgap * 2 + insets.top + insets.bottom));
168             if (comp.isVisible()) {
169                 currentFound = true;
170             }
171         }
172
173         if (!currentFound && ncomponents > 0) {
174             parent.getComponent(0).setVisible(true);
175         }
176     }
177
178     void checkLayout(Container parent) {
179     }
180
181     public void first(Container parent) {
182         int ncomponents = parent.getComponentCount();
183         for (int i = 0; i < ncomponents; i++) {
184             Component comp = parent.getComponent(i);
185             if (comp.isVisible()) {
186                 comp.setVisible(false);
187                 break;
188             }
189         }
190         if (ncomponents > 0) {
191             currentCard = 0;
192             parent.getComponent(0).setVisible(true);
193             parent.validate();
194         }
195     }
196
197     public void next(Container parent) {
198         int ncomponents = parent.getComponentCount();
199         for (int i = 0; i < ncomponents; i++) {
200             Component comp = parent.getComponent(i);
201             if (comp.isVisible()) {
202                 comp.setVisible(false);
203                 currentCard = (i + 1) % ncomponents;
204                 comp = parent.getComponent(currentCard);
205                 comp.setVisible(true);
206                 parent.validate();
207                 return;
208             }
209         }
210         showFirstAvailableComponent(parent);
211     }
212
213     public void previous(Container parent) {
214         int ncomponents = parent.getComponentCount();
215         for (int i = 0; i < ncomponents; i++) {
216             Component comp = parent.getComponent(i);
217             if (comp.isVisible()) {
218                 comp.setVisible(false);
219                 currentCard = ((i > 0) ? i - 1 : ncomponents - 1);
220                 comp = parent.getComponent(currentCard);
221                 comp.setVisible(true);
222                 parent.validate();
223                 return;
224             }
225         }
226         showFirstAvailableComponent(parent);
227     }
228
229     public void last(Container parent) {
230         int ncomponents = parent.getComponentCount();
231         for (int i = 0; i < ncomponents; i++) {
232             Component comp = parent.getComponent(i);
233             if (comp.isVisible()) {
234                 comp.setVisible(false);
235                 break;
236             }
237         }
238         if (ncomponents > 0) {
239             currentCard = ncomponents - 1;
240             parent.getComponent(currentCard).setVisible(true);
241             parent.validate();
242         }
243     }
244     
245     public void show(Container parent, String JavaDoc name) {
246         Component next = null;
247         int ncomponents = cards.size();
248         for (int i = 0; i < ncomponents; i++) {
249             Card card = (Card) cards.get(i);
250             if (card.name.equals(name)) {
251                 next = card.comp;
252                 currentCard = i;
253                 break;
254             }
255         }
256         if ((next != null) && !next.isVisible()) {
257             ncomponents = parent.getComponentCount();
258             for (int i = 0; i < ncomponents; i++) {
259                 Component comp = parent.getComponent(i);
260                 if (comp.isVisible()) {
261                     comp.setVisible(false);
262                     break;
263                 }
264             }
265             next.setVisible(true);
266             parent.validate();
267         }
268     }
269     
270     
271     
272     private void showFirstAvailableComponent(Container parent) {
273         if (parent.getComponentCount() > 0) {
274             currentCard = 0;
275             parent.getComponent(0).setVisible(true);
276             parent.validate();
277         }
278     }
279     
280     Vector JavaDoc cards = new Vector JavaDoc();
281     
282     private class Card {
283         public String JavaDoc name;
284         public Component comp;
285         public Card(String JavaDoc cardName, Component cardComponent) {
286             name = cardName;
287             comp = cardComponent;
288         }
289     }
290
291     public String JavaDoc toString() {
292         return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
293     }
294 }
295
Popular Tags