View Javadoc

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