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 com.eviware.soapui.impl.support.AbstractHttpRequest;
16  import com.eviware.soapui.impl.wsdl.MutableTestPropertyHolder;
17  import com.eviware.soapui.impl.wsdl.WsdlProject;
18  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
19  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
20  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
21  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
22  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
23  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
24  import com.eviware.soapui.model.ModelItem;
25  import com.eviware.soapui.model.TestModelItem;
26  import com.eviware.soapui.model.TestPropertyHolder;
27  import com.eviware.soapui.model.iface.Operation;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
29  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionImpl;
30  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
31  import com.eviware.soapui.model.testsuite.TestProperty;
32  import com.eviware.soapui.support.StringUtils;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.components.ShowPopupAction;
35  import com.eviware.soapui.support.xml.JXEditTextArea;
36  import com.eviware.soapui.support.xml.XmlUtils;
37  import org.apache.xmlbeans.XmlObject;
38  
39  import javax.swing.*;
40  import javax.swing.event.PopupMenuEvent;
41  import javax.swing.event.PopupMenuListener;
42  import javax.swing.text.JTextComponent;
43  import java.awt.*;
44  import java.awt.dnd.DnDConstants;
45  import java.awt.dnd.DropTarget;
46  import java.awt.event.ActionEvent;
47  
48  public class PropertyExpansionPopupListener implements PopupMenuListener
49  {
50     private final Container targetMenu;
51     private final ModelItem modelItem;
52     private final PropertyExpansionTarget target;
53  
54     public PropertyExpansionPopupListener( Container transferMenu, ModelItem modelItem, PropertyExpansionTarget target )
55     {
56        this.targetMenu = transferMenu;
57        this.modelItem = modelItem;
58        this.target = target;
59     }
60  
61     public void popupMenuCanceled( PopupMenuEvent arg0 )
62     {
63     }
64  
65     public void popupMenuWillBecomeInvisible( PopupMenuEvent arg0 )
66     {
67     }
68  
69     public void popupMenuWillBecomeVisible( PopupMenuEvent arg0 )
70     {
71        // create transfer menus
72        targetMenu.removeAll();
73  
74        WsdlTestStep testStep = null;
75        WsdlTestCase testCase = null;
76        WsdlTestSuite testSuite = null;
77        WsdlProject project = null;
78        WsdlMockService mockService = null;
79        WsdlMockResponse mockResponse = null;
80  
81        if( modelItem instanceof WsdlTestStep )
82        {
83           testStep = (WsdlTestStep) modelItem;
84           testCase = testStep.getTestCase();
85           testSuite = testCase.getTestSuite();
86           project = testSuite.getProject();
87        }
88        else if( modelItem instanceof WsdlTestCase )
89        {
90           testCase = (WsdlTestCase) modelItem;
91           testSuite = testCase.getTestSuite();
92           project = testSuite.getProject();
93        }
94        else if( modelItem instanceof WsdlTestSuite )
95        {
96           testSuite = (WsdlTestSuite) modelItem;
97           project = testSuite.getProject();
98        }
99        else if( modelItem instanceof WsdlMockService )
100       {
101          project = ( (WsdlMockService) modelItem ).getProject();
102       }
103       else if( modelItem instanceof WsdlMockResponse )
104       {
105          mockResponse = (WsdlMockResponse) modelItem;
106          mockService = ( mockResponse ).getMockOperation().getMockService();
107          project = mockService.getProject();
108       }
109       else if( modelItem instanceof WsdlProject )
110       {
111          project = (WsdlProject) modelItem;
112       }
113       else if( modelItem instanceof AbstractHttpRequest )
114       {
115          project = ( (AbstractHttpRequest) modelItem ).getOperation().getInterface().getProject();
116       }
117       else if( modelItem instanceof Operation )
118       {
119          project = (WsdlProject) ( (Operation) modelItem ).getInterface().getProject();
120       }
121 
122       TestPropertyHolder globalProperties = PropertyExpansionUtils.getGlobalProperties();
123       if( globalProperties.getProperties().size() > 0 )
124          targetMenu.add( createPropertyMenu( "Global", globalProperties ) );
125 
126       if( project != null )
127          targetMenu.add( createPropertyMenu( "Project: [" + project.getName() + "]", project ) );
128 
129       if( testSuite != null )
130          targetMenu.add( createPropertyMenu( "TestSuite: [" + testSuite.getName() + "]", testSuite ) );
131 
132       if( mockService != null )
133          targetMenu.add( createPropertyMenu( "MockService: [" + mockService.getName() + "]", mockService ) );
134 
135       if( mockResponse != null )
136          targetMenu.add( createPropertyMenu( "MockResponse: [" + mockResponse.getName() + "]", mockResponse ) );
137 
138       if( testCase != null )
139       {
140          targetMenu.add( createPropertyMenu( "TestCase: [" + testCase.getName() + "]", testCase ) );
141 
142          for( int c = 0; c < testCase.getTestStepCount(); c++ )
143          {
144             testStep = testCase.getTestStepAt( c );
145             if( testStep == modelItem || testStep.getPropertyNames().length == 0 )
146                continue;
147 
148             if( targetMenu.getComponentCount() == 3 )
149                targetMenu.add( new JSeparator() );
150 
151             targetMenu.add( createPropertyMenu( "Step " + ( c + 1 ) + ": [" + testStep.getName() + "]", testStep ) );
152          }
153       }
154 
155 //		if( targetMenu.getComponentCount() > 0 )
156 //			targetMenu.add( new JSeparator() );
157 //		
158 //		targetMenu.add( new JMenuItem( new TransferFromPropertyActionInvoker()));
159    }
160 
161    private JMenu createPropertyMenu( String string, TestPropertyHolder holder )
162    {
163       JMenu menu = new JMenu( string );
164       if( holder instanceof TestModelItem )
165          menu.setIcon( ( (TestModelItem) holder ).getIcon() );
166 
167       String[] propertyNames = holder.getPropertyNames();
168 
169       for( String name : propertyNames )
170       {
171          menu.add( new TransferFromPropertyActionInvoker( holder, name ) );
172       }
173 
174       if( holder instanceof MutableTestPropertyHolder )
175       {
176          if( menu.getMenuComponentCount() > 0 )
177             menu.addSeparator();
178 
179          menu.add( new TransferFromPropertyActionInvoker( (MutableTestPropertyHolder) holder ) );
180       }
181 
182       return menu;
183    }
184 
185    public class TransferFromPropertyActionInvoker extends AbstractAction
186    {
187       private TestPropertyHolder sourceStep;
188       private String sourceProperty;
189 
190       public TransferFromPropertyActionInvoker( TestPropertyHolder sourceStep, String sourceProperty )
191       {
192          super( "Property [" + sourceProperty + "]" );
193          this.sourceStep = sourceStep;
194          this.sourceProperty = sourceProperty;
195       }
196 
197       public TransferFromPropertyActionInvoker( MutableTestPropertyHolder testStep )
198       {
199          super( "Create new.." );
200          this.sourceStep = testStep;
201       }
202 
203       public void actionPerformed( ActionEvent arg0 )
204       {
205          if( sourceProperty == null && sourceStep instanceof MutableTestPropertyHolder )
206          {
207             MutableTestPropertyHolder step = (MutableTestPropertyHolder) sourceStep;
208             sourceProperty = target.getNameForCreation();
209 
210             sourceProperty = UISupport.prompt( "Specify name of source property to create", "Create source property", sourceProperty );
211             while( sourceProperty != null && step.getProperty( sourceProperty ) != null )
212             {
213                sourceProperty = UISupport.prompt( "Name is taken, specify unique name of source property to create",
214                        "Create source property", sourceProperty );
215             }
216 
217             if( sourceProperty == null )
218             {
219                return;
220             }
221 
222             ( (MutableTestPropertyHolder) sourceStep ).addProperty( sourceProperty );
223          }
224 
225          String sourceXPath = "";
226 
227          try
228          {
229             String val = sourceStep.getPropertyValue( sourceProperty );
230             if( StringUtils.isNullOrEmpty( val ) )
231             {
232                String defaultValue = sourceStep.getProperty( sourceProperty ).getDefaultValue();
233                if( StringUtils.hasContent( defaultValue ) )
234                {
235                   if( UISupport.confirm( "Missing property value, use default value instead?", "Get Data" ) )
236                   {
237                      val = defaultValue;
238                   }
239                }
240             }
241 
242             if( XmlUtils.seemsToBeXml( val ) )
243             {
244                XmlObject.Factory.parse( val );
245                sourceXPath = UISupport.selectXPath( "Select XPath", "Select source xpath for property transfer", val, null );
246             }
247          }
248          catch( Throwable e )
249          {
250             // just ignore.. this wasn't xml..
251          }
252 
253          if( StringUtils.hasContent( sourceXPath ) )
254          {
255             sourceXPath = XmlUtils.removeXPathNamespaceDeclarations( sourceXPath );
256             if( sourceXPath.length() > 0 )
257                sourceXPath = sourceXPath.replace( '\n', ' ' );
258          }
259 
260          TestProperty property = sourceStep.getProperty( sourceProperty );
261          PropertyExpansion pe = new PropertyExpansionImpl( property, sourceXPath );
262 
263          String valueForCreation = target.getValueForCreation();
264          target.insertPropertyExpansion( pe, null );
265 
266          if( !StringUtils.hasContent( sourceXPath ) && StringUtils.hasContent( valueForCreation ) && !property.isReadOnly() )
267          {
268             valueForCreation = UISupport.prompt( "Init property value to", "Get Data", valueForCreation );
269             if( valueForCreation != null )
270             {
271                property.setValue( valueForCreation );
272             }
273          }
274       }
275    }
276 
277    public static void addMenu( JPopupMenu popup, String menuName, ModelItem item, PropertyExpansionTarget component )
278    {
279       JMenu menu = new JMenu( menuName );
280       popup.add( menu );
281       popup.addPopupMenuListener( new PropertyExpansionPopupListener( menu, item, component ) );
282    }
283 
284    public static void enable( JTextComponent textField, ModelItem modelItem, JPopupMenu popup )
285    {
286       JTextComponentPropertyExpansionTarget target = new JTextComponentPropertyExpansionTarget( textField, modelItem );
287       DropTarget dropTarget = new DropTarget( textField, new PropertyExpansionDropTarget( target ) );
288       dropTarget.setDefaultActions( DnDConstants.ACTION_COPY_OR_MOVE );
289 
290       textField.setComponentPopupMenu( popup );
291 
292       if( popup != null )
293       {
294          PropertyExpansionPopupListener.addMenu( popup, "Get Data..", target.getContextModelItem(), target );
295       }
296    }
297 
298    public static JPanel addPropertyExpansionPopup( JTextField textField, JPopupMenu popup, ModelItem modelItem )
299    {
300       PropertyExpansionPopupListener.enable( textField, modelItem, popup );
301 
302       JButton popupButton = new JButton();
303       popupButton.setAction( new ShowPopupAction( textField, popupButton ) );
304       popupButton.setBackground( Color.WHITE );
305       popupButton.setForeground( Color.WHITE );
306       popupButton.setBorder( null );
307       popupButton.setOpaque( true );
308       JPanel panel = new JPanel( new BorderLayout() );
309       panel.add( textField, BorderLayout.CENTER );
310       panel.add( popupButton, BorderLayout.EAST );
311       panel.setBorder( textField.getBorder() );
312       textField.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) );
313 
314       return panel;
315    }
316 
317    public static void enable( JXEditTextArea textField, ModelItem modelItem )
318    {
319       JXEditTextAreaPropertyExpansionTarget target = new JXEditTextAreaPropertyExpansionTarget( textField, modelItem );
320       DropTarget dropTarget = new DropTarget( textField, new PropertyExpansionDropTarget( target ) );
321       dropTarget.setDefaultActions( DnDConstants.ACTION_COPY_OR_MOVE );
322 
323       JPopupMenu popup = textField.getRightClickPopup();
324 
325       if( popup != null )
326       {
327          PropertyExpansionPopupListener.addMenu( popup, "Get Data..", target.getContextModelItem(), target );
328       }
329    }
330 
331    public static void enable( GroovyEditor groovyEditor, ModelItem modelItem )
332    {
333       GroovyEditorPropertyExpansionTarget target = new GroovyEditorPropertyExpansionTarget( groovyEditor, modelItem );
334       DropTarget dropTarget = new DropTarget( groovyEditor.getEditArea(), new PropertyExpansionDropTarget( target ) );
335       dropTarget.setDefaultActions( DnDConstants.ACTION_COPY_OR_MOVE );
336 
337       JPopupMenu popup = groovyEditor.getEditArea().getComponentPopupMenu();
338 
339       if( popup != null )
340       {
341          JMenu menu = new JMenu( "Get Data.." );
342          popup.insert( menu, 0 );
343          popup.addPopupMenuListener( new PropertyExpansionPopupListener( menu, target.getContextModelItem(), target ) );
344          popup.insert( new JSeparator(), 1 );
345       }
346    }
347 
348    public static void enable( JTextComponent textField, ModelItem modelItem )
349    {
350       JPopupMenu popupMenu = textField.getComponentPopupMenu();
351       if( popupMenu == null )
352       {
353          popupMenu = new JPopupMenu();
354          textField.setComponentPopupMenu( popupMenu );
355       }
356 
357       enable( textField, modelItem, popupMenu );
358    }
359 
360    public static void disable( GroovyEditor editor )
361    {
362    }
363 }