KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
23 import java.awt.Button JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.FlowLayout JavaDoc;
26 import java.awt.Frame JavaDoc;
27 import java.awt.Panel JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Vector JavaDoc;
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 /**
43  * Displays a frame that monitors all active ports (local and
44  * remote) and any tunnels that may be running over them.
45  *
46  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
47  */

48 public class AWTPortMonitorWindow extends Frame JavaDoc {
49     
50     // Private instance variables
51

52     private Grid portGrid;
53     private PortModel model;
54     private Button JavaDoc stopButton;
55     private Button JavaDoc closeButton;
56     private Thread JavaDoc updateThread;
57
58     /**
59      * Constructor.
60      *
61      * @param vpnClient
62      */

63     public AWTPortMonitorWindow(Agent vpnClient) {
64         super(Messages.getString("PortMonitor.title")); //$NON-NLS-1$
65
setIconImage(UIUtil.loadImage(getClass(), "/images/frame-agent.gif")); //$NON-NLS-1$
66

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 JavaDoc() {
75             public void actionPerformed(ActionEvent JavaDoc evt) {
76                 setAvailableActions();
77             }
78         });
79         
80         closeButton = new Button JavaDoc(Messages.getString("PortMonitor.close")); //$NON-NLS-1$
81
closeButton.addActionListener(new ActionListener JavaDoc() {
82             public void actionPerformed(ActionEvent JavaDoc evt) {
83                 setVisible(false);
84             }
85         });
86         
87         stopButton = new Button JavaDoc(Messages.getString("PortMonitor.stop")); //$NON-NLS-1$
88
stopButton.addActionListener(new ActionListener JavaDoc() {
89             Option stop = new Option(Messages.getString("PortMonitor.stop")); //$NON-NLS-1$
90
public void actionPerformed(ActionEvent JavaDoc evt) {
91                Option opt = OptionDialog.prompt(AWTPortMonitorWindow.this, OptionDialog.WARNING, Messages.getString("PortMonitor.close.title"), //$NON-NLS-1$
92
Messages.getString("PortMonitor.close.text"), //$NON-NLS-1$
93
new Option[] { stop, OptionDialog.CHOICE_CANCEL } );
94                if(opt == stop) {
95                    for(Enumeration JavaDoc e = getSelectedPorts().elements(); e.hasMoreElements(); ) {
96                        AbstractPortItem t = (AbstractPortItem)e.nextElement();
97                        t.stop();
98                    }
99                }
100            }
101         });
102         
103         Panel JavaDoc p = new Panel JavaDoc(new BorderLayout JavaDoc());
104         p.add(portGrid, BorderLayout.CENTER);
105         
106         Panel JavaDoc f = new Panel JavaDoc(new FlowLayout JavaDoc(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 JavaDoc());
113         add(p, BorderLayout.CENTER);
114         add(f, BorderLayout.SOUTH);
115         pack();
116         setAvailableActions();
117     }
118     
119     /* (non-Javadoc)
120      * @see java.awt.Component#setVisible(boolean)
121      */

122     public void setVisible(boolean visible) {
123         super.setVisible(visible);
124         if(visible && updateThread == null) {
125             updateThread = new Thread JavaDoc() {
126                 public void run() {
127                     while(updateThread != null) {
128                         try {
129                             Thread.sleep(1000);
130                         }
131                         catch(Exception JavaDoc e) {
132                         }
133                         try {
134                             Method JavaDoc invokeAndWaitMethod = Class.forName("java.awt.EventQueue").getMethod("invokeAndWait", new Class JavaDoc[] { Runnable JavaDoc.class }); //$NON-NLS-1$ //$NON-NLS-2$
135
Runnable JavaDoc r = new Runnable JavaDoc() {
136                                 public void run() {
137                                     model.refresh();
138                                 }
139                             };
140                             invokeAndWaitMethod.invoke(null, new Object JavaDoc[] { r });
141                         }
142                         catch(Exception JavaDoc 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     /**
156      * Get a list of {@link AbstractPortItem} objects that are
157      * currently selected.
158      *
159      * @return selected ports
160      */

161     public Vector JavaDoc getSelectedPorts() {
162         int[] r = portGrid.getSelectedRows();
163         Vector JavaDoc v = new Vector JavaDoc();
164         for(int i = 0 ; i < r.length; i++) {
165             v.addElement(model.getItemAt(r[i]));
166         }
167         return v;
168     }
169     
170     /**
171      * Get the port model.
172      *
173      * @return model
174      */

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