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.Dialog;
044: import java.awt.datatransfer.Transferable;
045: import java.io.IOException;
046: import java.util.Collection;
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.retriever.catalog.Utilities;
052: import org.netbeans.modules.xml.schema.model.SchemaModel;
053: import org.netbeans.modules.xml.schema.model.SchemaModelReference;
054: import org.netbeans.modules.xml.wsdl.model.Definitions;
055: import org.netbeans.modules.xml.wsdl.model.Types;
056: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
057: import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
058: import org.netbeans.modules.xml.wsdl.ui.actions.ActionHelper;
059: import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
060: import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
061: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
062: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
063: import org.openide.DialogDescriptor;
064: import org.openide.DialogDisplayer;
065: import org.openide.NotifyDescriptor;
066: import org.openide.loaders.DataObject;
067: import org.openide.util.HelpCtx;
068: import org.openide.util.NbBundle;
069: import org.openide.util.datatransfer.PasteType;
070:
071: public class SchemaImportPasteType extends PasteType {
072:
073: private final WSDLModel currModel;
074: private final SchemaModel schemaModel;
075:
076: public SchemaImportPasteType(WSDLModel currModel,
077: SchemaModel schemaModel) {
078: this .currModel = currModel;
079: this .schemaModel = schemaModel;
080:
081: }
082:
083: @Override
084: public Transferable paste() throws IOException {
085: String errorMessage = validate(schemaModel, currModel);
086: if (errorMessage != null) {
087: DialogDisplayer.getDefault().notify(
088: new NotifyDescriptor.Exception(new Exception(
089: errorMessage)));
090: return null;
091: }
092:
093: String prefix = NameGenerator.getInstance()
094: .generateNamespacePrefix(null, currModel);
095: DataObject dObj = ActionHelper.getDataObject(schemaModel);
096: Project project = FileOwnerQuery
097: .getOwner(dObj.getPrimaryFile());
098: DnDImportPanel panel = new DnDImportPanel();
099: panel
100: .setNamespace(schemaModel.getSchema()
101: .getTargetNamespace());
102: panel.setProject(project);
103: panel.setPrefix(prefix);
104: panel.setFileName(dObj.getPrimaryFile());
105:
106: Set prefixes = ((AbstractDocumentComponent) currModel
107: .getDefinitions()).getPrefixes().keySet();
108: panel.setPrefixes(prefixes);
109:
110: final DialogDescriptor descriptor = new DialogDescriptor(panel,
111: NbBundle.getMessage(SchemaImportPasteType.class,
112: "LBL_ConfirmSchemaDocDrop"), true,
113: DialogDescriptor.OK_CANCEL_OPTION,
114: DialogDescriptor.OK_OPTION,
115: DialogDescriptor.DEFAULT_ALIGN, new HelpCtx(
116: SchemaImportPasteType.class), null);
117: panel.setDialogDescriptor(descriptor);
118:
119: Dialog dlg = DialogDisplayer.getDefault().createDialog(
120: descriptor);
121: dlg.getAccessibleContext().setAccessibleDescription(
122: dlg.getTitle());
123: dlg.setVisible(true);
124:
125: if (descriptor.getValue() == DialogDescriptor.OK_OPTION) {
126: prefix = panel.getPrefix();
127:
128: org.netbeans.modules.xml.schema.model.Import newImport = null;
129: try {
130: currModel.startTransaction();
131: Utility.addNamespacePrefix(schemaModel.getSchema(),
132: currModel, prefix);
133: newImport = Utility.addSchemaImport(schemaModel,
134: currModel);
135: } finally {
136: if (currModel.isIntransaction()) {
137: currModel.endTransaction();
138: }
139: }
140: if (newImport != null) {
141: ActionHelper.selectNode(newImport, currModel);
142: }
143: }
144: return null;
145: }
146:
147: private String validate(SchemaModel model, WSDLModel wsdlModel) {
148: if (model.getState() != SchemaModel.State.VALID) {
149: return NbBundle.getMessage(SchemaImportPasteType.class,
150: "ERRMSG_SchemaIsNotValid");
151: }
152:
153: if (!Utility.canImport(model, wsdlModel)) {
154: return NbBundle.getMessage(SchemaImportPasteType.class,
155: "ERRMSG_ProjectNotReferenceable");
156: }
157: //For wsdl, imported schema's should have a namespace.
158:
159: String tns = model.getSchema().getTargetNamespace();
160: if (tns == null) {
161: return NbBundle.getMessage(SchemaImportPasteType.class,
162: "ERRMSG_NoNamespace");
163: }
164:
165: if (model.equals(wsdlModel)) {
166: return NbBundle.getMessage(SchemaImportPasteType.class,
167: "ERRMSG_SameModel");
168: }
169:
170: Definitions def = wsdlModel.getDefinitions();
171: String namespace = def.getTargetNamespace();
172: // This is an import, which must have no namespace, or a
173: // different one than the customized component.
174: if (tns != null && !Utilities.NO_NAME_SPACE.equals(tns)
175: && namespace.equals(tns)) {
176: return NbBundle.getMessage(SchemaImportPasteType.class,
177: "ERRMSG_SameNamespace");
178: }
179:
180: Types types = def.getTypes();
181:
182: if (types == null) {
183: return null;
184: }
185:
186: Collection<WSDLSchema> wsdlSchemas = types
187: .getExtensibilityElements(WSDLSchema.class);
188: for (WSDLSchema wSchema : wsdlSchemas) {
189: SchemaModel sm = wSchema.getSchemaModel();
190: Collection<SchemaModelReference> references = sm
191: .getSchema().getSchemaReferences();
192: // Ensure the selected document is not already among the
193: // set that have been included.
194: for (SchemaModelReference ref : references) {
195: try {
196: SchemaModel otherModel = ref
197: .resolveReferencedModel();
198: if (model.equals(otherModel)) {
199: return NbBundle.getMessage(
200: SchemaImportPasteType.class,
201: "ERRMSG_AlreadyRefd");
202: }
203: } catch (CatalogModelException cme) {
204: // Ignore that one as it does not matter.
205: }
206: }
207: }
208:
209: return null;
210: }
211: }
|