1 package org.jacorb.imr.util; 2 3 23 24 import javax.swing.*; 25 import java.awt.*; 26 import java.awt.event.*; 27 import javax.swing.tree.*; 28 import javax.swing.border.*; 29 import javax.swing.event.*; 30 import javax.swing.table.*; 31 import javax.swing.event.ListSelectionListener ; 32 import javax.swing.event.ListSelectionEvent ; 33 import java.awt.event.MouseEvent ; 34 import java.awt.event.MouseListener ; 35 36 import org.jacorb.imr.*; 37 38 45 46 public class ImRManagerGUI 47 extends JFrame 48 { 49 private JMenuItem m_exit_mi; 50 private JMenuItem m_about_mi; 51 private JMenuItem m_refresh_all_mi; 52 private JMenuItem m_connect_mi; 53 private JMenuItem m_shutdown_mi; 54 private JMenuItem m_forcedown_mi; 55 private JMenuItem m_save_mi; 56 private JMenuItem m_add_mi; 57 private JMenuItem m_remove_mi; 58 private JMenuItem m_hold_mi; 59 private JMenuItem m_release_mi; 60 private JMenuItem m_setdown_mi; 61 private JMenuItem m_refresh_mi; 62 private JMenuItem m_auto_refresh_mi; 63 64 private EventHandler m_handler; 65 private ImRModel m_model; 66 67 private DefaultMutableTreeNode m_top_node; 68 private JTree m_tree; 69 70 private JPanel m_table_panel; 71 private JScrollPane m_server_view; 72 private JScrollPane m_poa_view; 73 private JScrollPane m_tree_view; 74 75 private JTable m_server_table; 76 private JTable m_poa_table; 77 78 private JPopupMenu m_context_menu; 79 private JMenuItem m_add_cmi; 80 private JMenuItem m_remove_cmi; 81 private JMenuItem m_hold_cmi; 82 private JMenuItem m_release_cmi; 83 private JMenuItem m_setdown_cmi; 84 private JMenuItem m_refresh_cmi; 85 86 87 90 public ImRManagerGUI() 91 { 92 super("ImR Manager"); 93 m_handler = new EventHandler(); 94 m_model = new ImRModel(); 95 96 try 97 { 98 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 99 } 100 catch (Exception e) 101 { } 102 103 GridBagConstraints _constraints = new GridBagConstraints(); 104 105 JMenuBar _menu_bar = new JMenuBar(); 107 setJMenuBar(_menu_bar); 108 109 JMenu _manager_menu = new JMenu("Manager"); 110 _menu_bar.add(_manager_menu); 111 112 JMenu _imr_menu = new JMenu("ImR"); 113 _menu_bar.add(_imr_menu); 114 115 JMenu _server_menu = new JMenu("Server"); 116 _menu_bar.add(_server_menu); 117 118 m_about_mi = new JMenuItem("About..."); 120 m_about_mi.addActionListener(m_handler); 121 _manager_menu.add(m_about_mi); 122 123 m_auto_refresh_mi = new JMenuItem("Automatic Refresh..."); 124 m_auto_refresh_mi.addActionListener(m_handler); 125 _manager_menu.add(m_auto_refresh_mi); 126 127 m_exit_mi = new JMenuItem("Exit"); 128 m_exit_mi.addActionListener(m_handler); 129 _manager_menu.add(m_exit_mi); 130 131 m_shutdown_mi = new JMenuItem("Shutdown"); 133 m_shutdown_mi.addActionListener(m_handler); 134 _imr_menu.add(m_shutdown_mi); 135 136 m_forcedown_mi = new JMenuItem("Force Down"); 137 m_forcedown_mi.addActionListener(m_handler); 138 _imr_menu.add(m_forcedown_mi); 139 140 m_save_mi = new JMenuItem("Save Server Table"); 141 m_save_mi.addActionListener(m_handler); 142 _imr_menu.add(m_save_mi); 143 144 _imr_menu.addSeparator(); 145 146 m_connect_mi = new JMenuItem("Connect..."); 147 m_connect_mi.addActionListener(m_handler); 148 _imr_menu.add(m_connect_mi); 149 150 m_refresh_all_mi = new JMenuItem("Refresh all"); 151 m_refresh_all_mi.addActionListener(m_handler); 152 _imr_menu.add(m_refresh_all_mi); 153 154 m_add_mi = new JMenuItem("Add..."); 156 m_add_mi.addActionListener(m_handler); 157 _server_menu.add(m_add_mi); 158 159 m_remove_mi = new JMenuItem("Remove"); 160 m_remove_mi.addActionListener(m_handler); 161 _server_menu.add(m_remove_mi); 162 163 m_hold_mi = new JMenuItem("Hold"); 164 m_hold_mi.addActionListener(m_handler); 165 _server_menu.add(m_hold_mi); 166 167 m_release_mi = new JMenuItem("Release"); 168 m_release_mi.addActionListener(m_handler); 169 _server_menu.add(m_release_mi); 170 171 m_setdown_mi = new JMenuItem("Set Down"); 172 m_setdown_mi.addActionListener(m_handler); 173 _server_menu.add(m_setdown_mi); 174 175 _server_menu.addSeparator(); 176 177 m_refresh_mi = new JMenuItem("Refresh"); 178 m_refresh_mi.addActionListener(m_handler); 179 _server_menu.add(m_refresh_mi); 180 181 m_context_menu = new JPopupMenu("Server manipulation"); 183 m_context_menu.setVisible(true); 184 185 m_add_cmi = new JMenuItem("Add..."); 186 m_add_cmi.addActionListener(m_handler); 187 m_context_menu.add(m_add_cmi); 188 189 m_remove_cmi = new JMenuItem("Remove"); 190 m_remove_cmi.addActionListener(m_handler); 191 m_context_menu.add(m_remove_cmi); 192 193 m_context_menu.addSeparator(); 194 195 m_hold_cmi = new JMenuItem("Hold"); 196 m_hold_cmi.addActionListener(m_handler); 197 m_context_menu.add(m_hold_cmi); 198 199 m_release_cmi = new JMenuItem("Release"); 200 m_release_cmi.addActionListener(m_handler); 201 m_context_menu.add(m_release_cmi); 202 203 m_setdown_cmi = new JMenuItem("Set Down"); 204 m_setdown_cmi.addActionListener(m_handler); 205 m_context_menu.add(m_setdown_cmi); 206 207 m_context_menu.addSeparator(); 208 209 m_refresh_cmi = new JMenuItem("Refresh"); 210 m_refresh_cmi.addActionListener(m_handler); 211 m_context_menu.add(m_refresh_cmi); 212 213 214 JPanel _top_panel = new JPanel(); 215 GridBagLayout _top_gbl = new GridBagLayout(); 216 _top_panel.setLayout(_top_gbl); 217 218 m_tree = m_model.getTree(); 219 m_tree.setEditable(false); 220 m_tree.setCellRenderer(new ImRTreeCellRenderer()); 221 m_tree.addTreeSelectionListener(m_handler); 222 m_tree.addMouseListener(m_handler); 223 224 ToolTipManager.sharedInstance().registerComponent(m_tree); 225 m_tree_view = new JScrollPane(m_tree); 226 m_tree_view.addMouseListener(m_handler); 227 228 m_table_panel = new JPanel(); 229 230 GridBagLayout _table_gbl = new GridBagLayout(); 231 m_table_panel.setLayout(_table_gbl); 232 233 TableModel _server_model = m_model.getServerTableModel(); 234 m_server_table = new JTable(_server_model); 235 m_server_table.addMouseListener(m_handler); 236 237 ListSelectionModel _selection_model = m_server_table.getSelectionModel(); 238 _selection_model.addListSelectionListener(m_handler); 239 240 TableColumn _host_col = m_server_table.getColumnModel().getColumn(1); 241 _host_col.setCellEditor(new ImRTableCellEditor(m_model.getHostSelector())); 242 243 m_server_view = new JScrollPane(m_server_table); 244 m_server_view.setVisible(true); 245 buildConstraints(_constraints, 0, 0, 1, 1, 1, 1); 246 _constraints.fill = GridBagConstraints.BOTH; 247 _table_gbl.setConstraints(m_server_view, _constraints); 248 m_table_panel.add(m_server_view); 249 250 251 TableModel _poa_model = m_model.getPOATableModel(); 252 m_poa_table = new JTable(_poa_model); 253 m_poa_view = new JScrollPane(m_poa_table); 254 m_poa_view.setVisible(false); 255 buildConstraints(_constraints, 0, 0, 1, 1, 1, 1); 256 _constraints.fill = GridBagConstraints.BOTH; 257 _table_gbl.setConstraints(m_poa_view, _constraints); 258 m_table_panel.add(m_poa_view); 259 260 261 JSplitPane _split_pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 262 m_tree_view, m_table_panel); 263 _split_pane.setOneTouchExpandable(true); 264 _split_pane.setDividerLocation(150); 265 266 buildConstraints(_constraints, 0, 0, 1, 1, 1, 1); 267 _constraints.fill = GridBagConstraints.BOTH; 268 _top_gbl.setConstraints(_split_pane, _constraints); 269 _top_panel.add(_split_pane); 270 271 getContentPane().add(_top_panel); 272 pack(); 273 setVisible(true); 274 } 275 276 279 private void showPOATable(){ 280 if (! m_poa_view.isVisible()){ 281 m_server_view.setVisible(false); 282 m_poa_view.setVisible(true); 283 pack(); 284 m_table_panel.repaint(); 285 } 286 } 287 288 291 private void showServerTable(){ 292 if (! m_server_view.isVisible()){ 293 m_poa_view.setVisible(false); 294 m_server_view.setVisible(true); 295 pack(); 296 m_table_panel.repaint(); 297 } 298 } 299 300 311 private void buildConstraints(GridBagConstraints gbc, int gx, int gy, 312 int gw, int gh, int wx, int wy){ 313 gbc.gridx = gx; 314 gbc.gridy = gy; 315 gbc.gridwidth = gw; 316 gbc.gridheight = gh; 317 gbc.weightx = wx; 318 gbc.weighty = wy; 319 } 320 321 324 public static void main(String [] args) { 325 new ImRManagerGUI(); 326 } 327 328 331 private class EventHandler 332 implements MouseListener , ListSelectionListener , TreeSelectionListener, ActionListener{ 333 334 340 public void actionPerformed(ActionEvent event) { 341 Object _source = event.getSource(); 342 343 if (_source instanceof JMenuItem){ 344 if ((JMenuItem) _source == m_exit_mi) 345 System.exit(0); 346 347 else if ((JMenuItem) _source == m_about_mi) 348 JOptionPane.showMessageDialog(new JFrame(), "FUB - Institute for Computer Sciences\n\n" + 349 "The man with the plan: Gerald Brose\n" + 350 "The imp to get along with the plans: Nicolas Noffke\n"); 351 352 353 else if ((JMenuItem) _source == m_auto_refresh_mi) 354 new RefreshWindow(m_model); 355 356 else if ((JMenuItem) _source == m_refresh_all_mi){ 357 m_model.fetchServers(); 358 showServerTable(); 359 } 360 361 else if ((JMenuItem) _source == m_connect_mi) 362 new ConnectWindow(m_model); 363 364 else if ((JMenuItem) _source == m_shutdown_mi) 365 m_model.imrShutdown(true); 366 367 else if ((JMenuItem) _source == m_forcedown_mi) 368 m_model.imrShutdown(false); 369 370 else if ((JMenuItem) _source == m_save_mi) 371 m_model.saveTable(); 372 373 else if ((JMenuItem) _source == m_add_mi || 374 (JMenuItem) _source == m_add_cmi) 375 new AddServerWindow(m_model); 376 377 else if ((JMenuItem) _source == m_remove_mi || 378 (JMenuItem) _source == m_remove_cmi){ 379 int _select = m_server_table.getSelectedRow(); 380 if (_select > -1) 381 m_model.removeServer(_select); 382 else{ 383 TreePath _selected_path = m_tree.getSelectionPath(); 384 if (_selected_path != null){ 385 DefaultMutableTreeNode _node = (DefaultMutableTreeNode) 386 _selected_path.getLastPathComponent(); 387 Object _imr_node = _node.getUserObject(); 388 389 if (_imr_node instanceof ServerInfo) 390 m_model.removeServer(((ServerInfo) _imr_node).name); 391 else 392 System.out.println("Please select a server node!"); 393 } 394 else 395 System.out.println("Please select a server to remove!"); 396 } 397 } 398 else if ((JMenuItem) _source == m_hold_mi || 399 (JMenuItem) _source == m_hold_cmi){ 400 int _select = m_server_table.getSelectedRow(); 401 if (_select > -1) 402 m_model.holdServer(_select); 403 else{ 404 TreePath _selected_path = m_tree.getSelectionPath(); 405 if (_selected_path != null){ 406 DefaultMutableTreeNode _node = (DefaultMutableTreeNode) 407 _selected_path.getLastPathComponent(); 408 Object _imr_node = _node.getUserObject(); 409 410 if (_imr_node instanceof ServerInfo) 411 m_model.holdServer(((ServerInfo) _imr_node).name); 412 else 413 System.out.println("Please select a server node!"); 414 } 415 else 416 System.out.println("Please select a server to hold!"); 417 } 418 } 419 else if ((JMenuItem) _source == m_refresh_mi || 420 (JMenuItem) _source == m_refresh_cmi){ 421 int _select = m_server_table.getSelectedRow(); 422 if (_select > -1) 423 m_model.refreshServer(_select); 424 else{ 425 TreePath _selected_path = m_tree.getSelectionPath(); 426 if (_selected_path != null){ 427 DefaultMutableTreeNode _node = (DefaultMutableTreeNode) 428 _selected_path.getLastPathComponent(); 429 Object _imr_node = _node.getUserObject(); 430 431 if (_imr_node instanceof ServerInfo) 432 m_model.refreshServer(((ServerInfo) _imr_node).name); 433 else 434 System.out.println("Please select a server node!"); 435 } 436 else 437 System.out.println("Please select a server to refresh!"); 438 } 439 } 440 else if ((JMenuItem) _source == m_release_mi || 441 (JMenuItem) _source == m_release_cmi){ 442 int _select = m_server_table.getSelectedRow(); 443 if (_select > -1) 444 m_model.releaseServer(_select); 445 else{ 446 TreePath _selected_path = m_tree.getSelectionPath(); 447 if (_selected_path != null){ 448 DefaultMutableTreeNode _node = (DefaultMutableTreeNode) 449 _selected_path.getLastPathComponent(); 450 Object _imr_node = _node.getUserObject(); 451 452 if (_imr_node instanceof ServerInfo) 453 m_model.releaseServer(((ServerInfo) _imr_node).name); 454 else 455 System.out.println("Please select a server node!"); 456 } 457 else 458 System.out.println("Please select a server to release!"); 459 } 460 } 461 else if ((JMenuItem) _source == m_setdown_mi || 462 (JMenuItem) _source == m_setdown_cmi){ 463 int _select = m_server_table.getSelectedRow(); 464 if (_select > -1) 465 m_model.setServerDown(_select); 466 else{ 467 TreePath _selected_path = m_tree.getSelectionPath(); 468 if (_selected_path != null){ 469 DefaultMutableTreeNode _node = (DefaultMutableTreeNode) 470 _selected_path.getLastPathComponent(); 471 Object _imr_node = _node.getUserObject(); 472 473 if (_imr_node instanceof ServerInfo) 474 m_model.setServerDown(((ServerInfo) _imr_node).name); 475 else 476 System.out.println("Please select a server node!"); 477 } 478 else 479 System.out.println("Please select a server to set down!"); 480 } 481 } 482 } 483 } 484 485 492 public void valueChanged(TreeSelectionEvent event) { 493 DefaultMutableTreeNode _select =(DefaultMutableTreeNode) (event.getPath().getLastPathComponent()); 494 Object _node_obj = _select.getUserObject(); 495 496 if (_node_obj instanceof ServerInfo){ 497 m_poa_table.clearSelection(); 498 m_server_table.clearSelection(); 499 showServerTable(); 500 } 501 else if (_node_obj instanceof ImRInfo){ 502 m_server_table.clearSelection(); 503 showServerTable(); 504 } 505 else if (_node_obj instanceof POAInfo){ 506 m_server_table.clearSelection(); 507 ServerInfo _parent = (ServerInfo) ((DefaultMutableTreeNode) 508 _select.getParent()).getUserObject(); 509 m_model.poaTableForServer(_parent); 510 511 if (m_poa_table.getRowCount() > 1){ 512 int _row = m_model.getRow(_parent, (POAInfo) _node_obj); 513 m_poa_table.clearSelection(); 514 m_poa_table.setRowSelectionInterval(_row, _row); 515 } 516 517 showPOATable(); 518 } 519 } 520 521 527 public void valueChanged(ListSelectionEvent event) { 528 if (! ((ListSelectionModel)event.getSource()).isSelectionEmpty()) 529 m_tree.clearSelection(); 530 } 531 532 533 539 public void mouseClicked(MouseEvent event) { 540 maybeShowContextMenu(event); 541 } 542 543 546 public void mouseEntered(MouseEvent param1) { 547 } 549 550 553 public void mouseExited(MouseEvent param1) { 554 } 556 557 560 public void mousePressed(MouseEvent event){ 561 maybeShowContextMenu(event); 562 } 563 564 565 568 public void mouseReleased(MouseEvent event) { 569 maybeShowContextMenu(event); 570 } 571 572 577 private void maybeShowContextMenu(MouseEvent event){ 578 if (event.isPopupTrigger()){ 579 Component _source = event.getComponent(); 580 if (_source == m_server_table){ 581 if (m_server_table.getSelectedRow() > -1) 582 m_context_menu.show(_source, event.getX(), event.getY()); 583 } 584 else if (_source == m_tree){ 585 TreePath _selected_path = m_tree.getSelectionPath(); 586 if (_selected_path != null){ 587 DefaultMutableTreeNode _node = (DefaultMutableTreeNode) 588 _selected_path.getLastPathComponent(); 589 590 if (_node.getUserObject() instanceof ServerInfo) 591 m_context_menu.show(_source, event.getX(), event.getY()); 592 } 593 } 594 } 595 } 596 } 598 } 600 601 | Popular Tags |