KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SCardLayout


1 /*
2  * $Id: SCardLayout.java,v 1.4 2004/12/01 07:54:06 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 /**
20  * This is a card layout
21  *
22  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
23  * @version $Revision: 1.4 $
24  */

25 public class SCardLayout
26         extends SAbstractLayoutManager {
27     protected HashMap JavaDoc tab = new HashMap JavaDoc();
28
29     /**
30      * Creates a new card layout
31      */

32     public SCardLayout() {
33     }
34
35     public void addComponent(SComponent c, Object JavaDoc constraint, int index) {
36         if (tab.size() > 0)
37             c.setVisible(false);
38         tab.put((constraint != null) ? constraint : c.getName(), c);
39     }
40
41     public void removeComponent(SComponent c) {
42         for (Iterator JavaDoc e = tab.keySet().iterator(); e.hasNext();) {
43             Object JavaDoc key = e.next();
44             if (tab.get(key) == c) {
45                 tab.remove(key);
46                 return;
47             }
48         }
49     }
50
51     /**
52      * Make sure that the Container really has a CardLayout installed.
53      * Otherwise havoc can ensue!
54      */

55     void checkLayout(SContainer parent) {
56         if (parent.getLayout() != this) {
57             throw new IllegalArgumentException JavaDoc("wrong parent for CardLayout");
58         }
59     }
60
61     /**
62      * Flips to the first card of the container.
63      *
64      * @param parent the name of the parent container
65      * in which to do the layout.
66      */

67     public void first(SContainer parent) {
68         checkLayout(parent);
69         for (int i = 0; i < parent.getComponentCount(); i++)
70             if (i > 0)
71                 parent.getComponent(i).setVisible(false);
72             else
73                 parent.getComponent(i).setVisible(true);
74     }
75
76     /**
77      * Flips to the next card of the specified container. If the
78      * currently visible card is the last one, this method flips to the
79      * first card in the layout.
80      *
81      * @param parent the name of the parent container
82      * in which to do the layout.
83      */

84     public void next(SContainer parent) {
85         checkLayout(parent);
86         int ncomponents = parent.getComponentCount();
87         for (int i = 0; i < ncomponents; i++) {
88             SComponent comp = parent.getComponent(i);
89             if (comp.isVisible()) {
90                 comp.setVisible(false);
91                 comp = parent.getComponent((i + 1 < ncomponents) ? i + 1 : 0);
92                 comp.setVisible(true);
93                 return;
94             }
95         }
96     }
97
98     /**
99      * Flips to the previous card of the specified container. If the
100      * currently visible card is the first one, this method flips to the
101      * last card in the layout.
102      *
103      * @param parent the name of the parent container
104      * in which to do the layout.
105      */

106     public void previous(SContainer parent) {
107         checkLayout(parent);
108         int ncomponents = parent.getComponentCount();
109         for (int i = 0; i < ncomponents; i++) {
110             SComponent comp = parent.getComponent(i);
111             if (comp.isVisible()) {
112                 comp.setVisible(false);
113                 comp = parent.getComponent((i > 0) ? i - 1 : ncomponents - 1);
114                 comp.setVisible(true);
115                 return;
116             }
117         }
118     }
119
120     /**
121      * Flips to the last card of the container.
122      *
123      * @param parent the name of the parent container
124      * in which to do the layout.
125      */

126     public void last(SContainer parent) {
127         checkLayout(parent);
128         int ncomponents = parent.getComponentCount();
129         for (int i = 0; i < ncomponents; i++) {
130             if (i < ncomponents - 1)
131                 parent.getComponent(i).setVisible(false);
132             else
133                 parent.getComponent(i).setVisible(true);
134         }
135     }
136
137
138     /**
139      * Flips to the component
140      */

141     public void show(SComponent comp) {
142         for (Iterator JavaDoc en = tab.values().iterator(); en.hasNext();) {
143             SComponent c = (SComponent) en.next();
144             c.setVisible(false);
145         }
146         comp.setVisible(true);
147     }
148
149     /**
150      * Flips to the component
151      */

152     public void show(Object JavaDoc constraint) {
153         SComponent visibleComponent = (SComponent) tab.get(constraint);
154         if (visibleComponent != null) {
155             for (Iterator JavaDoc en = tab.values().iterator(); en.hasNext();) {
156                 SComponent c = (SComponent) en.next();
157                 c.setVisible(false);
158             }
159             visibleComponent.setVisible(true);
160         }
161     }
162
163     /**
164      * Flips to the component that was added to this layout with the
165      * specified <code>name</code>, using <code>addLayoutComponent</code>.
166      * If no such component exists, then nothing happens.
167      *
168      * @param parent the name of the parent container
169      * in which to do the layout.
170      * @param name the component name.
171      */

172     public void show(SContainer parent, Object JavaDoc name) {
173         checkLayout(parent);
174         SComponent next = (SComponent) tab.get(name);
175         if ((next != null) && !next.isVisible()) {
176             for (int i = 0; i < parent.getComponentCount(); i++)
177                 parent.getComponent(i).setVisible(false);
178             next.setVisible(true);
179         }
180     }
181
182
183     public SComponent getVisibleComponent() {
184         for (Iterator JavaDoc en = tab.values().iterator(); en.hasNext();) {
185             SComponent c = (SComponent) en.next();
186             if (c.isVisible())
187                 return c;
188         }
189         return null;
190     }
191
192
193     public Object JavaDoc getVisibleConstraint() {
194         for (Iterator JavaDoc en = tab.keySet().iterator(); en.hasNext();) {
195             Object JavaDoc constraint = en.next();
196             SComponent c = (SComponent) tab.get(constraint);
197             if (c.isVisible())
198                 return constraint;
199         }
200         return null;
201     }
202 }
203
204
205
Popular Tags