KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > gui > swt > SWTPortMonitor


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.swt;
21
22 import java.text.DateFormat JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Vector JavaDoc;
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 JavaDoc items;
59     private DateFormat JavaDoc dateFormat;
60     private Thread JavaDoc updateThread;
61     private Button stopButton;
62
63     private static final String JavaDoc[] columnNames = new String JavaDoc[] { Messages.getString("PortModel.type"), Messages.getString("PortModel.name"), Messages.getString("PortModel.localPort"), Messages.getString("PortModel.active"), Messages.getString("PortModel.lastData"), Messages.getString("PortModel.total") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
64
};
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 JavaDoc();
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")); //$NON-NLS-1$
79
shell.setImage(gui.loadImage(SWTSystemTrayGUI.class, "/images/frame-agent.png")); //$NON-NLS-1$
80
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         // for right click
87
// table.setMenu(createPopUpMenu());
88
table.addSelectionListener(new SelectionAdapter() {
89             public void widgetDefaultSelected(SelectionEvent e) {
90                 checkAvailable();
91                 TableItem[] items = table.getSelection();
92                 if (items.length > 0) {
93                     // double click
94
}
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 JavaDoc 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     /**
160      * Get a list of {@link AbstractPortItem} objects that are currently
161      * selected.
162      *
163      * @return selected ports
164      */

165     public Vector JavaDoc getSelectedPorts() {
166         int[] r = table.getSelectionIndices();
167         Vector JavaDoc v = new Vector JavaDoc();
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 JavaDoc() {
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 JavaDoc 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 JavaDoc() {
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 JavaDoc() {
217             public void run() {
218                 update(idx, getItemAt(idx));
219                 checkAvailable();
220             }
221         });
222     }
223
224     void update(int idx, AbstractPortItem portItem) {
225         String JavaDoc[] labels = new String JavaDoc[] { portItem.getType(),
226             portItem.getName(),
227             String.valueOf(portItem.getLocalPort()),
228             String.valueOf(portItem.getActiveTunnelCount()),
229             dateFormat.format(new Date JavaDoc(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 JavaDoc() {
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 JavaDoc() {
252                 public void run() {
253                     while(updateThread != null) {
254                         try {
255                             Thread.sleep(1000);
256                         }
257                         catch(Exception JavaDoc e) {
258                         }
259                         shell.getDisplay().asyncExec(new Runnable JavaDoc() {
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 JavaDoc[][] data = new String JavaDoc[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             // reverse order if the current column is selected again
305
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     /**
316      * To compare entries (rows) by the given column
317      */

318     private class RowComparator implements Comparator JavaDoc {
319         private int column;
320
321         /**
322          * Constructs a RowComparator given the column index
323          *
324          * @param col The index (starting at zero) of the column
325          */

326         public RowComparator(int col) {
327             column = col;
328         }
329
330         /**
331          * Compares two rows (type String[]) using the specified column entry.
332          *
333          * @param obj1 First row to compare
334          * @param obj2 Second row to compare
335          * @return negative if obj1 less than obj2, positive if obj1 greater
336          * than obj2, and zero if equal.
337          */

338         public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
339             String JavaDoc[] row1 = (String JavaDoc[]) obj1;
340             String JavaDoc[] row2 = (String JavaDoc[]) 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 JavaDoc() {
349                 public void run() {
350                     shell.dispose();
351                 }
352             });
353         }
354     }
355
356 }
Popular Tags