KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > wizard > ImportSchemaCreator


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.wsdl.ui.wizard;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import org.netbeans.modules.xml.schema.model.Schema;
29 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
30 import org.netbeans.modules.xml.wsdl.ui.view.ImportSchemaCustomizer;
31 import org.netbeans.modules.xml.xam.Model;
32 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDataNode;
33 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceNode;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.loaders.DataObject;
37 import org.openide.nodes.Node;
38
39 /**
40  * An import customizer for schema in a WSDL document from the new WSDL
41  * file wizard.
42  *
43  * @author Nathan Fiedler
44  */

45 public class ImportSchemaCreator extends ImportSchemaCustomizer {
46     /** silence compiler warnings */
47     private static final long serialVersionUID = 1L;
48     /** URIs of previously selected files. */
49     private List JavaDoc<String JavaDoc> selectedFiles;
50     /** If true, ignore the property change event. */
51     private boolean ignorePropertyChange;
52
53     /**
54      * Creates a new instance of ImportCustomizer
55      *
56      * @param schema component to contain the import(s).
57      * @param model the WSDL model.
58      * @param files comma-separated file URIs.
59      */

60     public ImportSchemaCreator(Schema schema, WSDLModel model, String JavaDoc files) {
61         super(schema, model);
62         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(files, ",");
63         selectedFiles = new ArrayList JavaDoc<String JavaDoc>();
64         while (tokenizer.hasMoreTokens()) {
65             selectedFiles.add(tokenizer.nextToken());
66         }
67     }
68
69     protected boolean allowEmptySelection() {
70         // Need to permit user to deselect files that were previously
71
// selected to the point of having no files selected at all.
72
return true;
73     }
74
75     @Override JavaDoc
76     public void applyChanges() throws IOException JavaDoc {
77         // Do NOT call the superclass, as we are operating from within
78
// the new WSDL file wizard, and there is nothing to which we
79
// can commit our changes.
80
}
81
82     public ExternalReferenceDataNode createExternalReferenceNode(Node original) {
83         ExternalReferenceDataNode erdn = super.createExternalReferenceNode(original);
84         // This method gets called from the superclass constructor, and we
85
// have nothing to add during that early phase, so just skip it.
86
if (selectedFiles != null) {
87             // Mark the node selected if it had been selected earlier.
88
DataObject dobj = (DataObject) erdn.getLookup().lookup(DataObject.class);
89             String JavaDoc location = dataObjectToURI(dobj);
90             if (selectedFiles.contains(location)) {
91                 ignorePropertyChange = true;
92                 erdn.setSelected(true);
93                 ignorePropertyChange = false;
94             }
95         }
96         return erdn;
97     }
98
99     /**
100      * Convert a DataObject to a file URI string.
101      *
102      * @param dobj the DataObject to convert.
103      * @return the URI string for the file.
104      */

105     private static String JavaDoc dataObjectToURI(DataObject dobj) {
106         FileObject fobj = dobj.getPrimaryFile();
107         File JavaDoc file = FileUtil.toFile(fobj);
108         return file.toURI().normalize().toString();
109     }
110
111     /**
112      * Return a String of comma-separated URIs for the selected files.
113      *
114      * @return selected files as URIs.
115      */

116     public String JavaDoc getSelectedFiles() {
117         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
118         for (String JavaDoc uri : selectedFiles) {
119             if (sb.length() > 0){
120                 sb.append(",");
121             }
122             sb.append(uri);
123         }
124         return sb.toString();
125     }
126
127     public void propertyChange(PropertyChangeEvent JavaDoc event) {
128         // Let superclass do its thing.
129
super.propertyChange(event);
130         // Manage our set of selected file URIs. This is done here since
131
// the user may never expand the nodes, and thus they may never
132
// become "selected", and yet we want any previously selected
133
// files (in the form of our URI list) to be maintained.
134
if (!ignorePropertyChange && event.getPropertyName().equals(
135                 ExternalReferenceDataNode.PROP_SELECTED)) {
136             ExternalReferenceDataNode erdn =
137                     (ExternalReferenceDataNode) event.getSource();
138             boolean selected = ((Boolean JavaDoc) event.getNewValue()).booleanValue();
139             DataObject dobj = (DataObject) erdn.getLookup().lookup(DataObject.class);
140             String JavaDoc uri = dataObjectToURI(dobj);
141             if (selected) {
142                 selectedFiles.add(uri);
143             } else {
144                 selectedFiles.remove(uri);
145             }
146         }
147     }
148 }
149
Popular Tags