001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.actions;
014:
015: import java.lang.reflect.Field;
016: import java.util.ArrayList;
017: import java.util.List;
018:
019: import com.eviware.soapui.SoapUI;
020: import com.eviware.soapui.model.settings.Settings;
021: import com.eviware.soapui.settings.Setting;
022: import com.eviware.soapui.support.components.DirectoryFormComponent;
023: import com.eviware.soapui.support.components.FileFormComponent;
024: import com.eviware.soapui.support.components.FileListFormComponent;
025: import com.eviware.soapui.support.components.SimpleForm;
026: import com.eviware.soapui.support.components.StringListFormComponent;
027: import com.eviware.soapui.support.types.StringToStringMap;
028:
029: /**
030: * Support class for annotation-based settings
031: *
032: * @author ole.matzura
033: */
034:
035: public class AnnotatedSettingsPrefs implements Prefs {
036: private SimpleForm simpleForm;
037: private Class settingsClass;
038: private final String title;
039:
040: public AnnotatedSettingsPrefs(Class settingsClass, String title) {
041: this .settingsClass = settingsClass;
042: this .title = title;
043: }
044:
045: public SimpleForm getForm() {
046: if (simpleForm == null) {
047: simpleForm = new SimpleForm();
048: simpleForm.addSpace(5);
049:
050: buildForm(simpleForm);
051:
052: simpleForm.addSpace(5);
053: }
054:
055: return simpleForm;
056: }
057:
058: public List<Setting> getSettings() {
059: ArrayList<Setting> settings = new ArrayList<Setting>();
060: for (Field field : settingsClass.getFields()) {
061: Setting annotation = field.getAnnotation(Setting.class);
062: if (annotation != null) {
063: settings.add(annotation);
064: }
065: }
066: return settings;
067: }
068:
069: private void buildForm(SimpleForm form) {
070: List<Setting> settings = getSettings();
071: for (Setting annotation : settings) {
072: switch (annotation.type()) {
073: case BOOLEAN: {
074: form.appendCheckBox(annotation.name(), annotation
075: .description(), false);
076: break;
077: }
078: case FILE: {
079: form.append(annotation.name(), new FileFormComponent(
080: annotation.description()));
081: break;
082: }
083: case FILELIST: {
084: form.append(annotation.name(),
085: new FileListFormComponent(annotation
086: .description()));
087: break;
088: }
089: case STRINGLIST: {
090: form.append(annotation.name(),
091: new StringListFormComponent(annotation
092: .description()));
093: break;
094: }
095: case FOLDER: {
096: form.append(annotation.name(),
097: new DirectoryFormComponent(annotation
098: .description()));
099: break;
100: }
101: case ENUMERATION: {
102: form.appendComboBox(annotation.name(), annotation
103: .values(), annotation.description());
104: break;
105: }
106: case PASSWORD: {
107: form.appendPasswordField(annotation.name(), annotation
108: .description());
109: break;
110: }
111: default: {
112: form.appendTextField(annotation.name(), annotation
113: .description());
114: break;
115: }
116: }
117: }
118: }
119:
120: public StringToStringMap getValues(Settings settings) {
121: StringToStringMap result = new StringToStringMap();
122:
123: for (Field field : settingsClass.getFields()) {
124: Setting annotation = field.getAnnotation(Setting.class);
125: if (annotation != null) {
126: try {
127: result.put(annotation.name(), settings.getString(
128: field.get(null).toString(), annotation
129: .defaultValue()));
130: } catch (Exception e) {
131: SoapUI.logError(e);
132: }
133: }
134: }
135:
136: return result;
137: }
138:
139: public void setFormValues(Settings settings) {
140: getForm().setValues(getValues(settings));
141: }
142:
143: public void getFormValues(Settings settings) {
144: StringToStringMap values = new StringToStringMap();
145: getForm().getValues(values);
146: storeValues(values, settings);
147: }
148:
149: public void storeValues(StringToStringMap values, Settings settings) {
150: for (Field field : settingsClass.getFields()) {
151: Setting annotation = field.getAnnotation(Setting.class);
152: if (annotation != null) {
153: try {
154: settings.setString(field.get(null).toString(),
155: values.get(annotation.name()));
156: } catch (IllegalArgumentException e) {
157: SoapUI.logError(e);
158: } catch (IllegalAccessException e) {
159: SoapUI.logError(e);
160: }
161: }
162: }
163: }
164:
165: public String getTitle() {
166: return title;
167: }
168:
169: }
|