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.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, SoapUITreeModel treeModel )
37  	{
38  		super( new PropertyModelItem( property, property.isReadOnly() ), parent, treeModel );
39  		this.property = property;
40  		readOnly = property.isReadOnly();
41  	}
42  	
43  	public static String buildName( TestProperty property )
44  	{
45  		String name = property.getName();
46  		String value = property.getValue();
47  		if( value == null )
48  			value = "";
49  		else
50  		{
51  			if( value.length() > 12 )
52  				value = value.substring( 0, 12 ) + "..";
53  			
54  			value = "'" + value + "'";
55  		}
56  		
57  		return name + " : " + value;
58  	}
59  	
60  	@Override
61  	public ActionList getActions()
62  	{
63  		if( !readOnly )
64  		{
65  			DefaultActionList actions = new DefaultActionList();
66  			SetPropertyValueAction setPropertyValueAction = new SetPropertyValueAction();
67  			actions.addAction( setPropertyValueAction );
68  			actions.setDefaultAction( setPropertyValueAction );
69  			return actions;
70  		}
71  		else
72  		{
73  			return super.getActions();
74  		}
75  	}
76  
77  	public static class PropertyModelItem extends EmptyModelItem
78  	{
79  		private final TestProperty property;
80  		private String xpath;
81  
82  		public PropertyModelItem( TestProperty property, boolean readOnly )
83  		{
84  			super( buildName( property ), readOnly ? UISupport.createImageIcon( "/bullet_black.gif" ) : 
85  				UISupport.createImageIcon( "/bullet_green.gif" ) );
86  			
87  			this.property = property;
88  		}
89  
90  		public TestProperty getProperty()
91  		{
92  			return property;
93  		}
94  
95  		public String getXPath()
96  		{
97  			return xpath;
98  		}
99  
100 		public void setXPath( String xpath )
101 		{
102 			this.xpath = xpath;
103 		}
104 	}
105 	
106 	private class SetPropertyValueAction extends AbstractAction
107 	{
108 		public SetPropertyValueAction()
109 		{
110 			super( "Set Value" );
111 			putValue(Action.SHORT_DESCRIPTION, "Prompts to set the value of this property");
112 		}
113 
114 		public void actionPerformed(ActionEvent e)
115 		{
116 			String value = UISupport.prompt("Specify property value", "Set Value", property.getValue() );
117 			if ( StringUtils.hasContent( value ))
118 			{
119 				property.setValue( value );
120 			}
121 		}
122 	}
123 }