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        	AField fieldAnnotation = field.getAnnotation( AField.class );
88        	if( fieldAnnotation != null )
89        	{
90        		XForm form = builder.createForm( fieldAnnotation.name() );
91              
92        		try
93  				{
94  					Class formClass = Class.forName( fieldAnnotation.description() );
95  					
96  					for( Field formField : formClass.getFields() )
97  					{
98  						AField formFieldAnnotation = formField.getAnnotation( AField.class );
99  						if( formFieldAnnotation != null )
100 						{
101 							addFormField( form, formFieldAnnotation );
102 						}
103 					}
104 				}
105 				catch( Exception e )
106 				{
107 					SoapUI.logError( e );
108 				}
109       	}
110       }
111       
112       ActionList defaultActions = formAnnotation.helpUrl().length() == 0 ? 
113       			builder.buildOkCancelActions() : builder.buildOkCancelHelpActions( formAnnotation.helpUrl() );
114 
115       if( actions == null )
116       	actions = defaultActions;
117       else
118       	actions.addActions( defaultActions );
119       
120       XFormDialog dialog = builder.buildDialog( actions, formAnnotation.description(), 
121       			UISupport.createImageIcon( formAnnotation.icon() ));
122       
123 		return dialog;
124 	}
125 
126 	private static void addFormField( XForm form, AField fieldAnnotation )
127 	{
128 		XFormField field = null;
129 		
130 		switch( fieldAnnotation.type() )
131 		{
132 			case STRING :
133 				field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.TEXT );
134 				break;
135 			case INT :
136 				field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.TEXT );
137 				((XFormTextField)field).setWidth( 10 );
138 				break;
139 			case STRINGAREA :
140 				field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.TEXTAREA );
141 				break;
142 			case BOOLEAN :
143 				field = form.addCheckBox( fieldAnnotation.name(), fieldAnnotation.description() );
144 				break;
145 			case FILE :
146 				field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.FILE );
147 				break;
148 			case FOLDER :
149 				field = form.addTextField( fieldAnnotation.name(), fieldAnnotation.description(), FieldType.FOLDER );
150 				break;
151 			case ENUMERATION :
152 				field = form.addComboBox( fieldAnnotation.name(), fieldAnnotation.values(), fieldAnnotation.description() );
153 				break;
154 			case RADIOGROUP :
155 				field = form.addComponent( fieldAnnotation.name(), new XFormRadioGroup( fieldAnnotation.values() ) );
156 				break;
157 			case MULTILIST :
158 				field = form.addComponent( fieldAnnotation.name(), new XFormMultiSelectList( fieldAnnotation.values() ) );
159 				break;
160 			default :
161 				System.out.println( "Unsupported field type: " + fieldAnnotation.type() );
162 		}
163 		
164 		if( field != null )
165 			field.setEnabled( fieldAnnotation.enabled() );
166 	}
167 
168 }