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