~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to xml/schema/src/org/netbeans/modules/xml/schema/wizard/RetrieveXMLResourceWizardIterator.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.xml.schema.wizard;
 
43
 
 
44
import java.awt.Component;
 
45
import java.io.File;
 
46
import java.io.IOException;
 
47
import java.net.URI;
 
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;
 
54
import java.util.Set;
 
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;
 
71
 
 
72
 
 
73
public final class RetrieveXMLResourceWizardIterator implements TemplateWizard.Iterator {
 
74
    /** silence compiler warnings */
 
75
    private static final long serialVersionUID = 1L;
 
76
    private int index;
 
77
    
 
78
    private WizardDescriptor wizard;
 
79
    private WizardDescriptor.Panel[] panels;
 
80
    
 
81
    /**
 
82
     * Initialize panels representing individual wizard's steps and sets
 
83
     * various properties for them influencing wizard appearance.
 
84
     */
 
85
    private WizardDescriptor.Panel[] getPanels() {
 
86
        if (panels == null) {
 
87
            panels = new WizardDescriptor.Panel[] {
 
88
                new RetrieveXMLResourceWizardPanel1()
 
89
            };
 
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();
 
98
                }
 
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);
 
111
                }
 
112
            }
 
113
        }
 
114
        return panels;
 
115
    }
 
116
    
 
117
    public Set instantiate(TemplateWizard wizard) throws IOException {
 
118
        
 
119
        RetrieveXMLResourceVisualPanel1.SourceType srcType = (RetrieveXMLResourceVisualPanel1.SourceType) wizard.getProperty(IConstants.SOURCE_LOCATION_TYPE_KEY);
 
120
        if(srcType == RetrieveXMLResourceVisualPanel1.SourceType.LOCAL_FILE){
 
121
            return instantiateLocalFile();
 
122
        }
 
123
        if(srcType == RetrieveXMLResourceVisualPanel1.SourceType.URL_ADDR){
 
124
            return instantiateURL();
 
125
        }
 
126
        return Collections.singleton(wizard.getTargetFolder());
 
127
    }
 
128
    
 
129
    private Set instantiateLocalFile() throws IOException{
 
130
        URI getFileURI = null;
 
131
        try {
 
132
            getFileURI = new URI((String) wizard.getProperty(IConstants.SOURCE_LOCATION_KEY));
 
133
        } catch (URISyntaxException ex) {
 
134
            throw new IOException(ex.getMessage());
 
135
        }
 
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"));
 
142
        }
 
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));
 
146
        if(fobj == null)
 
147
            return Collections.EMPTY_SET;
 
148
        return Collections.singleton(fobj);
 
149
    }
 
150
    
 
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);
 
160
        else
 
161
            rent = new RetrieveEntry(null, sourceURL, null, saveRootFile, DocumentTypesEnum.schema, false);
 
162
        instance.addResourceToRetrieve(rent);
 
163
        instance.setFileOverwrite(overwriteFiles);
 
164
        instance.start();
 
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"));
 
168
        }
 
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));
 
172
        if(fobj == null)
 
173
            return Collections.EMPTY_SET;
 
174
        return Collections.singleton(fobj);
 
175
    }
 
176
    
 
177
    public void initialize(TemplateWizard wizard) {
 
178
        
 
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));
 
189
        }
 
190
        DataFolder folder = DataFolder.findFolder(roots.get(0).getRootFolder());
 
191
        DataFolder projectFolder =
 
192
                DataFolder.findFolder(currentProject.getProjectDirectory());
 
193
        try {
 
194
            if (wizard.getTargetFolder().equals(projectFolder)) {
 
195
                wizard.setTargetFolder(folder);
 
196
            }
 
197
        } catch (IOException ioe) {
 
198
            wizard.setTargetFolder(folder);
 
199
        }
 
200
        
 
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;
 
206
    }
 
207
    
 
208
    public void uninitialize(TemplateWizard wizard) {
 
209
        //System.out.println("UNINIT");
 
210
        //System.out.println("$$$$"+wizard.getValue()+"%%%"+wizard.FINISH_OPTION+"^^^"+wizard.CLOSED_OPTION);
 
211
        panels = null;
 
212
    }
 
213
    
 
214
    public WizardDescriptor.Panel current() {
 
215
        return getPanels()[index];
 
216
    }
 
217
    
 
218
    public String name() {
 
219
        return index + 1 + ". from " + getPanels().length;
 
220
    }
 
221
    
 
222
    public boolean hasNext() {
 
223
        return index < getPanels().length - 1;
 
224
    }
 
225
    
 
226
    public boolean hasPrevious() {
 
227
        return index > 0;
 
228
    }
 
229
    
 
230
    public void nextPanel() {
 
231
        if (!hasNext()) {
 
232
            throw new NoSuchElementException();
 
233
        }
 
234
        index++;
 
235
    }
 
236
    
 
237
    public void previousPanel() {
 
238
        if (!hasPrevious()) {
 
239
            throw new NoSuchElementException();
 
240
        }
 
241
        index--;
 
242
    }
 
243
    
 
244
    // If nothing unusual changes in the middle of the wizard, simply:
 
245
    public void addChangeListener(ChangeListener l) {}
 
246
    public void removeChangeListener(ChangeListener l) {}
 
247
    
 
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();
 
251
    /*
 
252
    private transient Set<ChangeListener> listeners = new HashSet<ChangeListener>(1);
 
253
    public final void addChangeListener(ChangeListener l) {
 
254
        synchronized (listeners) {
 
255
            listeners.add(l);
 
256
        }
 
257
    }
 
258
    public final void removeChangeListener(ChangeListener l) {
 
259
        synchronized (listeners) {
 
260
            listeners.remove(l);
 
261
        }
 
262
    }
 
263
    protected final void fireChangeEvent() {
 
264
        Iterator<ChangeListener> it;
 
265
        synchronized (listeners) {
 
266
            it = new HashSet<ChangeListener>(listeners).iterator();
 
267
        }
 
268
        ChangeEvent ev = new ChangeEvent(this);
 
269
        while (it.hasNext()) {
 
270
            it.next().stateChanged(ev);
 
271
        }
 
272
    }
 
273
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
 
274
        in.defaultReadObject();
 
275
        listeners = new HashSet<ChangeListener>(1);
 
276
    }
 
277
     */
 
278
    
 
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
 
282
    // client code.
 
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;
 
288
        }
 
289
        
 
290
        if (beforeSteps == null) {
 
291
            beforeSteps = new String[0];
 
292
        }
 
293
        
 
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];
 
298
            } else {
 
299
                res[i] = panels[i - beforeSteps.length + 1].getComponent().getName();
 
300
            }
 
301
        }
 
302
        return res;
 
303
    }
 
304
    
 
305
}
 
306