View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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.dnd.DnDConstants;
31  import java.awt.dnd.DragGestureEvent;
32  import java.awt.dnd.DragGestureListener;
33  import java.awt.dnd.DragSource;
34  import java.awt.dnd.DragSourceDragEvent;
35  import java.awt.dnd.DragSourceDropEvent;
36  import java.awt.dnd.DragSourceEvent;
37  import java.awt.dnd.DragSourceListener;
38  import java.awt.dnd.DropTarget;
39  import java.awt.dnd.DropTargetDragEvent;
40  import java.awt.dnd.DropTargetDropEvent;
41  import java.awt.dnd.DropTargetEvent;
42  import java.awt.dnd.DropTargetListener;
43  
44  import javax.swing.DefaultListModel;
45  import javax.swing.JList;
46  import javax.swing.ListModel;
47  
48  import com.eviware.soapui.SoapUI;
49  
50  public class DNDList extends JList implements DropTargetListener, DragSourceListener, DragGestureListener
51  {
52  
53  	/***
54  	 * enables this component to be a dropTarget
55  	 */
56  
57  	DropTarget dropTarget = null;
58  
59  	/***
60  	 * enables this component to be a Drag Source
61  	 */
62  	DragSource dragSource = null;
63  
64  	/***
65  	 * constructor - initializes the DropTarget and DragSource.
66  	 */
67  
68  	public DNDList( ListModel dataModel )
69  	{
70  		super( dataModel );
71  		dropTarget = new DropTarget( this, this );
72  		dragSource = new DragSource();
73  		dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this );
74  	}
75  
76  	/***
77  	 * is invoked when you are dragging over the DropSite
78  	 * 
79  	 */
80  
81  	public void dragEnter( DropTargetDragEvent event )
82  	{
83  
84  		// debug messages for diagnostics
85  		System.out.println( "dragEnter" );
86  		event.acceptDrag( DnDConstants.ACTION_MOVE );
87  	}
88  
89  	/***
90  	 * is invoked when you are exit the DropSite without dropping
91  	 * 
92  	 */
93  
94  	public void dragExit( DropTargetEvent event )
95  	{
96  		System.out.println( "dragExit" );
97  
98  	}
99  
100 	/***
101 	 * is invoked when a drag operation is going on
102 	 * 
103 	 */
104 
105 	public void dragOver( DropTargetDragEvent event )
106 	{
107 		System.out.println( "dragOver" );
108 	}
109 
110 	/***
111 	 * a drop has occurred
112 	 * 
113 	 */
114 
115 	public void drop( DropTargetDropEvent event )
116 	{
117 
118 		try
119 		{
120 			Transferable transferable = event.getTransferable();
121 
122 			// we accept only Strings
123 			if( transferable.isDataFlavorSupported( DataFlavor.stringFlavor ) )
124 			{
125 
126 				event.acceptDrop( DnDConstants.ACTION_MOVE );
127 				String s = ( String )transferable.getTransferData( DataFlavor.stringFlavor );
128 				addElement( s );
129 				event.getDropTargetContext().dropComplete( true );
130 			}
131 			else
132 			{
133 				event.rejectDrop();
134 			}
135 		}
136 		catch( Exception exception )
137 		{
138 			SoapUI.logError( exception );
139 			System.err.println( "Exception" + exception.getMessage() );
140 			event.rejectDrop();
141 		}
142 	}
143 
144 	/***
145 	 * is invoked if the use modifies the current drop gesture
146 	 * 
147 	 */
148 
149 	public void dropActionChanged( DropTargetDragEvent event )
150 	{
151 	}
152 
153 	/***
154 	 * a drag gesture has been initiated
155 	 * 
156 	 */
157 
158 	public void dragGestureRecognized( DragGestureEvent event )
159 	{
160 
161 		Object selected = getSelectedValue();
162 		if( selected != null )
163 		{
164 			StringSelection text = new StringSelection( selected.toString() );
165 
166 			// as the name suggests, starts the dragging
167 			dragSource.startDrag( event, DragSource.DefaultMoveDrop, text, this );
168 		}
169 		else
170 		{
171 			System.out.println( "nothing was selected" );
172 		}
173 	}
174 
175 	/***
176 	 * this message goes to DragSourceListener, informing it that the dragging
177 	 * has ended
178 	 * 
179 	 */
180 
181 	public void dragDropEnd( DragSourceDropEvent event )
182 	{
183 		if( event.getDropSuccess() )
184 		{
185 			removeElement();
186 		}
187 	}
188 
189 	/***
190 	 * this message goes to DragSourceListener, informing it that the dragging
191 	 * has entered the DropSite
192 	 * 
193 	 */
194 
195 	public void dragEnter( DragSourceDragEvent event )
196 	{
197 		System.out.println( " dragEnter" );
198 	}
199 
200 	/***
201 	 * this message goes to DragSourceListener, informing it that the dragging
202 	 * has exited the DropSite
203 	 * 
204 	 */
205 
206 	public void dragExit( DragSourceEvent event )
207 	{
208 		System.out.println( "dragExit" );
209 
210 	}
211 
212 	/***
213 	 * this message goes to DragSourceListener, informing it that the dragging is
214 	 * currently ocurring over the DropSite
215 	 * 
216 	 */
217 
218 	public void dragOver( DragSourceDragEvent event )
219 	{
220 		System.out.println( "dragExit" );
221 
222 	}
223 
224 	/***
225 	 * is invoked when the user changes the dropAction
226 	 * 
227 	 */
228 
229 	public void dropActionChanged( DragSourceDragEvent event )
230 	{
231 		System.out.println( "dropActionChanged" );
232 	}
233 
234 	/***
235 	 * adds elements to itself
236 	 * 
237 	 */
238 
239 	public void addElement( Object s )
240 	{
241 		( ( DefaultListModel )getModel() ).addElement( s.toString() );
242 	}
243 
244 	/***
245 	 * removes an element from itself
246 	 */
247 
248 	public void removeElement()
249 	{
250 		( ( DefaultListModel )getModel() ).removeElement( getSelectedValue() );
251 	}
252 
253 }