KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > ConnectionMonitor


1 package net.suberic.pooka.gui;
2
3 import java.util.*;
4 import javax.swing.*;
5 import java.awt.event.*;
6 import java.awt.Insets JavaDoc;
7
8 import net.suberic.pooka.*;
9 import net.suberic.util.*;
10 import net.suberic.util.gui.ConfigurablePopupMenu;
11 import net.suberic.util.gui.IconManager;
12
13 /**
14  * This class monitors the status of the network connection(s) that
15  * Pooka uses.
16  *
17  * @author Allen Petersen
18  * @version $Revision: 1499 $
19  */

20 public class ConnectionMonitor extends Box implements NetworkConnectionListener, net.suberic.util.ItemListChangeListener {
21
22   /** the Image for CONNECTED connections. */
23   public ImageIcon connectedImage = null;
24
25   /** the Image for DISCONNECTED connections. */
26   public ImageIcon disconnectedImage = null;
27
28   /** the Image for UNAVAILABLE connections. */
29   public ImageIcon unavailableImage = null;
30   
31   /** The combo box for selecting which connection to show. */
32   JComboBox comboBox;
33
34   /** The panel where the status is shown. */
35   JLabel statusPanel;
36
37   ConfigurablePopupMenu popupMenu;
38
39   /** The default actions supported by this component. */
40   Action[] defaultActions = new Action[] {
41     new ConnectAction(),
42     new DisconnectAction(),
43     new UnavailableAction()
44       };
45
46   /**
47    * Creates a new, empty ConnectionMonitor.
48    */

49   public ConnectionMonitor() {
50     super(BoxLayout.X_AXIS);
51     loadImages();
52     setupComponents();
53   }
54
55   /**
56    * Creates the graphical parts of this component. There are basically
57    * two parts here: a JComboBox for the list of connections, and a
58    * JPanel to show the status of the current connection.
59    */

60   private void setupComponents() {
61     comboBox = new JComboBox();
62     comboBox.addItemListener(new ItemListener() {
63     public void itemStateChanged(ItemEvent e) {
64       if (e.getStateChange() == ItemEvent.SELECTED) {
65         updateStatus();
66       }
67     }
68       });
69
70     statusPanel = new JLabel();
71     statusPanel.addMouseListener(new MouseAdapter() {
72     
73     public void mousePressed(MouseEvent e) {
74       if (e.isPopupTrigger()) {
75         showPopupMenu(e);
76       }
77     }
78
79     public void mouseReleased(MouseEvent e) {
80       if (e.isPopupTrigger()) {
81         showPopupMenu(e);
82       }
83     }
84       });
85
86     statusPanel.setIcon(connectedImage);
87     statusPanel.setBorder(BorderFactory.createEmptyBorder());
88     comboBox.setBorder(BorderFactory.createEmptyBorder());
89     this.setBorder(BorderFactory.createEmptyBorder());
90
91     //statusPanel.getInsets().set(0,0,0,0);
92
//comboBox.getInsets().set(0,0,0,0);
93
//this.getInsets().set(0,0,0,0);
94
this.add(comboBox);
95     this.add(statusPanel);
96   }
97
98   /**
99    * Sets this ConnectionMonitor up to monitor all the connection controlled
100    * by the given NetworkConnectionManager.
101    */

102   public void monitorConnectionManager(NetworkConnectionManager newManager) {
103     java.util.Vector JavaDoc currentList = newManager.getConnectionList();
104     if (currentList != null && currentList.size() > 0) {
105       NetworkConnection[] newConnections = new NetworkConnection[currentList.size()];
106       System.arraycopy(currentList.toArray(), 0, newConnections, 0, currentList.size());
107       addConnections(newConnections);
108     }
109
110     newManager.addItemListChangeListener(this);
111   }
112
113   /**
114    * Loads the images for the ConnectionMonitor.
115    */

116   private void loadImages() {
117     IconManager iconManager = Pooka.getUIFactory().getIconManager();
118     connectedImage = iconManager.getIcon(Pooka.getProperty("ConnectionMonitor.connectedIcon", "ConnectionMonitor.ConnectedIcon"));
119     disconnectedImage = iconManager.getIcon(Pooka.getProperty("ConnectionMonitor.disconnectedIcon", "ConnectionMonitor.DisconnectedIcon"));
120     unavailableImage = iconManager.getIcon(Pooka.getProperty("ConnectionMonitor.unavailableIcon", "ConnectionMonitor.UnavailableIcon"));
121   }
122
123   /**
124    * This creates and shows a PopupMenu for this component.
125    */

126   public void showPopupMenu(MouseEvent e) {
127     if (popupMenu == null) {
128       popupMenu = new ConfigurablePopupMenu();
129       popupMenu.configureComponent("ConnectionMonitor.popupMenu", Pooka.getResources());
130       popupMenu.setActive(getActions());
131     }
132
133     popupMenu.show(this, e.getX(), e.getY());
134     
135   }
136
137   /**
138    * Updates the status for the currently selected Connection.
139    */

140   public void updateStatus() {
141     NetworkConnection selectedConnection = getSelectedConnection();
142     if (selectedConnection != null) {
143       int status = selectedConnection.getStatus();
144       if (status == NetworkConnection.CONNECTED) {
145     statusPanel.setIcon(connectedImage);
146     statusPanel.setToolTipText("Connected");
147       } else if (status == NetworkConnection.DISCONNECTED) {
148     statusPanel.setIcon(disconnectedImage);
149     statusPanel.setToolTipText("Disonnected");
150       } else if (status == NetworkConnection.UNAVAILABLE) {
151     statusPanel.setIcon(unavailableImage);
152     statusPanel.setToolTipText("Unavailable");
153       }
154     } else {
155       statusPanel.setIcon(connectedImage);
156     }
157   }
158
159   /**
160    * Notifies this component that the state of a network connection has
161    * changed.
162    */

163   public void connectionStatusChanged(NetworkConnection connection, int newStatus) {
164     NetworkConnection currentConnection = getSelectedConnection();
165     if (connection == currentConnection) {
166       updateStatus();
167     }
168   }
169
170   /**
171    * Handles added or removed events from the NetworkConnectionManager.
172    */

173   public void itemListChanged(ItemListChangeEvent e) {
174     Item[] added = e.getAdded();
175     if (added != null && added.length > 0) {
176       NetworkConnection[] addedConnections = new NetworkConnection[added.length];
177       System.arraycopy(added, 0, addedConnections, 0, added.length);
178       addConnections(addedConnections);
179     }
180
181     Item[] removed = e.getRemoved();
182     if (removed != null && removed.length > 0) {
183       NetworkConnection[] removedConnections = new NetworkConnection[removed.length];
184       System.arraycopy(removed, 0, removedConnections, 0, removed.length);
185       removeConnections((NetworkConnection[]) removedConnections);
186     }
187   }
188
189   /**
190    * Adds the given NetworkConnection(s) to the JComboBox list.
191    */

192   public void addConnections(NetworkConnection[] newConnections) {
193     if (newConnections != null && newConnections.length > 0) {
194       for (int i = 0 ; i < newConnections.length; i++) {
195     if (newConnections[i] != null) {
196       comboBox.addItem(newConnections[i]);
197       newConnections[i].addConnectionListener(this);
198     }
199       }
200     }
201   }
202
203   /**
204    * Removed the given NetworkConnection(s) from being monitored by
205    * this component.
206    */

207   public void removeConnections(NetworkConnection[] toRemove) {
208     if (toRemove != null && toRemove.length > 0) {
209       for (int i = 0 ; i < toRemove.length; i++) {
210     if (toRemove[i] != null) {
211       comboBox.removeItem(toRemove[i]);
212       toRemove[i].removeConnectionListener(this);
213     }
214       }
215     }
216   }
217
218   /**
219    * Returns the currently selected NetworkConnection.
220    */

221   public NetworkConnection getSelectedConnection() {
222     return (NetworkConnection) comboBox.getSelectedItem();
223   }
224
225   /**
226    * Returns the Actions supported by this Component.
227    */

228   public Action[] getActions() {
229     return defaultActions;
230   }
231
232   public class ConnectAction extends AbstractAction {
233
234     ConnectAction() {
235       super("connection-connect");
236     }
237
238     public void actionPerformed(ActionEvent e) {
239       NetworkConnection connection = getSelectedConnection();
240       if (connection != null && connection.getStatus() != NetworkConnection.CONNECTED) {
241     connection.connect(true, true);
242       }
243     }
244   }
245
246   public class DisconnectAction extends AbstractAction {
247
248     DisconnectAction() {
249       super("connection-disconnect");
250     }
251
252     public void actionPerformed(ActionEvent e) {
253       NetworkConnection connection = getSelectedConnection();
254       if (connection != null && connection.getStatus() == NetworkConnection.CONNECTED) {
255     connection.disconnect();
256       }
257     }
258   }
259
260   public class UnavailableAction extends AbstractAction {
261
262     UnavailableAction() {
263       super("connection-unavailable");
264     }
265
266     public void actionPerformed(ActionEvent e) {
267       NetworkConnection connection = getSelectedConnection();
268       if (connection != null) {
269     connection.makeUnavailable();
270       }
271     }
272   }
273
274
275 }
276
Popular Tags