KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > ui > NewJ2SEFreeformProjectWizardIterator


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.freeform.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.api.java.project.JavaProjectConstants;
33 import org.netbeans.api.progress.ProgressHandle;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectManager;
36 import org.netbeans.api.project.ProjectUtils;
37 import org.netbeans.api.project.SourceGroup;
38 import org.netbeans.api.project.Sources;
39 import org.netbeans.modules.ant.freeform.spi.TargetDescriptor;
40 import org.netbeans.modules.ant.freeform.spi.support.NewFreeformProjectSupport;
41 import org.netbeans.modules.java.freeform.spi.support.NewJavaFreeformProjectSupport;
42 import org.netbeans.spi.project.support.ant.AntProjectHelper;
43 import org.netbeans.spi.project.ui.support.ProjectChooser;
44 import org.openide.WizardDescriptor;
45 import org.openide.filesystems.FileObject;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.util.NbBundle;
48
49 /**
50  * @author David Konecny
51  */

52 public class NewJ2SEFreeformProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator {
53
54     public static final String JavaDoc PROP_PROJECT_MODEL = "projectModel"; // <List> NOI18N
55

56     private static final long serialVersionUID = 1L;
57     
58     private transient int index;
59     private transient WizardDescriptor.Panel[] panels;
60     private transient WizardDescriptor wiz;
61     
62     public NewJ2SEFreeformProjectWizardIterator() {
63     }
64     
65     private WizardDescriptor.Panel[] createPanels () {
66         return new WizardDescriptor.Panel[] {
67                 NewFreeformProjectSupport.createBasicProjectInfoWizardPanel(),
68                 NewFreeformProjectSupport.createTargetMappingWizardPanel(new ArrayList JavaDoc<TargetDescriptor>()), // NOI18N
69
new SourceFoldersWizardPanel(),
70                 new ClasspathWizardPanel(),
71             };
72     }
73     
74     public Set JavaDoc<?/*FileObject*/> instantiate() throws IOException JavaDoc {
75         throw new AssertionError JavaDoc();
76     }
77
78     public Set JavaDoc<?/*FileObject*/> instantiate(final ProgressHandle handle) throws IOException JavaDoc {
79         handle.start(6);
80         final WizardDescriptor wiz = this.wiz;
81         final IOException JavaDoc[] ioe = new IOException JavaDoc[1];
82         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
83             public void run() {
84                 try {
85                     AntProjectHelper helper = NewFreeformProjectSupport.instantiateBasicProjectInfoWizardPanel(wiz);
86                     handle.progress(1);
87                     NewFreeformProjectSupport.instantiateTargetMappingWizardPanel(helper, wiz);
88                     handle.progress(2);
89                     ProjectModel pm = (ProjectModel)wiz.getProperty(PROP_PROJECT_MODEL);
90                     ProjectModel.instantiateJavaProject(helper, pm);
91                     handle.progress(3);
92                     Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
93                     handle.progress(4);
94                     ProjectManager.getDefault().saveProject(p);
95                     handle.progress(5);
96                 } catch (IOException JavaDoc e) {
97                     ioe[0] = e;
98                     return;
99                 }
100             }});
101         if (ioe[0] != null) {
102             throw ioe[0];
103         }
104         ProjectModel pm = (ProjectModel)wiz.getProperty(PROP_PROJECT_MODEL);
105         File JavaDoc nbProjectFolder = pm.getNBProjectFolder();
106         Set JavaDoc<FileObject> resultSet = new HashSet JavaDoc<FileObject>();
107         resultSet.add(FileUtil.toFileObject(nbProjectFolder));
108         Project p = ProjectManager.getDefault().findProject(FileUtil.toFileObject(nbProjectFolder));
109         if (p != null) {
110             Sources srcs = ProjectUtils.getSources(p);
111             if (srcs != null) {
112                 SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
113                 if (grps != null && grps.length > 0) {
114                     resultSet.add(grps[0].getRootFolder());
115                 }
116             }
117         }
118         
119         File JavaDoc f = nbProjectFolder.getParentFile();
120         if (f != null) {
121             ProjectChooser.setProjectsFolder(f);
122         }
123         handle.finish();
124         return resultSet;
125     }
126     
127         
128     public void initialize(WizardDescriptor wiz) {
129         this.wiz = wiz;
130         index = 0;
131         panels = createPanels();
132         
133         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
134         for (int i = 0; i < panels.length; i++) {
135             Component JavaDoc c = panels[i].getComponent();
136             assert c instanceof JComponent JavaDoc;
137             JComponent JavaDoc jc = (JComponent JavaDoc)c;
138             l.add(jc.getName());
139         }
140         String JavaDoc[] steps = l.toArray(new String JavaDoc[l.size()]);
141         
142         for (int i = 0; i < panels.length; i++) {
143             Component JavaDoc c = panels[i].getComponent();
144             assert c instanceof JComponent JavaDoc;
145             JComponent JavaDoc jc = (JComponent JavaDoc)c;
146             // Step #.
147
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
148
// Step name (actually the whole list for reference).
149
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
150
// set title
151
jc.putClientProperty ("NewProjectWizard_Title", NbBundle.getMessage (NewJ2SEFreeformProjectWizardIterator.class, "TXT_NewJ2SEFreeformProjectWizardIterator_NewProjectWizardTitle")); // NOI18N
152
}
153     }
154     
155     public void uninitialize(WizardDescriptor wiz) {
156         NewFreeformProjectSupport.uninitializeBasicProjectInfoWizardPanel(wiz);
157         NewFreeformProjectSupport.uninitializeTargetMappingWizardPanel(wiz);
158         NewJavaFreeformProjectSupport.uninitializeJavaPanels(wiz);
159         this.wiz = null;
160         panels = null;
161     }
162     
163     public String JavaDoc name() {
164         return NbBundle.getMessage(NewJ2SEFreeformProjectWizardIterator.class, "TXT_NewJ2SEFreeformProjectWizardIterator_TitleFormat", index + 1, panels.length);
165     }
166     
167     public boolean hasNext() {
168         if (current() instanceof SourceFoldersWizardPanel) {
169             assert current().getComponent() instanceof SourceFoldersPanel;
170             SourceFoldersPanel sfp = (SourceFoldersPanel)current().getComponent();
171             if (!sfp.hasSomeSourceFolder()) {
172                 return false;
173             }
174         }
175         return index < panels.length - 1;
176     }
177     public boolean hasPrevious() {
178         return index > 0;
179     }
180     public void nextPanel() {
181         if (!hasNext()) throw new NoSuchElementException JavaDoc();
182         index++;
183     }
184     public void previousPanel() {
185         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
186         index--;
187     }
188     public WizardDescriptor.Panel current () {
189         return panels[index];
190     }
191     
192     // If nothing unusual changes in the middle of the wizard, simply:
193
public final void addChangeListener(ChangeListener JavaDoc l) {}
194     public final void removeChangeListener(ChangeListener JavaDoc l) {}
195     
196 }
197
Popular Tags