KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > client > DisplayTable


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 /**
21  * DisplayTable.java
22  *
23  *
24  * Created: Mon Jan 29 16:43:09 2001
25  *
26  * @author Ana von Klopp
27  * @version
28  */

29
30
31 package org.netbeans.modules.web.monitor.client;
32
33 import java.awt.Font JavaDoc;
34 import java.awt.FontMetrics JavaDoc;
35 import java.awt.Graphics JavaDoc;
36 import javax.swing.BorderFactory JavaDoc;
37 import javax.swing.DefaultCellEditor JavaDoc;
38 import javax.swing.JComboBox JavaDoc;
39 import javax.swing.JTable JavaDoc;
40 import javax.swing.border.Border JavaDoc;
41 import javax.swing.table.TableCellEditor JavaDoc;
42 import javax.swing.table.TableColumn JavaDoc;
43 import javax.swing.table.TableColumnModel JavaDoc;
44 import javax.swing.table.TableModel JavaDoc;
45 import javax.swing.event.TableModelEvent JavaDoc;
46 import javax.swing.event.TableModelListener JavaDoc;
47 import java.awt.Color JavaDoc;
48 import java.awt.Dimension JavaDoc;
49 import java.awt.SystemColor JavaDoc;
50
51 import org.netbeans.modules.web.monitor.data.Param;
52 import org.openide.util.NbBundle;
53
54 public class DisplayTable extends JTable JavaDoc {
55
56     private static final boolean debug = false;
57      
58     // Type of data displayed
59
public static final int UNEDITABLE = 0;
60     public static final int REQUEST = 1;
61     public static final int SERVER = 2;
62     public static final int HEADERS = 3;
63     public static final int PARAMS = 4;
64     public static final int COOKIES = 5;
65
66     // Sorting states
67
public static final int NEUTRAL = 0;
68     public static final int A2Z = 1;
69     public static final int Z2A = 2;
70
71     private int numRows = 0;
72     private int numCols = 3;
73
74     private Object JavaDoc[][] data = null;
75
76     private TableCellEditor JavaDoc[][] cellEditors = null;
77
78     // Can we edit the fields?
79
private boolean editableNames = false;
80     private int editable = UNEDITABLE;
81
82     // Do we sort?
83
private int sort = NEUTRAL;
84     private boolean sortable = false;
85
86     // Handle resizing for larger fonts
87
boolean fontChanged = true;
88     
89     public DisplayTable(String JavaDoc[] categories) {
90     this(categories, null, UNEDITABLE, false);
91     }
92
93     public DisplayTable(String JavaDoc[] categories, boolean sortable) {
94     this(categories, null, UNEDITABLE, sortable);
95     }
96
97     public DisplayTable(String JavaDoc[] categories, int editable) {
98     this(categories, null, editable, false);
99     }
100
101     public DisplayTable(String JavaDoc[] categories, int editable, boolean sortable) {
102     this(categories, null, editable, sortable);
103     }
104
105     public DisplayTable(String JavaDoc[] names, String JavaDoc[] values) {
106     this(names, values, UNEDITABLE, false);
107     }
108
109     public DisplayTable(String JavaDoc[] names, String JavaDoc[] values, boolean sortable) {
110     this(names, values, UNEDITABLE, sortable);
111     }
112
113     public DisplayTable(String JavaDoc[] names, String JavaDoc[] values, int editable) {
114     this(names, values, editable, false);
115     }
116
117     public DisplayTable(String JavaDoc[] names, String JavaDoc[] values, int editable,
118             boolean sortable) {
119     
120     super();
121     numRows = names.length;
122     editableNames = false;
123     this.editable = editable;
124     this.sortable = sortable;
125     
126     data = new Object JavaDoc[numRows][numCols];
127     cellEditors = new TableCellEditor JavaDoc[numRows][numCols];
128     for(int i=0; i<numRows; ++i) {
129         data[i][0] = names[i];
130         if (values == null) {
131         data[i][1] = new String JavaDoc(""); // NOI18N
132
} else {
133         data[i][1] = values[i];
134         }
135         data[i][2] = NbBundle.getBundle(DisplayTable.class).getString("MON_Edit_dots"); // NOI18N
136
cellEditors[i][2] =
137         NameValueCellEditor.createCellEditor((JTable JavaDoc)this, data,
138                              false, i, editable);
139     }
140     setMyModel(data, editable > UNEDITABLE);
141     setup();
142     }
143     
144     public DisplayTable(Param[] params) {
145     this(params, UNEDITABLE, false);
146     }
147
148     public DisplayTable(Param[] params, boolean sortable) {
149     this(params, UNEDITABLE, sortable);
150     }
151    
152     
153     public DisplayTable(Param[] params, int editable) {
154     this(params, editable, false);
155     }
156     
157
158     public DisplayTable(Param[] params, int editable, boolean sortable) {
159
160     super();
161
162     if(editable < 3)
163         editableNames = false;
164     else
165         editableNames = true;
166
167     this.editable = editable;
168     this.sortable = sortable;
169
170     numRows = params.length;
171     data = new Object JavaDoc[numRows][numCols];
172     cellEditors = new TableCellEditor JavaDoc[numRows][numCols];
173     for(int i=0; i<numRows; ++i) {
174         data[i][0] = params[i].getAttributeValue("name"); // NOI18N
175
data[i][1] = params[i].getAttributeValue("value"); // NOI18N
176
data[i][2] = NbBundle.getBundle(DisplayTable.class).getString("MON_Edit_dots"); // NOI18N
177
cellEditors[i][2] =
178         NameValueCellEditor.createCellEditor((JTable JavaDoc)this, data,
179                              true, i, editable);
180     }
181     setMyModel(data, editable > UNEDITABLE);
182     setup();
183     }
184
185     private void setup() {
186     setBorderAndColorScheme();
187     Dimension JavaDoc margins = new Dimension JavaDoc(6, 4);
188     setIntercellSpacing(margins);
189     sort();
190     }
191
192     /**
193      * Set the border and colors for the table.
194      * Depends on whether the table is ediable or not.
195      */

196     private void setBorderAndColorScheme() {
197     setBorderAndColorScheme(editable != UNEDITABLE);
198     }
199
200     private void setBorderAndColorScheme(boolean editable) {
201     Color JavaDoc bg;
202     this.setBorder(BorderFactory.createLoweredBevelBorder());
203     if (!editable) {
204         //bg = SystemColor.control;
205
bg = this.getBackground().darker();
206     } else {
207         bg = Color.white;
208     }
209     this.setTableHeader(null);
210     this.setBackground(bg);
211     }
212     
213     /**
214      * Creates a combobox for a cell editor.
215      *
216      * @return the combobox that is used as the editor.
217      */

218     public JComboBox JavaDoc setChoices(int row, int col, String JavaDoc[] choices,
219                 boolean editable) {
220         TableCellEditor JavaDoc ed = new ComboBoxTableCellEditor(choices);
221     cellEditors[row][col] = ed;
222
223     // if the table is editable, we should turn off the [...] editor
224
// when there's a choice on the row.
225
data[row][2]=NbBundle.getBundle(DisplayTable.class).getString("MON_Editing");
226     cellEditors[row][2] = null;
227         
228         return ((ComboBoxTableCellEditor)ed).getComboBox();
229     }
230
231     /**
232      * Override the getter for the cell editors, so that customized
233      * cell editors will show up.
234      */

235     public TableCellEditor JavaDoc getCellEditor(int row, int col) {
236     TableCellEditor JavaDoc ed = cellEditors[row][col];
237     if (ed == null) {
238         return super.getCellEditor(row, col);
239     }
240     return ed;
241     }
242
243     public void setSorting(int state) {
244     sort = state;
245     if(getModel() instanceof DisplayTableSorter)
246         ((DisplayTableSorter)getModel()).sort(sort);
247     }
248
249     public void sort() {
250     if(getModel() instanceof DisplayTableSorter)
251         ((DisplayTableSorter)getModel()).sort(sort);
252     }
253
254     private void setMyModel(Object JavaDoc[][] data, boolean canEdit) {
255      
256     DisplayTableModel model = new DisplayTableModel(data,
257                             canEdit,
258                             editable > 2);
259     if(sortable) {
260         DisplayTableSorter sorter = new DisplayTableSorter(model);
261         setModel(sorter);
262     }
263     else {
264         setModel(model);
265     }
266
267     // PENDING - the column size does not shrink the way it should
268
TableColumnModel JavaDoc tcm = getColumnModel();
269     if (tcm.getColumnCount() > 0) {
270         TableColumn JavaDoc column = tcm.getColumn(0);
271         column.setPreferredWidth(10);
272         tcm.getColumn(2).setMaxWidth(5);
273     }
274     }
275
276     public void addTableModelListener(TableModelListener JavaDoc tml) {
277     TableModel JavaDoc tableModel = getModel();
278     if (tableModel != null) {
279         tableModel.addTableModelListener(tml);
280     }
281     }
282
283     public void removeTableModelListener(TableModelListener JavaDoc tml) {
284     TableModel JavaDoc tableModel = getModel();
285     if (tableModel != null) {
286         tableModel.removeTableModelListener(tml);
287     }
288     }
289
290     public Object JavaDoc[][] getData() {
291     return data;
292     }
293
294     public void setFont(Font JavaDoc f) {
295     fontChanged = true;
296     super.setFont(f);
297     }
298
299     /**
300      * When paint is first invoked, we set the rowheight based on the
301      * size of the font. */

302     public void paint(Graphics JavaDoc g) {
303     if (fontChanged) {
304         Font JavaDoc f = getFont();
305         FontMetrics JavaDoc fm = g.getFontMetrics(f);
306         int rowHeight = fm.getHeight();
307         fontChanged = false;
308         //triggers paint, just return afterwards
309
this.setRowHeight(rowHeight);
310         return;
311     }
312     super.paint(g);
313     }
314     
315     private void log(String JavaDoc s) {
316     System.out.println("DisplayTable::" + s); //NOI18N
317
}
318
319 } // DisplayTable
320
Popular Tags