2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.xml.schema.wizard;
44
import java.awt.Component;
46
import java.io.IOException;
48
import java.net.URISyntaxException;
49
import java.util.ArrayList;
50
import java.util.Arrays;
51
import java.util.Collections;
52
import java.util.List;
53
import java.util.NoSuchElementException;
55
import javax.swing.JComponent;
56
import javax.swing.event.ChangeListener;
57
import org.netbeans.api.project.Project;
58
import org.netbeans.api.project.ProjectUtils;
59
import org.netbeans.api.project.SourceGroup;
60
import org.netbeans.api.project.Sources;
61
import org.netbeans.modules.xml.retriever.catalog.Utilities.DocumentTypesEnum;
62
import org.netbeans.modules.xml.retriever.RetrieveEntry;
63
import org.netbeans.modules.xml.retriever.RetrieverEngine;
64
import org.netbeans.modules.xml.xam.ui.ProjectConstants;
65
import org.netbeans.spi.project.ui.templates.support.Templates;
66
import org.openide.WizardDescriptor;
67
import org.openide.filesystems.FileObject;
68
import org.openide.filesystems.FileUtil;
69
import org.openide.loaders.DataFolder;
70
import org.openide.loaders.TemplateWizard;
73
public final class RetrieveXMLResourceWizardIterator implements TemplateWizard.Iterator {
74
/** silence compiler warnings */
75
private static final long serialVersionUID = 1L;
78
private WizardDescriptor wizard;
79
private WizardDescriptor.Panel[] panels;
82
* Initialize panels representing individual wizard's steps and sets
83
* various properties for them influencing wizard appearance.
85
private WizardDescriptor.Panel[] getPanels() {
87
panels = new WizardDescriptor.Panel[] {
88
new RetrieveXMLResourceWizardPanel1()
90
String[] steps = createSteps();
91
for (int i = 0; i < panels.length; i++) {
92
Component c = panels[i].getComponent();
93
if (steps[i] == null) {
94
// Default step name to component name of panel. Mainly
95
// useful for getting the name of the target chooser to
96
// appear in the list of steps.
97
steps[i] = c.getName();
99
if (c instanceof JComponent) { // assume Swing components
100
JComponent jc = (JComponent) c;
101
// Sets step number of a component
102
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
103
// Sets steps names for a panel
104
jc.putClientProperty("WizardPanel_contentData", steps);
105
// Turn on subtitle creation on each step
106
jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
107
// Show steps on the left side with the image on the background
108
jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
109
// Turn on numbering of all steps
110
jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
117
public Set instantiate(TemplateWizard wizard) throws IOException {
119
RetrieveXMLResourceVisualPanel1.SourceType srcType = (RetrieveXMLResourceVisualPanel1.SourceType) wizard.getProperty(IConstants.SOURCE_LOCATION_TYPE_KEY);
120
if(srcType == RetrieveXMLResourceVisualPanel1.SourceType.LOCAL_FILE){
121
return instantiateLocalFile();
123
if(srcType == RetrieveXMLResourceVisualPanel1.SourceType.URL_ADDR){
124
return instantiateURL();
126
return Collections.singleton(wizard.getTargetFolder());
129
private Set instantiateLocalFile() throws IOException{
130
URI getFileURI = null;
132
getFileURI = new URI((String) wizard.getProperty(IConstants.SOURCE_LOCATION_KEY));
133
} catch (URISyntaxException ex) {
134
throw new IOException(ex.getMessage());
136
File storedFile = (File) wizard.getProperty(IConstants.TARGET_FILE_KEY);
137
boolean overwriteFiles = ((Boolean)wizard.getProperty(IConstants.OVERWRITE_FILES)).booleanValue();
138
new ImportDirectory(new File(getFileURI), storedFile, overwriteFiles);
139
if (storedFile == null) {
140
// Doesn't matter what it is, just so it's not null.
141
storedFile = new File(System.getProperty("user.dir"));
143
// Must put something in the result set so the wizard appears in
144
// the projectui module's New file menu.
145
FileObject fobj = FileUtil.toFileObject(FileUtil.normalizeFile(storedFile));
147
return Collections.EMPTY_SET;
148
return Collections.singleton(fobj);
151
private Set instantiateURL() throws IOException{
152
File saveRootFile = (File) wizard.getProperty(IConstants.TARGET_FILE_KEY);
153
String sourceURL = (String) wizard.getProperty(IConstants.SOURCE_LOCATION_KEY);
154
File selectedSaveRootFolder = (File) wizard.getProperty(IConstants.USER_SELECTED_SAVE_ROOT);
155
boolean overwriteFiles = ((Boolean)wizard.getProperty(IConstants.OVERWRITE_FILES)).booleanValue();
156
RetrieverEngine instance = RetrieverEngine.getRetrieverEngine(selectedSaveRootFolder);
157
RetrieveEntry rent = null;
158
if(((Boolean) wizard.getProperty(IConstants.RETRIVE_CLOSURE_KEY)).booleanValue())
159
rent = new RetrieveEntry(null, sourceURL, null, null, DocumentTypesEnum.schema, true);
161
rent = new RetrieveEntry(null, sourceURL, null, saveRootFile, DocumentTypesEnum.schema, false);
162
instance.addResourceToRetrieve(rent);
163
instance.setFileOverwrite(overwriteFiles);
165
if (saveRootFile == null) {
166
// Doesn't matter what it is, just so it's not null.
167
saveRootFile = new File(System.getProperty("user.dir"));
169
// Must put something in the result set so the wizard appears in
170
// the projectui module's New file menu.
171
FileObject fobj = FileUtil.toFileObject(FileUtil.normalizeFile(saveRootFile));
173
return Collections.EMPTY_SET;
174
return Collections.singleton(fobj);
177
public void initialize(TemplateWizard wizard) {
179
//System.out.println("Initialize");
180
Project currentProject = Templates.getProject(wizard);
181
Sources sources = ProjectUtils.getSources(currentProject);
182
List<SourceGroup> roots = new ArrayList<SourceGroup>();
183
SourceGroup[] javaRoots =
184
sources.getSourceGroups(ProjectConstants.JAVA_SOURCES_TYPE);
185
roots.addAll(Arrays.asList(javaRoots));
186
if (roots.isEmpty()) {
187
SourceGroup[] sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
188
roots.addAll(Arrays.asList(sourceGroups));
190
DataFolder folder = DataFolder.findFolder(roots.get(0).getRootFolder());
191
DataFolder projectFolder =
192
DataFolder.findFolder(currentProject.getProjectDirectory());
194
if (wizard.getTargetFolder().equals(projectFolder)) {
195
wizard.setTargetFolder(folder);
197
} catch (IOException ioe) {
198
wizard.setTargetFolder(folder);
201
wizard.putProperty(IConstants.THIS_WIZARD_ITERATOR_KEY, this);
202
wizard.putProperty(IConstants.CURRENT_PROJECT_KEY, currentProject);
203
FileObject currentFolder = Templates.getTargetFolder(wizard);
204
wizard.putProperty(IConstants.CURRENT_FOLDER_KEY, currentFolder);
205
this.wizard = wizard;
208
public void uninitialize(TemplateWizard wizard) {
209
//System.out.println("UNINIT");
210
//System.out.println("$$$$"+wizard.getValue()+"%%%"+wizard.FINISH_OPTION+"^^^"+wizard.CLOSED_OPTION);
214
public WizardDescriptor.Panel current() {
215
return getPanels()[index];
218
public String name() {
219
return index + 1 + ". from " + getPanels().length;
222
public boolean hasNext() {
223
return index < getPanels().length - 1;
226
public boolean hasPrevious() {
230
public void nextPanel() {
232
throw new NoSuchElementException();
237
public void previousPanel() {
238
if (!hasPrevious()) {
239
throw new NoSuchElementException();
244
// If nothing unusual changes in the middle of the wizard, simply:
245
public void addChangeListener(ChangeListener l) {}
246
public void removeChangeListener(ChangeListener l) {}
248
// If something changes dynamically (besides moving between panels), e.g.
249
// the number of panels changes in response to user input, then uncomment
250
// the following and call when needed: fireChangeEvent();
252
private transient Set<ChangeListener> listeners = new HashSet<ChangeListener>(1);
253
public final void addChangeListener(ChangeListener l) {
254
synchronized (listeners) {
258
public final void removeChangeListener(ChangeListener l) {
259
synchronized (listeners) {
263
protected final void fireChangeEvent() {
264
Iterator<ChangeListener> it;
265
synchronized (listeners) {
266
it = new HashSet<ChangeListener>(listeners).iterator();
268
ChangeEvent ev = new ChangeEvent(this);
269
while (it.hasNext()) {
270
it.next().stateChanged(ev);
273
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
274
in.defaultReadObject();
275
listeners = new HashSet<ChangeListener>(1);
279
// You could safely ignore this method. Is is here to keep steps which were
280
// there before this wizard was instantiated. It should be better handled
281
// by NetBeans Wizard API itself rather than needed to be implemented by a
283
private String[] createSteps() {
284
String[] beforeSteps = null;
285
Object prop = wizard.getProperty("WizardPanel_contentData");
286
if (prop != null && prop instanceof String[]) {
287
beforeSteps = (String[]) prop;
290
if (beforeSteps == null) {
291
beforeSteps = new String[0];
294
String[] res = new String[(beforeSteps.length - 1) + panels.length];
295
for (int i = 0; i < res.length; i++) {
296
if (i < (beforeSteps.length - 1)) {
297
res[i] = beforeSteps[i];
299
res[i] = panels[i - beforeSteps.length + 1].getComponent().getName();