1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.components;
14
15 /***
16 * This is an example of a component, which serves as a DragSource as
17 * well as Drop Target.
18 * To illustrate the concept, JList has been used as a droppable target
19 * and a draggable source.
20 * Any component can be used instead of a JList.
21 * The code also contains debugging messages which can be used for
22 * diagnostics and understanding the flow of events.
23 *
24 * @version 1.0
25 */
26
27 import java.awt.datatransfer.DataFlavor;
28 import java.awt.datatransfer.StringSelection;
29 import java.awt.datatransfer.Transferable;
30 import java.awt.datatransfer.UnsupportedFlavorException;
31 import java.awt.dnd.DnDConstants;
32 import java.awt.dnd.DragGestureEvent;
33 import java.awt.dnd.DragGestureListener;
34 import java.awt.dnd.DragSource;
35 import java.awt.dnd.DragSourceDragEvent;
36 import java.awt.dnd.DragSourceDropEvent;
37 import java.awt.dnd.DragSourceEvent;
38 import java.awt.dnd.DragSourceListener;
39 import java.awt.dnd.DropTarget;
40 import java.awt.dnd.DropTargetDragEvent;
41 import java.awt.dnd.DropTargetDropEvent;
42 import java.awt.dnd.DropTargetEvent;
43 import java.awt.dnd.DropTargetListener;
44 import java.io.IOException;
45
46 import javax.swing.DefaultListModel;
47 import javax.swing.JList;
48 import javax.swing.ListModel;
49
50
51
52
53 public class DNDList extends JList
54 implements DropTargetListener,DragSourceListener, DragGestureListener {
55
56 /***
57 * enables this component to be a dropTarget
58 */
59
60 DropTarget dropTarget = null;
61
62 /***
63 * enables this component to be a Drag Source
64 */
65 DragSource dragSource = null;
66
67
68 /***
69 * constructor - initializes the DropTarget and DragSource.
70 */
71
72 public DNDList( ListModel dataModel ) {
73 super( dataModel );
74 dropTarget = new DropTarget (this, this);
75 dragSource = new DragSource();
76 dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this);
77 }
78
79 /***
80 * is invoked when you are dragging over the DropSite
81 *
82 */
83
84 public void dragEnter (DropTargetDragEvent event) {
85
86
87 System.out.println( "dragEnter");
88 event.acceptDrag (DnDConstants.ACTION_MOVE);
89 }
90
91 /***
92 * is invoked when you are exit the DropSite without dropping
93 *
94 */
95
96 public void dragExit (DropTargetEvent event) {
97 System.out.println( "dragExit");
98
99 }
100
101 /***
102 * is invoked when a drag operation is going on
103 *
104 */
105
106 public void dragOver (DropTargetDragEvent event) {
107 System.out.println( "dragOver");
108 }
109
110 /***
111 * a drop has occurred
112 *
113 */
114
115
116 public void drop (DropTargetDropEvent event) {
117
118 try {
119 Transferable transferable = event.getTransferable();
120
121
122 if (transferable.isDataFlavorSupported (DataFlavor.stringFlavor)){
123
124 event.acceptDrop(DnDConstants.ACTION_MOVE);
125 String s = (String)transferable.getTransferData ( DataFlavor.stringFlavor);
126 addElement( s );
127 event.getDropTargetContext().dropComplete(true);
128 }
129 else{
130 event.rejectDrop();
131 }
132 }
133 catch (IOException exception) {
134 exception.printStackTrace();
135 System.err.println( "Exception" + exception.getMessage());
136 event.rejectDrop();
137 }
138 catch (UnsupportedFlavorException ufException ) {
139 ufException.printStackTrace();
140 System.err.println( "Exception" + ufException.getMessage());
141 event.rejectDrop();
142 }
143 }
144
145 /***
146 * is invoked if the use modifies the current drop gesture
147 *
148 */
149
150
151 public void dropActionChanged ( DropTargetDragEvent event ) {
152 }
153
154 /***
155 * a drag gesture has been initiated
156 *
157 */
158
159 public void dragGestureRecognized( DragGestureEvent event) {
160
161 Object selected = getSelectedValue();
162 if ( selected != null ){
163 StringSelection text = new StringSelection( selected.toString());
164
165
166 dragSource.startDrag (event, DragSource.DefaultMoveDrop, text, this);
167 } else {
168 System.out.println( "nothing was selected");
169 }
170 }
171
172 /***
173 * this message goes to DragSourceListener, informing it that the dragging
174 * has ended
175 *
176 */
177
178 public void dragDropEnd (DragSourceDropEvent event) {
179 if ( event.getDropSuccess()){
180 removeElement();
181 }
182 }
183
184 /***
185 * this message goes to DragSourceListener, informing it that the dragging
186 * has entered the DropSite
187 *
188 */
189
190 public void dragEnter (DragSourceDragEvent event) {
191 System.out.println( " dragEnter");
192 }
193
194 /***
195 * this message goes to DragSourceListener, informing it that the dragging
196 * has exited the DropSite
197 *
198 */
199
200 public void dragExit (DragSourceEvent event) {
201 System.out.println( "dragExit");
202
203 }
204
205 /***
206 * this message goes to DragSourceListener, informing it that the dragging is currently
207 * ocurring over the DropSite
208 *
209 */
210
211 public void dragOver (DragSourceDragEvent event) {
212 System.out.println( "dragExit");
213
214 }
215
216 /***
217 * is invoked when the user changes the dropAction
218 *
219 */
220
221 public void dropActionChanged ( DragSourceDragEvent event) {
222 System.out.println( "dropActionChanged");
223 }
224
225 /***
226 * adds elements to itself
227 *
228 */
229
230 public void addElement( Object s ){
231 (( DefaultListModel )getModel()).addElement (s.toString());
232 }
233
234 /***
235 * removes an element from itself
236 */
237
238 public void removeElement(){
239 (( DefaultListModel)getModel()).removeElement( getSelectedValue());
240 }
241
242 }