KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > gui > awt > AbstractAWTGUI


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client.gui.awt;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Image JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.WindowAdapter JavaDoc;
29 import java.awt.event.WindowEvent JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import com.maverick.http.HttpAuthenticator;
33 import com.sshtools.ui.awt.Toaster;
34 import com.sshtools.ui.awt.UIUtil;
35 import com.sshtools.ui.awt.options.Option;
36 import com.sshtools.ui.awt.options.OptionDialog;
37 import com.sslexplorer.agent.client.ActionCallback;
38 import com.sslexplorer.agent.client.Agent;
39 import com.sslexplorer.agent.client.AgentClientGUI;
40 import com.sslexplorer.agent.client.Console;
41 import com.sslexplorer.agent.client.PortMonitor;
42 import com.sslexplorer.agent.client.TaskProgress;
43 import com.sslexplorer.agent.client.tunneling.AbstractPortItem;
44 import com.sslexplorer.agent.client.util.BrowserLauncher;
45
46 /**
47  * Abstract implementation of a {@link AgentClientGUI} that provides a
48  * <i>Toaster</i> style popup component.
49  *
50  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
51  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
52  */

53 public abstract class AbstractAWTGUI implements AgentClientGUI {
54
55     // #ifdef DEBUG
56
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(AbstractAWTGUI.class);
57     // #endif
58

59     // Private instance variables
60
private Toaster popup;
61     private Image JavaDoc popupImage;
62     private PortMonitor portMonitor;
63     private Console console;
64     private Agent agent;
65
66     /**
67      * Constructor.
68      */

69     public AbstractAWTGUI() {
70         OptionDialog.INFORMATION_ICON = "/images/dialog-information.gif"; //$NON-NLS-1$
71
OptionDialog.WARNING_ICON = "/images/dialog-warning.gif"; //$NON-NLS-1$
72
OptionDialog.QUESTION_ICON = "/images/dialog-question.gif"; //$NON-NLS-1$
73
OptionDialog.ERROR_ICON = "/images/dialog-error.gif"; //$NON-NLS-1$
74
popup = new Toaster(Toaster.BOTTOM_RIGHT, new Dimension JavaDoc(300, 100));
75         popup.setTextAlign(Component.LEFT_ALIGNMENT);
76         Font JavaDoc norm = new Font JavaDoc("Arial", Font.PLAIN, 10); //$NON-NLS-1$
77
Font JavaDoc title = new Font JavaDoc("Arial Bold", Font.BOLD, 11); //$NON-NLS-1$
78
popup.setTextFont(norm);
79         popup.setTitleFont(title);
80         popupImage = UIUtil.loadImage(AbstractAWTGUI.class, "/images/agent.gif");
81
82     }
83     
84     /* (non-Javadoc)
85      * @see com.sslexplorer.agent.client.AgentClientGUI#init(com.sslexplorer.agent.client.Agent)
86      */

87     public void init(Agent agent) {
88         this.agent = agent;
89     }
90
91     /**
92      * Get the Agent instance this GUI is being used for
93      *
94      * @return agent
95      */

96     public Agent getAgent() {
97         return agent;
98     }
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see com.sslexplorer.agent.client.AgentClientGUI#confirm(int, java.lang.String,
104      * java.lang.String, java.lang.String, java.lang.String)
105      */

106     public boolean confirm(int dialogType, String JavaDoc okText, String JavaDoc cancelText, String JavaDoc title, String JavaDoc message) {
107         Option ok = new Option(okText);
108         Option cancel = cancelText == null ? null : new Option(cancelText);
109         if (OptionDialog.prompt(getGUIComponent(), dialogType, title, message, cancel == null ? new Option[] { ok }
110             : new Option[] { ok, cancel }) != ok) { //$NON-NLS-1$$ //$NON-NLS-2$$
111
return false;
112         }
113         return true;
114     }
115
116     /*
117      * (non-Javadoc)
118      *
119      * @see com.sslexplorer.agent.client.AgentClientGUI#error(java.lang.String,
120      * java.lang.String, java.lang.String, java.lang.String,
121      * java.lang.Throwable)
122      */

123     public boolean error(String JavaDoc okText, String JavaDoc cancelText, String JavaDoc title, String JavaDoc message, Throwable JavaDoc ex) {
124         Option ok = new Option(okText);
125         Option cancel = cancelText == null ? null : new Option(cancelText);
126         if (OptionDialog.error(getGUIComponent(), title, message, ex, cancel == null ? new Option[] { ok }
127             : new Option[] { ok, cancel }) != ok) { //$NON-NLS-1$$ //$NON-NLS-2$$
128
return false;
129         }
130         return true;
131
132     }
133
134     /*
135      * (non-Javadoc)
136      *
137      * @see com.sslexplorer.vpn.client.VPNClientGUI#getGUIComponent()
138      */

139     public abstract Component JavaDoc getGUIComponent();
140
141     /*
142      * (non-Javadoc)
143      *
144      * @see com.sslexplorer.vpn.client.VPNClientGUI#popup(java.awt.event.ActionListener,
145      * java.lang.String, java.lang.String, java.awt.Image, int)
146      */

