001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin.common;
005:
006: import java.awt.event.KeyEvent;
007: import java.awt.event.MouseAdapter;
008: import java.awt.event.MouseEvent;
009: import java.awt.event.MouseListener;
010:
011: import javax.swing.Action;
012: import javax.swing.ActionMap;
013: import javax.swing.InputMap;
014: import javax.swing.JPopupMenu;
015: import javax.swing.KeyStroke;
016: import javax.swing.SwingUtilities;
017: import javax.swing.tree.DefaultMutableTreeNode;
018: import javax.swing.tree.TreeModel;
019: import javax.swing.tree.TreePath;
020:
021: public class XTree extends org.dijon.Tree {
022: protected JPopupMenu m_popupMenu;
023:
024: public XTree() {
025: super ();
026:
027: setShowsRootHandles(true);
028: setRootVisible(false);
029: setCellRenderer(new XTreeCellRendererDelegate());
030:
031: MouseListener ml = new MouseAdapter() {
032: public void mousePressed(MouseEvent e) {
033: testPopup(e);
034: }
035:
036: public void mouseReleased(MouseEvent e) {
037: testPopup(e);
038: }
039:
040: public void testPopup(MouseEvent e) {
041: if (e.isPopupTrigger()) {
042: TreePath path = getPathForLocation(e.getX(), e
043: .getY());
044: Object comp = path != null ? path
045: .getLastPathComponent() : null;
046:
047: XTreeNode node = comp instanceof XTreeNode ? (XTreeNode) comp
048: : null;
049: JPopupMenu menu = node != null ? node
050: .getPopupMenu() : XTree.this .getPopupMenu();
051:
052: if (menu != null) {
053: menu.show(XTree.this , e.getX(), e.getY());
054: }
055: }
056: }
057: };
058: addMouseListener(ml);
059: }
060:
061: public void setPopupMenu(JPopupMenu popupMenu) {
062: if (popupMenu != null) {
063: add(m_popupMenu = popupMenu);
064: }
065: }
066:
067: public JPopupMenu getPopupMenu() {
068: return m_popupMenu;
069: }
070:
071: private Action getAction(KeyStroke ks) {
072: Action result = null;
073: Object object = getLastSelectedPathComponent();
074:
075: if (object instanceof XTreeNode) {
076: XTreeNode node = (XTreeNode) object;
077:
078: while (node != null) {
079: ActionMap actionMap = node.getActionMap();
080:
081: if (actionMap != null) {
082: InputMap inputMap = node.getInputMap();
083:
084: if (inputMap != null) {
085: Object binding = inputMap.get(ks);
086:
087: if (binding != null) {
088: if ((result = actionMap.get(binding)) != null) {
089: return result;
090: }
091: }
092: }
093: }
094:
095: node = (XTreeNode) node.getParent();
096: }
097: }
098:
099: return null;
100: }
101:
102: protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
103: int condition, boolean pressed) {
104: Action action = getAction(ks);
105:
106: if (action != null) {
107: return SwingUtilities.notifyAction(action, ks, e, this , e
108: .getModifiers());
109: } else {
110: return super .processKeyBinding(ks, e, condition, pressed);
111: }
112: }
113:
114: public void setModel(TreeModel model) {
115: super .setModel(model);
116: selectTop();
117: }
118:
119: public void selectTop() {
120: if (getRowCount() > 0) {
121: setSelectionRow(0);
122: expandTop();
123: }
124: }
125:
126: public void expandTop() {
127: expandRow(0);
128: }
129:
130: public void expandAll() {
131: DefaultMutableTreeNode root = (DefaultMutableTreeNode) getModel()
132: .getRoot();
133: DefaultMutableTreeNode node = root.getFirstLeaf();
134: DefaultMutableTreeNode parent;
135:
136: while (node != null) {
137: parent = (DefaultMutableTreeNode) node.getParent();
138:
139: if (parent != null) {
140: expandPath(new TreePath(parent.getPath()));
141: }
142:
143: node = node.getNextLeaf();
144: }
145:
146: revalidate();
147: repaint();
148: }
149:
150: public void createOverlayListener() {/**/
151: }
152: }
|