1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.x.form.support;
14
15 import java.lang.reflect.Field;
16
17 import com.eviware.soapui.SoapUI;
18 import com.eviware.soapui.support.UISupport;
19 import com.eviware.soapui.support.action.swing.ActionList;
20 import com.eviware.x.form.XForm;
21 import com.eviware.x.form.XFormDialog;
22 import com.eviware.x.form.XFormDialogBuilder;
23 import com.eviware.x.form.XFormFactory;
24 import com.eviware.x.form.XFormField;
25 import com.eviware.x.form.XFormTextField;
26 import com.eviware.x.form.XForm.FieldType;
27 import com.eviware.x.impl.swing.JStringListFormField;
28
29 /***
30 * Builds XFormDialogs from AForm/AField annotated classes/interfaces
31 *
32 * @author ole.matzura
33 */
34
35 public class ADialogBuilder
36 {
37 public static XFormDialog buildDialog( Class<? extends Object> formClass )
38 {
39 return buildDialog( formClass, null );
40 }
41
42 public static XFormDialog buildDialog( Class<? extends Object> formClass, ActionList actions )
43 {
44 AForm formAnnotation = formClass.getAnnotation( AForm.class );
45 if( formAnnotation == null )
46 {
47 throw new RuntimeException( "formClass is not annotated correctly.." );
48 }
49
50 XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name());
51 XForm form = builder.createForm( "Basic" );
52
53 for( Field field : formClass.getFields() )
54 {
55 AField fieldAnnotation = field.getAnnotation( AField.class );
56 if( fieldAnnotation != null )
57 {
58 addFormField( form, fieldAnnotation );
59 }
60 }
61
62 ActionList defaultActions = formAnnotation.helpUrl() == null ?
63 builder.buildOkCancelActions() : builder.buildOkCancelHelpActions( formAnnotation.helpUrl() );
64
65 if( actions == null )
66 actions = defaultActions;
67 else
68 actions.addActions( defaultActions );
69
70 XFormDialog dialog = builder.buildDialog( actions, formAnnotation.description(),
71 UISupport.createImageIcon( formAnnotation.icon() ));
72
73 return dialog;
74 }
75
76 public static XFormDialog buildTabbedDialog( Class<? extends Object> tabbedFormClass, ActionList actions )
77 {
78 AForm formAnnotation = tabbedFormClass.getAnnotation( AForm.class );
79 if( formAnnotation == null )
80 {
81 throw new RuntimeException( "formClass is not annotated correctly.." );
82 }
83
84 XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name());
85
86 for( Field field : tabbedFormClass.getFields() )
87 {
88 APage pageAnnotation = field.getAnnotation( APage.class );
89 if( pageAnnotation != null )
90 {
91 buildForm(builder, pageAnnotation.name(), field.getType());
92 }
93
94 AField fieldAnnotation = field.getAnnotation( AField.class );
95 if( fieldAnnotation != null )
96 {
97 try
98 {
99 Class formClass = Class.forName( fieldAnnotation.description() );
100 buildForm(builder, fieldAnnotation.name(), formClass);
101 }
102 catch( Exception e )
103 {
104 SoapUI.logError( e );
105 }
106 }
107 }
108
109 ActionList defaultActions = formAnnotation.helpUrl().length() == 0 ?
110 builder.buildOkCancelActions() : builder.buildOkCancelHelpActions( formAnnotation.helpUrl() );
111
112 if( actions == null )
113 actions = defaultActions;
114 else
115 actions.addActions( defaultActions );
116
117 XFormDialog dialog = builder.buildDialog( actions, formAnnotation.description(),
118 UISupport.createImageIcon( formAnnotation.icon() ));
119
120 return dialog;
121 }
122
123 public static XFormDialog buildWizard( Class<? extends Object> tabbedFormClass )
124 {
125 AForm formAnnotation = tabbedFormClass.getAnnotation( AForm.class );
126 if( formAnnotation == null )
127 {
128 throw new RuntimeException( "formClass is not annotated correctly.." );
129 }
130
131 XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name());
132
133 for( Field field : tabbedFormClass.getFields() )
134 {
135 APage pageAnnotation = field.getAnnotation( APage.class );
136 if( pageAnnotation != null )
137 {
138 buildForm(builder, pageAnnotation.name(), field.getType());
139 }
140 }
141
142 XFormDialog dialog = builder.buildWizard( formAnnotation.description(),
143 UISupport.createImageIcon( formAnnotation.icon() ),
144 formAnnotation.helpUrl() );
145
146 return dialog;
147 }
148
149 private static void buildForm(XFormDialogBuilder builder, String name, Class formClass)
150 {
151 XForm form = builder.createForm( name );
152 for( Field formField : formClass.getFields() )
153 {
154 AField formFieldAnnotation = formField.getAnnotation( AField.class );
155 if( formFieldAnnotation != null )
156 {
157 addFormField( form, formFieldAnnotation );
158 }
159 }
160 }
161
162 private static void addFormField( XForm form, AField fieldAnnotation )
163 {
164 XFormField field = null;
165
166 switch( fieldAnnotation.type() )
167 {
168 case STRING :
169 field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.TEXT );
170 break;
171 case INT :
172 field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.TEXT );
173 ((XFormTextField)field).setWidth( 10 );
174 break;
175 case STRINGAREA :
176 field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.TEXTAREA );
177 break;
178 case BOOLEAN :
179 field = form.addCheckBox( fieldAnnotation.name(), fieldAnnotation.description() );
180 break;
181 case FILE :
182 field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.FILE );
183 break;
184 case FOLDER :
185 field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.FOLDER );
186 break;
187 case ENUMERATION :
188 field = form.addComboBox( fieldAnnotation.name(), fieldAnnotation.values(), fieldAnnotation.description() );
189 break;
190 case RADIOGROUP :
191 field = form.addComponent( fieldAnnotation.name(), new XFormRadioGroup( fieldAnnotation.values() ) );
192 break;
193 case MULTILIST :
194 field = form.addComponent( fieldAnnotation.name(), new XFormMultiSelectList( fieldAnnotation.values() ) );
195 break;
196 case STRINGLIST :
197 field = form.addComponent( fieldAnnotation.name(), new JStringListFormField(fieldAnnotation.description()) );
198 break;
199 default :
200 System.out.println( "Unsupported field type: " + fieldAnnotation.type() );
201 }
202
203 if( field != null )
204 field.setEnabled( fieldAnnotation.enabled() );
205 }
206
207 }