1 /* 2 * The contents of this file are subject to the terms of the Common Development 3 * and Distribution License (the License). You may not use this file except in 4 * compliance with the License. 5 * 6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html 7 * or http://www.netbeans.org/cddl.txt. 8 * 9 * When distributing Covered Code, include this CDDL Header Notice in each file 10 * and include the License file at http://www.netbeans.org/cddl.txt. 11 * If applicable, add the following below the CDDL Header, with the fields 12 * enclosed by brackets [] replaced by your own identifying information: 13 * "Portions Copyrighted [year] [name of copyright owner]" 14 * 15 * The Original Software is NetBeans. The Initial Developer of the Original 16 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun 17 * Microsystems, Inc. All Rights Reserved. 18 */ 19 20 package org.openide.text; 21 22 import java.awt.datatransfer.DataFlavor; 23 import javax.swing.text.JTextComponent; 24 25 /** 26 * ActiveEditorDrop with artificial DataFlavor. Drag and drop initiator sometimes needs 27 * to be notified about a target component, where the drop operation was performed. 28 * Initiator should implement this interface and use the required artificial DataFlavor. 29 * Component that will support drop operation of the ActiveEditorDrop should call handleTransfer 30 * method. 31 * <br> 32 * Sample usage of the client: <br> 33 * <pre> 34 * private class MyDrop extends StringSelection implements ActiveEditorDrop { 35 * 36 * public MyDrop(String text){ 37 * super(text); //NOI18N 38 * } 39 * 40 * public boolean isDataFlavorSupported(DataFlavor f) { 41 * return super.isDataFlavorSupported(f) || ActiveEditorDrop.FLAVOR == f; 42 * } 43 * 44 * public final DataFlavor[] getTransferDataFlavors() { 45 * DataFlavor delegatorFlavor[] = super.getTransferDataFlavors(); 46 * int delegatorFlavorLength = delegatorFlavor.length; 47 * DataFlavor newArray[] = new DataFlavor[delegatorFlavorLength + 1]; 48 * System.arraycopy(delegatorFlavor, 0, newArray, 0, delegatorFlavorLength); 49 * newArray[delegatorFlavorLength] = ActiveEditorDrop.FLAVOR; 50 * return newArray; 51 * } 52 * 53 * public final Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 54 * if (flavor == ActiveEditorDrop.FLAVOR) { 55 * return this; 56 * } 57 * return super.getTransferData(flavor); 58 * } 59 * 60 * public boolean handleTransfer(JTextComponent targetComponent) { 61 * // your implementation 62 * } 63 * } 64 * </pre> 65 * 66 * or simplified solution: <br> 67 * <pre> 68 * 69 * private class MyDrop implements ActiveEditorDrop, Transferable { 70 * 71 * public MyDrop(){ 72 * } 73 * 74 * public boolean isDataFlavorSupported(DataFlavor f) { 75 * return ActiveEditorDrop.FLAVOR == f; 76 * } 77 * 78 * public final DataFlavor[] getTransferDataFlavors() { 79 * DataFlavor delegatorFlavor[] = new DataFlavor[1]; 80 * delegatorFlavor[0] = ActiveEditorDrop.FLAVOR; 81 * return delegatorFlavor; 82 * } 83 * 84 * public final Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 85 * return (flavor == ActiveEditorDrop.FLAVOR) ? this : null; 86 * } 87 * 88 * public boolean handleTransfer(JTextComponent targetComponent) { 89 * //your implementation 90 * } 91 * 92 * } 93 * 94 * </pre> 95 * 96 * 97 * @author Martin Roskanin 98 * @since org.openide.text 6.5 99 */ 100 public interface ActiveEditorDrop { 101 102 /** 103 * Active editor DataFlavor used for communication between DragSource and DragTarget. 104 * This DataFlavor should be used for case where target component is instance 105 * of JTextComponent. 106 */ 107 DataFlavor FLAVOR = QuietEditorPane.constructActiveEditorDropFlavor(); 108 109 /** 110 * A method called from the drop target that supports the artificial DataFlavor. 111 * @param targetComponent a Component where drop operation occured. 112 * @return true if implementor allowed a drop operation into the targetComponent 113 */ 114 boolean handleTransfer(JTextComponent targetComponent); 115 116 } 117