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