001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.wsdl.ui.wizard;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.io.File;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.List;
049: import java.util.StringTokenizer;
050:
051: import org.netbeans.modules.xml.schema.model.Schema;
052: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
053: import org.netbeans.modules.xml.wsdl.ui.view.ImportSchemaCustomizer;
054: import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDataNode;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.loaders.DataObject;
058: import org.openide.nodes.Node;
059:
060: /**
061: * An import customizer for schema in a WSDL document from the new WSDL
062: * file wizard.
063: *
064: * @author Nathan Fiedler
065: */
066: public class ImportSchemaCreator extends ImportSchemaCustomizer {
067: /** silence compiler warnings */
068: private static final long serialVersionUID = 1L;
069: /** URIs of previously selected files. */
070: private List<String> selectedFiles;
071: /** If true, ignore the property change event. */
072: private boolean ignorePropertyChange;
073:
074: /**
075: * Creates a new instance of ImportCustomizer
076: *
077: * @param schema component to contain the import(s).
078: * @param model the WSDL model.
079: * @param files comma-separated file URIs.
080: */
081: public ImportSchemaCreator(Schema schema, WSDLModel model,
082: String files) {
083: super (schema, model);
084: StringTokenizer tokenizer = new StringTokenizer(files, ",");
085: selectedFiles = new ArrayList<String>();
086: while (tokenizer.hasMoreTokens()) {
087: selectedFiles.add(tokenizer.nextToken());
088: }
089: }
090:
091: protected boolean allowEmptySelection() {
092: // Need to permit user to deselect files that were previously
093: // selected to the point of having no files selected at all.
094: return true;
095: }
096:
097: @Override
098: public void applyChanges() throws IOException {
099: // Do NOT call the superclass, as we are operating from within
100: // the new WSDL file wizard, and there is nothing to which we
101: // can commit our changes.
102: }
103:
104: public ExternalReferenceDataNode createExternalReferenceNode(
105: Node original) {
106: ExternalReferenceDataNode erdn = super
107: .createExternalReferenceNode(original);
108: // This method gets called from the superclass constructor, and we
109: // have nothing to add during that early phase, so just skip it.
110: if (selectedFiles != null) {
111: // Mark the node selected if it had been selected earlier.
112: DataObject dobj = erdn.getLookup().lookup(DataObject.class);
113: String location = dataObjectToURI(dobj);
114: if (selectedFiles.contains(location)) {
115: ignorePropertyChange = true;
116: erdn.setSelected(true);
117: ignorePropertyChange = false;
118: }
119: }
120: return erdn;
121: }
122:
123: /**
124: * Convert a DataObject to a file URI string.
125: *
126: * @param dobj the DataObject to convert.
127: * @return the URI string for the file.
128: */
129: private static String dataObjectToURI(DataObject dobj) {
130: FileObject fobj = dobj.getPrimaryFile();
131: File file = FileUtil.toFile(fobj);
132: return file.toURI().normalize().toString();
133: }
134:
135: /**
136: * Return a String of comma-separated URIs for the selected files.
137: *
138: * @return selected files as URIs.
139: */
140: public String getSelectedFiles() {
141: StringBuilder sb = new StringBuilder();
142: for (String uri : selectedFiles) {
143: if (sb.length() > 0) {
144: sb.append(",");
145: }
146: sb.append(uri);
147: }
148: return sb.toString();
149: }
150:
151: public void propertyChange(PropertyChangeEvent event) {
152: // Let superclass do its thing.
153: super .propertyChange(event);
154: // Manage our set of selected file URIs. This is done here since
155: // the user may never expand the nodes, and thus they may never
156: // become "selected", and yet we want any previously selected
157: // files (in the form of our URI list) to be maintained.
158: if (!ignorePropertyChange
159: && event.getPropertyName().equals(
160: ExternalReferenceDataNode.PROP_SELECTED)) {
161: ExternalReferenceDataNode erdn = (ExternalReferenceDataNode) event
162: .getSource();
163: boolean selected = ((Boolean) event.getNewValue())
164: .booleanValue();
165: DataObject dobj = erdn.getLookup().lookup(DataObject.class);
166: String uri = dataObjectToURI(dobj);
167: if (selected) {
168: selectedFiles.add(uri);
169: } else {
170: selectedFiles.remove(uri);
171: }
172: }
173: }
174: }
|