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.x.form.support;
014:
015: import java.lang.reflect.Field;
016:
017: import com.eviware.soapui.SoapUI;
018: import com.eviware.soapui.support.UISupport;
019: import com.eviware.soapui.support.action.swing.ActionList;
020: import com.eviware.x.form.XForm;
021: import com.eviware.x.form.XFormDialog;
022: import com.eviware.x.form.XFormDialogBuilder;
023: import com.eviware.x.form.XFormFactory;
024: import com.eviware.x.form.XFormField;
025: import com.eviware.x.form.XFormTextField;
026: import com.eviware.x.form.XForm.FieldType;
027:
028: /**
029: * Builds XFormDialogs from AForm/AField annotated classes/interfaces
030: *
031: * @author ole.matzura
032: */
033:
034: public class ADialogBuilder {
035: public static XFormDialog buildDialog(
036: Class<? extends Object> formClass) {
037: return buildDialog(formClass, null);
038: }
039:
040: public static XFormDialog buildDialog(
041: Class<? extends Object> formClass, ActionList actions) {
042: AForm formAnnotation = formClass.getAnnotation(AForm.class);
043: if (formAnnotation == null) {
044: throw new RuntimeException(
045: "formClass is not annotated correctly..");
046: }
047:
048: XFormDialogBuilder builder = XFormFactory
049: .createDialogBuilder(formAnnotation.name());
050: XForm form = builder.createForm("Basic");
051:
052: for (Field field : formClass.getFields()) {
053: AField fieldAnnotation = field.getAnnotation(AField.class);
054: if (fieldAnnotation != null) {
055: addFormField(form, fieldAnnotation);
056: }
057: }
058:
059: ActionList defaultActions = formAnnotation.helpUrl().length() == 0 ? builder
060: .buildOkCancelActions()
061: : builder.buildOkCancelHelpActions(formAnnotation
062: .helpUrl());
063:
064: if (actions == null)
065: actions = defaultActions;
066: else
067: actions.addActions(defaultActions);
068:
069: XFormDialog dialog = builder.buildDialog(actions,
070: formAnnotation.description(), UISupport
071: .createImageIcon(formAnnotation.icon()));
072:
073: return dialog;
074: }
075:
076: public static XFormDialog buildTabbedDialog(
077: Class<? extends Object> tabbedFormClass, ActionList actions) {
078: AForm formAnnotation = tabbedFormClass
079: .getAnnotation(AForm.class);
080: if (formAnnotation == null) {
081: throw new RuntimeException(
082: "formClass is not annotated correctly..");
083: }
084:
085: XFormDialogBuilder builder = XFormFactory
086: .createDialogBuilder(formAnnotation.name());
087:
088: for (Field field : tabbedFormClass.getFields()) {
089: AField fieldAnnotation = field.getAnnotation(AField.class);
090: if (fieldAnnotation != null) {
091: XForm form = builder.createForm(fieldAnnotation.name());
092:
093: try {
094: Class formClass = Class.forName(fieldAnnotation
095: .description());
096:
097: for (Field formField : formClass.getFields()) {
098: AField formFieldAnnotation = formField
099: .getAnnotation(AField.class);
100: if (formFieldAnnotation != null) {
101: addFormField(form, formFieldAnnotation);
102: }
103: }
104: } catch (Exception e) {
105: SoapUI.logError(e);
106: }
107: }
108: }
109:
110: ActionList defaultActions = formAnnotation.helpUrl().length() == 0 ? builder
111: .buildOkCancelActions()
112: : builder.buildOkCancelHelpActions(formAnnotation
113: .helpUrl());
114:
115: if (actions == null)
116: actions = defaultActions;
117: else
118: actions.addActions(defaultActions);
119:
120: XFormDialog dialog = builder.buildDialog(actions,
121: formAnnotation.description(), UISupport
122: .createImageIcon(formAnnotation.icon()));
123:
124: return dialog;
125: }
126:
127: private static void addFormField(XForm form, AField fieldAnnotation) {
128: XFormField field = null;
129:
130: switch (fieldAnnotation.type()) {
131: case STRING:
132: field = form.addTextField(fieldAnnotation.name(),
133: fieldAnnotation.description(), FieldType.TEXT);
134: break;
135: case INT:
136: field = form.addTextField(fieldAnnotation.name(),
137: fieldAnnotation.description(), FieldType.TEXT);
138: ((XFormTextField) field).setWidth(10);
139: break;
140: case STRINGAREA:
141: field = form.addTextField(fieldAnnotation.name(),
142: fieldAnnotation.description(), FieldType.TEXTAREA);
143: break;
144: case BOOLEAN:
145: field = form.addCheckBox(fieldAnnotation.name(),
146: fieldAnnotation.description());
147: break;
148: case FILE:
149: field = form.addTextField(fieldAnnotation.name(),
150: fieldAnnotation.description(), FieldType.FILE);
151: break;
152: case FOLDER:
153: field = form.addTextField(fieldAnnotation.name(),
154: fieldAnnotation.description(), FieldType.FOLDER);
155: break;
156: case ENUMERATION:
157: field = form.addComboBox(fieldAnnotation.name(),
158: fieldAnnotation.values(), fieldAnnotation
159: .description());
160: break;
161: case RADIOGROUP:
162: field = form.addComponent(fieldAnnotation.name(),
163: new XFormRadioGroup(fieldAnnotation.values()));
164: break;
165: case MULTILIST:
166: field = form.addComponent(fieldAnnotation.name(),
167: new XFormMultiSelectList(fieldAnnotation.values()));
168: break;
169: default:
170: System.out.println("Unsupported field type: "
171: + fieldAnnotation.type());
172: }
173:
174: if (field != null)
175: field.setEnabled(fieldAnnotation.enabled());
176: }
177:
178: }
|