View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.Point;
16  import java.awt.datatransfer.DataFlavor;
17  import java.awt.datatransfer.Transferable;
18  import java.awt.dnd.DropTargetDragEvent;
19  import java.awt.dnd.DropTargetDropEvent;
20  import java.awt.dnd.DropTargetEvent;
21  import java.awt.dnd.DropTargetListener;
22  
23  import javax.swing.JTable;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.model.ModelItem;
27  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionImpl;
29  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
30  import com.eviware.soapui.model.tree.nodes.PropertyTreeNode.PropertyModelItem;
31  import com.eviware.soapui.support.UISupport;
32  import com.eviware.soapui.support.xml.XmlUtils;
33  
34  public class JTableTestPropertyDropTarget implements DropTargetListener
35  {
36  	private final JTable table;
37  	private final ModelItem modelItem;
38  
39  	public JTableTestPropertyDropTarget( ModelItem modelItem, JTable table )
40  	{
41  		this.modelItem = modelItem;
42  		this.table = table;
43  	}
44  
45  	public void dragEnter( DropTargetDragEvent dtde )
46  	{
47  		if( !isAcceptable( dtde.getTransferable(), dtde.getLocation() ) )
48  			dtde.rejectDrag();
49  	}
50  
51  	public void dragExit( DropTargetEvent dtde )
52  	{
53  	}
54  
55  	public void dragOver( DropTargetDragEvent dtde )
56  	{
57  		if( !isAcceptable( dtde.getTransferable(), dtde.getLocation() ) )
58  		{
59  			dtde.rejectDrag();
60  		}
61  		else
62  		{
63  			dtde.acceptDrag( dtde.getDropAction() );
64  		}
65  	}
66  
67  	public void drop( DropTargetDropEvent dtde )
68  	{
69  		if( !isAcceptable( dtde.getTransferable(), dtde.getLocation() ) )
70  		{
71  			dtde.rejectDrop();
72  		}
73  		else
74  		{
75  			try
76  			{
77  				Transferable transferable = dtde.getTransferable();
78  				Object transferData = transferable.getTransferData( transferable.getTransferDataFlavors()[0] );
79  				if( transferData instanceof PropertyModelItem )
80  				{
81  					dtde.acceptDrop( dtde.getDropAction() );
82  					PropertyModelItem modelItem = ( PropertyModelItem )transferData;
83  
84  					String xpath = modelItem.getXPath();
85  					if( xpath == null && XmlUtils.seemsToBeXml( modelItem.getProperty().getValue() ) )
86  					{
87  						xpath = UISupport.selectXPath( "Create PropertyExpansion", "Select XPath below", modelItem
88  								.getProperty().getValue(), null );
89  
90  						if( xpath != null )
91  							xpath = XmlUtils.removeXPathNamespaceDeclarations( xpath );
92  					}
93  
94  					PropertyExpansion propertyExpansion = new PropertyExpansionImpl( modelItem.getProperty(), xpath );
95  
96  					Point point = dtde.getLocation();
97  					int column = table.columnAtPoint( point );
98  					int row = table.rowAtPoint( point );
99  					table.setValueAt( propertyExpansion.toString(), row, column );
100 
101 					dtde.dropComplete( true );
102 				}
103 			}
104 			catch( Exception e )
105 			{
106 				SoapUI.logError( e );
107 			}
108 		}
109 	}
110 
111 	public void dropActionChanged( DropTargetDragEvent dtde )
112 	{
113 	}
114 
115 	public boolean isAcceptable( Transferable transferable, Point point )
116 	{
117 		int column = table.columnAtPoint( point );
118 		int row = table.rowAtPoint( point );
119 		if( !table.isCellEditable( row, column ) )
120 			return false;
121 
122 		DataFlavor[] flavors = transferable.getTransferDataFlavors();
123 		for( int i = 0; i < flavors.length; i++ )
124 		{
125 			DataFlavor flavor = flavors[i];
126 			if( flavor.isMimeTypeEqual( DataFlavor.javaJVMLocalObjectMimeType ) )
127 			{
128 				try
129 				{
130 					Object modelItem = transferable.getTransferData( flavor );
131 					if( modelItem instanceof PropertyModelItem )
132 					{
133 						return PropertyExpansionUtils.canExpandProperty( this.modelItem, ( ( PropertyModelItem )modelItem )
134 								.getProperty() );
135 					}
136 				}
137 				catch( Exception ex )
138 				{
139 					SoapUI.logError( ex );
140 				}
141 			}
142 		}
143 
144 		return false;
145 	}
146 }