View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.model.tree.nodes;
14  
15  import java.awt.event.ActionEvent;
16  
17  import javax.swing.AbstractAction;
18  import javax.swing.Action;
19  
20  import com.eviware.soapui.model.ModelItem;
21  import com.eviware.soapui.model.TestPropertyHolder;
22  import com.eviware.soapui.model.testsuite.TestProperty;
23  import com.eviware.soapui.model.tree.AbstractModelItemTreeNode;
24  import com.eviware.soapui.model.tree.SoapUITreeModel;
25  import com.eviware.soapui.model.tree.nodes.support.EmptyModelItem;
26  import com.eviware.soapui.support.StringUtils;
27  import com.eviware.soapui.support.UISupport;
28  import com.eviware.soapui.support.action.swing.ActionList;
29  import com.eviware.soapui.support.action.swing.DefaultActionList;
30  
31  public class PropertyTreeNode extends AbstractModelItemTreeNode<PropertyTreeNode.PropertyModelItem>
32  {
33  	private boolean readOnly;
34  	private final TestProperty property;
35  
36  	protected PropertyTreeNode( TestProperty property, ModelItem parent, TestPropertyHolder holder,
37  			SoapUITreeModel treeModel )
38  	{
39  		super( new PropertyModelItem( property, property.isReadOnly() ), parent, treeModel );
40  		this.property = property;
41  		readOnly = property.isReadOnly();
42  	}
43  
44  	public static String buildName( TestProperty property )
45  	{
46  		String name = property.getName();
47  		String value = property.getValue();
48  		if( value == null )
49  			value = "";
50  		else
51  		{
52  			if( value.length() > 12 )
53  				value = value.substring( 0, 12 ) + "..";
54  
55  			value = "'" + value + "'";
56  		}
57  
58  		return name + " : " + value;
59  	}
60  
61  	@Override
62  	public ActionList getActions()
63  	{
64  		if( !readOnly )
65  		{
66  			DefaultActionList actions = new DefaultActionList();
67  			SetPropertyValueAction setPropertyValueAction = new SetPropertyValueAction();
68  			actions.addAction( setPropertyValueAction );
69  			actions.setDefaultAction( setPropertyValueAction );
70  			return actions;
71  		}
72  		else
73  		{
74  			return super.getActions();
75  		}
76  	}
77  
78  	public static class PropertyModelItem extends EmptyModelItem
79  	{
80  		private final TestProperty property;
81  		private String xpath;
82  
83  		public PropertyModelItem( TestProperty property, boolean readOnly )
84  		{
85  			super( buildName( property ), readOnly ? UISupport.createImageIcon( "/bullet_black.gif" ) : UISupport
86  					.createImageIcon( "/bullet_green.gif" ) );
87  
88  			this.property = property;
89  		}
90  
91  		public TestProperty getProperty()
92  		{
93  			return property;
94  		}
95  
96  		public String getXPath()
97  		{
98  			return xpath;
99  		}
100 
101 		public void setXPath( String xpath )
102 		{
103 			this.xpath = xpath;
104 		}
105 	}
106 
107 	private class SetPropertyValueAction extends AbstractAction
108 	{
109 		public SetPropertyValueAction()
110 		{
111 			super( "Set Value" );
112 			putValue( Action.SHORT_DESCRIPTION, "Prompts to set the value of this property" );
113 		}
114 
115 		public void actionPerformed( ActionEvent e )
116 		{
117 			String value = UISupport.prompt( "Specify property value", "Set Value", property.getValue() );
118 			if( StringUtils.hasContent( value ) )
119 			{
120 				property.setValue( value );
121 			}
122 		}
123 	}
124 }