KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > TemplateWizardIterImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Set JavaDoc;
24 import javax.swing.event.*;
25 import org.netbeans.api.progress.ProgressHandle;
26
27 import org.openide.WizardDescriptor;
28
29 /** Implementation of template wizard's iterator that allows to
30 * delegate all functionality to another wizard.
31 *
32 * @author Jaroslav Tulach
33 */

34 class TemplateWizardIterImpl extends Object JavaDoc
35     implements WizardDescriptor.Iterator, ChangeListener {
36
37     /** iterator to delegate to */
38     private TemplateWizard.Iterator iterator;
39
40     /** bridge */
41     private ChangeListener iteratorListener;
42     
43     /** is currently panel displayed? */
44     private boolean showingPanel = false;
45     private boolean newIteratorInstalled = false;
46
47     /** Utility field used by event firing mechanism. */
48     private javax.swing.event.EventListenerList JavaDoc listenerList = null;
49     
50     /** Instance of the template wizard that uses this Iterator.
51      */

52     private TemplateWizard wizardInstance;
53     
54     /** */
55     private boolean iteratorInitialized = false;
56
57     /** Getter for the first panel.
58      * @return the first and default panel
59      */

60     private WizardDescriptor.Panel<WizardDescriptor> firstPanel () {
61         return wizardInstance.templateChooser();
62     }
63
64     /** Resets the iterator to first screen.
65     */

66     public void first () {
67         showingPanel = true;
68         fireStateChanged ();
69     }
70
71     /** Change the additional iterator.
72     */

73     public void setIterator (TemplateWizard.Iterator it, boolean notify) {
74         if ((this.iterator != null) && (iteratorInitialized)) {
75             this.iterator.removeChangeListener(iteratorListener);
76             this.iterator.uninitialize(wizardInstance);
77         }
78         it.initialize(wizardInstance);
79         iteratorListener = new ChangeListener() {
80             public void stateChanged(ChangeEvent e) {
81                 TemplateWizardIterImpl.this.stateChanged(e);
82             }
83         };
84         it.addChangeListener(iteratorListener);
85         iteratorInitialized = true;
86         
87         iterator = it;
88         if (notify) {
89             // bugfix #46589, don't call nextPanel() on new iterator
90
if (showingPanel) {
91                 newIteratorInstalled = true;
92             }
93             showingPanel = false;
94             fireStateChanged ();
95         }
96     }
97
98     /** Getter for current iterator.
99     */

100     public TemplateWizard.Iterator getIterator () {
101         if (iterator == null) {
102             // first of all initialize the default iterator
103
setIterator (wizardInstance.defaultIterator (), false);
104         }
105         if (!iteratorInitialized) {
106             if (iterator != null) {
107                 iterator.initialize(wizardInstance);
108                 iteratorInitialized = true;
109             }
110         }
111         return iterator;
112     }
113
114     /** Get the current panel.
115      * @return the panel
116      */

117     public WizardDescriptor.Panel<WizardDescriptor> current() {
118         return showingPanel ? firstPanel () : getIterator ().current ();
119     }
120
121
122     /** Get the name of the current panel.
123      * @return the name
124      */

125     public String JavaDoc name() {
126         return showingPanel ? "" : getIterator ().name (); // NOI18N
127
}
128
129     /** Test whether there is a next panel.
130      * @return <code>true</code> if so
131      */

132     public boolean hasNext() {
133         return showingPanel || getIterator ().hasNext();
134     }
135
136     /** Test whether there is a previous panel.
137      * @return <code>true</code> if so
138      */

139     public boolean hasPrevious() {
140         return !showingPanel;
141     }
142
143     /** Move to the next panel.
144      * I.e. increment its index, need not actually change any GUI itself.
145      * @exception NoSuchElementException if the panel does not exist
146      */

147     public void nextPanel() {
148         if (showingPanel || newIteratorInstalled) {
149             showingPanel = false;
150             newIteratorInstalled = false;
151         } else {
152             getIterator ().nextPanel ();
153         }
154     }
155
156     /** Move to the previous panel.
157      * I.e. decrement its index, need not actually change any GUI itself.
158      * @exception NoSuchElementException if the panel does not exist
159      */

160     public void previousPanel() {
161         if (getIterator ().hasPrevious ()) {
162             getIterator ().previousPanel ();
163         } else {
164             showingPanel = true;
165             newIteratorInstalled = false;
166         }
167     }
168
169     /** Refires the info to listeners */
170     public void stateChanged(final javax.swing.event.ChangeEvent JavaDoc p1) {
171         fireStateChanged ();
172     }
173
174     /** Registers ChangeListener to receive events.
175      *@param listener The listener to register.
176      */

177     public synchronized void addChangeListener(javax.swing.event.ChangeListener JavaDoc listener) {
178         if (listenerList == null ) {
179             listenerList = new javax.swing.event.EventListenerList JavaDoc();
180         }
181         listenerList.add (javax.swing.event.ChangeListener JavaDoc.class, listener);
182     }
183     /** Removes ChangeListener from the list of listeners.
184      *@param listener The listener to remove.
185      */

186     public synchronized void removeChangeListener(
187         javax.swing.event.ChangeListener JavaDoc listener
188     ) {
189         if (listenerList != null) {
190             listenerList.remove (javax.swing.event.ChangeListener JavaDoc.class, listener);
191         }
192     }
193     
194     public void initialize (WizardDescriptor wiz) {
195         if (!(wiz instanceof TemplateWizard)) {
196             throw new IllegalArgumentException JavaDoc ("WizardDescriptor must be instance of TemplateWizard, but is " + wiz); // NOI18N
197
}
198         this.wizardInstance = (TemplateWizard)wiz;
199         if (wizardInstance.getTemplate () == null) {
200             showingPanel = true;
201         } else {
202             newIteratorInstalled = false;
203             showingPanel = false;
204         }
205         TemplateWizard.Iterator it = iterator;
206     if ((it != null)&&(!iteratorInitialized)) {
207         it.initialize(wizardInstance);
208             iteratorInitialized = true;
209     }
210     }
211     
212     public void uninitialize() {
213     if (iterator != null && wizardInstance != null) {
214         iterator.uninitialize(wizardInstance);
215             iteratorInitialized = false;
216     }
217         showingPanel = true;
218     }
219     
220     public void uninitialize (WizardDescriptor wiz) {
221         uninitialize ();
222     }
223     
224     public Set JavaDoc<DataObject> instantiate () throws IOException JavaDoc {
225         assert wizardInstance != null : "wizardInstance cannot be null when instantiate() called."; // NOI18N
226
return wizardInstance.instantiateNewObjects (null);
227     }
228     
229     public Set JavaDoc<DataObject> instantiate (ProgressHandle handle) throws IOException JavaDoc {
230         assert wizardInstance != null : "wizardInstance cannot be null when instantiate() called."; // NOI18N
231
return wizardInstance.instantiateNewObjects (handle);
232     }
233     
234     /** Notifies all registered listeners about the event.
235      *
236      *@param param1 Parameter #1 of the <CODE>ChangeEvent<CODE> constructor.
237      */

238     private void fireStateChanged() {
239         if (listenerList == null)
240             return;
241         
242         javax.swing.event.ChangeEvent JavaDoc e = null;
243         Object JavaDoc[] listeners = listenerList.getListenerList ();
244         for (int i = listeners.length-2; i>=0; i-=2) {
245             if (listeners[i]==javax.swing.event.ChangeListener JavaDoc.class) {
246                 if (e == null)
247                     e = new javax.swing.event.ChangeEvent JavaDoc (this);
248                 ((javax.swing.event.ChangeListener JavaDoc)listeners[i+1]).stateChanged (e);
249             }
250         }
251     }
252
253 }
254
255
Popular Tags