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.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.
62  	 * I did not make it abstrac because it would require 
63  	 * refactoring when SimpleDialog is used. 
64  	 * Robert.
65  	 */
66  	protected void modifyButtons() {};
67  
68  	public SimpleDialog(String title, String description, String helpUrl)
69  	{
70  		this(title, description, helpUrl, true);
71  	}
72  
73  	protected abstract Component buildContent();
74  
75  	public ActionList buildActions(String url, boolean okAndCancel)
76  	{
77  		DefaultActionList actions = new DefaultActionList("Actions");
78  		if (url != null)
79  			actions.addAction(new HelpAction(url));
80  
81  		OKAction okAction = new OKAction();
82  		actions.addAction(okAction);
83  		if (okAndCancel)
84  		{
85  			actions.addAction(new CancelAction());
86  			actions.setDefaultAction(okAction);
87  		}
88  		return actions;
89  	}
90  
91  	protected abstract boolean handleOk();
92  
93  	@Override
94  	public void setVisible(boolean b)
95  	{
96  		if (b)
97  			beforeShow();
98  		else
99  			beforeHide();
100 
101 		UISupport.centerDialog(this);
102 		super.setVisible(b);
103 
104 		if (b)
105 			afterShow();
106 		else
107 			afterHide();
108 	}
109 
110 	protected void afterHide()
111 	{
112 	}
113 
114 	protected void afterShow()
115 	{
116 	}
117 
118 	protected void beforeHide()
119 	{
120 	}
121 
122 	protected void beforeShow()
123 	{
124 	}
125 
126 	protected boolean handleCancel()
127 	{
128 		return true;
129 	}
130 
131 	protected final class OKAction extends AbstractAction
132 	{
133 		public OKAction()
134 		{
135 			super("OK");
136 		}
137 
138 		public void actionPerformed(ActionEvent e)
139 		{
140 			if (handleOk())
141 			{
142 				setVisible(false);
143 			}
144 		}
145 	}
146 
147 	protected final class CancelAction extends AbstractAction
148 	{
149 		public CancelAction()
150 		{
151 			super("Cancel");
152 		}
153 
154 		public void actionPerformed(ActionEvent e)
155 		{
156 			if (handleCancel())
157 			{
158 				setVisible(false);
159 			}
160 		}
161 	}
162 
163 	public final class HelpAction extends AbstractAction implements HelpActionMarker
164 	{
165 		private final String url;
166 
167 		public HelpAction(String url)
168 		{
169 			this("Online Help", url, UISupport.getKeyStroke("F1"));
170 		}
171 
172 		public HelpAction(String title, String url)
173 		{
174 			this(title, url, null);
175 		}
176 
177 		public HelpAction(String title, String url, KeyStroke accelerator)
178 		{
179 			super(title);
180 			this.url = url;
181 			putValue(Action.SHORT_DESCRIPTION, "Show online help");
182 			if (accelerator != null)
183 				putValue(Action.ACCELERATOR_KEY, accelerator);
184 
185 			putValue(Action.SMALL_ICON, UISupport.HELP_ICON);
186 		}
187 
188 		public void actionPerformed(ActionEvent e)
189 		{
190 			Tools.openURL(url);
191 		}
192 	}
193 }