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