1 package org.oddjob.monitor.view; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Dimension ; 5 import java.awt.event.MouseAdapter ; 6 import java.awt.event.MouseEvent ; 7 8 import javax.swing.JPanel ; 9 import javax.swing.JPopupMenu ; 10 import javax.swing.JScrollPane ; 11 import javax.swing.JSplitPane ; 12 import javax.swing.JTree ; 13 import javax.swing.ToolTipManager ; 14 import javax.swing.tree.TreePath ; 15 import javax.swing.tree.TreeSelectionModel ; 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 30 31 public class ExplorerComponent extends JPanel { 32 private static final long serialVersionUID = 0; 33 34 private final JTree tree; 35 36 private final JScrollPane 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 53 public ExplorerComponent(ExplorerModel explorerModel, 54 MonitorMenuBar menuBar, PropertyPolling propertyPolling) { 55 this.menuBar = menuBar; 56 this.propertyPolling = propertyPolling; 57 58 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 (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 ()); 91 treeScroll = new JScrollPane (); 92 treeScroll.setPreferredSize(new Dimension (200, 350)); 93 94 treeScroll.setViewportView(tree); 95 96 JSplitPane split = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, 97 treeScroll, detailView); 98 99 add(split); 100 } 101 102 106 public void destroy() { 107 detailModel.deleteObserver(propertyPolling); 108 } 109 110 114 class PopupListener extends MouseAdapter { 115 public void mousePressed(MouseEvent e) { 116 maybeShowPopup(e); 117 } 118 public void mouseClicked(MouseEvent e) { 119 maybeShowPopup(e); 120 } 121 public void mouseReleased(MouseEvent e) { 122 maybeShowPopup(e); 123 } 124 125 private void maybeShowPopup(MouseEvent e) { 126 if (!e.isPopupTrigger()) { 127 return; 128 } 129 TreePath path = tree.getPathForLocation(e.getX(), e.getY()); 130 tree.setSelectionPath(path); 131 JPopupMenu menu = menuBar.getPopupMenu(); 132 if (menu != null) { 133 menu.show(e.getComponent(), e.getX(), e.getY()); 134 } 135 } 136 } 137 138 } 139 | Popular Tags |