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