1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.x.impl.swing;
14
15 import java.awt.BorderLayout;
16 import java.awt.CardLayout;
17 import java.awt.Cursor;
18 import java.awt.Dimension;
19 import java.awt.event.ActionEvent;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.List;
24
25 import javax.swing.AbstractAction;
26 import javax.swing.Action;
27 import javax.swing.BorderFactory;
28 import javax.swing.ImageIcon;
29 import javax.swing.JComponent;
30 import javax.swing.JDialog;
31 import javax.swing.JPanel;
32
33 import com.eviware.soapui.SoapUI;
34 import com.eviware.soapui.actions.UpdateableAction;
35 import com.eviware.soapui.support.DescriptionPanel;
36 import com.eviware.soapui.support.UISupport;
37 import com.eviware.soapui.support.action.swing.DefaultActionList;
38 import com.eviware.soapui.support.components.JButtonBar;
39 import com.eviware.soapui.support.types.StringToStringMap;
40 import com.eviware.x.form.WizardPage;
41 import com.eviware.x.form.XForm;
42 import com.eviware.x.form.XFormDialog;
43 import com.eviware.x.form.XFormField;
44
45 public class JWizardDialog extends SwingXFormDialog
46 {
47 private String name;
48 private ArrayList<String> pageNames = new ArrayList<String>();
49
50 private JDialog dialog;
51 private DescriptionPanel descriptionPanel;
52 private List<SwingXFormImpl> forms = new ArrayList<SwingXFormImpl>();
53 private JPanel pages;
54 private CardLayout cardLayout;
55
56 private HashMap<String, WizardPage> controllers = new HashMap<String, WizardPage>();
57 private int currentPage = 0;
58
59 private DefaultActionList actions;
60
61 public JWizardDialog(String name, XForm[] forms, Action helpAction, String description, ImageIcon icon)
62 {
63 this.name = name;
64 dialog = new JDialog( UISupport.getMainFrame(), name, true );
65
66 initActions(helpAction);
67
68 cardLayout = new CardLayout();
69 pages = new JPanel( cardLayout );
70 for(XForm form : forms)
71 {
72 SwingXFormImpl swingFormImpl = (SwingXFormImpl) form;
73 this.forms.add( swingFormImpl );
74
75 JPanel panel = swingFormImpl.getPanel();
76 panel.setBorder( BorderFactory.createEmptyBorder( 0, 0, 5, 0 ));
77
78 addPage( form.getName(), panel );
79 }
80
81 JButtonBar buttons = UISupport.initDialogActions( actions, dialog );
82
83 if( description != null || icon != null )
84 {
85 descriptionPanel = UISupport.buildDescription( name, description, icon );
86 dialog.getContentPane().add( descriptionPanel, BorderLayout.NORTH );
87 }
88
89 dialog.getContentPane().add( pages, BorderLayout.CENTER );
90
91 buttons.setBorder( BorderFactory.createEmptyBorder( 3, 5, 3, 5 ));
92 dialog.getContentPane().add( buttons, BorderLayout.SOUTH );
93 dialog.pack();
94 Dimension size = dialog.getSize();
95 if( size.getHeight() < 300 )
96 {
97 dialog.setSize( new Dimension( ( int ) size.getWidth(), 300 ) );
98 }
99 }
100
101 public void dispose()
102 {
103 dialog.dispose();
104 }
105
106 private void initActions(Action helpAction)
107 {
108 actions = new DefaultActionList();
109 actions.addAction( new BackAction() );
110 actions.addAction( new NextAction() );
111 actions.addAction( new CancelAction() );
112 actions.addAction( new FinishAction() );
113 if(helpAction != null)
114 actions.addAction( helpAction );
115 }
116
117 private void addPage( String name, JComponent component )
118 {
119 pages.add( component, name );
120 if(!pageNames.contains(name))
121 pageNames.add( name );
122 actions.update();
123 }
124
125 public void addPageController( WizardPage controller )
126 {
127 controllers.put( controller.getName(), controller );
128 }
129
130 public void addPageAndController( JComponent component, WizardPage controller )
131 {
132 addPage( controller.getName(), component );
133 addPageController( controller );
134 }
135
136 public void setValues(StringToStringMap values)
137 {
138 for( XForm form : forms )
139 form.setValues( values );
140 }
141
142 public void setOptions(String field, Object[] options)
143 {
144 for( XForm form : forms )
145 form.setOptions( field, options );
146 }
147
148 public XFormField getFormField( String name )
149 {
150 for( XForm form : forms )
151 {
152 XFormField formField = form.getFormField( name );
153 if( formField != null )
154 return formField;
155 }
156
157 return null;
158 }
159
160 public StringToStringMap getValues()
161 {
162 StringToStringMap result = new StringToStringMap();
163
164 for( XForm form : forms )
165 result.putAll( form.getValues());
166
167 return result;
168 }
169
170 public void setVisible(boolean visible)
171 {
172 if( visible )
173 {
174 if( showPage( 0 ) )
175 {
176 UISupport.centerDialog( dialog );
177 dialog.setVisible( visible );
178 }
179 }
180 else
181 {
182 dialog.setVisible( visible );
183 }
184 }
185
186 public boolean validate()
187 {
188 return true;
189 }
190
191 public void setFormFieldProperty(String name, Object value)
192 {
193 for(XForm form : forms)
194 form.setFormFieldProperty( name, value );
195 }
196
197 public String getValue( String field )
198 {
199 for(XForm form : forms)
200 {
201 if( form.getComponent( field ) != null )
202 return form.getComponent( field ).getValue();
203 }
204
205 return null;
206 }
207
208 public void setValue( String field, String value )
209 {
210 for(XForm form : forms)
211 {
212 if( form.getComponent( field ) != null )
213 form.getComponent( field ).setValue( value );
214 }
215 }
216
217 public int getValueIndex( String name )
218 {
219 for(SwingXFormImpl form : forms)
220 {
221 if( form.getComponent( name ) != null )
222 {
223 String [] options = form.getOptions( name );
224 if( options == null )
225 return -1;
226
227 return Arrays.asList( options ).indexOf( form.getComponentValue( name ) );
228 }
229 }
230
231 return -1;
232 }
233
234 public boolean show()
235 {
236 setReturnValue( XFormDialog.CANCEL_OPTION );
237 show( new StringToStringMap() );
238 return getReturnValue() == XFormDialog.OK_OPTION;
239 }
240
241 public void setWidth( int i )
242 {
243 dialog.setPreferredSize( new Dimension( i, ( int ) dialog.getPreferredSize().getHeight()) );
244 }
245
246 public void setSize(int w, int h)
247 {
248 dialog.setSize(w, h);
249 }
250
251 private boolean showPage(int pageNo)
252 {
253 currentPage = pageNo;
254 String pageName = pageNames.get(currentPage);
255 WizardPage page = controllers.get( pageName );
256
257 descriptionPanel.setTitle(page.getName());
258 descriptionPanel.setDescription(page.getDescription());
259 cardLayout.show( pages, pageName);
260
261 if( initPage(pageName, page) )
262 {
263 actions.update();
264 return true;
265 }
266 else
267 {
268 setVisible(false);
269 return false;
270 }
271 }
272
273 private boolean initPage(String pageName, WizardPage page)
274 {
275 try
276 {
277 dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
278 return page.init();
279 }
280 catch(Exception e)
281 {
282 dialog.setCursor(Cursor.getDefaultCursor());
283 SoapUI.logError(e);
284 UISupport.showInfoMessage( pageName + " could not be initialized", this.name );
285 return false;
286 }
287 finally
288 {
289 dialog.setCursor(Cursor.getDefaultCursor());
290 }
291 }
292
293 private boolean runCurrentPage()
294 {
295 String pageName = pageNames.get(currentPage);
296 WizardPage controller = controllers.get( pageName );
297 try
298 {
299 dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
300 return controller.run();
301 }
302 catch(Exception e)
303 {
304 dialog.setCursor(Cursor.getDefaultCursor());
305 SoapUI.logError(e);
306 UISupport.showInfoMessage( pageName + " failed", this.name );
307 return false;
308 }
309 finally
310 {
311 dialog.setCursor(Cursor.getDefaultCursor());
312 }
313 }
314
315 private class BackAction extends AbstractAction implements UpdateableAction
316 {
317 public BackAction()
318 {
319 super( "< Back" );
320 }
321
322 public void update()
323 {
324 boolean enable = false;
325 if(currentPage > 0)
326 {
327 String pageName = pageNames.get(currentPage-1);
328 WizardPage prevPage = controllers.get( pageName );
329 enable = prevPage.canGoBack();
330 }
331 setEnabled(enable);
332 }
333
334 public void actionPerformed(ActionEvent e)
335 {
336 showPage(currentPage-1);
337 }
338 }
339
340 private class NextAction extends AbstractAction implements UpdateableAction
341 {
342 public NextAction()
343 {
344 super( "Next >" );
345 }
346
347 public void update()
348 {
349 setEnabled( currentPage + 1 < pageNames.size() );
350 }
351
352 public void actionPerformed(ActionEvent evt)
353 {
354 if( runCurrentPage() )
355 showPage(currentPage+1);
356 else
357 setVisible(false);
358 }
359 }
360
361 private final class CancelAction extends AbstractAction implements UpdateableAction
362 {
363 public CancelAction()
364 {
365 super( "Cancel" );
366 }
367
368 public void update()
369 {
370 }
371
372 public void actionPerformed(ActionEvent e)
373 {
374 setReturnValue( XFormDialog.CANCEL_OPTION );
375 setVisible( false );
376 }
377 }
378
379 private final class FinishAction extends AbstractAction implements UpdateableAction
380 {
381 public FinishAction()
382 {
383 super( "Finish" );
384 }
385
386 public void update()
387 {
388 setEnabled( currentPage == pageNames.size() - 1 );
389 }
390
391 public void actionPerformed(ActionEvent e)
392 {
393 runCurrentPage();
394 setReturnValue( XFormDialog.OK_OPTION );
395 setVisible( false );
396 }
397 }
398
399 public void release()
400 {
401 dialog.dispose();
402 }
403 }