001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util.dnd;
019:
020: import java.awt.datatransfer.DataFlavor;
021: import java.awt.datatransfer.Transferable;
022: import java.awt.datatransfer.UnsupportedFlavorException;
023: import java.awt.dnd.DnDConstants;
024: import java.awt.dnd.DragGestureEvent;
025: import java.awt.dnd.DragGestureListener;
026: import java.awt.dnd.DragSource;
027: import java.awt.dnd.DragSourceDragEvent;
028: import java.awt.dnd.DragSourceDropEvent;
029: import java.awt.dnd.DragSourceEvent;
030: import java.awt.dnd.DragSourceListener;
031: import java.awt.dnd.DropTarget;
032: import java.awt.dnd.DropTargetDragEvent;
033: import java.awt.dnd.DropTargetDropEvent;
034: import java.awt.dnd.DropTargetEvent;
035: import java.awt.dnd.DropTargetListener;
036: import java.io.IOException;
037:
038: import javax.swing.JTextField;
039:
040: import de.finix.contelligent.client.base.ContelligentComponent;
041:
042: public class JTextFieldDND extends JTextField implements
043: DropTargetListener, DragSourceListener, DragGestureListener {
044: public JTextFieldDND() {
045: super ();
046: DropTarget dropTarget = new DropTarget(this , this );
047: DragSource dragSource = new DragSource();
048: dragSource.createDefaultDragGestureRecognizer(this ,
049: DnDConstants.ACTION_MOVE, this );
050: }
051:
052: public JTextFieldDND(String text) {
053: super (text);
054: DropTarget dropTarget = new DropTarget(this , this );
055: DragSource dragSource = new DragSource();
056: dragSource.createDefaultDragGestureRecognizer(this ,
057: DnDConstants.ACTION_MOVE, this );
058: }
059:
060: /**
061: * is invoked when you are dragging over the DropSite
062: */
063: public void dragEnter(DropTargetDragEvent event) {
064: // debug messages for diagnostics
065: if (getDragEnabled()) {
066: event.acceptDrag(DnDConstants.ACTION_MOVE);
067: }
068: }
069:
070: /**
071: * is invoked when you are exit the DropSite without dropping
072: */
073: public void dragExit(DropTargetEvent event) {
074: }
075:
076: /**
077: * is invoked when a drag operation is going on
078: */
079: public void dragOver(DropTargetDragEvent event) {
080: }
081:
082: /**
083: * a drop has occurred
084: */
085: public void drop(DropTargetDropEvent event) {
086: if (getDragEnabled()) {
087: try {
088: Transferable transferable = event.getTransferable();
089:
090: int offset = getCaretPosition();
091:
092: // we accept ContelligentComponent and Strings
093: if (transferable
094: .isDataFlavorSupported(SingleComponentTransferable.componentFlavor)) {
095: event.acceptDrop(DnDConstants.ACTION_MOVE);
096: event.getDropTargetContext().dropComplete(true);
097: ContelligentComponent component = (ContelligentComponent) transferable
098: .getTransferData(SingleComponentTransferable.componentFlavor);
099: setText(component.getPath());
100: fireActionPerformed();
101: } else if (transferable
102: .isDataFlavorSupported(DataFlavor.stringFlavor)) {
103: event.acceptDrop(DnDConstants.ACTION_MOVE);
104: event.getDropTargetContext().dropComplete(true);
105: String draggedObjectAsString = (String) transferable
106: .getTransferData(DataFlavor.stringFlavor);
107: setText(draggedObjectAsString);
108: } else {
109: event.rejectDrop();
110: }
111: } catch (IOException exception) {
112: exception.printStackTrace();
113: event.rejectDrop();
114: } catch (UnsupportedFlavorException ufException) {
115: ufException.printStackTrace();
116: event.rejectDrop();
117: }
118: }
119: }
120:
121: /**
122: * is invoked if the use modifies the current drop gesture
123: */
124: public void dropActionChanged(DropTargetDragEvent event) {
125: }
126:
127: /**
128: * a drag gesture has been initiated
129: */
130: public void dragGestureRecognized(DragGestureEvent event) {
131: }
132:
133: /**
134: * this message goes to DragSourceListener, informing it that the dragging
135: * has ended
136: *
137: */
138: public void dragDropEnd(DragSourceDropEvent event) {
139: if (event.getDropSuccess()) {
140: // removeElement((ContelligentComponent)event.getDragSourceContext().getTransferable());
141: }
142: }
143:
144: /**
145: * this message goes to DragSourceListener, informing it that the dragging
146: * has entered the DropSite
147: */
148: public void dragEnter(DragSourceDragEvent event) {
149: }
150:
151: /**
152: * this message goes to DragSourceListener, informing it that the dragging
153: * has exited the DropSite
154: */
155: public void dragExit(DragSourceEvent event) {
156: }
157:
158: /**
159: * this message goes to DragSourceListener, informing it that the dragging
160: * is currently ocurring over the DropSite
161: */
162: public void dragOver(DragSourceDragEvent event) {
163: }
164:
165: /**
166: * is invoked when the user changes the dropAction
167: */
168: public void dropActionChanged(DragSourceDragEvent event) {
169: }
170: }
|