1 19 20 package com.sslexplorer.agent.client.gui.awt; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.Image ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.WindowAdapter ; 29 import java.awt.event.WindowEvent ; 30 import java.io.IOException ; 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 53 public abstract class AbstractAWTGUI implements AgentClientGUI { 54 55 static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(AbstractAWTGUI.class); 57 59 private Toaster popup; 61 private Image popupImage; 62 private PortMonitor portMonitor; 63 private Console console; 64 private Agent agent; 65 66 69 public AbstractAWTGUI() { 70 OptionDialog.INFORMATION_ICON = "/images/dialog-information.gif"; OptionDialog.WARNING_ICON = "/images/dialog-warning.gif"; OptionDialog.QUESTION_ICON = "/images/dialog-question.gif"; OptionDialog.ERROR_ICON = "/images/dialog-error.gif"; popup = new Toaster(Toaster.BOTTOM_RIGHT, new Dimension (300, 100)); 75 popup.setTextAlign(Component.LEFT_ALIGNMENT); 76 Font norm = new Font ("Arial", Font.PLAIN, 10); Font title = new Font ("Arial Bold", Font.BOLD, 11); popup.setTextFont(norm); 79 popup.setTitleFont(title); 80 popupImage = UIUtil.loadImage(AbstractAWTGUI.class, "/images/agent.gif"); 81 82 } 83 84 87 public void init(Agent agent) { 88 this.agent = agent; 89 } 90 91 96 public Agent getAgent() { 97 return agent; 98 } 99 100 106 public boolean confirm(int dialogType, String okText, String cancelText, String title, String 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) { return false; 112 } 113 return true; 114 } 115 116 123 public boolean error(String okText, String cancelText, String title, String message, Throwable 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) { return false; 129 } 130 return true; 131 132 } 133 134 139 public abstract Component getGUIComponent(); 140 141 147 public void popup(final ActionCallback callback, String message, String title, String imageName, int timeout) { 148 Image image = imageName == null ? null : UIUtil.loadImage(AbstractAWTGUI.class, "/images/" + imageName + ".gif"); 149 ActionListener l = callback == null ? null : new ActionListener () { 150 public void actionPerformed(ActionEvent evt) { 151 callback.actionPerformed(); 152 } 153 }; 154 popup.popup(l, message, title, image == null ? popupImage : image, timeout); 155 } 156 157 163 public TaskProgress createTaskProgress(String message, String title, long maxValue, boolean allowCancel) { 164 TaskProgress progress = new TaskProgressBar(getGUIComponent(), message, title, maxValue, allowCancel); 165 return progress; 166 } 167 168 173 public PortMonitor getPortMonitor() { 174 if (portMonitor == null) { 175 portMonitor = new AWTPortMonitor(agent); 176 } 177 return portMonitor; 178 } 179 180 185 public synchronized Console getConsole() { 186 if (console == null) { 187 console = new AWTConsoleOutputStream("true".equals(System.getProperty("console.toSysOut", "false")) ? System.out : null); } 189 return console; 190 } 191 192 195 public boolean promptForCredentials(boolean proxy, HttpAuthenticator authenticator) { 196 return AWTAuthenticationDialog.promptForCredentials(proxy, authenticator); 197 } 198 199 202 public void openBrowser(String path) { 203 try { 204 String browserPath = "https://" + agent.getSSLExplorerHost() + ":" + agent.getSSLExplorerPort() + ( path == null ? "" : ( "/" + path )); 206 log.info("Opening browser to " + browserPath); 208 BrowserLauncher.openURL(browserPath); 210 } catch (IOException ioe) { 211 log.error(ioe); 213 } 215 } 216 217 public void dispose() { 218 getPortMonitor().dispose(); 219 getConsole().dispose(); 220 } 221 222 225 class TaskProgressBar extends ProgressBar implements TaskProgress { 226 227 public TaskProgressBar(Component parentComponent, String message, String 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 () { 240 public void windowClosing(WindowEvent 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 |