KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
23 import java.util.Comparator JavaDoc;
24
25 import javax.swing.event.TreeModelEvent JavaDoc;
26 import javax.swing.event.TreeModelListener JavaDoc;
27 import javax.swing.tree.DefaultTreeModel JavaDoc;
28 import javax.swing.tree.TreePath JavaDoc;
29 import org.netbeans.modules.tasklist.core.table.SortingModel;
30
31 /**
32  * Default model for TreeTable
33  */

34 public class DefaultTreeTableModel extends DefaultTreeModel JavaDoc implements
35 TreeTableModel {
36     private static final long serialVersionUID = 1;
37
38     private String JavaDoc columnNames[];
39     
40     /**
41      * Creates a tree in which any node can have children.
42      *
43      * @param root a TreeNode object that is the root of the tree
44      * @param columnNames names for columns
45      * @see #DefaultTreeModel(TreeNode, boolean)
46      */

47     public DefaultTreeTableModel(TreeTableNode root, String JavaDoc[] columnNames) {
48         super(root);
49         this.columnNames = columnNames;
50     }
51
52     /**
53      * Creates a tree specifying whether any node can have children,
54      * or whether only certain nodes can have children.
55      *
56      * @param root a TreeNode object that is the root of the tree
57      * @param askAllowsChildren a boolean, false if any node can
58      * have children, true if each node is asked to see if
59      * it can have children
60      * @param columnNames names for columns
61      * @see #asksAllowsChildren
62      */

63     public DefaultTreeTableModel(TreeTableNode root, boolean asksAllowsChildren,
64     int columnNumber) {
65         super(root, asksAllowsChildren);
66         this.columnNames = new String JavaDoc[columnNumber];
67         Arrays.fill(columnNames, ""); // NOI18N
68
}
69
70     public int getColumnCount() {
71         return columnNames.length;
72     }
73     
74     public String JavaDoc getColumnName(int column) {
75         return columnNames[column];
76     }
77     
78     public Class JavaDoc getColumnClass(int column) {
79         if (column == 0)
80             return TreeTableModel.class;
81         else
82             return Object JavaDoc.class;
83     }
84     
85     public Object JavaDoc getValueAt(Object JavaDoc node, int column) {
86         return ((TreeTableNode) node).getValueAt(column);
87     }
88     
89     public boolean isCellEditable(Object JavaDoc node, int column) {
90         // CHANGEEDIT
91
//return column == 0 || ((TreeTableNode) node).isCellEditable(column);
92
return ((TreeTableNode) node).isCellEditable(column);
93     }
94     
95     public void setValueAt(Object JavaDoc aValue, Object JavaDoc node, int column) {
96         ((TreeTableNode) node).setValueAt(aValue, column);
97     }
98
99     public static class ToStringComparator implements Comparator JavaDoc {
100         public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
101             String JavaDoc s1 = (obj1 == null) ? "" : obj1.toString(); // NOI18N
102
String JavaDoc s2 = (obj2 == null) ? "" : obj2.toString(); // NOI18N
103
if (s1 == null)
104                 s1 = ""; // NOI18N
105
if (s2 == null)
106                 s2 = ""; // NOI18N
107
return s1.compareTo(s2);
108         }
109     }
110
111     /*
112      * Notify all listeners that have registered interest for
113      * notification on this event type. The event instance
114      * is lazily created using the parameters passed into
115      * the fire method.
116      * @see EventListenerList
117      */

118     public void fireTreeStructureChanged(Object JavaDoc source, Object JavaDoc[] path) {
119         // Guaranteed to return a non-null array
120
Object JavaDoc[] listeners = listenerList.getListenerList();
121         TreeModelEvent JavaDoc e = null;
122         TreePath JavaDoc tp = path == null ? null : new TreePath JavaDoc(path);
123         // Process the listeners last to first, notifying
124
// those that are interested in this event
125
for (int i = listeners.length-2; i>=0; i-=2) {
126             if (listeners[i]==TreeModelListener JavaDoc.class) {
127                 // Lazily create the event:
128
if (e == null)
129                     e = new TreeModelEvent JavaDoc(source, tp);
130                 ((TreeModelListener JavaDoc)listeners[i+1]).treeStructureChanged(e);
131             }
132         }
133     }
134     
135     public void fireTreeNodesRemoved(Object JavaDoc source, Object JavaDoc[] path,
136     int[] childIndices, Object JavaDoc[] children) {
137         super.fireTreeNodesRemoved(source, path, childIndices, children);
138     }
139     
140     public void fireTreeNodesInserted(Object JavaDoc source, Object JavaDoc[] path,
141     int[] childIndices, Object JavaDoc[] children) {
142         super.fireTreeNodesInserted(source, path, childIndices, children);
143     }
144     
145     public void fireTreeNodesChanged(Object JavaDoc source, Object JavaDoc[] path,
146     int[] childIndices, Object JavaDoc[] children) {
147         super.fireTreeNodesChanged(source, path, childIndices, children);
148     }
149     
150     public void fireTreeStructureChanged(Object JavaDoc source, Object JavaDoc[] path,
151     int[] childIndices, Object JavaDoc[] children) {
152         super.fireTreeStructureChanged(source, path, childIndices, children);
153     }
154     
155     public void sort(SortingModel sm) {
156     }
157 }
158
Popular Tags