Index: ActiveEditorDrop.java =================================================================== RCS file: ActiveEditorDrop.java diff -N ActiveEditorDrop.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ActiveEditorDrop.java 28 Jul 2005 07:51:42 -0000 @@ -0,0 +1,44 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.openide.text; + +import java.awt.Component; +import java.awt.datatransfer.DataFlavor; + +/** + * ActiveEditorDrop with artificial DataFlavor. Drag and drop initiator sometimes needs + * to be notified about a target component, where the drop operation was performed. + * Initiator should implement this interface and use the required artificial DataFlavor. + * Component that will support drop operation of the ActiveEditorDrop should call handleTransfer + * method. For details please refer {@link QuietEditorPane}. + * + * @author Martin Roskanin + */ +public interface ActiveEditorDrop { + + /** + * Active editor DataFlavor used for communication between DragSource and DragTarget. + * This DataFlavor should be used for case where target component is instance + * of JTextComponent. + */ + static final DataFlavor FLAVOR = + new DataFlavor("text/active_editor_flavor", "Active Editor Flavor"); //NOI18N + + /** + * A method called from the drop target that supports the artificial DataFlavor. + * @param targetComponent a Component where drop operation occured. + * @return true if implementor allowed a drop operation into the targetComponent + */ + abstract boolean handleTransfer(Component targetComponent); + +} Index: CallbackTransferable.java cvs server: could not get info for `CallbackTransferable.java': Not owner =================================================================== RCS file: CallbackTransferable.java diff -N CallbackTransferable.java cvs server: extra operand cvs server: Try `diff --help' for more information. Index: QuietEditorPane.java =================================================================== RCS file: /cvs/openide/text/src/org/openide/text/QuietEditorPane.java,v retrieving revision 1.1 diff -u -r1.1 QuietEditorPane.java --- QuietEditorPane.java 21 Apr 2005 20:16:02 -0000 1.1 +++ QuietEditorPane.java 28 Jul 2005 07:51:43 -0000 @@ -12,7 +12,16 @@ */ package org.openide.text; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.Transferable; +import java.awt.dnd.DropTarget; +import java.awt.event.InputEvent; +import javax.swing.Icon; +import javax.swing.JComponent; import javax.swing.JEditorPane; +import javax.swing.TransferHandler; +import javax.swing.plaf.UIResource; import javax.swing.text.*; @@ -32,7 +41,22 @@ /** is firing of events enabled? */ int working = FIRE; // [Mila] firing since begining, otherwise doesn't work well - + + + public void setDocument(Document doc) { + super.setDocument(doc); + + // Setting DelegatingTransferHandler, where CallbackTransferable will + // be handled in importData method. + // For more details, please refer issue #53439 + if (doc != null){ + TransferHandler thn = getTransferHandler(); + DelegatingTransferHandler dth = new DelegatingTransferHandler(thn); + setTransferHandler(dth); + } + } + + public void setWorking(int x) { working = x; } @@ -88,4 +112,86 @@ super.repaint(); } } + + /** + * Delegating TransferHandler. + * The main purpose is hooking on importData method where CallbackTransferable + * is handled. For more details, please refer issue #53439 + */ + private class DelegatingTransferHandler extends TransferHandler{ + + TransferHandler delegator; + + public DelegatingTransferHandler(TransferHandler delegator){ + this.delegator = delegator; + } + + public void exportAsDrag(JComponent comp, InputEvent e, int action) { + delegator.exportAsDrag(comp, e, action); + } + + public void exportToClipboard(JComponent comp, Clipboard clip, int action) { + delegator.exportToClipboard(comp, clip, action); + } + + public boolean importData(JComponent comp, Transferable t) { + try { + if (t.isDataFlavorSupported(ActiveEditorDrop.FLAVOR)){ + Object obj = t.getTransferData(ActiveEditorDrop.FLAVOR); + if (obj instanceof ActiveEditorDrop){ + return ((ActiveEditorDrop)obj).handleTransfer(comp); + } + } + } catch (Exception exc){ + exc.printStackTrace(); + } + return delegator.importData(comp, t); + } + + public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) { + return delegator.canImport(comp, transferFlavors); + } + + public int getSourceActions(JComponent c) { + return delegator.getSourceActions(c); + } + + public Icon getVisualRepresentation(Transferable t) { + return delegator.getVisualRepresentation(t); + } + + protected void exportDone(JComponent source, Transferable data, int action) { + try { + java.lang.reflect.Method method = delegator.getClass().getDeclaredMethod( + "exportDone", // NOI18N + new Class[] {javax.swing.JComponent.class, Transferable.class, int.class}); + method.setAccessible(true); + method.invoke(delegator, new Object[] {source, data, new Integer(action)}); + } catch (NoSuchMethodException ex) { + ex.printStackTrace(); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } catch (java.lang.reflect.InvocationTargetException ex) { + ex.printStackTrace(); + } + } + + protected Transferable createTransferable(JComponent comp) { + try { + java.lang.reflect.Method method = delegator.getClass().getDeclaredMethod( + "createTransferable", // NOI18N + new Class[] {javax.swing.JComponent.class}); + method.setAccessible(true); + return (Transferable)method.invoke(delegator, new Object[] {comp}); + } catch (NoSuchMethodException ex) { + ex.printStackTrace(); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } catch (java.lang.reflect.InvocationTargetException ex) { + ex.printStackTrace(); + } + return null; + } + } + }