001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.swing;
024:
025: import java.awt.Component;
026: import java.awt.Container;
027: import java.awt.Dimension;
028: import java.awt.Insets;
029: import java.awt.LayoutManager;
030: import java.awt.Rectangle;
031: import java.util.Vector;
032:
033: import javax.swing.JPanel;
034: import javax.swing.event.ChangeEvent;
035: import javax.swing.event.ChangeListener;
036:
037: /**
038: * Enhanced version of a container using a variation of the CardLayout.
039: * <p>
040: * This panel simplifies creating a container that supports a cardLayout in a more intuitive manner.
041: *
042: * @author Mark A. Kobold.
043: * @see java.awt.CardLayout
044: * @version 1.0
045: */
046: public class WizardPanel extends JPanel {
047:
048: private static final Insets insets = new Insets(10, 10, 10, 10);
049: private static final long serialVersionUID = -4942953182059358257L;
050: private transient Vector<ChangeListener> changeListeners;
051:
052: /**
053: * Default constructor for creating a card panel.
054: * <p>
055: */
056: public WizardPanel() {
057:
058: super (true);
059: super .setLayout(new Layout());
060: }
061:
062: /**
063: * @param name
064: * @return
065: */
066: public Component getCard(String name) {
067:
068: int nChildren = getComponentCount();
069: for (int i = 0; i < nChildren; i++) {
070: Component child = getComponent(i);
071: if (name.equals(child.getName())) {
072: return child;
073: }
074: }
075: return null;
076: }
077:
078: /**
079: * @param card
080: */
081: public void showCard(Component card) {
082:
083: if (card.getParent() != this ) {
084: add(card);
085: }
086: int index = getVisibleChildIndex();
087: if (index != -1) {
088: getComponent(index).setVisible(false);
089: }
090: card.setVisible(true);
091: fireStateChanged(new ChangeEvent(this ));
092: revalidate();
093: repaint();
094: }
095:
096: /**
097: * Show the card with the specified name.
098: *
099: * @see java.awt.Component#getName
100: */
101: /**
102: * @param name
103: */
104: public void showCard(String name) {
105:
106: int nChildren = getComponentCount();
107: for (int i = 0; i < nChildren; i++) {
108: Component child = getComponent(i);
109: if (child.getName().equals(name)) {
110: showCard(child);
111: break;
112: }
113: }
114: }
115:
116: /**
117: *
118: */
119: public void showNextCard() {
120:
121: if (getComponentCount() <= 0) {
122: return;
123: }
124: try {
125: String card = getCurrentCard();
126: int index = getVisibleChildIndex();
127: if (index >= getComponentCount() - 1) {
128: showLastCard();
129: } else {
130: card = getComponent(++index).getName();
131: showCard(card);
132: }
133: } catch (Throwable t) {
134: }
135: }
136:
137: /**
138: *
139: */
140: public void showPreviousCard() {
141:
142: if (getComponentCount() <= 0) {
143: return;
144: }
145: try {
146: String card = getCurrentCard();
147: int index = getVisibleChildIndex();
148: if (index >= 1) {
149: card = getComponent(--index).getName();
150: showCard(card);
151: } else {
152: showFirstCard();
153: }
154: } catch (Throwable t) {
155: }
156: }
157:
158: /**
159: * @return
160: */
161: public String getCurrentCard() {
162:
163: try {
164: return getComponent(getVisibleChildIndex()).getName();
165: } catch (Exception e) {
166: return "";
167: }
168: }
169:
170: /**
171: * @param name
172: * @return
173: */
174: public Component removeCard(String name) {
175:
176: int nChildren = getComponentCount();
177: for (int i = 0; i < nChildren; i++) {
178: Component child = getComponent(i);
179: if (child.getName().equals(name)) {
180: remove(child);
181: return child;
182: }
183: }
184: return null;
185: }
186:
187: /**
188: *
189: */
190: public void showFirstCard() {
191:
192: if (getComponentCount() <= 0) {
193: return;
194: }
195: try {
196: String card = getComponent(0).getName();
197: showCard(card);
198: } catch (Throwable t) {
199: }
200: }
201:
202: /**
203: *
204: */
205: public void showLastCard() {
206:
207: if (getComponentCount() <= 0) {
208: return;
209: }
210: try {
211: String card = getComponent(getComponentCount() - 1)
212: .getName();
213: showCard(card);
214: } catch (Throwable t) {
215: }
216: }
217:
218: /**
219: * @param l
220: */
221: public synchronized void removeChangeListener(ChangeListener l) {
222:
223: if (changeListeners != null && changeListeners.contains(l)) {
224: Vector v = changeListeners;
225: v.removeElement(l);
226: }
227: }
228:
229: /**
230: * @param l
231: */
232: public synchronized void addChangeListener(ChangeListener l) {
233:
234: Vector<ChangeListener> v = changeListeners == null ? new Vector<ChangeListener>(
235: 2)
236: : changeListeners;
237: if (!v.contains(l)) {
238: v.addElement(l);
239: changeListeners = v;
240: }
241: }
242:
243: @Override
244: public void setLayout(LayoutManager mgr) {
245:
246: }
247:
248: protected void fireStateChanged(ChangeEvent e) {
249:
250: if (changeListeners != null) {
251: Vector listeners = changeListeners;
252: int count = listeners.size();
253: for (int i = 0; i < count; i++) {
254: ((ChangeListener) listeners.elementAt(i))
255: .stateChanged(e);
256: }
257: }
258: }
259:
260: protected int getVisibleChildIndex() {
261:
262: int nChildren = getComponentCount();
263: for (int i = 0; i < nChildren; i++) {
264: Component child = getComponent(i);
265: if (child.isVisible()) {
266: return i;
267: }
268: }
269: return -1;
270: }
271:
272: public static class Layout implements LayoutManager {
273:
274: public void addLayoutComponent(String name, Component child) {
275:
276: if (name != null) {
277: child.setName(name);
278: }
279: child
280: .setVisible(child.getParent().getComponentCount() == 1);
281: }
282:
283: public void removeLayoutComponent(Component child) {
284:
285: if (child.isVisible()) {
286: Container parent = child.getParent();
287: if (parent.getComponentCount() > 0) {
288: parent.getComponent(0).setVisible(true);
289: layoutContainer(parent);
290: }
291: }
292: }
293:
294: public Dimension preferredLayoutSize(Container parent) {
295:
296: int nChildren = parent.getComponentCount();
297: int width = insets.left + insets.right;
298: int height = insets.top + insets.bottom;
299: for (int i = 0; i < nChildren; i++) {
300: Dimension d = parent.getComponent(i).getPreferredSize();
301: if (d.width > width) {
302: width = d.width;
303: }
304: if (d.height > height) {
305: height = d.height;
306: }
307: }
308: return new Dimension(width, height);
309: }
310:
311: public Dimension minimumLayoutSize(Container parent) {
312:
313: int nChildren = parent.getComponentCount();
314: int width = insets.left + insets.right;
315: int height = insets.top + insets.bottom;
316: for (int i = 0; i < nChildren; i++) {
317: Dimension d = parent.getComponent(i).getMinimumSize();
318: if (d.width > width) {
319: width = d.width;
320: }
321: if (d.height > height) {
322: height = d.height;
323: }
324: }
325: return new Dimension(width, height);
326: }
327:
328: public void layoutContainer(Container parent) {
329:
330: int nChildren = parent.getComponentCount();
331: for (int i = 0; i < nChildren; i++) {
332: Component child = parent.getComponent(i);
333: if (child.isVisible()) {
334: Rectangle r = parent.getBounds();
335: int width = r.width - (insets.left + insets.right);
336: int height = r.height
337: - (insets.top + insets.bottom);
338: child.setBounds(insets.left, insets.top, width,
339: height);
340: break;
341: }
342: }
343: }
344: }
345:
346: }
|