KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > client > InstrumentManagerTree


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.client;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.awt.event.MouseAdapter JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.JPopupMenu JavaDoc;
29 import javax.swing.JScrollPane JavaDoc;
30 import javax.swing.JTree JavaDoc;
31 import javax.swing.ToolTipManager JavaDoc;
32 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
33 import javax.swing.tree.TreeModel JavaDoc;
34 import javax.swing.tree.TreePath JavaDoc;
35 import javax.swing.tree.TreeSelectionModel JavaDoc;
36
37 /**
38  *
39  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
40  */

41 class InstrumentManagerTree
42     extends JComponent JavaDoc
43 {
44     private final InstrumentManagerConnection m_connection;
45
46     private final TreeModel JavaDoc m_treeModel;
47     private final JTree JavaDoc m_tree;
48
49     /*---------------------------------------------------------------
50      * Constructors
51      *-------------------------------------------------------------*/

52     InstrumentManagerTree( InstrumentManagerConnection connection )
53     {
54         m_connection = connection;
55
56         m_treeModel = m_connection.getTreeModel();
57
58
59         m_tree = new JTree JavaDoc( m_treeModel );
60         //m_tree.setEditable( true ); // Makes it possible to edit the node names in line.
61
m_tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
62         m_tree.setRootVisible( false ); // Hide the root node.
63
m_tree.setShowsRootHandles( true ); // The root's children become "roots"
64
m_tree.setCellRenderer( new InstrumentManagerTreeCellRenderer() );
65         m_tree.putClientProperty( "JTree.lineStyle", "Angled" );
66
67         m_tree.addMouseListener( new MouseAdapter JavaDoc()
68         {
69             public void mouseClicked( MouseEvent JavaDoc 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 JavaDoc 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 JavaDoc 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         // Register the tree to work with tooltips.
113
ToolTipManager.sharedInstance().registerComponent( m_tree );
114
115         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc( m_tree );
116
117         setLayout( new BorderLayout JavaDoc() );
118         add( scrollPane, BorderLayout.CENTER );
119     }
120
121     /*---------------------------------------------------------------
122      * Methods
123      *-------------------------------------------------------------*/

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 JavaDoc treePath = m_tree.getPathForRow( row );
133         if ( treePath.getLastPathComponent() instanceof DefaultMutableTreeNode JavaDoc )
134         {
135             DefaultMutableTreeNode JavaDoc treeNode = (DefaultMutableTreeNode JavaDoc)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 JavaDoc treePath = m_tree.getPathForRow( row );
147
148         if ( treePath.getLastPathComponent() instanceof DefaultMutableTreeNode JavaDoc )
149         {
150             DefaultMutableTreeNode JavaDoc treeNode = (DefaultMutableTreeNode JavaDoc)treePath.getLastPathComponent();
151             if ( treeNode.getUserObject() instanceof NodeData )
152             {
153                 NodeData nodeData = (NodeData)treeNode.getUserObject();
154                 JPopupMenu JavaDoc popup = nodeData.getPopupMenu();
155                 if ( popup != null )
156                 {
157                     // Need to figure out where to display the popup.
158
Rectangle JavaDoc bounds = m_tree.getRowBounds( row );
159
160                     /*
161                     // Anchor the popup menu at the location of the node.
162                     int x = bounds.x + 24;
163                     int y = bounds.y + bounds.height;
164                     */

165
166                     // Anchor the popup menu where the user clicked.
167
int x = mouseX;
168                     int y = mouseY;
169
170                     popup.show( m_tree, x, y );
171                 }
172             }
173         }
174     }
175 }
176
Popular Tags