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