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.actions;
14  
15  import java.lang.reflect.Field;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.model.settings.Settings;
21  import com.eviware.soapui.settings.Setting;
22  import com.eviware.soapui.support.components.DirectoryFormComponent;
23  import com.eviware.soapui.support.components.FileFormComponent;
24  import com.eviware.soapui.support.components.FileListFormComponent;
25  import com.eviware.soapui.support.components.SimpleForm;
26  import com.eviware.soapui.support.components.StringListFormComponent;
27  import com.eviware.soapui.support.types.StringToStringMap;
28  
29  /***
30   * Support class for annotation-based settings
31   * 
32   * @author ole.matzura
33   */
34  
35  public class AnnotatedSettingsPrefs implements Prefs
36  {
37  	private SimpleForm simpleForm;
38  	private Class<?> settingsClass;
39  	private final String title;
40  
41  	public AnnotatedSettingsPrefs( Class<?> settingsClass, String title )
42  	{
43  		this.settingsClass = settingsClass;
44  		this.title = title;
45  	}
46  
47  	public SimpleForm getForm()
48  	{
49  		if( simpleForm == null )
50  		{
51  			simpleForm = new SimpleForm();
52  			simpleForm.addSpace( 5 );
53  
54  			buildForm( simpleForm );
55  
56  			simpleForm.addSpace( 5 );
57  		}
58  
59  		return simpleForm;
60  	}
61  
62  	public List<Setting> getSettings()
63  	{
64  		ArrayList<Setting> settings = new ArrayList<Setting>();
65  		for( Field field : settingsClass.getFields() )
66  		{
67  			Setting annotation = field.getAnnotation( Setting.class );
68  			if( annotation != null )
69  			{
70  				settings.add( annotation );
71  			}
72  		}
73  		return settings;
74  	}
75  
76  	private void buildForm( SimpleForm form )
77  	{
78  		List<Setting> settings = getSettings();
79  		for( Setting annotation : settings )
80  		{
81  			switch( annotation.type() )
82  			{
83  			case BOOLEAN :
84  			{
85  				form.appendCheckBox( annotation.name(), annotation.description(), false );
86  				break;
87  			}
88  			case FILE :
89  			{
90  				form.append( annotation.name(), new FileFormComponent( annotation.description() ) );
91  				break;
92  			}
93  			case FILELIST :
94  			{
95  				form.append( annotation.name(), new FileListFormComponent( annotation.description() ) );
96  				break;
97  			}
98  			case STRINGLIST :
99  			{
100 				form.append( annotation.name(), new StringListFormComponent( annotation.description() ) );
101 				break;
102 			}
103 			case FOLDER :
104 			{
105 				form.append( annotation.name(), new DirectoryFormComponent( annotation.description() ) );
106 				break;
107 			}
108 			case ENUMERATION :
109 			{
110 				form.appendComboBox( annotation.name(), annotation.values(), annotation.description() );
111 				break;
112 			}
113 			case PASSWORD :
114 			{
115 				form.appendPasswordField( annotation.name(), annotation.description() );
116 				break;
117 			}
118 			default :
119 			{
120 				form.appendTextField( annotation.name(), annotation.description() );
121 				break;
122 			}
123 			}
124 		}
125 	}
126 
127 	public StringToStringMap getValues( Settings settings )
128 	{
129 		StringToStringMap result = new StringToStringMap();
130 
131 		for( Field field : settingsClass.getFields() )
132 		{
133 			Setting annotation = field.getAnnotation( Setting.class );
134 			if( annotation != null )
135 			{
136 				try
137 				{
138 					result.put( annotation.name(), settings.getString( field.get( null ).toString(), annotation
139 							.defaultValue() ) );
140 				}
141 				catch( Exception e )
142 				{
143 					SoapUI.logError( e );
144 				}
145 			}
146 		}
147 
148 		return result;
149 	}
150 
151 	public void setFormValues( Settings settings )
152 	{
153 		getForm().setValues( getValues( settings ) );
154 	}
155 
156 	public void getFormValues( Settings settings )
157 	{
158 		StringToStringMap values = new StringToStringMap();
159 		getForm().getValues( values );
160 		storeValues( values, settings );
161 	}
162 
163 	public void storeValues( StringToStringMap values, Settings settings )
164 	{
165 		for( Field field : settingsClass.getFields() )
166 		{
167 			Setting annotation = field.getAnnotation( Setting.class );
168 			if( annotation != null )
169 			{
170 				try
171 				{
172 					settings.setString( field.get( null ).toString(), values.get( annotation.name() ) );
173 				}
174 				catch( IllegalArgumentException e )
175 				{
176 					SoapUI.logError( e );
177 				}
178 				catch( IllegalAccessException e )
179 				{
180 					SoapUI.logError( e );
181 				}
182 			}
183 		}
184 	}
185 
186 	public String getTitle()
187 	{
188 		return title;
189 	}
190 
191 }