KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > imr > util > ImRManagerGUI


1 package org.jacorb.imr.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

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 JavaDoc;
32 import javax.swing.event.ListSelectionEvent JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import java.awt.event.MouseListener JavaDoc;
35
36 import org.jacorb.imr.*;
37
38 /**
39  * The GUI'ed version of the ImRManager.
40  *
41  * @author Nicolas Noffke
42  * @version $Id: ImRManagerGUI.java,v 1.8 2004/05/06 12:39:59 nicolas Exp $
43  *
44  */

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     /**
88      * The constructor. Instanciates all components and brings up the window.
89      */

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 JavaDoc e)
101         { }
102     
103     GridBagConstraints _constraints = new GridBagConstraints();
104
105     //Build menu bar
106
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     //build manager menu entries
119
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     //build ImR menu entries
132
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     //build server menu entries
155
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     //build popup menu
182
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     /**
277      * Shows the POA Table in right part of SplitPane.
278      */

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     /**
289      * Shows the Server Table in right part of SplitPane.
290      */

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     /**
301      * Convenience method for setting up the GridBagConstraints.
302      *
303      * @param gbc a GridBagConstraint object.
304      * @param gx x value.
305      * @param gy y value.
306      * @param gw width.
307      * @param gh height.
308      * @param wx weight x.
309      * @param wy weight y.
310      */

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     /**
322      * Main method. Brings the GUI up.
323      */

324     public static void main(String JavaDoc[] args) {
325     new ImRManagerGUI();
326     }
327     
328     /**
329      * This class handles all occuring events.
330      */

331     private class EventHandler
332     implements MouseListener JavaDoc, ListSelectionListener JavaDoc, TreeSelectionListener, ActionListener{
333     
334     // implementation of java.awt.event.ActionListener interface
335
/**
336      * This method responds to menu selections.
337      *
338      * @param event
339      */

340     public void actionPerformed(ActionEvent event) {
341         Object JavaDoc _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 JavaDoc _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 JavaDoc _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 JavaDoc _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 JavaDoc _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 JavaDoc _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     // implementation of javax.swing.event.TreeSelectionListener interface
486
/**
487      * Respond to node selections in tree. Shows brings up desired table,
488      * and deletes the previous selection in the tables.
489      *
490      * @param event
491      */

492     public void valueChanged(TreeSelectionEvent event) {
493         DefaultMutableTreeNode _select =(DefaultMutableTreeNode) (event.getPath().getLastPathComponent());
494         Object JavaDoc _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     // implementation of javax.swing.event.ListSelectionListener interface
522
/**
523      * Delete selection in tree, if a table row has been selected.
524      *
525      * @param event
526      */

527     public void valueChanged(ListSelectionEvent JavaDoc event) {
528         if (! ((ListSelectionModel)event.getSource()).isSelectionEmpty())
529         m_tree.clearSelection();
530     }
531     
532
533     // implementation of java.awt.event.MouseListener interface
534
/**
535      * Bring up popup menu.
536      *
537      * @param event
538      */

539     public void mouseClicked(MouseEvent JavaDoc event) {
540          maybeShowContextMenu(event);
541     }
542     
543     /**
544      * NOT IMPLEMENTED
545      */

546     public void mouseEntered(MouseEvent JavaDoc param1) {
547         // NOT IMPLEMENTED
548
}
549     
550     /**
551      * NOT IMPLEMENTED
552      */

553     public void mouseExited(MouseEvent JavaDoc param1) {
554         // NOT IMPLEMENTED
555
}
556     
557     /**
558      * Bring up popup menu.
559      */

560     public void mousePressed(MouseEvent JavaDoc event){
561         maybeShowContextMenu(event);
562     }
563
564     
565     /**
566      * Bring up popup menu.
567      */

568     public void mouseReleased(MouseEvent JavaDoc event) {
569         maybeShowContextMenu(event);
570     }
571     
572     /**
573      * Test whether to bring up popup menu, or not.
574      * This is the case, when a server is selected
575      * (in the tree or in the table).
576      */

577     private void maybeShowContextMenu(MouseEvent JavaDoc 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     }//EventHandler
597

598 } // ImRManagerGUI
599

600
601
Popular Tags