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