KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > common > XTree


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.common;
5
6 import java.awt.event.KeyEvent JavaDoc;
7 import java.awt.event.MouseAdapter JavaDoc;
8 import java.awt.event.MouseEvent JavaDoc;
9 import java.awt.event.MouseListener JavaDoc;
10
11 import javax.swing.Action JavaDoc;
12 import javax.swing.ActionMap JavaDoc;
13 import javax.swing.InputMap JavaDoc;
14 import javax.swing.JPopupMenu JavaDoc;
15 import javax.swing.KeyStroke JavaDoc;
16 import javax.swing.SwingUtilities JavaDoc;
17 import javax.swing.tree.TreeModel JavaDoc;
18 import javax.swing.tree.TreePath JavaDoc;
19
20 public class XTree extends org.dijon.Tree {
21   protected JPopupMenu JavaDoc m_popupMenu;
22   
23   public XTree() {
24     super();
25
26     setShowsRootHandles(true);
27     setRootVisible(false);
28     setCellRenderer(new XTreeCellRendererDelegate());
29
30     MouseListener JavaDoc ml = new MouseAdapter JavaDoc() {
31       public void mousePressed(MouseEvent JavaDoc e) {
32         testPopup(e);
33       }
34
35       public void mouseReleased(MouseEvent JavaDoc e) {
36         testPopup(e);
37       }
38
39       public void testPopup(MouseEvent JavaDoc e) {
40         if(e.isPopupTrigger()) {
41           TreePath JavaDoc path = getPathForLocation(e.getX(), e.getY());
42           XTreeNode node = path != null ? (XTreeNode)path.getLastPathComponent() : null;
43           JPopupMenu JavaDoc menu = node != null ? node.getPopupMenu() : XTree.this.getPopupMenu();
44
45           if(menu != null) {
46             menu.show(XTree.this, e.getX(), e.getY());
47           }
48         }
49       }
50     };
51     addMouseListener(ml);
52   }
53
54   public void setPopupMenu(JPopupMenu JavaDoc popupMenu) {
55     if(popupMenu != null) {
56       add(m_popupMenu = popupMenu);
57     }
58   }
59   
60   public JPopupMenu JavaDoc getPopupMenu() {
61     return m_popupMenu;
62   }
63   
64   private Action JavaDoc getAction(KeyStroke JavaDoc ks) {
65     Action JavaDoc result = null;
66     XTreeNode node = (XTreeNode)getLastSelectedPathComponent();
67
68     while(node != null) {
69       ActionMap JavaDoc actionMap = node.getActionMap();
70
71       if(actionMap != null) {
72         InputMap JavaDoc inputMap = node.getInputMap();
73
74         if(inputMap != null) {
75           Object JavaDoc binding = inputMap.get(ks);
76
77           if(binding != null) {
78             if((result = actionMap.get(binding)) != null) {
79               return result;
80             }
81           }
82         }
83       }
84
85       node = (XTreeNode)node.getParent();
86     }
87
88     return null;
89   }
90
91   protected boolean processKeyBinding(KeyStroke JavaDoc ks, KeyEvent JavaDoc e,
92                                       int condition, boolean pressed)
93   {
94     Action JavaDoc action = getAction(ks);
95
96     if(action != null) {
97       return SwingUtilities.notifyAction(action, ks, e, this, e.getModifiers());
98     }
99     else {
100       return super.processKeyBinding(ks, e, condition, pressed);
101     }
102   }
103
104   public void setModel(TreeModel JavaDoc model) {
105     super.setModel(model);
106     selectTop();
107   }
108
109   public void selectTop() {
110     if(getRowCount() > 0) {
111       setSelectionRow(0);
112       expandTop();
113     }
114   }
115
116   public void expandTop() {
117     expandRow(0);
118   }
119
120   public void expandAll() {
121     XRootNode root = (XRootNode)((XTreeModel)getModel()).getRoot();
122     XTreeNode node = (XTreeNode)root.getFirstLeaf();
123     XTreeNode parent;
124
125     while(node != null) {
126       parent = (XTreeNode)node.getParent();
127       
128       if(parent != null) {
129         expandPath(new TreePath JavaDoc(parent.getPath()));
130       }
131
132       node = (XTreeNode)node.getNextLeaf();
133     }
134
135     revalidate();
136     repaint();
137   }
138 }
139
Popular Tags