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 = XmlUtils.removeXPathNamespaceDeclarations( xpath );
108 					}
109 
110 					target.insertPropertyExpansion( new PropertyExpansionImpl( modelItem.getProperty(), xpath ), dtde
111 							.getLocation() );
112 
113 					dtde.dropComplete( true );
114 				}
115 			}
116 			catch( Exception e )
117 			{
118 				SoapUI.logError( e );
119 			}
120 
121 			if( dtde.getDropTargetContext().getComponent() instanceof JTextComponent )
122 				( ( JTextComponent )dtde.getDropTargetContext().getComponent() ).getCaret().setVisible( false );
123 			else if( dtde.getDropTargetContext().getComponent() instanceof JXEditTextArea )
124 				( ( JXEditTextArea )dtde.getDropTargetContext().getComponent() ).setCaretVisible( false );
125 		}
126 	}
127 
128 	public void dropActionChanged( DropTargetDragEvent dtde )
129 	{
130 	}
131 
132 	public boolean isAcceptable( Transferable transferable )
133 	{
134 		DataFlavor[] flavors = transferable.getTransferDataFlavors();
135 		for( int i = 0; i < flavors.length; i++ )
136 		{
137 			DataFlavor flavor = flavors[i];
138 			if( flavor.isMimeTypeEqual( DataFlavor.javaJVMLocalObjectMimeType ) )
139 			{
140 				try
141 				{
142 					Object modelItem = transferable.getTransferData( flavor );
143 					if( modelItem instanceof PropertyModelItem )
144 					{
145 						return PropertyExpansionUtils.canExpandProperty( target.getContextModelItem(),
146 								( ( PropertyModelItem )modelItem ).getProperty() );
147 					}
148 				}
149 				catch( Exception ex )
150 				{
151 					SoapUI.logError( ex );
152 				}
153 			}
154 		}
155 
156 		return false;
157 	}
158 }