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