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 ; 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 20 public class ConnectionMonitor extends Box implements NetworkConnectionListener, net.suberic.util.ItemListChangeListener { 21 22 23 public ImageIcon connectedImage = null; 24 25 26 public ImageIcon disconnectedImage = null; 27 28 29 public ImageIcon unavailableImage = null; 30 31 32 JComboBox comboBox; 33 34 35 JLabel statusPanel; 36 37 ConfigurablePopupMenu popupMenu; 38 39 40 Action[] defaultActions = new Action[] { 41 new ConnectAction(), 42 new DisconnectAction(), 43 new UnavailableAction() 44 }; 45 46 49 public ConnectionMonitor() { 50 super(BoxLayout.X_AXIS); 51 loadImages(); 52 setupComponents(); 53 } 54 55 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 this.add(comboBox); 95 this.add(statusPanel); 96 } 97 98 102 public void monitorConnectionManager(NetworkConnectionManager newManager) { 103 java.util.Vector 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 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 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 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 163 public void connectionStatusChanged(NetworkConnection connection, int newStatus) { 164 NetworkConnection currentConnection = getSelectedConnection(); 165 if (connection == currentConnection) { 166 updateStatus(); 167 } 168 } 169 170 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 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 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 221 public NetworkConnection getSelectedConnection() { 222 return (NetworkConnection) comboBox.getSelectedItem(); 223 } 224 225 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 |