KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > treetable > ExpandCollapseAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.treetable;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import javax.swing.AbstractAction JavaDoc;
24 import javax.swing.event.ListSelectionEvent JavaDoc;
25 import javax.swing.event.ListSelectionListener JavaDoc;
26 import javax.swing.tree.TreePath JavaDoc;
27 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
28 import org.openide.util.NbBundle;
29
30 /**
31  * Expanding/collapsing nodes.
32  *
33  * @author tl
34  */

35 public class ExpandCollapseAction extends AbstractAction JavaDoc implements
36 ListSelectionListener JavaDoc {
37     private TreeTable tt;
38     private boolean expand;
39     
40     /**
41      * Creates a new instance of ExpandCollapseAction.
42      *
43      * @param expand true = expand
44      * @param tt a TreeTable
45      */

46     public ExpandCollapseAction(boolean expand, TreeTable tt) {
47         super(NbBundle.getMessage(ExpandCollapseAction.class,
48                 expand ? "Expand" : "Collapse")); // NOI18N
49
this.tt = tt;
50         this.expand = expand;
51         tt.getSelectionModel().addListSelectionListener(this);
52     }
53
54     public void actionPerformed(ActionEvent JavaDoc e) {
55         TreePath JavaDoc tp = tt.getSelectedPath();
56         if (expand)
57             tt.expandPath(tp);
58         else
59             tt.getTree().collapsePath(tp);
60         tt.select(tp);
61     }
62
63     public void valueChanged(ListSelectionEvent JavaDoc e) {
64         TreePath JavaDoc tp = tt.getSelectedPath();
65         if (tp != null) {
66             boolean expanded = tt.getTree().isExpanded(tp);
67             boolean children = tt.getTree().getModel().getChildCount(
68                     tp.getLastPathComponent()) > 0;
69             setEnabled(children && (expanded != this.expand));
70         } else {
71             setEnabled(false);
72         }
73     }
74 }
75
Popular Tags