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-2006 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.vmd.midp.general;
043:
044: import org.netbeans.modules.vmd.api.model.*;
045: import org.netbeans.modules.vmd.api.model.common.AcceptPresenter;
046: import org.netbeans.modules.vmd.api.model.common.AcceptSuggestion;
047: import org.netbeans.modules.vmd.api.model.common.DocumentSupport;
048: import org.netbeans.modules.vmd.midp.components.MidpArraySupport;
049: import org.netbeans.modules.vmd.midp.components.MidpProjectSupport;
050: import org.openide.filesystems.FileObject;
051: import org.openide.filesystems.FileUtil;
052: import org.openide.loaders.DataObject;
053: import org.openide.nodes.Node;
054: import org.openide.nodes.NodeTransfer;
055:
056: import java.awt.datatransfer.Transferable;
057: import java.io.File;
058: import java.io.FileInputStream;
059: import java.io.FileNotFoundException;
060: import java.io.InputStream;
061: import java.util.Arrays;
062: import java.util.HashMap;
063: import java.util.Map;
064:
065: /**
066: *
067: * @author Karol Harezlak
068: */
069: public abstract class FileAcceptPresenter extends AcceptPresenter {
070:
071: private Map<String, TypeID> extensionsMap;
072: private Map<String, String> propertyNamesMap;
073:
074: public FileAcceptPresenter(String propertyName, TypeID typeID,
075: String... fileExtensions) {
076: super (AcceptPresenter.Kind.TRANSFERABLE);
077: extensionsMap = new HashMap<String, TypeID>();
078: propertyNamesMap = new HashMap<String, String>();
079: addFileExtensions(propertyName, typeID, fileExtensions);
080: }
081:
082: public FileAcceptPresenter addFileExtensions(String propertyName,
083: TypeID typeID, String... fileExtensions) {
084: assert typeID != null;
085: assert fileExtensions.length != 0;
086:
087: for (String fe : fileExtensions) {
088: extensionsMap.put(fe, typeID);
089: propertyNamesMap.put(fe, propertyName);
090: }
091: return this ;
092: }
093:
094: @Override
095: public boolean isAcceptable(Transferable transferable,
096: AcceptSuggestion suggestion) {
097: assert (!extensionsMap.isEmpty());
098: FileObject fileObject = getNodeFileObject(transferable);
099: if (fileObject == null && !belongsToProject(fileObject))
100: return false;
101: DesignDocument document = getComponent().getDocument();
102: Map<FileObject, String> mapFile = MidpProjectSupport
103: .getAllFilesForProjectByExt(document, extensionsMap
104: .keySet());
105: if (mapFile.get(fileObject) == null)
106: return false;
107: TypeID typeID = getTypeForExtension(fileObject.getExt());
108: if (typeID != null)
109: return true;
110: return false;
111: }
112:
113: @Override
114: public ComponentProducer.Result accept(Transferable transferable,
115: AcceptSuggestion suggestion) {
116: FileObject fileObject = getNodeFileObject(transferable);
117: TypeID typeID = getTypeForExtension(fileObject.getExt());
118: String propertyName = getPropertyNameForExtension(fileObject
119: .getExt());
120: if (propertyName == null)
121: return super .accept(transferable, suggestion);
122: DesignDocument document = getComponent().getDocument();
123: final ComponentProducer producer = DocumentSupport
124: .getComponentProducer(document, typeID.toString());
125: if (document == null || producer == null)
126: return super .accept(transferable, suggestion);
127: DesignComponent newComponent = producer.createComponent(
128: document).getMainComponent();
129: if (getComponent().readProperty(propertyName).getKind() == PropertyValue.Kind.ARRAY)
130: MidpArraySupport.append(getComponent(), propertyName,
131: newComponent);
132: else
133: getComponent().writeProperty(
134: propertyName,
135: PropertyValue
136: .createComponentReference(newComponent));
137: return new ComponentProducer.Result(newComponent);
138: }
139:
140: protected boolean belongsToProject(FileObject fileObject) {
141: DesignDocument document = getComponent().getDocument();
142: Map<FileObject, String> fileMap = MidpProjectSupport
143: .getAllFilesForProjectByExt(document, extensionsMap
144: .keySet());
145: return fileMap.get(fileObject) != null;
146: }
147:
148: protected InputStream getInputStream(Transferable transferable) {
149: try {
150: FileObject fileNode = getNodeFileObject(transferable);
151: return fileNode != null ? fileNode.getInputStream() : null;
152: } catch (FileNotFoundException ex) {
153: Debug.warning(ex);
154: }
155: return null;
156: }
157:
158: protected String getFileClasspath(FileObject fileObject) {
159: DesignDocument document = getComponent().getDocument();
160: Map<FileObject, String> fileMap = MidpProjectSupport
161: .getAllFilesForProjectByExt(document, Arrays
162: .asList(fileObject.getExt()));
163: return fileMap.get(fileObject);
164: }
165:
166: protected FileObject getNodeFileObject(Transferable transferable) {
167: Node node = NodeTransfer.node(transferable,
168: NodeTransfer.DND_COPY_OR_MOVE);
169: if (node == null)
170: return null;
171: DataObject dataObject = (DataObject) node.getLookup().lookup(
172: DataObject.class);
173: if (dataObject == null)
174: return null;
175: FileObject file = dataObject.getPrimaryFile();
176: return file;
177: }
178:
179: private TypeID getTypeForExtension(String fe) {
180: for (String key : extensionsMap.keySet()) {
181: if (key.equalsIgnoreCase(fe))
182: return extensionsMap.get(key);
183: }
184: return null;
185: }
186:
187: private String getPropertyNameForExtension(String fe) {
188: for (String key : propertyNamesMap.keySet()) {
189: if (key.equalsIgnoreCase(fe))
190: return propertyNamesMap.get(key);
191: }
192: return null;
193: }
194:
195: }
|