View Javadoc

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