KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > columns > ColumnsConfiguration


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.core.columns;
21
22 import java.util.ArrayList JavaDoc;
23 import javax.swing.event.ChangeEvent JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import javax.swing.table.TableColumnModel JavaDoc;
26 import javax.swing.table.TableColumn JavaDoc;
27
28 import org.netbeans.modules.tasklist.core.ColumnProperty;
29 import org.netbeans.modules.tasklist.core.TaskListView;
30
31 /**
32  * View's columns
33  */

34 public class ColumnsConfiguration {
35     private ArrayList JavaDoc listeners = new ArrayList JavaDoc();
36
37     /** Widths of visible columns */
38     private int widths[];
39
40     private String JavaDoc[] properties;
41
42     /** property of the sorting column or null */
43     private String JavaDoc sortingColumn;
44
45     private boolean ascending;
46
47     /**
48      * Constructor
49      *
50      * @param properties names of the properties for visible columns
51      * @param widths widths of the visible columns
52      * @param sort name of the sorting column or null
53      * @param ascending true = ascending order, false = descending order
54      */

55     public ColumnsConfiguration(String JavaDoc[] properties, int[] widths, String JavaDoc sort,
56     boolean ascending) {
57         this.properties = properties;
58         this.widths = widths;
59         this.sortingColumn = sort;
60         this.ascending = ascending;
61     }
62     
63     /**
64      * Sets the values
65      *
66      * @param properties names of the properties for visible columns
67      * @param widths widths of the visible columns
68      * @param sort name of the sorting column or null
69      * @param ascending true = ascending order, false = descending order
70      */

71     public void setValues(String JavaDoc[] properties, int[] widths, String JavaDoc sort,
72     boolean ascending) {
73         this.properties = properties;
74         this.widths = widths;
75         this.sortingColumn = sort;
76         this.ascending = ascending;
77         fireChange();
78     }
79     
80     /**
81      * Returns widths of the columns
82      *
83      * @return widths
84      */

85     public int[] getWidths() {
86         return widths;
87     }
88     
89     /**
90      * Returns names of the properties for visible columns
91      *
92      * @return properties. != null
93      */

94     public String JavaDoc[] getProperties() {
95         return properties;
96     }
97
98     /**
99      * Returns property of the column the view is sorted on or null.
100      *
101      * @return name of a property or null
102      */

103     public String JavaDoc getSortingColumn() {
104         return sortingColumn;
105     }
106     
107     /**
108      * Returns sorting order
109      *
110      * @return true = ascending, false = descending
111      */

112     public boolean getSortingOrder() {
113         return ascending;
114     }
115     
116     /**
117      * Sets sorting order
118      *
119      * @param ascending true = ascending
120      */

121     public void setSortingOrder(boolean ascending) {
122         this.ascending = ascending;
123     }
124     
125     /**
126      * Adds a ChangeListener to the listener list.
127      *
128      * @param l The listener to add.
129      */

130     public void addChangeListener(ChangeListener JavaDoc l) {
131         listeners.add(l);
132     }
133     
134     /**
135      * Removes a ChangeListener from the listener list.
136      *
137      * @param l The listener to remove.
138      */

139     public void removeChangeListener(ChangeListener JavaDoc l) {
140         listeners.remove(l);
141     }
142     
143     /**
144      * Fires a change event
145      */

146     protected final void fireChange() {
147         ChangeEvent JavaDoc e = null;
148         for (int i = 0; i < listeners.size(); i++) {
149             ChangeListener JavaDoc l = (ChangeListener JavaDoc) listeners.get(i);
150             if (e == null)
151                 e = new ChangeEvent JavaDoc(this);
152             l.stateChanged(e);
153         }
154     }
155
156     /**
157      * Loads column configuration from a view
158      *
159      * @param v a view XXX replace by ColumnProperty[] and TableColumnModel
160      * @param cc a columns configuration
161      */

162     public static void loadColumnsFrom(TaskListView v, ColumnsConfiguration cc) {
163 // LOGGER.log(Level.FINE, "loading columns from " + v.getDisplayName(),
164
// new Exception());
165

166         // first find the tree column. It will be always the first one.
167
ColumnProperty columns[] = v.getColumns();
168         ColumnProperty treeColumn = null;
169         for (int i = 0; i < columns.length; i++) {
170             Object JavaDoc b = columns[i].getValue("TreeColumnTTV"); // NOI18N
171
if (b instanceof Boolean JavaDoc && ((Boolean JavaDoc) b).booleanValue()) {
172                 treeColumn = columns[i];
173                 break;
174             }
175         }
176         assert treeColumn != null;
177
178         // find the sorted column
179
ColumnProperty sortedColumn = null;
180         boolean ascending = false;
181         for (int i = 0; i < columns.length; i++) {
182             Boolean JavaDoc sorting = (Boolean JavaDoc) columns[i].getValue(
183                 "SortingColumnTTV"); // NOI18N
184
if ((sorting != null) && (sorting.booleanValue())) {
185                 sortedColumn = columns[i];
186                 Boolean JavaDoc desc = (Boolean JavaDoc) columns[i].getValue(
187                     "DescendingOrderTTV"); // NOI18N
188
ascending = (desc != Boolean.TRUE);
189             }
190         }
191
192         // widths
193
TableColumnModel JavaDoc m = v.getTable().getColumnModel();
194         int[] widths = new int[m.getColumnCount()];
195         String JavaDoc[] properties = new String JavaDoc[m.getColumnCount()];
196         for (int i = 0; i < m.getColumnCount(); i++) {
197             TableColumn JavaDoc tc = m.getColumn(i);
198             ColumnProperty cp = null;
199             for (int j = 0; j < columns.length; j++) {
200                 if (columns[j].getDisplayName().equals(tc.getHeaderValue())) {
201                     cp = columns[j];
202                 }
203             }
204             if (cp != null) {
205                 properties[i] = cp.getName();
206             } else {
207                 // tree column
208
properties[i] = treeColumn.getName();
209             }
210             widths[i] = tc.getWidth();
211         }
212
213         cc.setValues(properties, widths,
214             sortedColumn == null ? null : sortedColumn.getName(), ascending);
215     }
216
217     /**
218      * Configures view's column widths.
219      *
220      * @param v view that should be configured XXX replace by ColumnProperty[]
221      * @param cc a columns configuration
222      */

223     public static void configureColumns(TaskListView v, ColumnsConfiguration cc) {
224         String JavaDoc[] properties = cc.getProperties();
225         int[] widths = cc.getWidths();
226         String JavaDoc sortingColumn = cc.getSortingColumn();
227         boolean ascending = cc.getSortingOrder();
228
229         ColumnProperty columns[] = v.getColumns();
230
231         for (int i = 0; i < columns.length; i++) {
232             // NOTE reverse logic: this is INvisible
233
columns[i].setValue("InvisibleInTreeTableView", // NOI18N
234
Boolean.TRUE);
235         }
236
237         for (int i = 0; i < properties.length; i++) {
238             ColumnProperty c = findColumn(columns, properties[i]);
239             if (c != null) {
240                 // Necessary because by default some columns
241
// set invisible by default, so I have to
242
// override these
243
// NOTE reverse logic: this is INvisible
244
c.setValue("InvisibleInTreeTableView", // NOI18N
245
Boolean.FALSE);
246                 c.width = widths[i];
247 // LOGGER.fine("configure width: " + c.getName() + " " + c.width);
248
}
249         }
250
251         // Set sorting attribute
252
if (sortingColumn != null) {
253             ColumnProperty c = findColumn(columns, sortingColumn);
254             if (c != null) {
255                 c.setValue("SortingColumnTTV", Boolean.TRUE); // NOI18N
256
// Descending sort?
257
c.setValue("DescendingOrderTTV", // NOI18N
258
(!ascending) ? Boolean.TRUE : Boolean.FALSE);
259             }
260         }
261     }
262
263     /**
264      * Searches a column by name
265      *
266      * @param columns view columns
267      * @param name name of a property
268      * @return found column or null
269      */

270     private static ColumnProperty findColumn(ColumnProperty columns[], String JavaDoc name) {
271         for (int i = 0; i < columns.length; i++) {
272             if (columns[i].getName().equals(name))
273                 return columns[i];
274         }
275
276         return null;
277     }
278 }
Popular Tags