001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /*
021: *
022: * Copyright 2005 Sun Microsystems, Inc.
023: *
024: * Licensed under the Apache License, Version 2.0 (the "License");
025: * you may not use this file except in compliance with the License.
026: * You may obtain a copy of the License at
027: *
028: * http://www.apache.org/licenses/LICENSE-2.0
029: *
030: * Unless required by applicable law or agreed to in writing, software
031: * distributed under the License is distributed on an "AS IS" BASIS,
032: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
033: * See the License for the specific language governing permissions and
034: * limitations under the License.
035: *
036: */
037: package org.netbeans.modules.jdbcwizard.wizards;
038:
039: import java.awt.Component;
040: import java.io.IOException;
041: import javax.swing.JComponent;
042: import org.openide.WizardDescriptor;
043:
044: import javax.swing.event.ChangeEvent;
045: import javax.swing.event.ChangeListener;
046: import java.util.HashSet;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.NoSuchElementException;
050: import java.util.Set;
051:
052: /**
053: * A wizard iterator (sequence of panels). Used to create a wizard. Create one or more panels from
054: * template as needed too.
055: */
056: public abstract class JDBCWizardIterator implements
057: WizardDescriptor.InstantiatingIterator {
058:
059: /** Tracks index of current panel */
060: protected transient int index = 0;
061:
062: /* Set <ChangeListener> */
063: private final transient Set listeners = new HashSet(1);
064:
065: /* Contains panels to be iterated */
066: private transient WizardDescriptor.Panel[] panels = null;
067:
068: private transient WizardDescriptor wiz;
069:
070: private transient WizardDescriptor.Iterator simpleIterator;
071:
072: /**
073: *
074: *
075: */
076: public JDBCWizardIterator() {
077: }
078:
079: // If something changes dynamically (besides moving between panels),
080: // e.g. the number of panels changes in response to user input, then
081: // implement fireChangeEvent().
082:
083: /**
084: * @see org.openide.WizardDescriptor.Iterator#addChangeListener
085: */
086: public final void addChangeListener(final ChangeListener l) {
087:
088: synchronized (this .listeners) {
089: this .listeners.add(l);
090: }
091: // getSimpleIterator().addChangeListener(l);
092: }
093:
094: public int getIndex() {
095: return this .index;
096: }
097:
098: /**
099: * @see org.openide.WizardDescriptor.Iterator#current
100: */
101: public WizardDescriptor.Panel current() {
102: return this .getPanels(this .wiz)[this .index];
103: // return getSimpleIterator().current();
104: }
105:
106: /**
107: * @see org.openide.WizardDescriptor.Iterator#hasNext
108: */
109: public boolean hasNext() {
110: return this .index < this .getPanels(this .wiz).length - 1;
111: // return getSimpleIterator().hasNext();
112: }
113:
114: /**
115: * @see org.openide.WizardDescriptor.Iterator#hasPrevious
116: */
117: public boolean hasPrevious() {
118: return this .index > 0;
119: // return getSimpleIterator().hasPrevious();
120: }
121:
122: /**
123: * @see org.openide.WizardDescriptor.Iterator#name
124: */
125: public abstract String name();
126:
127: /**
128: * @see org.openide.WizardDescriptor.Iterator#nextPanel
129: */
130: public void nextPanel() {
131: if (!this .hasNext()) {
132: throw new NoSuchElementException();
133: }
134:
135: this .index++;
136: // getSimpleIterator().nextPanel();
137: }
138:
139: /**
140: * @see org.openide.WizardDescriptor.Iterator#previousPanel
141: */
142: public void previousPanel() {
143: if (!this .hasPrevious()) {
144: throw new NoSuchElementException();
145: }
146:
147: this .index--;
148: // getSimpleIterator().previousPanel();
149: }
150:
151: /**
152: * @see org.openide.WizardDescriptor.Iterator#removeChangeListener
153: */
154: public final void removeChangeListener(final ChangeListener l) {
155: synchronized (this .listeners) {
156: this .listeners.remove(l);
157: }
158: // getSimpleIterator().removeChangeListener(l);
159: }
160:
161: /**
162: * Creates list of panels to be displayed.
163: *
164: * @return List of panels
165: */
166: protected abstract List createPanels(WizardDescriptor wiz);
167:
168: /**
169: * Creates array of step descriptions
170: *
171: * @return array of Strings representing task summaries for each panel
172: */
173: protected abstract String[] createSteps();
174:
175: /**
176: * Gets panels to be displayed.
177: *
178: * @return array of WizardDescriptor.Panel objects
179: */
180: protected final WizardDescriptor.Panel[] getPanels(
181: final WizardDescriptor wiz) {
182: if (this .panels == null) {
183: final List myPanels = this .createPanels(wiz);
184:
185: final WizardDescriptor.Panel[] pnlArray = new WizardDescriptor.Panel[myPanels
186: .size()];
187: this .panels = (WizardDescriptor.Panel[]) myPanels
188: .toArray(pnlArray);
189: }
190: return this .panels;
191: }
192:
193: /**
194: * Gets list of steps corresponding to each panel
195: *
196: * @return array of Strings summarizing the task in each panel
197: */
198: protected final String[] getSteps() {
199: return this .createSteps();
200: }
201:
202: public void initialize(final WizardDescriptor wiz) {
203: this .panels = this .getPanels(wiz);
204: this .wiz = wiz;
205:
206: final Object prop = wiz.getProperty("WizardPanel_contentData"); // NOI18N
207: if (prop != null && prop instanceof String[]) {
208: }
209: final String[] steps = this .createSteps();
210: for (int i = 0; i < this .panels.length; i++) {
211: final Component c = this .panels[i].getComponent();
212: if (steps[i] == null) {
213: // Default step name to component name of panel.
214: // Mainly useful for getting the name of the target
215: // chooser to appear in the list of steps.
216: steps[i] = c.getName();
217: }
218: if (c instanceof JComponent) { // assume Swing components
219: final JComponent jc = (JComponent) c;
220: // Step #.
221: jc.putClientProperty(
222: "WizardPanel_contentSelectedIndex", Integer
223: .valueOf(String.valueOf(i))); // NOI18N
224: // Step name (actually the whole list for reference).
225: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
226: }
227: }
228: }
229:
230: public void uninitialize(final WizardDescriptor wiz) {
231: this .wiz = null;
232: this .panels = null;
233: }
234:
235: public Set instantiate() throws IOException {
236: return new HashSet();
237: }
238:
239: protected WizardDescriptor.Iterator getSimpleIterator() {
240: if (simpleIterator == null) {
241: assert (panels != null) && (panels.length > 0);
242: simpleIterator = new WizardDescriptor.ArrayIterator(panels);
243:
244: }
245:
246: return simpleIterator;
247: }
248: }
|