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.support;
14  
15  import java.beans.PropertyChangeEvent;
16  import java.beans.PropertyChangeListener;
17  
18  import org.apache.commons.beanutils.PropertyUtils;
19  
20  import com.eviware.soapui.model.ModelItem;
21  import com.eviware.soapui.model.settings.Settings;
22  
23  public class ModelItemPropertyEditorModel<T extends ModelItem> extends AbstractEditorModel implements
24  		PropertyChangeListener
25  {
26  	private T modelItem;
27  	private String propertyName;
28  
29  	public ModelItemPropertyEditorModel( T modelItem, String propertyName )
30  	{
31  		this.modelItem = modelItem;
32  		this.propertyName = propertyName;
33  
34  		modelItem.addPropertyChangeListener( propertyName, this );
35  	}
36  
37  	public Settings getSettings()
38  	{
39  		return modelItem.getSettings();
40  	}
41  
42  	public String getEditorText()
43  	{
44  		try
45  		{
46  			Object value = PropertyUtils.getSimpleProperty( modelItem, propertyName );
47  			return value == null ? "" : String.valueOf( value );
48  		}
49  		catch( Exception e )
50  		{
51  			e.printStackTrace();
52  		}
53  
54  		return null;
55  	}
56  
57  	public void setEditorText( String text )
58  	{
59  		try
60  		{
61  			PropertyUtils.setSimpleProperty( modelItem, propertyName, text );
62  		}
63  		catch( Exception e )
64  		{
65  			e.printStackTrace();
66  		}
67  	}
68  
69  	public void release()
70  	{
71  		modelItem.removePropertyChangeListener( propertyName, this );
72  	}
73  
74  	public void propertyChange( PropertyChangeEvent evt )
75  	{
76  		fireEditorTextChanged( String.valueOf( evt.getOldValue() ), String.valueOf( evt.getNewValue() ) );
77  	}
78  }