001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.components;
014:
015: /**
016: * This is an example of a component, which serves as a DragSource as
017: * well as Drop Target.
018: * To illustrate the concept, JList has been used as a droppable target
019: * and a draggable source.
020: * Any component can be used instead of a JList.
021: * The code also contains debugging messages which can be used for
022: * diagnostics and understanding the flow of events.
023: *
024: * @version 1.0
025: */
026:
027: import java.awt.datatransfer.DataFlavor;
028: import java.awt.datatransfer.StringSelection;
029: import java.awt.datatransfer.Transferable;
030: import java.awt.dnd.DnDConstants;
031: import java.awt.dnd.DragGestureEvent;
032: import java.awt.dnd.DragGestureListener;
033: import java.awt.dnd.DragSource;
034: import java.awt.dnd.DragSourceDragEvent;
035: import java.awt.dnd.DragSourceDropEvent;
036: import java.awt.dnd.DragSourceEvent;
037: import java.awt.dnd.DragSourceListener;
038: import java.awt.dnd.DropTarget;
039: import java.awt.dnd.DropTargetDragEvent;
040: import java.awt.dnd.DropTargetDropEvent;
041: import java.awt.dnd.DropTargetEvent;
042: import java.awt.dnd.DropTargetListener;
043:
044: import javax.swing.DefaultListModel;
045: import javax.swing.JList;
046: import javax.swing.ListModel;
047:
048: import com.eviware.soapui.SoapUI;
049:
050: public class DNDList extends JList implements DropTargetListener,
051: DragSourceListener, DragGestureListener {
052:
053: /**
054: * enables this component to be a dropTarget
055: */
056:
057: DropTarget dropTarget = null;
058:
059: /**
060: * enables this component to be a Drag Source
061: */
062: DragSource dragSource = null;
063:
064: /**
065: * constructor - initializes the DropTarget and DragSource.
066: */
067:
068: public DNDList(ListModel dataModel) {
069: super (dataModel);
070: dropTarget = new DropTarget(this , this );
071: dragSource = new DragSource();
072: dragSource.createDefaultDragGestureRecognizer(this ,
073: DnDConstants.ACTION_MOVE, this );
074: }
075:
076: /**
077: * is invoked when you are dragging over the DropSite
078: *
079: */
080:
081: public void dragEnter(DropTargetDragEvent event) {
082:
083: // debug messages for diagnostics
084: System.out.println("dragEnter");
085: event.acceptDrag(DnDConstants.ACTION_MOVE);
086: }
087:
088: /**
089: * is invoked when you are exit the DropSite without dropping
090: *
091: */
092:
093: public void dragExit(DropTargetEvent event) {
094: System.out.println("dragExit");
095:
096: }
097:
098: /**
099: * is invoked when a drag operation is going on
100: *
101: */
102:
103: public void dragOver(DropTargetDragEvent event) {
104: System.out.println("dragOver");
105: }
106:
107: /**
108: * a drop has occurred
109: *
110: */
111:
112: public void drop(DropTargetDropEvent event) {
113:
114: try {
115: Transferable transferable = event.getTransferable();
116:
117: // we accept only Strings
118: if (transferable
119: .isDataFlavorSupported(DataFlavor.stringFlavor)) {
120:
121: event.acceptDrop(DnDConstants.ACTION_MOVE);
122: String s = (String) transferable
123: .getTransferData(DataFlavor.stringFlavor);
124: addElement(s);
125: event.getDropTargetContext().dropComplete(true);
126: } else {
127: event.rejectDrop();
128: }
129: } catch (Exception exception) {
130: SoapUI.logError(exception);
131: System.err.println("Exception" + exception.getMessage());
132: event.rejectDrop();
133: }
134: }
135:
136: /**
137: * is invoked if the use modifies the current drop gesture
138: *
139: */
140:
141: public void dropActionChanged(DropTargetDragEvent event) {
142: }
143:
144: /**
145: * a drag gesture has been initiated
146: *
147: */
148:
149: public void dragGestureRecognized(DragGestureEvent event) {
150:
151: Object selected = getSelectedValue();
152: if (selected != null) {
153: StringSelection text = new StringSelection(selected
154: .toString());
155:
156: // as the name suggests, starts the dragging
157: dragSource.startDrag(event, DragSource.DefaultMoveDrop,
158: text, this );
159: } else {
160: System.out.println("nothing was selected");
161: }
162: }
163:
164: /**
165: * this message goes to DragSourceListener, informing it that the dragging
166: * has ended
167: *
168: */
169:
170: public void dragDropEnd(DragSourceDropEvent event) {
171: if (event.getDropSuccess()) {
172: removeElement();
173: }
174: }
175:
176: /**
177: * this message goes to DragSourceListener, informing it that the dragging
178: * has entered the DropSite
179: *
180: */
181:
182: public void dragEnter(DragSourceDragEvent event) {
183: System.out.println(" dragEnter");
184: }
185:
186: /**
187: * this message goes to DragSourceListener, informing it that the dragging
188: * has exited the DropSite
189: *
190: */
191:
192: public void dragExit(DragSourceEvent event) {
193: System.out.println("dragExit");
194:
195: }
196:
197: /**
198: * this message goes to DragSourceListener, informing it that the dragging is currently
199: * ocurring over the DropSite
200: *
201: */
202:
203: public void dragOver(DragSourceDragEvent event) {
204: System.out.println("dragExit");
205:
206: }
207:
208: /**
209: * is invoked when the user changes the dropAction
210: *
211: */
212:
213: public void dropActionChanged(DragSourceDragEvent event) {
214: System.out.println("dropActionChanged");
215: }
216:
217: /**
218: * adds elements to itself
219: *
220: */
221:
222: public void addElement(Object s) {
223: ((DefaultListModel) getModel()).addElement(s.toString());
224: }
225:
226: /**
227: * removes an element from itself
228: */
229:
230: public void removeElement() {
231: ((DefaultListModel) getModel())
232: .removeElement(getSelectedValue());
233: }
234:
235: }
|