KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > NewFileIterator


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.project.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectUtils;
30 import org.netbeans.api.project.Sources;
31 import org.netbeans.spi.project.ui.templates.support.Templates;
32 import org.openide.WizardDescriptor;
33 import org.openide.filesystems.FileObject;
34 import org.openide.loaders.DataFolder;
35 import org.openide.loaders.DataObject;
36
37
38 /** Iterator useful for NewFileWizard. Implements WD.InstantiatingIterator,
39  * rest of methods delegates to WD.ArrayIterator created only with SimpleTargetChooserPanel.
40  *
41  * @author Jiri Rechtacek
42  */

43 public class NewFileIterator implements WizardDescriptor.InstantiatingIterator {
44     
45     private static final long serialVersionUID = 1L;
46     
47     private transient boolean isLibrary;
48     private transient WizardDescriptor.Iterator simpleIterator;
49     private transient WizardDescriptor.Panel[] panels;
50     private transient WizardDescriptor wiz;
51     private transient Project currentProject;
52     
53     private transient boolean isFolder;
54     
55     
56     /** Create a new wizard iterator. */
57     private NewFileIterator (boolean isFolderIterator) {
58         isFolder = isFolderIterator;
59     }
60     
61     public static NewFileIterator genericFileIterator () {
62         return new NewFileIterator (false);
63     }
64     
65     public static NewFileIterator folderIterator () {
66         return new NewFileIterator (true);
67     }
68     
69     public static NewFileIterator emptyFileIterator () {
70         return new NewFileIterator (false);
71     }
72     
73     private WizardDescriptor.Iterator getSimpleIterator () {
74         if (simpleIterator == null) {
75             assert panels != null && panels.length > 0;
76             simpleIterator = new WizardDescriptor.ArrayIterator (panels);
77         }
78         return simpleIterator;
79     }
80             
81     private WizardDescriptor.Panel[] getPanels (WizardDescriptor wizardDescriptor) {
82         Project project = Templates.getProject( wizardDescriptor );
83         assert project != null : wizardDescriptor;
84         if (!project.equals (currentProject) || panels == null) {
85             currentProject = project;
86             Sources sources = ProjectUtils.getSources(project);
87             if (isFolder) {
88                 panels = new WizardDescriptor.Panel[] {
89                     new SimpleTargetChooserPanel (project, sources.getSourceGroups (Sources.TYPE_GENERIC), null, true)
90                 };
91             } else {
92                 panels = new WizardDescriptor.Panel[] {
93                     Templates.createSimpleTargetChooser (project, sources.getSourceGroups (Sources.TYPE_GENERIC))
94                 };
95             }
96         }
97         return panels;
98     }
99     
100     private String JavaDoc[] createSteps (String JavaDoc[] before) {
101         assert panels != null;
102         
103         if (before == null) {
104             before = new String JavaDoc[0];
105         }
106         
107         String JavaDoc[] res = new String JavaDoc[ (before.length - 1) + panels.length];
108         for (int i = 0; i < res.length; i++) {
109             if (i < (before.length - 1)) {
110                 res[i] = before[i];
111             } else {
112                 res[i] = panels[i - before.length + 1].getComponent ().getName ();
113             }
114         }
115         return res;
116     }
117     
118     public Set JavaDoc/*<FileObject>*/ instantiate () throws IOException JavaDoc {
119         FileObject dir = Templates.getTargetFolder( wiz );
120         
121         DataFolder df = DataFolder.findFolder( dir );
122         FileObject template = Templates.getTemplate( wiz );
123         
124         DataObject dTemplate = DataObject.find( template );
125         DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wiz ) );
126         
127         return Collections.singleton (dobj.getPrimaryFile ());
128     }
129     
130     public void initialize(WizardDescriptor wiz) {
131         panels = getPanels (wiz);
132         this.wiz = wiz;
133         
134         // Make sure list of steps is accurate.
135
String JavaDoc[] beforeSteps = null;
136         Object JavaDoc prop = wiz.getProperty ("WizardPanel_contentData"); // NOI18N
137
if (prop != null && prop instanceof String JavaDoc[]) {
138             beforeSteps = (String JavaDoc[])prop;
139         }
140         String JavaDoc[] steps = createSteps (beforeSteps);
141         for (int i = 0; i < panels.length; i++) {
142             Component JavaDoc c = panels[i].getComponent();
143             if (steps[i] == null) {
144                 // Default step name to component name of panel.
145
// Mainly useful for getting the name of the target
146
// chooser to appear in the list of steps.
147
steps[i] = c.getName();
148             }
149             if (c instanceof JComponent JavaDoc) { // assume Swing components
150
JComponent JavaDoc jc = (JComponent JavaDoc)c;
151                 // Step #.
152
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
153
// Step name (actually the whole list for reference).
154
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
155
}
156         }
157     }
158
159     public void uninitialize (WizardDescriptor wiz) {
160         this.simpleIterator = null;
161         this.wiz = null;
162         panels = null;
163     }
164     
165     public String JavaDoc name() {
166         return getSimpleIterator ().name ();
167     }
168     
169     public boolean hasNext() {
170         return getSimpleIterator ().hasNext ();
171     }
172     public boolean hasPrevious() {
173         return getSimpleIterator ().hasPrevious ();
174     }
175     public void nextPanel() {
176         getSimpleIterator ().nextPanel ();
177     }
178     public void previousPanel() {
179         getSimpleIterator ().previousPanel ();
180     }
181     public WizardDescriptor.Panel current() {
182         return getSimpleIterator ().current ();
183     }
184     public final void addChangeListener(ChangeListener JavaDoc l) {
185         getSimpleIterator ().addChangeListener (l);
186     }
187     public final void removeChangeListener(ChangeListener JavaDoc l) {
188         getSimpleIterator ().removeChangeListener (l);
189     }
190 }
191
Popular Tags