View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.dnd;
14  
15  import java.awt.Component;
16  import java.awt.Point;
17  import java.awt.datatransfer.DataFlavor;
18  import java.awt.datatransfer.Transferable;
19  import java.awt.dnd.DnDConstants;
20  import java.awt.dnd.DropTarget;
21  import java.awt.dnd.DropTargetDragEvent;
22  import java.awt.dnd.DropTargetDropEvent;
23  import java.awt.dnd.DropTargetEvent;
24  import java.awt.dnd.DropTargetListener;
25  
26  import com.eviware.soapui.SoapUI;
27  
28  public abstract class AbstractSoapUIDropTarget implements DropTargetListener
29  {
30  	public AbstractSoapUIDropTarget( )
31  	{
32  	}
33  	
34  	public void dragEnter( DropTargetDragEvent dtde )
35  	{
36  		if( !isAcceptable( dtde.getTransferable(), dtde.getLocation() ) )
37  			dtde.rejectDrag();
38  	}
39  
40  	public void dragExit( DropTargetEvent dtde )
41  	{
42  	}
43  
44  	public void dragOver( DropTargetDragEvent dtde )
45  	{
46  		if( !isAcceptable( dtde.getTransferable(), dtde.getLocation() ) )
47  		{
48  			dtde.rejectDrag();
49  		}
50  		else
51  		{
52  			dtde.acceptDrag( dtde.getDropAction() );
53  		}
54  	}
55  
56  	public void drop( DropTargetDropEvent dtde )
57  	{
58  		if( !isAcceptable( dtde.getTransferable(), dtde.getLocation() ) )
59  		{
60  			dtde.rejectDrop();
61  		}
62  		else
63  		{
64  			try
65  			{
66  				Object testCase = getTransferData( dtde.getTransferable() );
67  				if( testCase != null )
68  				{
69  					dtde.acceptDrop( dtde.getDropAction() );
70  					
71  					handleDrop( testCase, dtde.getLocation() );
72  					
73  					dtde.dropComplete( true );
74  				}
75  			}
76  			catch( Exception e )
77  			{
78  				SoapUI.logError( e );
79  			}
80  		}
81  	}
82  
83  	protected abstract boolean handleDrop( Object target, Point point );
84  	
85  	protected abstract boolean isAcceptable( Object target, Point point );
86  
87  	public void dropActionChanged( DropTargetDragEvent dtde )
88  	{
89  	}
90  	
91  	public boolean isAcceptable( Transferable transferable, Point point )
92  	{
93  		return isAcceptable( getTransferData( transferable ), point);
94  	}
95  	
96  	@SuppressWarnings("unchecked")
97  	private Object getTransferData( Transferable transferable )
98  	{
99  		DataFlavor[] flavors = transferable.getTransferDataFlavors();
100 		for( int i = 0; i < flavors.length; i++ )
101 		{
102 			DataFlavor flavor = flavors[i];
103 			if( flavor.isMimeTypeEqual( DataFlavor.javaJVMLocalObjectMimeType ) )
104 			{
105 				try
106 				{
107 					return  transferable.getTransferData( flavor );
108 				}
109 				catch( Exception ex )
110 				{
111 					SoapUI.logError( ex );
112 				}
113 			}
114 		}
115 		
116 		return null;
117 	}
118 	
119 	public static void addDropTarget( Component component, AbstractSoapUIDropTarget target )
120 	{
121 		DropTarget dropTarget = new DropTarget( component, target );
122 		dropTarget.setDefaultActions( DnDConstants.ACTION_COPY_OR_MOVE );
123 	}
124 }