KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > view > ExplorerComponent


1 package org.oddjob.monitor.view;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.event.MouseAdapter JavaDoc;
6 import java.awt.event.MouseEvent JavaDoc;
7
8 import javax.swing.JPanel JavaDoc;
9 import javax.swing.JPopupMenu JavaDoc;
10 import javax.swing.JScrollPane JavaDoc;
11 import javax.swing.JSplitPane JavaDoc;
12 import javax.swing.JTree JavaDoc;
13 import javax.swing.ToolTipManager JavaDoc;
14 import javax.swing.tree.TreePath JavaDoc;
15 import javax.swing.tree.TreeSelectionModel JavaDoc;
16
17 import org.oddjob.monitor.control.DetailController;
18 import org.oddjob.monitor.control.IconControl;
19 import org.oddjob.monitor.control.PropertyPolling;
20 import org.oddjob.monitor.model.DetailModel;
21 import org.oddjob.monitor.model.ExplorerModel;
22 import org.oddjob.monitor.model.JobTreeModel;
23 import org.oddjob.monitor.model.JobTreeNode;
24
25 /**
26  * The tree view.
27  *
28  * @author Rob Gordon
29  */

30
31 public class ExplorerComponent extends JPanel JavaDoc {
32     private static final long serialVersionUID = 0;
33
34     private final JTree JavaDoc tree;
35     
36     private final JScrollPane JavaDoc treeScroll;
37
38     private final IconControl nodeControl;
39
40     private final MonitorMenuBar menuBar;
41
42     private final DetailModel detailModel;
43     
44     private final PropertyPolling propertyPolling;
45     
46     /**
47      * Constructor. Create the main Explorer Component.
48      *
49      * @param explorerModel The Model for this explorer component.
50      * @param menuBar The Main Menu Bar.
51      * @param propertyPolling The property polling.
52      */

53     public ExplorerComponent(ExplorerModel explorerModel,
54             MonitorMenuBar menuBar, PropertyPolling propertyPolling) {
55         this.menuBar = menuBar;
56         this.propertyPolling = propertyPolling;
57         
58         // create the only detail model. At the moment this component
59
// can only show the detail on one node at a time but the
60
// model is written so that this could change.
61
detailModel = new DetailModel();
62         DetailView detailView = new DetailView(detailModel);
63         propertyPolling.setPropertyModel(detailModel.getPropertyModel());
64         detailModel.addObserver(propertyPolling);
65         DetailController detailControl = new DetailController(detailModel, detailView);
66         menuBar.setSession(detailModel);
67
68         JobTreeModel treeModel = new JobTreeModel();
69         JobTreeNode rootTreeNode = new JobTreeNode(explorerModel, treeModel);
70         rootTreeNode.visible();
71         rootTreeNode.build();
72         treeModel.setRootTreeNode(rootTreeNode);
73         tree = new JTree JavaDoc(treeModel);
74
75         nodeControl = new IconControl(tree);
76         tree.setShowsRootHandles(true);
77         treeModel.addTreeModelListener(nodeControl);
78
79         
80         tree.getSelectionModel().setSelectionMode
81             (TreeSelectionModel.SINGLE_TREE_SELECTION);
82
83         tree.addTreeSelectionListener(detailControl);
84         tree.addTreeExpansionListener(nodeControl);
85         tree.addMouseListener(new PopupListener());
86
87         tree.setCellRenderer(new JobTreeCellRenderer());
88         ToolTipManager.sharedInstance().registerComponent(tree);
89         
90         setLayout(new BorderLayout JavaDoc());
91         treeScroll = new JScrollPane JavaDoc();
92         treeScroll.setPreferredSize(new Dimension JavaDoc(200, 350));
93         
94         treeScroll.setViewportView(tree);
95
96         JSplitPane JavaDoc split = new JSplitPane JavaDoc(JSplitPane.HORIZONTAL_SPLIT,
97             treeScroll, detailView);
98
99         add(split);
100     }
101
102     /**
103      * Clear up the component.
104      *
105      */

106     public void destroy() {
107         detailModel.deleteObserver(propertyPolling);
108     }
109     
110     /**
111      * Listen to mouse events to trigger the popup.
112      *
113      */

114     class PopupListener extends MouseAdapter JavaDoc {
115         public void mousePressed(MouseEvent JavaDoc e) {
116             maybeShowPopup(e);
117         }
118         public void mouseClicked(MouseEvent JavaDoc e) {
119             maybeShowPopup(e);
120         }
121         public void mouseReleased(MouseEvent JavaDoc e) {
122             maybeShowPopup(e);
123         }
124         
125         private void maybeShowPopup(MouseEvent JavaDoc e) {
126             if (!e.isPopupTrigger()) {
127                 return;
128             }
129             TreePath JavaDoc path = tree.getPathForLocation(e.getX(), e.getY());
130             tree.setSelectionPath(path);
131             JPopupMenu JavaDoc menu = menuBar.getPopupMenu();
132             if (menu != null) {
133                 menu.show(e.getComponent(), e.getX(), e.getY());
134             }
135         }
136     }
137
138 }
139
Popular Tags