KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > platform > wizard > PlatformInstallIterator


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.java.platform.wizard;
21
22 import java.io.IOException JavaDoc;
23 import java.util.*;
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.event.*;
26 import org.netbeans.modules.java.platform.InstallerRegistry;
27 import org.netbeans.spi.java.platform.CustomPlatformInstall;
28 import org.netbeans.spi.java.platform.GeneralPlatformInstall;
29 import org.netbeans.spi.java.platform.PlatformInstall;
30
31 import org.openide.loaders.*;
32 import org.openide.util.NbBundle;
33 import org.openide.WizardDescriptor;
34
35 /**
36  *
37  * @author sd99038
38  */

39 public class PlatformInstallIterator implements WizardDescriptor.InstantiatingIterator, ChangeListener {
40     
41     WizardDescriptor.InstantiatingIterator typeIterator;
42     int panelIndex; // -1 - not set, 0 - the first panel, 1 - files chooser, 2 - custom panel from PlatformInstall, 3 - custom panel from CustomPlatformInstall
43
boolean hasSelectorPanel;
44     WizardDescriptor wizard;
45     int panelNumber = -1;
46
47     ResourceBundle bundle = NbBundle.getBundle(PlatformInstallIterator.class);
48     LocationChooser.Panel locationPanel = new LocationChooser.Panel();
49     SelectorPanel.Panel selectorPanel = new SelectorPanel.Panel ();
50     Collection listeners = new ArrayList();
51     
52     PlatformInstallIterator() {
53         selectorPanel.addChangeListener(this);
54         locationPanel.addChangeListener(this);
55     }
56     
57     public static PlatformInstallIterator create() {
58         return new PlatformInstallIterator();
59     }
60     
61     
62     /**
63      * Used by unit tests
64      * Returns the current state of the wizard iterator
65      */

66     int getPanelIndex () {
67         return this.panelIndex;
68     }
69     
70     void updatePanelsList (JComponent JavaDoc[] where) {
71         Collection c = new LinkedList();
72         if (this.hasSelectorPanel) {
73             c.add (bundle.getString("TXT_SelectPlatformTypeTitle"));
74         }
75         if (this.panelIndex == 1 || this.panelIndex == 2 ||
76             (this.panelIndex == 0 && this.selectorPanel.getInstallerIterator()==null)) {
77             c.add(bundle.getString("TXT_PlatformFolderTitle")); // NOI18N
78
}
79         if (typeIterator != null) {
80             // try to suck stuff out of the iterator's first panel :-(
81
WizardDescriptor.Panel p = typeIterator.current();
82             if (p != null) {
83                 javax.swing.JComponent JavaDoc pc = (javax.swing.JComponent JavaDoc)p.getComponent();
84                 String JavaDoc[] steps = (String JavaDoc[])pc.getClientProperty("WizardPanel_contentData"); // NOI18N
85
if (steps != null)
86                     c.addAll(Arrays.asList(steps));
87             }
88         } else {
89             c.add(bundle.getString("TITLE_PlatformLocationUnknown")); // NOI18N
90
}
91         String JavaDoc[] names = (String JavaDoc[])c.toArray(new String JavaDoc[c.size()]);
92         for (int i=0; i< where.length; i++) {
93             where[i].putClientProperty("WizardPanel_contentData",names); // NOI18N
94
}
95     }
96     
97     public void addChangeListener(ChangeListener l) {
98         listeners.add(l);
99     }
100     
101     public WizardDescriptor.Panel current() {
102         if (panelIndex == 0) {
103             return selectorPanel;
104         }
105         else if (panelIndex == 1) {
106             return locationPanel;
107         } else {
108             return typeIterator.current();
109         }
110     }
111  
112     /**
113      * The overall iterator has the next panel iff:
114      * - the current panel is the location chooser && the chooser has an iterator
115      * selected && that iterator has at least one panel
116      * - the current iterator reports it has the next panel
117      */

118     public boolean hasNext() {
119         if (panelIndex == 0) {
120             WizardDescriptor.InstantiatingIterator typeIt = this.selectorPanel.getInstallerIterator();
121             // need to decide
122
if (typeIt == null) {
123                 return true;
124             }
125             else {
126                 return typeIt.current() != null;
127             }
128         }
129         else if (panelIndex == 1) {
130             WizardDescriptor.InstantiatingIterator typeIt = locationPanel.getInstallerIterator();
131             if (typeIt == null) {
132                 return false;
133             } else {
134                 WizardDescriptor.Panel p = typeIt.current();
135                 return p != null;
136             }
137         } else {
138             return this.typeIterator.hasNext();
139         }
140     }
141     
142     public boolean hasPrevious() {
143         return this.panelIndex != 0 &&
144              !(this.panelIndex == 1 && !hasSelectorPanel) &&
145              !(this.panelIndex == 3 && !hasSelectorPanel && this.typeIterator != null && !this.typeIterator.hasPrevious());
146     }
147     
148     public void initialize(WizardDescriptor wiz) {
149         this.wizard = wiz;
150         List installers = InstallerRegistry.getDefault().getAllInstallers();
151         if (installers.size()>1) {
152             panelIndex = 0;
153             hasSelectorPanel = true;
154         }
155         else {
156             if (installers.get(0) instanceof CustomPlatformInstall) {
157                 panelIndex = 3;
158                 hasSelectorPanel = false;
159                 this.typeIterator = ((CustomPlatformInstall) installers.get(0)).createIterator();
160             }
161             else {
162                 panelIndex = 1;
163                 hasSelectorPanel = false;
164                 this.locationPanel.setPlatformInstall((PlatformInstall) installers.get(0));
165             }
166         }
167         updatePanelsList(new JComponent JavaDoc[]{((JComponent JavaDoc)current().getComponent())});
168         this.wizard.setTitle(NbBundle.getMessage(PlatformInstallIterator.class,"TXT_AddPlatformTitle"));
169         panelNumber = 0;
170         wizard.putProperty("WizardPanel_contentSelectedIndex", // NOI18N
171
new Integer JavaDoc(panelNumber));
172     }
173     
174     public java.util.Set JavaDoc instantiate() throws IOException JavaDoc {
175         return typeIterator.instantiate();
176     }
177     
178     public String JavaDoc name() {
179         if (panelIndex == 0) {
180             return bundle.getString("TXT_PlatformSelectorTitle");
181         }
182         else if (panelIndex == 1) {
183             return bundle.getString("TXT_PlatformFolderTitle");
184         } else {
185             return typeIterator.name();
186         }
187     }
188     
189     public void nextPanel() {
190         if (this.panelIndex == 0) {
191             if (this.selectorPanel.getInstallerIterator() == null) {
192                 panelIndex = 1;
193             }
194             else {
195                 panelIndex = 3;
196             }
197         } else if (panelIndex == 1) {
198             panelIndex = 2;
199         }
200         else {
201             typeIterator.nextPanel();
202         }
203         panelNumber++;
204         wizard.putProperty("WizardPanel_contentSelectedIndex", // NOI18N
205
new Integer JavaDoc(panelNumber));
206     }
207     
208     public void previousPanel() {
209         if (panelIndex == 1) {
210             panelIndex = 0;
211         }
212         else if (panelIndex == 2) {
213             if (typeIterator.hasPrevious()) {
214                 typeIterator.previousPanel();
215             } else {
216                 panelIndex = 1;
217             }
218         } else if (panelIndex == 3) {
219             if (typeIterator.hasPrevious()) {
220                 typeIterator.previousPanel();
221             } else {
222                 panelIndex = 0;
223             }
224         }
225         panelNumber--;
226         wizard.putProperty("WizardPanel_contentSelectedIndex", // NOI18N
227
new Integer JavaDoc(panelNumber));
228     }
229     
230     public void removeChangeListener(ChangeListener l) {
231         listeners.remove(l);
232     }
233     
234     public void uninitialize(WizardDescriptor wiz) {
235         if (this.typeIterator != null)
236             typeIterator.uninitialize (wiz);
237     }
238     
239     public void stateChanged(ChangeEvent e) {
240         WizardDescriptor.InstantiatingIterator it;
241         if (e.getSource() == this.locationPanel) {
242             it = locationPanel.getInstallerIterator();
243         }
244         else if (e.getSource() == this.selectorPanel) {
245             GeneralPlatformInstall installer = this.selectorPanel.getInstaller();
246             if (installer instanceof CustomPlatformInstall) {
247                 it = ((CustomPlatformInstall)installer).createIterator();
248             }
249             else {
250                 it = null;
251                 this.locationPanel.setPlatformInstall ((PlatformInstall)installer);
252             }
253         }
254         else {
255             assert false : "Unknown event source"; //NOI18N
256
return;
257         }
258         if (it != typeIterator) {
259             if (this.typeIterator != null) {
260                 this.typeIterator.uninitialize (this.wizard);
261             }
262             typeIterator = it;
263             if (this.typeIterator != null) {
264                 typeIterator.initialize (this.wizard);
265                 updatePanelsList(new JComponent JavaDoc[]{
266                     (JComponent JavaDoc)selectorPanel.getComponent(),
267                     (JComponent JavaDoc)locationPanel.getComponent(),
268                     (JComponent JavaDoc)typeIterator.current().getComponent(),
269                 });
270             }
271             else {
272                 updatePanelsList(new JComponent JavaDoc[]{
273                     (JComponent JavaDoc)selectorPanel.getComponent(),
274                     (JComponent JavaDoc)locationPanel.getComponent()
275                 });
276             }
277             wizard.putProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(panelNumber)); // NOI18N
278
}
279     }
280 }
281
Popular Tags