147     public void popup(final ActionCallback callback, String JavaDoc message, String JavaDoc title, String JavaDoc imageName, int timeout) {
148         Image JavaDoc image = imageName == null ? null : UIUtil.loadImage(AbstractAWTGUI.class, "/images/" + imageName + ".gif");
149         ActionListener JavaDoc l = callback == null ? null : new ActionListener JavaDoc() {
150             public void actionPerformed(ActionEvent JavaDoc evt) {
151                 callback.actionPerformed();
152             }
153         };
154         popup.popup(l, message, title, image == null ? popupImage : image, timeout);
155     }
156
157     /*
158      * (non-Javadoc)
159      *
160      * @see com.sslexplorer.agent.client.AgentClientGUI#createTaskProgress(java.lang.String,
161      * java.lang.String, int, int)
162      */

163     public TaskProgress createTaskProgress(String JavaDoc message, String JavaDoc title, long maxValue, boolean allowCancel) {
164         TaskProgress progress = new TaskProgressBar(getGUIComponent(), message, title, maxValue, allowCancel);
165         return progress;
166     }
167
168     /*
169      * (non-Javadoc)
170      *
171      * @see com.sslexplorer.agent.client.AgentClientGUI#getPortMonitor()
172      */

173     public PortMonitor getPortMonitor() {
174         if (portMonitor == null) {
175             portMonitor = new AWTPortMonitor(agent);
176         }
177         return portMonitor;
178     }
179
180     /*
181      * (non-Javadoc)
182      *
183      * @see com.sslexplorer.agent.client.AgentClientGUI#getConsole()
184      */

185     public synchronized Console getConsole() {
186         if (console == null) {
187             console = new AWTConsoleOutputStream("true".equals(System.getProperty("console.toSysOut", "false")) ? System.out : null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
188
}
189         return console;
190     }
191
192     /* (non-Javadoc)
193      * @see com.sslexplorer.agent.client.AgentClientGUI#promptForCredentials(com.maverick.http.HttpAuthenticator)
194      */

195     public boolean promptForCredentials(boolean proxy, HttpAuthenticator authenticator) {
196         return AWTAuthenticationDialog.promptForCredentials(proxy, authenticator);
197     }
198
199     /* (non-Javadoc)
200      * @see com.sslexplorer.agent.client.AgentClientGUI#openBrowser(java.lang.String)
201      */

202     public void openBrowser(String JavaDoc path) {
203         try {
204             String JavaDoc browserPath = "https://" + agent.getSSLExplorerHost() + ":" + agent.getSSLExplorerPort() + //$NON-NLS-1$ //$NON-NLS-2$
205
( path == null ? "" : ( "/" + path ));
206             // #ifdef DEBUG
207
log.info("Opening browser to " + browserPath);
208             // #endif
209
BrowserLauncher.openURL(browserPath);
210         } catch (IOException JavaDoc ioe) {
211             // #ifdef DEBUG
212
log.error(ioe);
213             // #endif
214
}
215     }
216
217     public void dispose() {
218         getPortMonitor().dispose();
219         getConsole().dispose();
220     }
221
222     /**
223      * Adapts the AWT progress monitor to {@link TaskProgress}
224      */

225     class TaskProgressBar extends ProgressBar implements TaskProgress {
226
227         public TaskProgressBar(Component JavaDoc parentComponent, String JavaDoc message, String JavaDoc title, long maxValue, boolean allowCancel) {
228             super(parentComponent, message, title, maxValue, allowCancel);
229         }
230
231     }
232
233     class AWTPortMonitor implements PortMonitor {
234
235         AWTPortMonitorWindow portMonitorWindow;
236
237         AWTPortMonitor(Agent agent) {
238             portMonitorWindow = new AWTPortMonitorWindow(agent);
239             portMonitorWindow.addWindowListener(new WindowAdapter JavaDoc() {
240                 public void windowClosing(WindowEvent JavaDoc evt) {
241                     portMonitorWindow.setVisible(false);
242                 }
243             });
244         }
245
246         public void addPortItem(AbstractPortItem portItem) {
247             portMonitorWindow.getModel().addPortItem(portItem);
248         }
249
250         public int getIndexForId(int id) {
251             return portMonitorWindow.getModel().getIndexForId(id);
252         }
253
254         public AbstractPortItem getItemAt(int idx) {
255             return portMonitorWindow.getModel().getItemAt(idx);
256         }
257
258         public boolean isVisible() {
259             return portMonitorWindow.isVisible();
260         }
261
262         public void removeItemAt(int idx) {
263             portMonitorWindow.getModel().removeItemAt(idx);
264         }
265
266         public void setVisible(boolean visible) {
267             boolean wasVisible = isVisible();
268             portMonitorWindow.setVisible(visible);
269             if (visible && !wasVisible) {
270                 portMonitorWindow.toFront();
271             }
272         }
273
274         public void updateItemAt(int idx) {
275             portMonitorWindow.getModel().updateItemAt(idx);
276         }
277
278         public void dispose() {
279             portMonitorWindow.dispose();
280         }
281     }
282
283 }
Popular Tags