001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import java.util.HashMap;
016: import java.util.Iterator;
017:
018: /**
019: * Swing-like card layout.
020: *
021: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
022: */
023: public class SCardLayout extends SAbstractLayoutManager {
024: protected HashMap tab = new HashMap();
025:
026: /**
027: * Creates a new card layout
028: */
029: public SCardLayout() {
030: }
031:
032: public void addComponent(SComponent c, Object constraint, int index) {
033: if (tab.size() > 0)
034: c.setVisible(false);
035: tab.put((constraint != null) ? constraint : c.getName(), c);
036: }
037:
038: public void removeComponent(SComponent c) {
039: for (Iterator e = tab.keySet().iterator(); e.hasNext();) {
040: Object key = e.next();
041: if (tab.get(key) == c) {
042: // if removing the current visible element fall back to previous one
043: if (c.isVisible() && (c.getParent() != null)) {
044: next(c.getParent());
045: }
046: tab.remove(key);
047: return;
048: }
049: }
050: }
051:
052: /**
053: * Make sure that the Container really has a CardLayout installed.
054: * Otherwise havoc can ensue!
055: */
056: void checkLayout(SContainer parent) {
057: if (parent.getLayout() != this ) {
058: throw new IllegalArgumentException(
059: "wrong parent for CardLayout");
060: }
061: }
062:
063: /**
064: * Flips to the first card of the container.
065: *
066: * @param parent the name of the parent container
067: * in which to do the layout.
068: */
069: public void first(SContainer parent) {
070: checkLayout(parent);
071: for (int i = 0; i < parent.getComponentCount(); i++)
072: if (i > 0)
073: parent.getComponent(i).setVisible(false);
074: else
075: parent.getComponent(i).setVisible(true);
076: }
077:
078: /**
079: * Flips to the next card of the specified container. If the
080: * currently visible card is the last one, this method flips to the
081: * first card in the layout.
082: *
083: * @param parent the name of the parent container
084: * in which to do the layout.
085: */
086: public void next(SContainer parent) {
087: checkLayout(parent);
088: int ncomponents = parent.getComponentCount();
089: for (int i = 0; i < ncomponents; i++) {
090: SComponent comp = parent.getComponent(i);
091: if (comp.isVisible()) {
092: comp.setVisible(false);
093: comp = parent
094: .getComponent((i + 1 < ncomponents) ? i + 1 : 0);
095: comp.setVisible(true);
096: return;
097: }
098: }
099: }
100:
101: /**
102: * Flips to the previous card of the specified container. If the
103: * currently visible card is the first one, this method flips to the
104: * last card in the layout.
105: *
106: * @param parent the name of the parent container
107: * in which to do the layout.
108: */
109: public void previous(SContainer parent) {
110: checkLayout(parent);
111: int ncomponents = parent.getComponentCount();
112: for (int i = 0; i < ncomponents; i++) {
113: SComponent comp = parent.getComponent(i);
114: if (comp.isVisible()) {
115: comp.setVisible(false);
116: comp = parent.getComponent((i > 0) ? i - 1
117: : ncomponents - 1);
118: comp.setVisible(true);
119: return;
120: }
121: }
122: }
123:
124: /**
125: * Flips to the last card of the container.
126: *
127: * @param parent the name of the parent container
128: * in which to do the layout.
129: */
130: public void last(SContainer parent) {
131: checkLayout(parent);
132: int ncomponents = parent.getComponentCount();
133: for (int i = 0; i < ncomponents; i++) {
134: if (i < ncomponents - 1)
135: parent.getComponent(i).setVisible(false);
136: else
137: parent.getComponent(i).setVisible(true);
138: }
139: }
140:
141: /**
142: * Flips to the component
143: */
144: public void show(SComponent comp) {
145: for (Iterator en = tab.values().iterator(); en.hasNext();) {
146: SComponent c = (SComponent) en.next();
147: c.setVisible(false);
148: }
149: comp.setVisible(true);
150: }
151:
152: /**
153: * Flips to the component
154: */
155: public void show(Object constraint) {
156: SComponent visibleComponent = (SComponent) tab.get(constraint);
157: if (visibleComponent != null) {
158: for (Iterator en = tab.values().iterator(); en.hasNext();) {
159: SComponent c = (SComponent) en.next();
160: c.setVisible(false);
161: }
162: visibleComponent.setVisible(true);
163: }
164: }
165:
166: /**
167: * Flips to the component that was added to this layout with the
168: * specified <code>name</code>, using <code>addLayoutComponent</code>.
169: * If no such component exists, then nothing happens.
170: *
171: * @param parent the name of the parent container
172: * in which to do the layout.
173: * @param name the component name.
174: */
175: public void show(SContainer parent, Object name) {
176: checkLayout(parent);
177: SComponent next = (SComponent) tab.get(name);
178: if ((next != null) && !next.isVisible()) {
179: for (int i = 0; i < parent.getComponentCount(); i++)
180: parent.getComponent(i).setVisible(false);
181: next.setVisible(true);
182: }
183: }
184:
185: public SComponent getVisibleComponent() {
186: for (Iterator en = tab.values().iterator(); en.hasNext();) {
187: SComponent c = (SComponent) en.next();
188: if (c.isVisible())
189: return c;
190: }
191: return null;
192: }
193:
194: public Object getVisibleConstraint() {
195: for (Iterator en = tab.keySet().iterator(); en.hasNext();) {
196: Object constraint = en.next();
197: SComponent c = (SComponent) tab.get(constraint);
198: if (c.isVisible())
199: return constraint;
200: }
201: return null;
202: }
203: }
|