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.support.UISupport;
18 import com.eviware.soapui.support.action.ActionList;
19 import com.eviware.x.form.XForm;
20 import com.eviware.x.form.XFormDialog;
21 import com.eviware.x.form.XFormDialogBuilder;
22 import com.eviware.x.form.XFormFactory;
23 import com.eviware.x.form.XFormField;
24 import com.eviware.x.form.XFormTextField;
25 import com.eviware.x.form.XForm.FieldType;
26
27 public class ADialogBuilder
28 {
29 public static XFormDialog buildDialog( Class<? extends Object> formClass )
30 {
31 return buildDialog( formClass, null );
32 }
33
34 public static XFormDialog buildDialog( Class<? extends Object> formClass, ActionList actions )
35 {
36 AForm formAnnotation = formClass.getAnnotation( AForm.class );
37 if( formAnnotation == null )
38 {
39 throw new RuntimeException( "formClass is not annotated correctly.." );
40 }
41
42 XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name());
43 XForm form = builder.createForm( "Basic" );
44
45 for( Field field : formClass.getFields() )
46 {
47 AField fieldAnnotation = field.getAnnotation( AField.class );
48 if( fieldAnnotation != null )
49 {
50 addFormField( form, fieldAnnotation );
51 }
52 }
53
54 ActionList defaultActions = formAnnotation.helpUrl().length() == 0 ?
55 builder.buildOkCancelActions() : builder.buildOkCancelHelpActions( formAnnotation.helpUrl() );
56
57 if( actions == null )
58 actions = defaultActions;
59 else
60 actions.addActions( defaultActions );
61
62 XFormDialog dialog = builder.buildDialog( actions, formAnnotation.description(),
63 UISupport.createImageIcon( formAnnotation.icon() ));
64
65 return dialog;
66 }
67
68 public static XFormDialog buildTabbedDialog( Class<? extends Object> tabbedFormClass, ActionList actions )
69 {
70 AForm formAnnotation = tabbedFormClass.getAnnotation( AForm.class );
71 if( formAnnotation == null )
72 {
73 throw new RuntimeException( "formClass is not annotated correctly.." );
74 }
75
76 XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name());
77
78 for( Field field : tabbedFormClass.getFields() )
79 {
80 AField fieldAnnotation = field.getAnnotation( AField.class );
81 if( fieldAnnotation != null )
82 {
83 XForm form = builder.createForm( fieldAnnotation.name() );
84
85 try
86 {
87 Class formClass = Class.forName( fieldAnnotation.description() );
88
89 for( Field formField : formClass.getFields() )
90 {
91 AField formFieldAnnotation = formField.getAnnotation( AField.class );
92 if( formFieldAnnotation != null )
93 {
94 addFormField( form, formFieldAnnotation );
95 }
96 }
97 }
98 catch( Exception e )
99 {
100 e.printStackTrace();
101 }
102 }
103 }
104
105 ActionList defaultActions = formAnnotation.helpUrl().length() == 0 ?
106 builder.buildOkCancelActions() : builder.buildOkCancelHelpActions( formAnnotation.helpUrl() );
107
108 if( actions == null )
109 actions = defaultActions;
110 else
111 actions.addActions( defaultActions );
112
113 XFormDialog dialog = builder.buildDialog( actions, formAnnotation.description(),
114 UISupport.createImageIcon( formAnnotation.icon() ));
115
116 return dialog;
117 }
118
119 private static void addFormField( XForm form, AField fieldAnnotation )
120 {
121 XFormField field = null;
122
123 switch( fieldAnnotation.type() )
124 {
125 case STRING :
126 {
127 field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.TEXT );
128 break;
129 }
130 case INT :
131 {
132 field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.TEXT );
133 ((XFormTextField)field).setWidth( 10 );
134 break;
135 }
136 case STRINGAREA :
137 {
138 field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.TEXTAREA );
139 break;
140 }
141 case BOOLEAN :
142 {
143 field = form.addCheckBox( fieldAnnotation.name(), fieldAnnotation.description() );
144 break;
145 }
146 case FILE :
147 {
148 field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.FILE );
149 break;
150 }
151 case ENUMERATION :
152 {
153 field = form.addComboBox( fieldAnnotation.name(), fieldAnnotation.values(), fieldAnnotation.description() );
154 break;
155 }
156 }
157
158 if( field != null )
159 field.setEnabled( fieldAnnotation.enabled() );
160 }
161
162 }