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