View Javadoc

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