View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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 FILE_OR_FOLDER :
219 			field = form.addTextField( name, description, FieldType.FILE_OR_FOLDER );
220 			break;
221 		case ENUMERATION :
222 			field = form.addComboBox( name, values, description );
223 			break;
224 		case RADIOGROUP :
225 			field = form.addComponent( name, new XFormRadioGroup( values ) );
226 			break;
227 		case MULTILIST :
228 			field = form.addComponent( name, new XFormMultiSelectList( values ) );
229 			break;
230 		case STRINGLIST :
231 			field = form.addComponent( name, new JStringListFormField( description, defaultValue ) );
232 			break;
233 		case TABLE :
234 			field = form.addComponent( name, new JTableFormField( description ) );
235 			break;
236 		case ACTION :
237 			field = form.addComponent( name, new ActionFormFieldComponent( name, description ) );
238 			break;
239 		case COMPONENT :
240 			field = form.addComponent( name, new JComponentFormField( name, description ) );
241 			break;
242 		case PASSWORD :
243 			field = form.addComponent( name, new JPasswordFieldFormField() );
244 			break;
245 		case SEPARATOR :
246 			form.addSeparator( description );
247 		default :
248 			System.out.println( "Unsupported field type: " + type );
249 		}
250 
251 		if( field != null )
252 			field.setEnabled( enabled );
253 	}
254 }