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: package org.netbeans.modules.xml.wsdl.ui.view.treeeditor.pastetype;
042:
043: import java.awt.datatransfer.Transferable;
044: import java.io.IOException;
045: import java.util.Collection;
046: import java.awt.Dialog;
047:
048: import java.util.Set;
049: import org.netbeans.api.project.FileOwnerQuery;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.modules.xml.wsdl.model.Import;
052: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
053: import org.netbeans.modules.xml.wsdl.ui.actions.ActionHelper;
054: import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
055: import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
056: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
057: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
058: import org.openide.DialogDescriptor;
059: import org.openide.DialogDisplayer;
060: import org.openide.NotifyDescriptor;
061: import org.openide.loaders.DataObject;
062: import org.openide.util.HelpCtx;
063: import org.openide.util.NbBundle;
064: import org.openide.util.datatransfer.PasteType;
065:
066: public class WSDLImportPasteType extends PasteType {
067:
068: private final WSDLModel newModel;
069: private final WSDLModel currModel;
070:
071: public WSDLImportPasteType(WSDLModel currModel, WSDLModel newModel) {
072: this .currModel = currModel;
073: this .newModel = newModel;
074:
075: }
076:
077: @Override
078: public Transferable paste() throws IOException {
079: if (newModel.equals(currModel)) {
080: return null;
081: }
082: String errorMessage = validate();
083: if (errorMessage != null) {
084: DialogDisplayer.getDefault().notify(
085: new NotifyDescriptor.Exception(new Exception(
086: errorMessage)));
087: return null;
088: }
089: Set prefixes = ((AbstractDocumentComponent) currModel
090: .getDefinitions()).getPrefixes().keySet();
091:
092: String prefix = NameGenerator.getInstance()
093: .generateNamespacePrefix(null, currModel);
094: DataObject dObj = ActionHelper.getDataObject(newModel);
095: Project project = FileOwnerQuery
096: .getOwner(dObj.getPrimaryFile());
097: DnDImportPanel panel = new DnDImportPanel();
098: panel.setNamespace(newModel.getDefinitions()
099: .getTargetNamespace());
100: panel.setProject(project);
101: panel.setPrefix(prefix);
102: panel.setFileName(dObj.getPrimaryFile());
103: panel.setPrefixes(prefixes);
104: final DialogDescriptor descriptor = new DialogDescriptor(panel,
105: NbBundle.getMessage(WSDLImportPasteType.class,
106: "LBL_ConfirmWSDLDocDrop"), true,
107: DialogDescriptor.OK_CANCEL_OPTION,
108: DialogDescriptor.OK_OPTION,
109: DialogDescriptor.DEFAULT_ALIGN, new HelpCtx(
110: WSDLImportPasteType.class), null);
111: panel.setDialogDescriptor(descriptor);
112:
113: Dialog dlg = DialogDisplayer.getDefault().createDialog(
114: descriptor);
115: dlg.getAccessibleContext().setAccessibleDescription(
116: dlg.getTitle());
117: dlg.setVisible(true);
118:
119: if (descriptor.getValue() == DialogDescriptor.OK_OPTION) {
120: prefix = panel.getPrefix();
121: Import newImport = null;
122: try {
123: currModel.startTransaction();
124: Utility.addNamespacePrefix(newModel, currModel, prefix);
125: newImport = Utility.addWSDLImport(newModel, currModel);
126: } finally {
127: if (currModel.isIntransaction()) {
128: currModel.endTransaction();
129: }
130: }
131: if (newImport != null) {
132: ActionHelper.selectNode(newImport);
133: }
134: }
135:
136: return null;
137: }
138:
139: String validate() {
140: DataObject dObj = ActionHelper.getDataObject(newModel);
141:
142: if (newModel.getState() != WSDLModel.State.VALID) {
143: return NbBundle.getMessage(WSDLImportPasteType.class,
144: "ERRMSG_ImportedWSDLIsNotValid", dObj
145: .getPrimaryFile().getNameExt());
146: }
147:
148: if (!Utility.canImport(newModel, currModel)) {
149: return NbBundle.getMessage(WSDLImportPasteType.class,
150: "ERRMSG_ProjectNotReferenceable");
151: }
152:
153: String impNamespace = newModel.getDefinitions()
154: .getTargetNamespace();
155: if (impNamespace == null) {
156: return NbBundle.getMessage(WSDLImportPasteType.class,
157: "ERRMSG_ImportedWSDLWithNoTargetNamespace", dObj
158: .getPrimaryFile().getNameExt());
159: }
160: Collection<Import> references = currModel.getDefinitions()
161: .getImports();
162: // Ensure the selected document is not already among the
163: // set that have been included.
164: for (Import ref : references) {
165: try {
166: WSDLModel otherModel = ref.getImportedWSDLModel();
167: if (newModel.equals(otherModel)) {
168: return NbBundle.getMessage(
169: WSDLImportPasteType.class,
170: "ERRMSG_AlreadyRefd");
171: }
172: } catch (CatalogModelException cme) {
173: // Ignore that one as it does not matter.
174: }
175: }
176: return null;
177: }
178: }
|