1 19 20 package org.apache.excalibur.instrument.client; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Rectangle ; 24 import java.awt.event.MouseAdapter ; 25 import java.awt.event.MouseEvent ; 26 27 import javax.swing.JComponent ; 28 import javax.swing.JPopupMenu ; 29 import javax.swing.JScrollPane ; 30 import javax.swing.JTree ; 31 import javax.swing.ToolTipManager ; 32 import javax.swing.tree.DefaultMutableTreeNode ; 33 import javax.swing.tree.TreeModel ; 34 import javax.swing.tree.TreePath ; 35 import javax.swing.tree.TreeSelectionModel ; 36 37 41 class InstrumentManagerTree 42 extends JComponent 43 { 44 private final InstrumentManagerConnection m_connection; 45 46 private final TreeModel m_treeModel; 47 private final JTree m_tree; 48 49 52 InstrumentManagerTree( InstrumentManagerConnection connection ) 53 { 54 m_connection = connection; 55 56 m_treeModel = m_connection.getTreeModel(); 57 58 59 m_tree = new JTree ( m_treeModel ); 60 m_tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION ); 62 m_tree.setRootVisible( false ); m_tree.setShowsRootHandles( true ); m_tree.setCellRenderer( new InstrumentManagerTreeCellRenderer() ); 65 m_tree.putClientProperty( "JTree.lineStyle", "Angled" ); 66 67 m_tree.addMouseListener( new MouseAdapter () 68 { 69 public void mouseClicked( MouseEvent event ) 70 { 71 if ( event.isPopupTrigger() ) 72 { 73 int row = m_tree.getRowForLocation( event.getX(), event.getY() ); 74 if ( row >= 0 ) 75 { 76 showNodePopup( row, event.getX(), event.getY() ); 77 } 78 } 79 if ( event.getClickCount() == 2 ) 80 { 81 int row = m_tree.getRowForLocation( event.getX(), event.getY() ); 82 if ( row >= 0 ) 83 { 84 nodeSelected( row ); 85 } 86 } 87 } 88 public void mousePressed( MouseEvent event ) 89 { 90 if ( event.isPopupTrigger() ) 91 { 92 int row = m_tree.getRowForLocation( event.getX(), event.getY() ); 93 if ( row >= 0 ) 94 { 95 showNodePopup( row, event.getX(), event.getY() ); 96 } 97 } 98 } 99 public void mouseReleased( MouseEvent event ) 100 { 101 if ( event.isPopupTrigger() ) 102 { 103 int row = m_tree.getRowForLocation( event.getX(), event.getY() ); 104 if ( row >= 0 ) 105 { 106 showNodePopup( row, event.getX(), event.getY() ); 107 } 108 } 109 } 110 }); 111 112 ToolTipManager.sharedInstance().registerComponent( m_tree ); 114 115 JScrollPane scrollPane = new JScrollPane ( m_tree ); 116 117 setLayout( new BorderLayout () ); 118 add( scrollPane, BorderLayout.CENTER ); 119 } 120 121 124 void dispose() 125 { 126 ToolTipManager.sharedInstance().unregisterComponent( m_tree ); 127 m_tree.setModel( null ); 128 } 129 130 private void nodeSelected( int row ) 131 { 132 TreePath treePath = m_tree.getPathForRow( row ); 133 if ( treePath.getLastPathComponent() instanceof DefaultMutableTreeNode ) 134 { 135 DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode )treePath.getLastPathComponent(); 136 if ( ( treeNode.isLeaf() ) && ( treeNode.getUserObject() instanceof NodeData ) ) 137 { 138 NodeData nodeData = (NodeData)treeNode.getUserObject(); 139 nodeData.select(); 140 } 141 } 142 } 143 144 private void showNodePopup( int row, int mouseX, int mouseY ) 145 { 146 TreePath treePath = m_tree.getPathForRow( row ); 147 148 if ( treePath.getLastPathComponent() instanceof DefaultMutableTreeNode ) 149 { 150 DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode )treePath.getLastPathComponent(); 151 if ( treeNode.getUserObject() instanceof NodeData ) 152 { 153 NodeData nodeData = (NodeData)treeNode.getUserObject(); 154 JPopupMenu popup = nodeData.getPopupMenu(); 155 if ( popup != null ) 156 { 157 Rectangle bounds = m_tree.getRowBounds( row ); 159 160 165 166 int x = mouseX; 168 int y = mouseY; 169 170 popup.show( m_tree, x, y ); 171 } 172 } 173 } 174 } 175 } 176 | Popular Tags |