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.event.ActionEvent;
16  
17  import javax.swing.AbstractAction;
18  import javax.swing.Action;
19  import javax.swing.ImageIcon;
20  import javax.swing.KeyStroke;
21  
22  import com.eviware.soapui.support.HelpActionMarker;
23  import com.eviware.soapui.support.Tools;
24  import com.eviware.soapui.support.UISupport;
25  import com.eviware.soapui.support.action.swing.ActionList;
26  import com.eviware.soapui.support.action.swing.DefaultActionList;
27  import com.eviware.x.form.XForm;
28  import com.eviware.x.form.XFormDialog;
29  import com.eviware.x.form.XFormDialogBuilder;
30  
31  public class SwingXFormDialogBuilder extends XFormDialogBuilder
32  {
33     private String name;
34  	private SwingXFormDialog dialog;
35  
36     public SwingXFormDialogBuilder(String name)
37     {
38        this.name = name;
39     }
40     
41     @Override
42     public XForm createForm(String name)
43     {
44        XForm form = new SwingXFormImpl(name);
45        addForm(form);
46        return form;
47     }
48  
49     @Override
50     public XFormDialog buildDialog(ActionList actions, String description, ImageIcon icon )
51     {
52     	XForm[] forms = getForms();
53  		dialog = forms.length > 1 ? new JTabbedFormDialog( name, forms, actions, description, icon ) : 
54  				new JFormDialog( name, ( SwingXFormImpl ) forms[0], actions, description, icon );
55  		
56  		return dialog;
57     }
58  
59     @Override
60     public XFormDialog buildWizard(String description, ImageIcon icon, String helpURL )
61     {
62        Action helpAction = (helpURL.length() > 0 ? new HelpAction(helpURL) : null);
63        XForm[] forms = getForms();
64        dialog = new JWizardDialog( name, forms, helpAction, description, icon );
65        
66        return dialog;
67     }
68  
69  	@Override
70     public ActionList buildOkCancelActions()
71  	{
72  		DefaultActionList actions = new DefaultActionList("Actions");
73  		actions.addAction( new OKAction() );
74  		actions.addAction( new CancelAction() );
75  		return actions;
76  	}
77  	
78  	@Override
79     public ActionList buildOkCancelHelpActions(String url)
80  	{
81  		DefaultActionList actions = new DefaultActionList("Actions");
82  		actions.addAction( new HelpAction( url ));
83  		OKAction okAction = new OKAction();
84  		actions.addAction( okAction );
85  		actions.addAction( new CancelAction() );
86  		actions.setDefaultAction( okAction );
87  		return actions;
88  	}
89  
90  	protected final class OKAction extends AbstractAction
91  	{
92  		public OKAction()
93  		{
94  			super( "OK" );
95  		}
96  		
97  		public void actionPerformed(ActionEvent e)
98  		{
99  			if( dialog != null )
100 			{
101 				dialog.setReturnValue( XFormDialog.OK_OPTION );
102 				dialog.setVisible( false );
103 			}
104 		}
105 	}
106 	
107 	protected final class CancelAction extends AbstractAction
108 	{
109 		public CancelAction()
110 		{
111 			super( "Cancel" );
112 		}
113 		
114 		public void actionPerformed(ActionEvent e)
115 		{
116 			if( dialog != null )
117 			{
118 				dialog.setReturnValue( XFormDialog.CANCEL_OPTION );
119 				dialog.setVisible( false );
120 			}
121 		}
122 	}
123 	
124 	public final class HelpAction extends AbstractAction implements HelpActionMarker
125 	{
126 		private final String url;
127 
128 		public HelpAction( String url )
129 		{
130 			this( "Online Help", url, UISupport.getKeyStroke( "F1" ) );
131 		}
132 		
133 		public HelpAction( String title, String url )
134 		{
135 			this( title, url, null );
136 		}
137 		
138 		public HelpAction( String title, String url, KeyStroke accelerator )
139 	   {
140 	      super( title );
141 			this.url = url;
142 	      putValue( Action.SHORT_DESCRIPTION, "Show online help" );
143 	      if( accelerator != null )
144 	      	putValue( Action.ACCELERATOR_KEY, accelerator );
145 	      
146 	      putValue( Action.SMALL_ICON, UISupport.HELP_ICON );
147 	   }
148 	   
149 	   public void actionPerformed(ActionEvent e)
150 		{
151 	   	Tools.openURL( url );
152 		}
153 	}
154 }