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.MessageSupport;
19 import com.eviware.soapui.support.UISupport;
20 import com.eviware.soapui.support.action.swing.ActionList;
21 import com.eviware.x.form.XForm;
22 import com.eviware.x.form.XFormDialog;
23 import com.eviware.x.form.XFormDialogBuilder;
24 import com.eviware.x.form.XFormFactory;
25 import com.eviware.x.form.XFormField;
26 import com.eviware.x.form.XFormTextField;
27 import com.eviware.x.form.XForm.FieldType;
28 import com.eviware.x.form.support.AField.AFieldType;
29 import com.eviware.x.impl.swing.ActionFormFieldComponent;
30 import com.eviware.x.impl.swing.JComponentFormField;
31 import com.eviware.x.impl.swing.JPasswordFieldFormField;
32 import com.eviware.x.impl.swing.JStringListFormField;
33 import com.eviware.x.impl.swing.JTableFormField;
34
35 /***
36 * Builds XFormDialogs from AForm/AField annotated classes/interfaces
37 *
38 * @author ole.matzura
39 */
40
41 public class ADialogBuilder
42 {
43 public static XFormDialog buildDialog( Class<? extends Object> formClass )
44 {
45 return buildDialog( formClass, null );
46 }
47
48 public static XFormDialog buildDialog( Class<? extends Object> formClass, ActionList actions )
49 {
50 AForm formAnnotation = formClass.getAnnotation( AForm.class );
51 if( formAnnotation == null )
52 {
53 throw new RuntimeException( "formClass is not annotated correctly.." );
54 }
55
56 MessageSupport messages = MessageSupport.getMessages( formClass );
57
58 XFormDialogBuilder builder = XFormFactory.createDialogBuilder( messages.get( formAnnotation.name() ) );
59 XForm form = builder.createForm( "Basic" );
60
61 for( Field field : formClass.getFields() )
62 {
63 AField fieldAnnotation = field.getAnnotation( AField.class );
64 if( fieldAnnotation != null )
65 {
66 try
67 {
68 addFormField( form, field, fieldAnnotation, messages );
69 }
70 catch( Exception e )
71 {
72 e.printStackTrace();
73 }
74 }
75 }
76
77 ActionList defaultActions = formAnnotation.helpUrl() == null ? builder.buildOkCancelActions() : builder
78 .buildOkCancelHelpActions( formAnnotation.helpUrl() );
79
80 if( actions == null )
81 actions = defaultActions;
82 else
83 actions.addActions( defaultActions );
84
85 XFormDialog dialog = builder.buildDialog( actions, messages.get( formAnnotation.description() ), UISupport
86 .createImageIcon( formAnnotation.icon() ) );
87
88 return dialog;
89 }
90
91 public static XFormDialog buildTabbedDialog( Class<? extends Object> tabbedFormClass, ActionList actions )
92 {
93 AForm formAnnotation = tabbedFormClass.getAnnotation( AForm.class );
94 if( formAnnotation == null )
95 {
96 throw new RuntimeException( "formClass is not annotated correctly.." );
97 }
98
99 MessageSupport messages = MessageSupport.getMessages( tabbedFormClass );
100 XFormDialogBuilder builder = XFormFactory.createDialogBuilder( formAnnotation.name() );
101
102 for( Field field : tabbedFormClass.getFields() )
103 {
104 APage pageAnnotation = field.getAnnotation( APage.class );
105 if( pageAnnotation != null )
106 {
107 buildForm( builder, pageAnnotation.name(), field.getType(), messages );
108 }
109
110 AField fieldAnnotation = field.getAnnotation( AField.class );
111 if( fieldAnnotation != null )
112 {
113 try
114 {
115 Class<?> formClass = Class.forName( fieldAnnotation.description() );
116 buildForm( builder, fieldAnnotation.name(), formClass, messages );
117 }
118 catch( Exception e )
119 {
120 SoapUI.logError( e );
121 }
122 }
123 }
124
125 ActionList defaultActions = formAnnotation.helpUrl().length() == 0 ? builder.buildOkCancelActions() : builder
126 .buildOkCancelHelpActions( formAnnotation.helpUrl() );
127
128 if( actions == null )
129 actions = defaultActions;
130 else
131 actions.addActions( defaultActions );
132
133 XFormDialog dialog = builder.buildDialog( actions, formAnnotation.description(), UISupport
134 .createImageIcon( formAnnotation.icon() ) );
135
136 return dialog;
137 }
138
139 public static XFormDialog buildWizard( Class<? extends Object> tabbedFormClass )
140 {
141 AForm formAnnotation = tabbedFormClass.getAnnotation( AForm.class );
142 if( formAnnotation == null )
143 {
144 throw new RuntimeException( "formClass is not annotated correctly.." );
145 }
146
147 MessageSupport messages = MessageSupport.getMessages( tabbedFormClass );
148 XFormDialogBuilder builder = XFormFactory.createDialogBuilder( formAnnotation.name() );
149
150 for( Field field : tabbedFormClass.getFields() )
151 {
152 APage pageAnnotation = field.getAnnotation( APage.class );
153 if( pageAnnotation != null )
154 {
155 buildForm( builder, pageAnnotation.name(), field.getType(), messages );
156 }
157 }
158
159 XFormDialog dialog = builder.buildWizard( formAnnotation.description(), UISupport.createImageIcon( formAnnotation
160 .icon() ), formAnnotation.helpUrl() );
161
162 return dialog;
163 }
164
165 private static void buildForm( XFormDialogBuilder builder, String name, Class<?> formClass, MessageSupport messages )
166 {
167 XForm form = builder.createForm( name );
168 for( Field formField : formClass.getFields() )
169 {
170 AField formFieldAnnotation = formField.getAnnotation( AField.class );
171 if( formFieldAnnotation != null )
172 {
173 try
174 {
175 addFormField( form, formField, formFieldAnnotation, messages );
176 }
177 catch( Exception e )
178 {
179 e.printStackTrace();
180 }
181 }
182 }
183 }
184
185 private static void addFormField( XForm form, Field formField, AField fieldAnnotation, MessageSupport messages )
186 throws Exception
187 {
188 AFieldType type = fieldAnnotation.type();
189 String fieldName = fieldAnnotation.name();
190 String name = messages.get( fieldName.length() == 0 ? formField.get( null ).toString() : fieldName );
191 String description = messages.get( fieldAnnotation.description() );
192 String[] values = messages.getArray( fieldAnnotation.values() );
193 String defaultValue = messages.get( fieldAnnotation.defaultValue() );
194 boolean enabled = fieldAnnotation.enabled();
195
196 XFormField field = null;
197 switch( type )
198 {
199 case STRING :
200 field = form.addTextField( name, description, FieldType.TEXT );
201 break;
202 case INT :
203 field = form.addTextField( name, description, FieldType.TEXT );
204 ( ( XFormTextField )field ).setWidth( 10 );
205 break;
206 case STRINGAREA :
207 field = form.addTextField( name, description, FieldType.TEXTAREA );
208 break;
209 case BOOLEAN :
210 field = form.addCheckBox( name, description );
211 break;
212 case FILE :
213 field = form.addTextField( name, description, FieldType.FILE );
214 break;
215 case FOLDER :
216 field = form.addTextField( name, description, FieldType.FOLDER );
217 break;
218 case ENUMERATION :
219 field = form.addComboBox( name, values, description );
220 break;
221 case RADIOGROUP :
222 field = form.addComponent( name, new XFormRadioGroup( values ) );
223 break;
224 case MULTILIST :
225 field = form.addComponent( name, new XFormMultiSelectList( values ) );
226 break;
227 case STRINGLIST :
228 field = form.addComponent( name, new JStringListFormField( description, defaultValue ) );
229 break;
230 case TABLE :
231 field = form.addComponent( name, new JTableFormField( description ) );
232 break;
233 case ACTION :
234 field = form.addComponent( name, new ActionFormFieldComponent( name, description ) );
235 break;
236 case COMPONENT :
237 field = form.addComponent( name, new JComponentFormField( name, description ) );
238 break;
239 case PASSWORD :
240 field = form.addComponent( name, new JPasswordFieldFormField() );
241 break;
242 case SEPARATOR :
243 form.addSeparator( description );
244 default :
245 System.out.println( "Unsupported field type: " + type );
246 }
247
248 if( field != null )
249 field.setEnabled( enabled );
250 }
251 }