001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.serverconfig.permissions.tree;
020:
021: import java.awt.event.MouseAdapter;
022: import org.w3c.dom.events.MouseEvent;
023:
024: import javax.swing.JTree;
025: import javax.swing.event.TreeSelectionEvent;
026: import javax.swing.event.TreeSelectionListener;
027: import javax.swing.tree.DefaultTreeModel;
028: import javax.swing.tree.TreePath;
029:
030: /**
031: * Listener for receiving permission tree selected events.
032: *
033: * @author Matthew Large
034: * @version $Revision: 1.1 $
035: *
036: */
037: public class PermissionsTreeSelectionListener extends MouseAdapter
038: implements TreeSelectionListener {
039:
040: /**
041: * Re-entry check.
042: */
043: private boolean m_bReentrant = false;
044:
045: /**
046: * Tree to listen to.
047: */
048: private JTree m_tree;
049:
050: /**
051: * Constructs a new permissions tree listener.
052: *
053: * @param tree tree to listen to.
054: */
055: public PermissionsTreeSelectionListener(JTree tree) {
056: super ();
057: this .m_tree = tree;
058: }
059:
060: /* (non-Javadoc)
061: * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
062: */
063: public void valueChanged(TreeSelectionEvent tse) {
064: if (!this .m_bReentrant) {
065: this .m_bReentrant = true;
066: Object obj = tse.getPath().getLastPathComponent();
067: if (obj instanceof PermissionsTreeNode
068: && ((PermissionsTreeNode) obj).getEnabled()
069: && ((PermissionsTreeNode) obj).isLeaf()) {
070: PermissionsTreeNode node = (PermissionsTreeNode) obj;
071: if (node.isSelected()) {
072: node.setSelected(false);
073: } else {
074: node.setSelected(true);
075: }
076: this .m_tree.revalidate();
077: this .m_tree.repaint();
078: this .m_tree.removeSelectionPath(this .m_tree
079: .getSelectionPath());
080: }
081: this .m_bReentrant = false;
082: }
083: }
084:
085: /* (non-Javadoc)
086: * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
087: */
088: public void mouseClicked(MouseEvent e) {
089: int x = e.getClientX();
090: int y = e.getClientY();
091: int row = m_tree.getRowForLocation(x, y);
092: TreePath path = m_tree.getPathForRow(row);
093: if (path != null) {
094: PermissionsTreeNode node = (PermissionsTreeNode) path
095: .getLastPathComponent();
096: if (node.getEnabled()) {
097: boolean isSelected = !(node.isSelected());
098: node.setSelected(isSelected);
099: ((DefaultTreeModel) m_tree.getModel())
100: .nodeChanged(node);
101: // I need revalidate if node is root. but why?
102: if (row == 0) {
103: m_tree.revalidate();
104: m_tree.repaint();
105: }
106: }
107: }
108: }
109:
110: }
|