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: * CodeClipDragAndDropHandler.java
043: *
044: * Created on August 2, 2006, 11:52 AM
045: *
046: * The drag and drop handler for palettes with codeclips.
047: *
048: * @author Joelle Lam <joelle.lam@sun.com>
049: * @version %I%, %G%
050: */
051:
052: package org.netbeans.modules.visualweb.palette.api;
053:
054: import org.netbeans.modules.visualweb.palette.codeclips.CodeClipUtilities;
055: import java.awt.datatransfer.DataFlavor;
056: import java.awt.datatransfer.Transferable;
057: import java.awt.datatransfer.UnsupportedFlavorException;
058: import java.io.IOException;
059: import java.util.MissingResourceException;
060: import org.netbeans.spi.palette.DragAndDropHandler;
061: import org.netbeans.spi.palette.PaletteController;
062: import org.openide.ErrorManager;
063: import org.openide.filesystems.FileObject;
064: import org.openide.loaders.DataFolder;
065: import org.openide.loaders.DataObject;
066: import org.openide.util.Lookup;
067: import org.openide.util.NbBundle;
068: import org.openide.util.datatransfer.ExTransferable;
069:
070: public class CodeClipDragAndDropHandler extends DragAndDropHandler {
071:
072: /** Creates a new instance of CodeClipDragAndDropHandler */
073: public CodeClipDragAndDropHandler() {
074: }
075:
076: /**
077: * Add your own custom DataFlavor as need to suppor drag-over a different
078: * parts of editor area.
079: *
080: * @param t Item's default Transferable.
081: * @param item Palette item's Lookup.
082: *
083: */
084: public void customize(ExTransferable exTransferable, Lookup lookup) {
085: //This is only called when dragging items off palette.
086: }
087:
088: /**
089: * @param targetCategory Lookup of the category under the drop cursor.
090: * @param flavors Supported DataFlavors.
091: * @param dndAction Drop action type.
092: *
093: * @return True if the given category can accept the item being dragged.
094: */
095: public boolean canDrop(Lookup targetCategory, DataFlavor[] flavors,
096: int dndAction) {
097: for (int i = 0; i < flavors.length; i++) {
098: if (PaletteController.ITEM_DATA_FLAVOR.equals(flavors[i])) {
099: return true;
100: } else if (java.awt.datatransfer.DataFlavor.stringFlavor
101: .equals(flavors[i])) {
102: return true;
103: }
104: }
105: return false;
106: }
107:
108: /**
109: * Perform the drop operation and add the dragged item into the given category.
110: *
111: * @param targetCategory Lookup of the category that accepts the drop.
112: * @param item Transferable holding the item being dragged.
113: * @param dndAction Drag'n'drop action type.
114: * @param dropIndex Zero-based position where the dragged item should be dropped.
115: *
116: * @return True if the drop has been successful, false otherwise.
117: */
118: public boolean doDrop(Lookup targetCategory, Transferable item,
119: int dndAction, int dropIndex) {
120: String body;
121:
122: if (item
123: .isDataFlavorSupported(PaletteController.ITEM_DATA_FLAVOR)) {
124: return super .doDrop(targetCategory, item, dndAction,
125: dropIndex);
126: }
127:
128: try {
129: body = (String) item
130: .getTransferData(java.awt.datatransfer.DataFlavor.stringFlavor);
131: } catch (IOException ex) {
132: ErrorManager.getDefault().notify(
133: ErrorManager.INFORMATIONAL, ex);
134: return false;
135: } catch (UnsupportedFlavorException ex) {
136: ErrorManager.getDefault().notify(
137: ErrorManager.INFORMATIONAL, ex);
138: return false;
139: }
140:
141: DataFolder categoryDatafolder = (DataFolder) targetCategory
142: .lookup(DataFolder.class);
143: FileObject folderObject = ((DataObject) categoryDatafolder)
144: .getPrimaryFile();
145: String localizingBundle = "org.netbeans.modules.visualweb.palette.codeclips.Bundle";
146:
147: try {
148: // String displayNameString = "CLIP";
149: String displayNameString = NbBundle.getMessage(
150: CodeClipDragAndDropHandler.class, "CLIP");
151: //This causes a problem if it tries to find "Clip" during the drop.
152: CodeClipUtilities.createCodeClipFile(folderObject, body,
153: displayNameString, localizingBundle, null);
154: } catch (IOException ex) {
155: ErrorManager.getDefault().notify(ex);
156: } catch (MissingResourceException mre) {
157: ErrorManager.getDefault().notify(mre);
158: }
159:
160: return true;
161: }
162:
163: }
|