View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.soapui.impl.wsdl.actions.project;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Component;
18  import java.awt.event.ActionEvent;
19  
20  import javax.swing.AbstractAction;
21  import javax.swing.Action;
22  import javax.swing.BorderFactory;
23  import javax.swing.JDialog;
24  import javax.swing.KeyStroke;
25  
26  import com.eviware.soapui.support.HelpActionMarker;
27  import com.eviware.soapui.support.Tools;
28  import com.eviware.soapui.support.UISupport;
29  import com.eviware.soapui.support.action.swing.ActionList;
30  import com.eviware.soapui.support.action.swing.DefaultActionList;
31  import com.eviware.soapui.support.components.JButtonBar;
32  
33  public abstract class SimpleDialog extends JDialog
34  {
35  	protected JButtonBar buttons = null;
36  
37  	public SimpleDialog( String title, String description, String helpUrl, boolean okAndCancel )
38  	{
39  		super( UISupport.getMainFrame(), title, true );
40  
41  		buttons = UISupport.initDialogActions( buildActions( helpUrl, okAndCancel ), this );
42  		buttons.setBorder( BorderFactory.createEmptyBorder( 5, 0, 0, 0 ) );
43  
44  		getContentPane().add(
45  				UISupport.buildDescription( title, description, UISupport.createImageIcon( UISupport.TOOL_ICON_PATH ) ),
46  				BorderLayout.NORTH );
47  
48  		getContentPane().add( buildContent(), BorderLayout.CENTER );
49  
50  		buttons.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory
51  				.createMatteBorder( 1, 0, 0, 0, Color.GRAY ), BorderFactory.createMatteBorder( 1, 0, 0, 0, Color.WHITE ) ),
52  				BorderFactory.createEmptyBorder( 3, 5, 3, 5 ) ) );
53  
54  		getContentPane().add( buttons, BorderLayout.SOUTH );
55  		modifyButtons();
56  		
57  		pack();
58  	}
59  
60  	/*
61  	 * overide this to change buttons at bottom of dialog. I did not make it
62  	 * abstrac because it would require refactoring when SimpleDialog is used.
63  	 * Robert.
64  	 */
65  	protected void modifyButtons()
66  	{
67  	};
68  
69  	public SimpleDialog( String title, String description, String helpUrl )
70  	{
71  		this( title, description, helpUrl, true );
72  	}
73  
74  	protected abstract Component buildContent();
75  
76  	public ActionList buildActions( String url, boolean okAndCancel )
77  	{
78  		DefaultActionList actions = new DefaultActionList( "Actions" );
79  		if( url != null )
80  			actions.addAction( new HelpAction( url ) );
81  
82  		OKAction okAction = new OKAction();
83  		actions.addAction( okAction );
84  		if( okAndCancel )
85  		{
86  			actions.addAction( new CancelAction() );
87  			actions.setDefaultAction( okAction );
88  		}
89  		return actions;
90  	}
91  
92  	protected abstract boolean handleOk();
93  
94  	@Override
95  	public void setVisible( boolean b )
96  	{
97  		if( b )
98  			beforeShow();
99  		else
100 			beforeHide();
101 
102 		UISupport.centerDialog( this );
103 		super.setVisible( b );
104 
105 		if( b )
106 			afterShow();
107 		else
108 			afterHide();
109 	}
110 
111 	protected void afterHide()
112 	{
113 	}
114 
115 	protected void afterShow()
116 	{
117 	}
118 
119 	protected void beforeHide()
120 	{
121 	}
122 
123 	protected void beforeShow()
124 	{
125 	}
126 
127 	protected boolean handleCancel()
128 	{
129 		return true;
130 	}
131 
132 	protected final class OKAction extends AbstractAction
133 	{
134 		public OKAction()
135 		{
136 			super( "OK" );
137 		}
138 
139 		public void actionPerformed( ActionEvent e )
140 		{
141 			if( handleOk() )
142 			{
143 				setVisible( false );
144 			}
145 		}
146 	}
147 
148 	protected final class CancelAction extends AbstractAction
149 	{
150 		public CancelAction()
151 		{
152 			super( "Cancel" );
153 		}
154 
155 		public void actionPerformed( ActionEvent e )
156 		{
157 			if( handleCancel() )
158 			{
159 				setVisible( false );
160 			}
161 		}
162 	}
163 
164 	public final class HelpAction extends AbstractAction implements HelpActionMarker
165 	{
166 		private final String url;
167 
168 		public HelpAction( String url )
169 		{
170 			this( "Online Help", url, UISupport.getKeyStroke( "F1" ) );
171 		}
172 
173 		public HelpAction( String title, String url )
174 		{
175 			this( title, url, null );
176 		}
177 
178 		public HelpAction( String title, String url, KeyStroke accelerator )
179 		{
180 			super( title );
181 			this.url = url;
182 			putValue( Action.SHORT_DESCRIPTION, "Show online help" );
183 			if( accelerator != null )
184 				putValue( Action.ACCELERATOR_KEY, accelerator );
185 
186 			putValue( Action.SMALL_ICON, UISupport.HELP_ICON );
187 		}
188 
189 		public void actionPerformed( ActionEvent e )
190 		{
191 			Tools.openURL( url );
192 		}
193 	}
194 }