KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > table > ColumnsConfig


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.table;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import javax.swing.JTable JavaDoc;
25 import javax.swing.table.TableColumn JavaDoc;
26 import javax.swing.table.TableColumnModel JavaDoc;
27
28 /**
29  * Columns configuration.
30  *
31  * @author tl
32  */

33 public class ColumnsConfig implements Serializable JavaDoc {
34     public static final long serialVersionUID = 2L;
35
36     /**
37      * Returns the columns configuration that could be serialized.
38      *
39      * @param table a table
40      * @return columns configuration (visible columns, sorting etc.)
41      */

42     public static ColumnsConfig getColumnsConfig(JTable JavaDoc table) {
43         ColumnsConfig cc = new ColumnsConfig();
44         
45         TableColumnModel JavaDoc ctm = table.getColumnModel();
46         assert ctm != null : "ctm == null"; // NOI18N
47

48         cc.columns = new int[ctm.getColumnCount()];
49         cc.columnWidths = new int[ctm.getColumnCount()];
50         for (int i = 0; i < ctm.getColumnCount(); i++) {
51             TableColumn JavaDoc c = ctm.getColumn(i);
52             cc.columns[i] = c.getModelIndex();
53             cc.columnWidths[i] = c.getWidth();
54         }
55         
56         if (table instanceof SortableTable) {
57             cc.sortedColumn =
58                     ((SortableTable) table).getSortingModel().getSortedColumn();
59             cc.ascending = !((SortableTable) table).getSortingModel().
60                     isSortOrderDescending();
61         }
62         
63         return cc;
64     }
65     
66     /**
67      * Sets columns configuration read from a stream.
68      *
69      * @param table a table
70      * @param config columns configuration
71      */

72     public static void setColumnsConfig(JTable JavaDoc table, ColumnsConfig config) {
73         //if (UTUtils.LOGGER.isLoggable(Level.FINE))
74
// Thread.dumpStack();
75

76         assert config != null : "config == null"; // NOI18N
77

78         table.createDefaultColumnsFromModel();
79
80         ColumnsConfig cc = (ColumnsConfig) config;
81         
82         ArrayList JavaDoc<TableColumn JavaDoc> newc = new ArrayList JavaDoc<TableColumn JavaDoc>();
83         TableColumnModel JavaDoc tcm = table.getColumnModel();
84         assert tcm != null : "tcm == null"; // NOI18N
85

86         for (int i = 0; i < cc.columns.length; i++) {
87             for (int j = 0; j < tcm.getColumnCount(); j++) {
88                 TableColumn JavaDoc c = tcm.getColumn(j);
89                 if (cc.columns[i] == c.getModelIndex()) {
90                     newc.add(c);
91                     tcm.removeColumn(c);
92                     c.setPreferredWidth(cc.columnWidths[i]);
93                     c.setWidth(cc.columnWidths[i]);
94                     break;
95                 }
96             }
97         }
98         while (tcm.getColumnCount() > 0) {
99             tcm.removeColumn(tcm.getColumn(0));
100         }
101         for (int i = 0; i < newc.size(); i ++) {
102             tcm.addColumn(newc.get(i));
103         }
104     }
105
106     /**
107      * Model indexes for visible columns
108      */

109     public int[] columns;
110
111     /**
112      * Widths of the columns in pixels
113      */

114     public int[] columnWidths;
115
116     /**
117      * Model index or -1
118      */

119     public int sortedColumn = -1;
120
121     /**
122      * Sorting order
123      */

124     public boolean ascending;
125
126     public String JavaDoc toString() {
127         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128         sb.append("ColumnsConfig["); // NOI18N
129
sb.append("sortedColumn=").append(sortedColumn); // NOI18N
130
sb.append(", ascending=").append(sortedColumn); // NOI18N
131
sb.append(", columns=["); // NOI18N
132
for (int i = 0; i < columns.length; i++) {
133             if (i != 0)
134                 sb.append(", "); // NOI18N
135
sb.append(columns[i]);
136             sb.append("->"); // NOI18N
137
sb.append(columnWidths[i]);
138         }
139         sb.append("]"); // NOI18N
140
sb.append("]"); // NOI18N
141
return sb.toString();
142     }
143 }
144
145
Popular Tags