KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > j2seimport > ui > BasicWizardIterator


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.netbeans.modules.projectimport.j2seimport.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27 import org.openide.WizardDescriptor;
28 import org.openide.util.HelpCtx;
29 import org.openide.util.NbBundle;
30
31 /**
32  * Convenient class for implementing {@link org.openide.WizardDescriptor.InstantiatingIterator}.
33  *
34  * @author Radek Matous
35  */

36 public abstract class BasicWizardIterator implements WizardDescriptor.InstantiatingIterator {
37     protected BasicWizardIterator.BasicDataModel data;
38     private int position = 0;
39     private BasicWizardIterator.PrivateWizardPanel[] wizardPanels;
40     
41     /** Create a new wizard iterator. */
42     protected BasicWizardIterator() {
43     }
44     
45     /** @return all panels provided by this {@link org.openide.WizardDescriptor.InstantiatingIterator} */
46     protected abstract BasicWizardIterator.Panel[] createPanels(WizardDescriptor wiz);
47     
48     protected abstract String JavaDoc getTitle();
49     
50     /** Basic visual panel.*/
51     public abstract static class Panel extends BasicVisualPanel {
52         
53         protected Panel(WizardDescriptor wiz) {
54             super(wiz);
55         }
56         
57         /**
58          * Returned name is used by a wizard. e.g. on its left side in the
59          * <em>step list</em>.
60          * @return name of panel
61          */

62         protected abstract String JavaDoc getPanelName();
63         
64         /**
65          * Gives a chance to store an instance of {@link
66          * BasicWizardIterator.BasicDataModel}. It is called when a panel is
67          * going to be <em>hidden</em> (e.g. when switching to next/previous
68          * panel).
69          */

70         protected abstract void storeToDataModel();
71         
72         /**
73          * Gives a chance to refresh a panel (usually by reading a state of an
74          * instance of {@link BasicWizardIterator.BasicDataModel}. It is called
75          * when a panel is going to be <em>displayed</em> (e.g. when switching
76          * from next/previous panel).
77          */

78         protected abstract void readFromDataModel();
79         
80         protected abstract HelpCtx getHelp();
81         
82     }
83     
84     /** DataModel that is passed through individual panels.*/
85     public static class BasicDataModel {
86     }
87     
88     public final void initialize(WizardDescriptor wiz) {
89         // mkleint: copied from the NewJavaFileWizardIterator.. there must be something painfully wrong..
90
wiz.setTitleFormat(new java.text.MessageFormat JavaDoc("{0}")); // NOI18N
91
wiz.setTitle(getTitle()); // NOI18N
92
String JavaDoc[] beforeSteps = null;
93         Object JavaDoc prop = wiz.getProperty("WizardPanel_contentData"); // NOI18N
94
if (prop != null && prop instanceof String JavaDoc[]) {
95             beforeSteps = (String JavaDoc[])prop;
96         }
97         position = 0;
98         BasicWizardIterator.Panel[] panels = createPanels(wiz);
99         String JavaDoc[] steps = BasicWizardIterator.createSteps(beforeSteps, panels);
100         wizardPanels = new BasicWizardIterator.PrivateWizardPanel[panels.length];
101         
102         for (int i = 0; i < panels.length; i++) {
103             wizardPanels[i] = new BasicWizardIterator.PrivateWizardPanel(panels[i], steps, i);
104         }
105     }
106     
107     // mkleint: copied from the NewJavaFileWizardIterator.. there must be something painfully wrong..
108
private static String JavaDoc[] createSteps(String JavaDoc[] before, BasicWizardIterator.Panel[] panels) {
109         assert panels != null;
110         // hack to use the steps set before this panel processed
111
int diff = 0;
112         if (before == null) {
113             before = new String JavaDoc[0];
114         } else if (before.length > 0) {
115             diff = ("...".equals(before[before.length - 1])) ? 1 : 0; // NOI18N
116
}
117         String JavaDoc[] res = new String JavaDoc[ (before.length - diff) + panels.length];
118         for (int i = 0; i < res.length; i++) {
119             if (i < (before.length - diff)) {
120                 res[i] = before[i];
121             } else {
122                 res[i] = panels[i - before.length + diff].getPanelName();
123             }
124         }
125         return res;
126     }
127     
128     public final void uninitialize(WizardDescriptor wiz) {
129         wizardPanels = null;
130         uninitialize();
131     }
132     
133     public void uninitialize() {
134         wizardPanels = null;
135     }
136     
137     public String JavaDoc name() {
138         return ((BasicWizardIterator.PrivateWizardPanel)
139         current()).getPanel().getPanelName();
140     }
141     
142     public boolean hasNext() {
143         return position < (wizardPanels.length - 1);
144     }
145     
146     public boolean hasPrevious() {
147         return position > 0;
148     }
149     
150     public void nextPanel() {
151         if (!hasNext()) {
152             throw new NoSuchElementException JavaDoc();
153         }
154         position++;
155     }
156     
157     public void previousPanel() {
158         if (!hasPrevious()) {
159             throw new NoSuchElementException JavaDoc();
160         }
161         position--;
162     }
163     
164     public WizardDescriptor.Panel current() {
165         return wizardPanels[position];
166     }
167     
168     /**
169      * Convenience method for accessing Bundle resources from this package.
170      */

171     protected final String JavaDoc getMessage(String JavaDoc key) {
172         return NbBundle.getMessage(getClass(), key);
173     }
174     
175     public final void addChangeListener(ChangeListener JavaDoc l) {}
176     public final void removeChangeListener(ChangeListener JavaDoc l) {}
177     
178
179     
180     private static final class PrivateWizardPanel extends BasicWizardPanel {
181         
182         private BasicWizardIterator.Panel panel;
183         
184         PrivateWizardPanel(BasicWizardIterator.Panel panel, String JavaDoc[] allSteps, int stepIndex) {
185             super(panel.getSettings());
186             panel.addPropertyChangeListener(this);
187             panel.setName(panel.getPanelName()); // NOI18N
188
this.panel = panel;
189             panel.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(stepIndex)); // NOI18N
190
// names of currently used steps
191
panel.putClientProperty("WizardPanel_contentData", allSteps); // NOI18N
192
panel.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
193
panel.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
194
panel.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
195
}
196         
197         private BasicWizardIterator.Panel getPanel() {
198             return panel;
199         }
200         
201         public Component JavaDoc getComponent() {
202             return getPanel();
203         }
204         
205         public void storeSettings(Object JavaDoc settings) {
206             WizardDescriptor wiz = (WizardDescriptor) settings;
207             if (WizardDescriptor.NEXT_OPTION.equals(wiz.getValue()) ||
208                     WizardDescriptor.FINISH_OPTION.equals(wiz.getValue())) {
209                 panel.storeToDataModel();
210             }
211         }
212         
213         public void readSettings(Object JavaDoc settings) {
214             WizardDescriptor wiz = (WizardDescriptor) settings;
215             
216             if (WizardDescriptor.NEXT_OPTION.equals(wiz.getValue()) || wiz.getValue() == null) {
217                 panel.readFromDataModel();
218             }
219         }
220         
221         public HelpCtx getHelp() {
222             return getPanel().getHelp();
223         }
224         
225     }
226
227     public void setData(BasicWizardIterator.BasicDataModel data) {
228         this.data = data;
229     }
230         
231     public BasicWizardIterator.BasicDataModel getData() {
232         return data;
233     }
234
235     final public Set JavaDoc instantiate() throws IOException JavaDoc {
236         return null;
237     }
238 }
239
240
Popular Tags