KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > retriever > RetrieveXMLResourceWizardIterator


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.ui.retriever;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URISyntaxException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.NoSuchElementException JavaDoc;
32 import java.util.Set JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.event.ChangeListener JavaDoc;
35 import org.netbeans.api.project.Project;
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.xml.retriever.catalog.Utilities.DocumentTypesEnum;
40 import org.netbeans.modules.xml.retriever.IConstants;
41 import org.netbeans.modules.xml.retriever.ImportDirectory;
42 import org.netbeans.modules.xml.retriever.RetrieveEntry;
43 import org.netbeans.modules.xml.retriever.RetrieverEngine;
44 import org.netbeans.modules.xml.xam.ui.ProjectConstants;
45 import org.netbeans.spi.project.ui.templates.support.Templates;
46 import org.openide.WizardDescriptor;
47 import org.openide.filesystems.FileObject;
48 import org.openide.filesystems.FileUtil;
49 import org.openide.loaders.DataFolder;
50 import org.openide.loaders.TemplateWizard;
51 import org.openide.util.NbBundle;
52
53
54 public final class RetrieveXMLResourceWizardIterator implements TemplateWizard.Iterator {
55     /** silence compiler warnings */
56     private static final long serialVersionUID = 1L;
57     private int index;
58     
59     private WizardDescriptor wizard;
60     private WizardDescriptor.Panel[] panels;
61     
62     /**
63      * Initialize panels representing individual wizard's steps and sets
64      * various properties for them influencing wizard appearance.
65      */

66     private WizardDescriptor.Panel[] getPanels() {
67         if (panels == null) {
68             panels = new WizardDescriptor.Panel[] {
69                 new RetrieveXMLResourceWizardPanel1()
70             };
71             String JavaDoc[] steps = createSteps();
72             for (int i = 0; i < panels.length; i++) {
73                 Component JavaDoc c = panels[i].getComponent();
74                 if (steps[i] == null) {
75                     // Default step name to component name of panel. Mainly
76
// useful for getting the name of the target chooser to
77
// appear in the list of steps.
78
steps[i] = c.getName();
79                 }
80                 if (c instanceof JComponent JavaDoc) { // assume Swing components
81
JComponent JavaDoc jc = (JComponent JavaDoc) c;
82                     // Sets step number of a component
83
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i));
84                     // Sets steps names for a panel
85
jc.putClientProperty("WizardPanel_contentData", steps);
86                     // Turn on subtitle creation on each step
87
jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
88                     // Show steps on the left side with the image on the background
89
jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
90                     // Turn on numbering of all steps
91
jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
92                 }
93             }
94         }
95         return panels;
96     }
97     
98     public Set JavaDoc instantiate(TemplateWizard wizard) throws IOException JavaDoc {
99         
100         RetrieveXMLResourceVisualPanel1.SourceType srcType = (RetrieveXMLResourceVisualPanel1.SourceType) wizard.getProperty(IConstants.SOURCE_LOCATION_TYPE_KEY);
101         if(srcType == RetrieveXMLResourceVisualPanel1.SourceType.LOCAL_FILE){
102             return instantiateLocalFile();
103         }
104         if(srcType == RetrieveXMLResourceVisualPanel1.SourceType.URL_ADDR){
105             return instantiateURL();
106         }
107         return Collections.singleton(wizard.getTargetFolder());
108     }
109     
110     private Set JavaDoc instantiateLocalFile() throws IOException JavaDoc{
111         URI JavaDoc getFileURI = null;
112         try {
113             getFileURI = new URI JavaDoc((String JavaDoc) wizard.getProperty(IConstants.SOURCE_LOCATION_KEY));
114         } catch (URISyntaxException JavaDoc ex) {
115             throw new IOException JavaDoc(ex.getMessage());
116         }
117         File JavaDoc storedFile = (File JavaDoc) wizard.getProperty(IConstants.TARGET_FILE_KEY);
118         boolean overwriteFiles = ((Boolean JavaDoc)wizard.getProperty(IConstants.OVERWRITE_FILES)).booleanValue();
119         new ImportDirectory(new File JavaDoc(getFileURI), storedFile, overwriteFiles);
120         if (storedFile == null) {
121             // Doesn't matter what it is, just so it's not null.
122
storedFile = new File JavaDoc(System.getProperty("user.dir"));
123         }
124         // Must put something in the result set so the wizard appears in
125
// the projectui module's New file menu.
126
FileObject fobj = FileUtil.toFileObject(FileUtil.normalizeFile(storedFile));
127         if(fobj == null)
128             return Collections.EMPTY_SET;
129         return Collections.singleton(fobj);
130     }
131     
132     private Set JavaDoc instantiateURL() throws IOException JavaDoc{
133         File JavaDoc saveRootFile = (File JavaDoc) wizard.getProperty(IConstants.TARGET_FILE_KEY);
134         String JavaDoc sourceURL = (String JavaDoc) wizard.getProperty(IConstants.SOURCE_LOCATION_KEY);
135         File JavaDoc selectedSaveRootFolder = (File JavaDoc) wizard.getProperty(IConstants.USER_SELECTED_SAVE_ROOT);
136         boolean overwriteFiles = ((Boolean JavaDoc)wizard.getProperty(IConstants.OVERWRITE_FILES)).booleanValue();
137         RetrieverEngine instance = new RetrieverEngine(selectedSaveRootFolder);
138         RetrieveEntry rent = null;
139         if(((Boolean JavaDoc) wizard.getProperty(IConstants.RETRIVE_CLOSURE_KEY)).booleanValue())
140             rent = new RetrieveEntry(null, sourceURL, null, null, DocumentTypesEnum.schema, true);
141         else
142             rent = new RetrieveEntry(null, sourceURL, null, saveRootFile, DocumentTypesEnum.schema, false);
143         instance.addResourceToRetrieve(rent);
144         instance.setFileOverwrite(overwriteFiles);
145         instance.start();
146         if (saveRootFile == null) {
147             // Doesn't matter what it is, just so it's not null.
148
saveRootFile = new File JavaDoc(System.getProperty("user.dir"));
149         }
150         // Must put something in the result set so the wizard appears in
151
// the projectui module's New file menu.
152
FileObject fobj = FileUtil.toFileObject(FileUtil.normalizeFile(saveRootFile));
153         if(fobj == null)
154             return Collections.EMPTY_SET;
155         return Collections.singleton(fobj);
156     }
157     
158     public void initialize(TemplateWizard wizard) {
159         
160         //System.out.println("Initialize");
161
Project currentProject = Templates.getProject(wizard);
162         Sources sources = ProjectUtils.getSources(currentProject);
163         List JavaDoc<SourceGroup> roots = new ArrayList JavaDoc<SourceGroup>();
164         SourceGroup[] javaRoots =
165                 sources.getSourceGroups(ProjectConstants.JAVA_SOURCES_TYPE);
166         roots.addAll(Arrays.asList(javaRoots));
167         if (roots.isEmpty()) {
168             SourceGroup[] sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
169             roots.addAll(Arrays.asList(sourceGroups));
170         }
171         DataFolder folder = DataFolder.findFolder(roots.get(0).getRootFolder());
172         DataFolder projectFolder =
173                 DataFolder.findFolder(currentProject.getProjectDirectory());
174         try {
175             if (wizard.getTargetFolder().equals(projectFolder)) {
176                 wizard.setTargetFolder(folder);
177             }
178         } catch (IOException JavaDoc ioe) {
179             wizard.setTargetFolder(folder);
180         }
181         
182         wizard.putProperty(IConstants.THIS_WIZARD_ITERATOR_KEY, this);
183         wizard.putProperty(IConstants.CURRENT_PROJECT_KEY, currentProject);
184         FileObject currentFolder = Templates.getTargetFolder(wizard);
185         wizard.putProperty(IConstants.CURRENT_FOLDER_KEY, currentFolder);
186         this.wizard = wizard;
187     }
188     
189     public void uninitialize(TemplateWizard wizard) {
190         //System.out.println("UNINIT");
191
//System.out.println("$$$$"+wizard.getValue()+"%%%"+wizard.FINISH_OPTION+"^^^"+wizard.CLOSED_OPTION);
192
panels = null;
193     }
194     
195     public WizardDescriptor.Panel current() {
196         return getPanels()[index];
197     }
198     
199     public String JavaDoc name() {
200         return index + 1 + ". from " + getPanels().length;
201     }
202     
203     public boolean hasNext() {
204         return index < getPanels().length - 1;
205     }
206     
207     public boolean hasPrevious() {
208         return index > 0;
209     }
210     
211     public void nextPanel() {
212         if (!hasNext()) {
213             throw new NoSuchElementException JavaDoc();
214         }
215         index++;
216     }
217     
218     public void previousPanel() {
219         if (!hasPrevious()) {
220             throw new NoSuchElementException JavaDoc();
221         }
222         index--;
223     }
224     
225     // If nothing unusual changes in the middle of the wizard, simply:
226
public void addChangeListener(ChangeListener JavaDoc l) {}
227     public void removeChangeListener(ChangeListener JavaDoc l) {}
228     
229     // If something changes dynamically (besides moving between panels), e.g.
230
// the number of panels changes in response to user input, then uncomment
231
// the following and call when needed: fireChangeEvent();
232
/*
233     private transient Set<ChangeListener> listeners = new HashSet<ChangeListener>(1);
234     public final void addChangeListener(ChangeListener l) {
235         synchronized (listeners) {
236             listeners.add(l);
237         }
238     }
239     public final void removeChangeListener(ChangeListener l) {
240         synchronized (listeners) {
241             listeners.remove(l);
242         }
243     }
244     protected final void fireChangeEvent() {
245         Iterator<ChangeListener> it;
246         synchronized (listeners) {
247             it = new HashSet<ChangeListener>(listeners).iterator();
248         }
249         ChangeEvent ev = new ChangeEvent(this);
250         while (it.hasNext()) {
251             it.next().stateChanged(ev);
252         }
253     }
254     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
255         in.defaultReadObject();
256         listeners = new HashSet<ChangeListener>(1);
257     }
258      */

259     
260     // You could safely ignore this method. Is is here to keep steps which were
261
// there before this wizard was instantiated. It should be better handled
262
// by NetBeans Wizard API itself rather than needed to be implemented by a
263
// client code.
264
private String JavaDoc[] createSteps() {
265         String JavaDoc[] beforeSteps = null;
266         Object JavaDoc prop = wizard.getProperty("WizardPanel_contentData");
267         if (prop != null && prop instanceof String JavaDoc[]) {
268             beforeSteps = (String JavaDoc[]) prop;
269         }
270         
271         if (beforeSteps == null) {
272             beforeSteps = new String JavaDoc[0];
273         }
274         
275         String JavaDoc[] res = new String JavaDoc[(beforeSteps.length - 1) + panels.length];
276         for (int i = 0; i < res.length; i++) {
277             if (i < (beforeSteps.length - 1)) {
278                 res[i] = beforeSteps[i];
279             } else {
280                 res[i] = panels[i - beforeSteps.length + 1].getComponent().getName();
281             }
282         }
283         return res;
284     }
285     
286 }
287
288
Popular Tags