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     public XForm createForm(String name)
42     {
43        XForm form = new SwingXFormImpl(name);
44        addForm(form);
45        return form;
46     }
47  
48     public XFormDialog buildDialog(ActionList actions, String description, ImageIcon icon )
49     {
50     	XForm[] forms = getForms();
51  		dialog = forms.length > 1 ? new JTabbedFormDialog( name, forms, actions, description, icon ) : 
52  				new JFormDialog( name, ( SwingXFormImpl ) forms[0], actions, description, icon );
53  		
54  		return dialog;
55     }
56  
57  	public ActionList buildOkCancelActions()
58  	{
59  		DefaultActionList actions = new DefaultActionList("Actions");
60  		actions.addAction( new OKAction() );
61  		actions.addAction( new CancelAction() );
62  		return actions;
63  	}
64  	
65  	public ActionList buildOkCancelHelpActions(String url)
66  	{
67  		DefaultActionList actions = new DefaultActionList("Actions");
68  		actions.addAction( new HelpAction( url ));
69  		OKAction okAction = new OKAction();
70  		actions.addAction( okAction );
71  		actions.addAction( new CancelAction() );
72  		actions.setDefaultAction( okAction );
73  		return actions;
74  	}
75  
76  	protected final class OKAction extends AbstractAction
77  	{
78  		public OKAction()
79  		{
80  			super( "OK" );
81  		}
82  		
83  		public void actionPerformed(ActionEvent e)
84  		{
85  			if( dialog != null )
86  			{
87  				dialog.setReturnValue( XFormDialog.OK_OPTION );
88  				dialog.setVisible( false );
89  			}
90  		}
91  	}
92  	
93  	protected final class CancelAction extends AbstractAction
94  	{
95  		public CancelAction()
96  		{
97  			super( "Cancel" );
98  		}
99  		
100 		public void actionPerformed(ActionEvent e)
101 		{
102 			if( dialog != null )
103 			{
104 				dialog.setReturnValue( XFormDialog.CANCEL_OPTION );
105 				dialog.setVisible( false );
106 			}
107 		}
108 	}
109 	
110 	public final class HelpAction extends AbstractAction implements HelpActionMarker
111 	{
112 		private final String url;
113 
114 		public HelpAction( String url )
115 		{
116 			this( "Online Help", url, UISupport.getKeyStroke( "F1" ) );
117 		}
118 		
119 		public HelpAction( String title, String url )
120 		{
121 			this( title, url, null );
122 		}
123 		
124 		public HelpAction( String title, String url, KeyStroke accelerator )
125 	   {
126 	      super( title );
127 			this.url = url;
128 	      putValue( Action.SHORT_DESCRIPTION, "Show online help" );
129 	      if( accelerator != null )
130 	      	putValue( Action.ACCELERATOR_KEY, accelerator );
131 	      
132 	      putValue( Action.SMALL_ICON, UISupport.HELP_ICON );
133 	   }
134 	   
135 	   public void actionPerformed(ActionEvent e)
136 		{
137 	   	Tools.openURL( url );
138 		}
139 	}
140 }