KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.event.MouseAdapter JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import java.beans.PropertyVetoException JavaDoc;
26 import java.util.logging.Level JavaDoc;
27
28 import javax.swing.Action JavaDoc;
29 import javax.swing.JPopupMenu JavaDoc;
30 import javax.swing.event.ListSelectionEvent JavaDoc;
31 import javax.swing.event.ListSelectionListener JavaDoc;
32 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
33 import org.openide.awt.MouseUtils;
34 import org.openide.explorer.ExplorerManager;
35 import org.openide.nodes.AbstractNode;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38 import org.openide.util.Utilities;
39
40 /**
41  * TreeTable with support for Nodes
42  */

43 public abstract class NodesTreeTable extends TreeTable {
44     /**
45      * Root node for the selection
46      */

47     private static class RootNode extends AbstractNode {
48         /**
49          * Constructor
50          */

51         public RootNode() {
52             super(new Children.Array());
53         }
54     }
55     
56     private ExplorerManager em;
57     
58     /**
59      * This root node prevents double update of the properties view
60      * after calling setRootContext and setExploredContext
61      */

62     private RootNode rootNode;
63     
64     /** Creates a new instance of NodesTreeTable */
65     public NodesTreeTable(ExplorerManager explorerManager, TreeTableModel ttm) {
66         super(ttm);
67         this.em = explorerManager;
68         this.rootNode = new RootNode();
69         this.em.setRootContext(rootNode);
70         
71         addMouseListener(new MouseUtils.PopupMouseAdapter() {
72             public void showPopup(MouseEvent JavaDoc e) {
73                 int row = rowAtPoint(e.getPoint());
74                 int col = columnAtPoint(e.getPoint());
75                 Action JavaDoc[] actions;
76                 if (row < 0 || col < 0) {
77                     actions = getFreeSpaceActions();
78                 } else {
79                     if (!getSelectionModel().isSelectedIndex(row)) {
80                         setRowSelectionInterval(row, row);
81                     }
82                     Node n = createNode(getNodeForRow(row));
83                     if (n == null)
84                         return;
85
86                     actions = n.getActions(false);
87                 }
88                 JPopupMenu JavaDoc pm = Utilities.actionsToPopup(actions,
89                     NodesTreeTable.this);
90                 if(pm != null)
91                     pm.show(NodesTreeTable.this, e.getX(), e.getY());
92             }
93         });
94         
95         addMouseListener(new MouseAdapter JavaDoc() {
96             public void mouseClicked(MouseEvent JavaDoc e) {
97                 if (!MouseUtils.isDoubleClick(e))
98                     return;
99                 
100                 int row = rowAtPoint(e.getPoint());
101                 int col = columnAtPoint(e.getPoint());
102                 if (row < 0 || col < 0)
103                     return;
104                 
105                 setRowSelectionInterval(row, row);
106                 Node n = createNode(getNodeForRow(row));
107                 if (n == null)
108                     return;
109                 
110                 Action JavaDoc action = n.getPreferredAction();
111                 if (action != null) {
112                     action.actionPerformed(
113                         new ActionEvent JavaDoc(this, ActionEvent.ACTION_PERFORMED,
114                         null));
115                 }
116             }
117         });
118         
119         getSelectionModel().addListSelectionListener(
120             new ListSelectionListener JavaDoc() {
121             public void valueChanged(ListSelectionEvent JavaDoc e) {
122                 int[] rows = getSelectedRows();
123                 Node[] nodes = new Node[rows.length];
124                 for (int i = 0; i < nodes.length; i++) {
125                     nodes[i] = createNode(getNodeForRow(rows[i]));
126                 }
127                 
128                 Children.Array ch = (Children.Array) rootNode.getChildren();
129                 ch.remove(ch.getNodes());
130                 ch.add(nodes);
131                 try {
132                     if (nodes.length > 0) {
133                         em.setExploredContext(nodes[0], nodes);
134                     } else {
135                         em.setSelectedNodes(nodes);
136                     }
137                 } catch (PropertyVetoException JavaDoc ex) {
138                     UTUtils.LOGGER.log(Level.WARNING, "", e); // NOI18N
139
}
140             }
141         }
142         );
143     }
144     
145     /**
146      * Creates a node for the specified Object returned by the TreeTableModel
147      *
148      * @parem obj an object returned by the TreeTableModel
149      * @return created Node if inappropriate
150      */

151     public abstract Node createNode(Object JavaDoc obj);
152     
153     /**
154      * Returns actions for the popup requested on the free space
155      *
156      * @return actions
157      */

158     public Action JavaDoc[] getFreeSpaceActions() {
159         return new Action JavaDoc[0];
160     }
161 }
162
Popular Tags