1 19 20 package com.sslexplorer.agent.client.gui.swt; 21 22 import java.text.DateFormat ; 23 import java.text.SimpleDateFormat ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Comparator ; 27 import java.util.Date ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Vector ; 32 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.events.SelectionAdapter; 35 import org.eclipse.swt.events.SelectionEvent; 36 import org.eclipse.swt.events.ShellAdapter; 37 import org.eclipse.swt.events.ShellEvent; 38 import org.eclipse.swt.layout.GridData; 39 import org.eclipse.swt.layout.GridLayout; 40 import org.eclipse.swt.widgets.Button; 41 import org.eclipse.swt.widgets.Event; 42 import org.eclipse.swt.widgets.Listener; 43 import org.eclipse.swt.widgets.Shell; 44 import org.eclipse.swt.widgets.Table; 45 import org.eclipse.swt.widgets.TableColumn; 46 import org.eclipse.swt.widgets.TableItem; 47 48 import com.sslexplorer.agent.client.AgentClientGUI; 49 import com.sslexplorer.agent.client.PortMonitor; 50 import com.sslexplorer.agent.client.tunneling.AbstractPortItem; 51 52 public class SWTPortMonitor implements PortMonitor { 53 54 private Shell shell; 55 private Table table; 56 private boolean open; 57 private int lastSortColumn = -1; 58 private List items; 59 private DateFormat dateFormat; 60 private Thread updateThread; 61 private Button stopButton; 62 63 private static final String [] columnNames = new String [] { Messages.getString("PortModel.type"), Messages.getString("PortModel.name"), Messages.getString("PortModel.localPort"), Messages.getString("PortModel.active"), Messages.getString("PortModel.lastData"), Messages.getString("PortModel.total") }; 65 private static final int[] columnWidths = new int[] { 70, 150, 70, 70, 100, 70 }; 66 67 public SWTPortMonitor(final SWTSystemTrayGUI gui) { 68 items = new ArrayList (); 69 dateFormat = SimpleDateFormat.getTimeInstance(); 70 shell = new Shell(gui.getDisplay(), SWT.RESIZE | SWT.TITLE); 71 GridLayout gridLayout = new GridLayout(); 72 gridLayout.numColumns = 3; 73 shell.setLayout(gridLayout); 74 shell.addShellListener(new ShellAdapter() { 75 public void shellClosed(ShellEvent e) { 76 } 77 }); 78 shell.setText(Messages.getString("PortMonitor.title")); shell.setImage(gui.loadImage(SWTSystemTrayGUI.class, "/images/frame-agent.png")); GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 81 shell.setLayoutData(data); 82 83 table = new Table(shell, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION | SWT.RESIZE); 84 table.setHeaderVisible(true); 85 table.setLinesVisible(true); 86 table.addSelectionListener(new SelectionAdapter() { 89 public void widgetDefaultSelected(SelectionEvent e) { 90 checkAvailable(); 91 TableItem[] items = table.getSelection(); 92 if (items.length > 0) { 93 } 95 } 96 97 public void widgetSelected(SelectionEvent e) { 98 checkAvailable(); 99 } 100 }); 101 for (int i = 0; i < columnNames.length; i++) { 102 TableColumn column = new TableColumn(table, SWT.NONE); 103 column.setText(columnNames[i]); 104 column.setWidth(columnWidths[i]); 105 final int columnIndex = i; 106 column.addSelectionListener(new SelectionAdapter() { 107 public void widgetSelected(SelectionEvent e) { 108 sort(columnIndex); 109 } 110 }); 111 } 112 113 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL 114 | GridData.VERTICAL_ALIGN_FILL 115 | GridData.GRAB_VERTICAL); 116 data.horizontalSpan = 3; 117 data.verticalSpan = 3; 118 data.heightHint = 300; 119 data.widthHint = 480; 120 table.setLayoutData(data); 121 122 stopButton = new Button(shell, SWT.PUSH); 123 stopButton.setText(Messages.getString("PortMonitor.stop")); 124 data = new GridData(); 125 data.horizontalAlignment = GridData.END; 126 data.horizontalSpan = 2; 127 data.grabExcessHorizontalSpace = true; 128 stopButton.setLayoutData(data); 129 130 final Button closeButton = new Button(shell, SWT.PUSH); 131 closeButton.setText(Messages.getString("PortMonitor.close")); 132 133 Listener listener = new Listener() { 134 public void handleEvent(Event event) { 135 if (event.widget == stopButton) { 136 if (gui.confirm(AgentClientGUI.WARNING, 137 "Yes", 138 "No", 139 Messages.getString("PortMonitor.close.title"), 140 Messages.getString("PortMonitor.close.text"))) { 141 for (Enumeration en = getSelectedPorts().elements(); en.hasMoreElements();) { 142 AbstractPortItem t = (AbstractPortItem) en.nextElement(); 143 t.stop(); 144 } 145 } 146 } else { 147 setVisible(false); 148 } 149 } 150 }; 151 stopButton.addListener(SWT.Selection, listener); 152 closeButton.addListener(SWT.Selection, listener); 153 154 shell.pack(); 155 156 checkAvailable(); 157 } 158 159 165 public Vector getSelectedPorts() { 166 int[] r = table.getSelectionIndices(); 167 Vector v = new Vector (); 168 for (int i = 0; i < r.length; i++) { 169 v.addElement(items.get(r[i])); 170 } 171 return v; 172 } 173 174 public void addPortItem(final AbstractPortItem portItem) { 175 synchronized(items) { 176 items.add(portItem); 177 shell.getDisplay().syncExec(new Runnable () { 178 public void run() { 179 new TableItem(table, SWT.NONE); 180 update(items.size() - 1, portItem); 181 checkAvailable(); 182 } 183 }); 184 } 185 } 186 187 public int getIndexForId(int id) { 188 synchronized(items) { 189 int idx = 0; 190 for (Iterator i = items.iterator(); i.hasNext();) { 191 AbstractPortItem api = (AbstractPortItem) i.next(); 192 if (api.getConfiguration().getId() == id) { 193 return idx; 194 } 195 idx++; 196 } 197 return -1; 198 } 199 } 200 201 public AbstractPortItem getItemAt(int idx) { 202 return (AbstractPortItem) items.get(idx); 203 } 204 205 public void removeItemAt(final int idx) { 206 items.remove(idx); 207 shell.getDisplay().syncExec(new Runnable () { 208 public void run() { 209 table.remove(idx, idx); 210 checkAvailable(); 211 } 212 }); 213 } 214 215 public void updateItemAt(final int idx) { 216 shell.getDisplay().syncExec(new Runnable () { 217 public void run() { 218 update(idx, getItemAt(idx)); 219 checkAvailable(); 220 } 221 }); 222 } 223 224 void update(int idx, AbstractPortItem portItem) { 225 String [] labels = new String [] { portItem.getType(), 226 portItem.getName(), 227 String.valueOf(portItem.getLocalPort()), 228 String.valueOf(portItem.getActiveTunnelCount()), 229 dateFormat.format(new Date (portItem.getDataLastTransferred())), 230 String.valueOf(portItem.getTotalTunnelCount()) }; 231 table.getItem(idx).setText(labels); 232 table.redraw(); 233 } 234 235 public boolean isVisible() { 236 return open && shell.isVisible(); 237 } 238 239 public void setVisible(final boolean visible) { 240 shell.getDisplay().syncExec(new Runnable () { 241 public void run() { 242 if (!open) { 243 shell.open(); 244 open = true; 245 } else { 246 shell.setVisible(visible); 247 } 248 } 249 }); 250 if(visible && updateThread == null) { 251 updateThread = new Thread () { 252 public void run() { 253 while(updateThread != null) { 254 try { 255 Thread.sleep(1000); 256 } 257 catch(Exception e) { 258 } 259 shell.getDisplay().asyncExec(new Runnable () { 260 public void run() { 261 synchronized(items) { 262 for(int i = 0 ; i < items.size(); i++) { 263 update(i, ((AbstractPortItem)items.get(i))); 264 } 265 } 266 } 267 }); 268 } 269 } 270 }; 271 updateThread.start(); 272 } 273 else if(!visible && updateThread != null){ 274 updateThread = null; 275 } 276 } 277 278 private void checkAvailable() { 279 stopButton.setEnabled(table.getSelectionCount() > 0); 280 } 281 282 private void sort(int column) { 283 if (table.getItemCount() <= 1) 284 return; 285 286 TableItem[] items = table.getItems(); 287 String [][] data = new String [items.length][table.getColumnCount()]; 288 for (int i = 0; i < items.length; i++) { 289 for (int j = 0; j < table.getColumnCount(); j++) { 290 data[i][j] = items[i].getText(j); 291 } 292 } 293 294 Arrays.sort(data, new RowComparator(column)); 295 296 if (lastSortColumn != column) { 297 table.setSortColumn(table.getColumn(column)); 298 table.setSortDirection(SWT.DOWN); 299 for (int i = 0; i < data.length; i++) { 300 items[i].setText(data[i]); 301 } 302 lastSortColumn = column; 303 } else { 304 table.setSortDirection(SWT.UP); 306 int j = data.length - 1; 307 for (int i = 0; i < data.length; i++) { 308 items[i].setText(data[j--]); 309 } 310 lastSortColumn = -1; 311 } 312 313 } 314 315 318 private class RowComparator implements Comparator { 319 private int column; 320 321 326 public RowComparator(int col) { 327 column = col; 328 } 329 330 338 public int compare(Object obj1, Object obj2) { 339 String [] row1 = (String []) obj1; 340 String [] row2 = (String []) obj2; 341 342 return row1[column].compareTo(row2[column]); 343 } 344 } 345 346 public void dispose() { 347 if (!shell.isDisposed()) { 348 shell.getDisplay().syncExec(new Runnable () { 349 public void run() { 350 shell.dispose(); 351 } 352 }); 353 } 354 } 355 356 } | Popular Tags |