View Javadoc

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