1 package rero.dck.items; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 import javax.swing.*; 7 import javax.swing.event.*; 8 9 import rero.dck.*; 10 import rero.gui.background.*; 11 12 import rero.dialogs.server.*; 13 import rero.dialogs.toolkit.*; 14 15 import rero.config.*; 16 17 import rero.gui.*; 18 import rero.client.*; 19 import rero.client.user.*; 20 21 import java.util.*; 22 23 public class ServerList extends JPanel implements DItem 24 { 25 protected int height; 26 protected int width; 27 28 protected JButton connect, edit; 29 protected JComboBox network; 30 31 protected JList list; 32 33 protected ServerData data; 34 protected StringList autoConnect; 35 protected JComponent component; 36 37 protected DCapabilities capabilities; 38 39 public ServerList(ServerData _data, int _width, int _height, DCapabilities _capabilities) 40 { 41 data = _data; 42 43 autoConnect = ClientState.getClientState().getStringList("auto.connect"); 44 45 capabilities = _capabilities; 46 47 width = _width; 48 height = _height; 49 50 component = this; 51 52 setBorder(BorderFactory.createEmptyBorder(0, width, 0, width)); 53 54 setLayout(new BorderLayout()); 55 56 connect = new JButton("Connect"); 57 connect.setMnemonic('C'); 58 59 edit = new JButton("Edit"); 60 edit.setMnemonic('E'); 61 62 JPanel buttons = new JPanel(); 63 buttons.setLayout(new FlowLayout(FlowLayout.CENTER)); 64 65 buttons.add(connect); 66 buttons.add(edit); 67 68 network = new JComboBox(new NetworkListModel()); 69 network.setPrototypeDisplayValue("Random Servers"); 70 71 buttons.add(network); 72 73 edit.addActionListener(new ActionListener() 74 { 75 public void actionPerformed(ActionEvent ev) 76 { 77 ServerEditorDialog temp = new ServerEditorDialog(component); 78 temp.getListbox().setSelectedIndex(list.getSelectedIndex()); 79 temp.getListbox().ensureIndexIsVisible(list.getSelectedIndex()); 80 temp.showDialog(component); 81 data.update(); 82 ((ServerListModel)(list.getModel())).fireChange(); 83 ((NetworkListModel)(network.getModel())).fireChange(); 84 } 85 }); 86 87 network.addActionListener(new ActionListener() 88 { 89 public void actionPerformed(ActionEvent ev) 90 { 91 data.setActive((ServerGroup)network.getSelectedItem()); 92 ((ServerListModel)(list.getModel())).fireChange(); 93 } 94 }); 95 96 connect.addActionListener(new ActionListener() 97 { 98 public void actionPerformed(ActionEvent ev) 99 { 100 handleConnectAction(); 101 } 102 }); 103 104 add(buttons, BorderLayout.SOUTH); 105 106 ServerListModel lmodel = new ServerListModel(); 107 108 list = new JList(lmodel); 109 list.setCellRenderer(lmodel); 110 111 list.addMouseListener(new MouseAdapter() 112 { 113 public void mouseClicked(MouseEvent ev) 114 { 115 if (ev.getClickCount() >= 2) 116 { 117 SwingUtilities.invokeLater(new Runnable () 118 { 119 public void run() 120 { 121 handleConnectAction(); 122 } 123 }); 124 ev.consume(); 125 } 126 } 127 }); 128 129 add(new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER); 130 131 setPreferredSize(new Dimension(0, height)); 132 } 133 134 public void handleConnectAction() 135 { 136 capabilities.forceSave(); 137 138 if (SessionManager.getGlobalCapabilities().getActiveSession().getCapabilities().isConnected()) 139 { 140 SessionManager.getGlobalCapabilities().createNewServer(); 141 } 142 Server connectToMe = (Server)list.getSelectedValue(); 143 144 SessionManager.getGlobalCapabilities().getActiveSession().executeCommand(connectToMe.getCommand()); 145 146 capabilities.closeDialog(); 147 } 148 149 public void setParent(DParent parent) 150 { 151 152 } 153 154 public void setEnabled(boolean b) 155 { 156 } 157 158 public void save() 159 { 160 if (network.getSelectedItem() != null) 161 { 162 ClientState.getClientState().setInteger("sdialog.selected", ((ServerGroup)network.getSelectedItem()).getNumber()); 163 } 164 data.save(); 165 } 166 167 public void refresh() 168 { 169 try 170 { 171 network.setSelectedIndex(ClientState.getClientState().getInteger("sdialog.selected", 0)); 172 data.update(); 173 ((ServerListModel)list.getModel()).fireChange(); 174 } 175 catch (Exception ex) { ex.printStackTrace(); } 176 } 177 178 public int getEstimatedWidth() 179 { 180 return 0; 181 } 182 183 public void setAlignWidth(int width) 184 { 185 } 186 187 public void setParentVariable(String parent) 188 { 189 190 } 191 192 public JComponent getComponent() 193 { 194 return this; 195 } 196 197 protected class ServerEditorDialog extends JDialog 198 { 199 protected JList alist; 200 201 public String showDialog(Component comp) 202 { 203 setLocationRelativeTo(comp); 204 setVisible(true); 205 return ""; 206 } 207 208 public JList getListbox() { return alist; } 209 210 public ServerEditorDialog(JComponent comp) 211 { 212 super(JOptionPane.getFrameForComponent(comp), "Server Editor", true); 213 214 getContentPane().setLayout(new BorderLayout()); 215 216 ServerListModel lmodel = new ServerListModel(); 217 218 alist = new JList(lmodel); 219 alist.setCellRenderer(lmodel); 220 221 JPanel lpanel = new JPanel(); 222 lpanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 223 lpanel.setLayout(new BorderLayout()); 224 lpanel.add(new JScrollPane(alist, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER); 225 226 getContentPane().add(lpanel, BorderLayout.CENTER); 227 228 JToolBar buttons = new JToolBar(); 229 buttons.setFloatable(false); 230 buttons.setLayout(new FlowLayout()); 231 232 JButton add, edit, remove, sort, ok; 233 234 add = new JButton("Add"); 235 add.setMnemonic('A'); 236 buttons.add(add); 237 238 edit = new JButton("Edit"); 239 edit.setMnemonic('E'); 240 buttons.add(edit); 241 242 remove = new JButton("Remove"); 243 remove.setMnemonic('R'); 244 buttons.add(remove); 245 246 buttons.addSeparator(); 247 248 sort = new JButton("Sort"); 249 sort.setMnemonic('S'); 250 buttons.add(sort); 251 252 buttons.addSeparator(); 253 254 ok = new JButton("Close"); 255 ok.setMnemonic('C'); 256 buttons.add(ok); 257 258 edit.addActionListener(new ActionListener() 259 { 260 public void actionPerformed(ActionEvent ev) 261 { 262 EditServerInfo editor = new EditServerInfo(); 263 editor.setupDialog(alist.getSelectedValue()); 264 265 ADialog dialog = new ADialog(component, "Edit Server", editor, alist.getSelectedValue()); 266 dialog.setSize(new Dimension(330, 280)); 267 if (dialog.showDialog(component) != null) { 269 ((ServerListModel)alist.getModel()).fireChange(); 270 } 271 } 272 }); 273 274 add.addActionListener(new ActionListener() 275 { 276 public void actionPerformed(ActionEvent ev) 277 { 278 EditServerInfo editor = new EditServerInfo(); 279 editor.setupDialog(null); 280 281 ADialog dialog = new ADialog(component, "New Server", editor, null); 282 dialog.setSize(new Dimension(330, 280)); 283 284 Server temp = (Server)dialog.showDialog(component); 285 if (temp != null) 286 { 287 data.addServer(temp); 288 data.update(); 289 ((ServerListModel)alist.getModel()).fireChange(); 290 } 291 } 292 }); 293 294 remove.addActionListener(new ActionListener() 295 { 296 public void actionPerformed(ActionEvent ev) 297 { 298 if (alist.getSelectedValue() != null) 299 { 300 data.removeServer((Server)alist.getSelectedValue()); 301 data.update(); 302 ((ServerListModel)alist.getModel()).fireChange(); 303 } 304 } 305 }); 306 307 sort.addActionListener(new ActionListener() 308 { 309 public void actionPerformed(ActionEvent ev) 310 { 311 data.sort(); 312 data.update(); 313 ((ServerListModel)alist.getModel()).fireChange(); 314 } 315 }); 316 317 ok.addActionListener(new ActionListener() 318 { 319 public void actionPerformed(ActionEvent ev) 320 { 321 setVisible(false); 322 } 323 }); 324 325 getContentPane().add(buttons, BorderLayout.SOUTH); 326 327 setSize(400, 200); 328 } 329 330 } 331 332 protected class ServerListModel extends AbstractListModel implements ListCellRenderer 333 { 334 protected JLabel cell = new JLabel(); 335 336 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 337 { 338 Server svalue = (Server)value; 339 340 if (index < 0 || index > getSize()) 341 { 342 return cell; 343 } 344 345 cell.setToolTipText(svalue.getHost() + ":" + svalue.getPorts()); 346 347 Color fore = UIManager.getColor("TextField.foreground"); 348 349 if (autoConnect.isValue(svalue.getHost())) 350 { 351 fore = Color.blue; 352 } 353 354 if (isSelected) 355 { 356 cell.setOpaque(true); 357 cell.setBackground(UIManager.getColor("TextField.selectionBackground")); 358 359 if (fore == Color.blue) 360 { 361 cell.setForeground(fore); 362 } 363 else 364 { 365 cell.setForeground(UIManager.getColor("TextField.selectionForeground")); 366 } 367 368 cell.setBorder(UIManager.getBorder("List.focusCellHighlightBorder")); 369 370 if (svalue.getNetwork().length() <= 2) 371 { 372 cell.setText("Random: " + svalue.getHost() + ":" + svalue.getPorts()); 373 } 374 else 375 { 376 if (svalue.isSecure()) 377 { 378 cell.setText(svalue.getNetwork() + ": " + svalue.getHost() + ":" + svalue.getPorts() + " (SSL)"); 379 } 380 else 381 { 382 cell.setText(svalue.getNetwork() + ": " + svalue.getHost() + ":" + svalue.getPorts()); 383 } 384 } 385 } 386 else 387 { 388 cell.setOpaque(false); 389 cell.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0)); 390 cell.setForeground(fore); 391 392 cell.setText(svalue.getDescription()); 393 } 394 395 return cell; 396 } 397 398 399 public void fireChange() { fireContentsChanged(this, 0, -1); } 400 401 public Object getElementAt(int index) 402 { 403 return data.getServers().get(index); 404 } 405 406 public int getSize() 407 { 408 return data.getServers().size(); 409 } 410 } 411 412 protected class NetworkListModel extends AbstractListModel implements ComboBoxModel 413 { 414 protected Object selected; 415 416 public Object getSelectedItem() 417 { 418 return selected; 419 } 420 421 public void setSelectedItem(Object item) 422 { 423 selected = item; 424 } 425 426 public void fireChange() { fireContentsChanged(this, 0, -1); } 427 428 public Object getElementAt(int index) 429 { 430 return data.getGroups().get(index); 431 } 432 433 public int getSize() 434 { 435 return data.getGroups().size(); 436 } 437 } 438 439 protected class EditServerInfo extends APanel 440 { 441 protected JTextField description = new JTextField(); 442 protected JTextField host = new JTextField(); 443 protected JTextField portRange = new JTextField(); 444 protected JTextField network = new JTextField(); 445 protected JCheckBox isSSL = new JCheckBox("Server requires SSL"); 446 protected JCheckBox isStartup = new JCheckBox("Connect to server on client startup"); 447 protected JPasswordField password = new JPasswordField(); 448 449 public void setupDialog(Object value) 450 { 451 if (value == null) 452 { 453 setBorder(BorderFactory.createTitledBorder(" Create New Server ")); 454 } 455 else 456 { 457 setBorder(BorderFactory.createTitledBorder(" Edit Server Information ")); 458 } 459 460 JLabel description_l, host_l, portRange_l, network_l, password_l; 461 462 LabelGroup labels = new LabelGroup(); 463 464 description_l = new JLabel(" Description: "); 465 host_l = new JLabel(" Hostname: "); 466 portRange_l = new JLabel(" Port(s): "); 467 network_l = new JLabel(" Network: "); 468 password_l = new JLabel(" Password: "); 469 470 labels.addLabel(description_l); 471 labels.addLabel(host_l); 472 labels.addLabel(portRange_l); 473 labels.addLabel(network_l); 474 labels.addLabel(password_l); 475 476 addComponent(mergeComponents(description_l, description, 20)); 477 addComponent(mergeComponents(host_l, host, 20)); 478 addComponent(mergeComponents(portRange_l, portRange, 100)); 479 addComponent(mergeComponents(network_l, network, 20)); 480 addComponent(isSSL); 481 addComponent(mergeComponents(password_l, password, 20)); 482 addComponent(isStartup); 483 484 labels.sync(); 485 486 if (value != null) 487 { 488 Server temp = (Server)value; 489 description.setText(temp.getDescription()); 490 host.setText(temp.getHost()); 491 portRange.setText(temp.getPorts()); 492 network.setText(temp.getNetwork()); 493 isSSL.setSelected(temp.isSecure()); 494 495 isStartup.setSelected( autoConnect.isValue(temp.getHost()) ); 496 497 password.setText(temp.getPassword()); 498 } 499 } 500 501 public Object getValue(Object value) 502 { 503 Server server; 504 505 if (value != null) 506 { 507 server = (Server)value; 508 } 509 else 510 { 511 server = new Server(); 512 } 513 514 if (isStartup.isSelected() && ! autoConnect.isValue(host.getText())) 515 { 516 System.out.println("Adding: " + host.getText()); 517 518 autoConnect.add(host.getText()); 519 autoConnect.save(); 520 } 521 else if (! isStartup.isSelected() && autoConnect.isValue(host.getText())) 522 { 523 System.out.println("Removing: " + host.getText()); 524 525 autoConnect.remove(host.getText()); 526 autoConnect.save(); 527 } 528 529 server.setValues(description.getText(), host.getText(), portRange.getText(), network.getText(), isSSL.isSelected(), password.getText()); 530 return server; 531 } 532 } 533 } 534 535 | Popular Tags |