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  	public SimpleDialog(String title, String description, String helpUrl, boolean okAndCancel)
36  	{
37  		super(UISupport.getMainFrame(), title, true);
38  
39  		JButtonBar buttons = UISupport.initDialogActions(buildActions(helpUrl, okAndCancel), this);
40  		buttons.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
41  
42  		getContentPane().add(
43  				UISupport.buildDescription(title, description, UISupport.createImageIcon(UISupport.TOOL_ICON_PATH)),
44  				BorderLayout.NORTH);
45  
46  		getContentPane().add(buildContent(), BorderLayout.CENTER);
47  
48  		buttons.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory
49  				.createMatteBorder(1, 0, 0, 0, Color.GRAY), BorderFactory.createMatteBorder(1, 0, 0, 0, Color.WHITE)),
50  				BorderFactory.createEmptyBorder(3, 5, 3, 5)));
51  
52  		getContentPane().add(buttons, BorderLayout.SOUTH);
53  		pack();
54  	}
55  
56  	public SimpleDialog(String title, String description, String helpUrl)
57  	{
58  		this(title, description, helpUrl, true);
59  	}
60  	
61  	protected abstract Component buildContent();
62  
63  	public ActionList buildActions(String url, boolean okAndCancel)
64  	{
65  		DefaultActionList actions = new DefaultActionList("Actions");
66  		if (url != null)
67  			actions.addAction(new HelpAction(url));
68  
69  		OKAction okAction = new OKAction();
70  		actions.addAction(okAction);
71  		if (okAndCancel)
72  		{
73  			actions.addAction(new CancelAction());
74  			actions.setDefaultAction(okAction);
75  		}
76  		return actions;
77  	}
78  
79  	protected abstract boolean handleOk();
80  
81  	@Override
82  	public void setVisible(boolean b)
83  	{
84  		if (b)
85  			beforeShow();
86  		else
87  			beforeHide();
88  
89  		UISupport.centerDialog(this);
90  		super.setVisible(b);
91  
92  		if (b)
93  			afterShow();
94  		else
95  			afterHide();
96  	}
97  
98  	protected void afterHide()
99  	{
100 	}
101 
102 	protected void afterShow()
103 	{
104 	}
105 
106 	protected void beforeHide()
107 	{
108 	}
109 
110 	protected void beforeShow()
111 	{
112 	}
113 
114 	protected boolean handleCancel()
115 	{
116 		return true;
117 	}
118 
119 	protected final class OKAction extends AbstractAction
120 	{
121 		public OKAction()
122 		{
123 			super("OK");
124 		}
125 
126 		public void actionPerformed(ActionEvent e)
127 		{
128 			if (handleOk())
129 			{
130 				setVisible(false);
131 			}
132 		}
133 	}
134 
135 	protected final class CancelAction extends AbstractAction
136 	{
137 		public CancelAction()
138 		{
139 			super("Cancel");
140 		}
141 
142 		public void actionPerformed(ActionEvent e)
143 		{
144 			if (handleCancel())
145 			{
146 				setVisible(false);
147 			}
148 		}
149 	}
150 
151 	public final class HelpAction extends AbstractAction implements HelpActionMarker
152 	{
153 		private final String url;
154 
155 		public HelpAction(String url)
156 		{
157 			this("Online Help", url, UISupport.getKeyStroke("F1"));
158 		}
159 
160 		public HelpAction(String title, String url)
161 		{
162 			this(title, url, null);
163 		}
164 
165 		public HelpAction(String title, String url, KeyStroke accelerator)
166 		{
167 			super(title);
168 			this.url = url;
169 			putValue(Action.SHORT_DESCRIPTION, "Show online help");
170 			if (accelerator != null)
171 				putValue(Action.ACCELERATOR_KEY, accelerator);
172 
173 			putValue(Action.SMALL_ICON, UISupport.HELP_ICON);
174 		}
175 
176 		public void actionPerformed(ActionEvent e)
177 		{
178 			Tools.openURL(url);
179 		}
180 	}
181 }