1 19 20 package com.sslexplorer.agent.client.gui.awt; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Button ; 24 import java.awt.Color ; 25 import java.awt.FlowLayout ; 26 import java.awt.Frame ; 27 import java.awt.Panel ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.lang.reflect.Method ; 31 import java.util.Enumeration ; 32 import java.util.Vector ; 33 34 import com.sshtools.ui.awt.UIUtil; 35 import com.sshtools.ui.awt.grid.Grid; 36 import com.sshtools.ui.awt.options.Option; 37 import com.sshtools.ui.awt.options.OptionDialog; 38 import com.sslexplorer.agent.client.Agent; 39 import com.sslexplorer.agent.client.tunneling.AbstractPortItem; 40 41 42 48 public class AWTPortMonitorWindow extends Frame { 49 50 52 private Grid portGrid; 53 private PortModel model; 54 private Button stopButton; 55 private Button closeButton; 56 private Thread updateThread; 57 58 63 public AWTPortMonitorWindow(Agent vpnClient) { 64 super(Messages.getString("PortMonitor.title")); setIconImage(UIUtil.loadImage(getClass(), "/images/frame-agent.gif")); 67 model = new PortModel(); 68 portGrid = new Grid(model); 69 portGrid.setBackground(Color.white); 70 portGrid.setForeground(Color.black); 71 portGrid.setSelectionBackground(Color.blue.darker().darker()); 72 portGrid.setSelectionForeground(Color.white); 73 portGrid.setColumnWidths(new int[] { 46, 220, 64, 64, 128, 64 } ); 74 portGrid.addActionListener(new ActionListener () { 75 public void actionPerformed(ActionEvent evt) { 76 setAvailableActions(); 77 } 78 }); 79 80 closeButton = new Button (Messages.getString("PortMonitor.close")); closeButton.addActionListener(new ActionListener () { 82 public void actionPerformed(ActionEvent evt) { 83 setVisible(false); 84 } 85 }); 86 87 stopButton = new Button (Messages.getString("PortMonitor.stop")); stopButton.addActionListener(new ActionListener () { 89 Option stop = new Option(Messages.getString("PortMonitor.stop")); public void actionPerformed(ActionEvent evt) { 91 Option opt = OptionDialog.prompt(AWTPortMonitorWindow.this, OptionDialog.WARNING, Messages.getString("PortMonitor.close.title"), Messages.getString("PortMonitor.close.text"), new Option[] { stop, OptionDialog.CHOICE_CANCEL } ); 94 if(opt == stop) { 95 for(Enumeration e = getSelectedPorts().elements(); e.hasMoreElements(); ) { 96 AbstractPortItem t = (AbstractPortItem)e.nextElement(); 97 t.stop(); 98 } 99 } 100 } 101 }); 102 103 Panel p = new Panel (new BorderLayout ()); 104 p.add(portGrid, BorderLayout.CENTER); 105 106 Panel f = new Panel (new FlowLayout (FlowLayout.RIGHT)); 107 f.setBackground(Color.gray); 108 f.setForeground(Color.black); 109 f.add(stopButton); 110 f.add(closeButton); 111 112 setLayout(new BorderLayout ()); 113 add(p, BorderLayout.CENTER); 114 add(f, BorderLayout.SOUTH); 115 pack(); 116 setAvailableActions(); 117 } 118 119 122 public void setVisible(boolean visible) { 123 super.setVisible(visible); 124 if(visible && updateThread == null) { 125 updateThread = new Thread () { 126 public void run() { 127 while(updateThread != null) { 128 try { 129 Thread.sleep(1000); 130 } 131 catch(Exception e) { 132 } 133 try { 134 Method invokeAndWaitMethod = Class.forName("java.awt.EventQueue").getMethod("invokeAndWait", new Class [] { Runnable .class }); Runnable r = new Runnable () { 136 public void run() { 137 model.refresh(); 138 } 139 }; 140 invokeAndWaitMethod.invoke(null, new Object [] { r }); 141 } 142 catch(Exception e) { 143 model.refresh(); 144 } 145 } 146 } 147 }; 148 updateThread.start(); 149 } 150 else if(!visible && updateThread != null){ 151 updateThread = null; 152 } 153 } 154 155 161 public Vector getSelectedPorts() { 162 int[] r = portGrid.getSelectedRows(); 163 Vector v = new Vector (); 164 for(int i = 0 ; i < r.length; i++) { 165 v.addElement(model.getItemAt(r[i])); 166 } 167 return v; 168 } 169 170 175 public PortModel getModel() { 176 return model; 177 } 178 179 protected void setAvailableActions() { 180 stopButton.setEnabled(portGrid.getSelectedRowCount() != 0); 181 } 182 183 } 184 | Popular Tags |