001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /**
043: * DisplayTable.java
044: *
045: *
046: * Created: Mon Jan 29 16:43:09 2001
047: *
048: * @author Ana von Klopp
049: * @version
050: */package org.netbeans.modules.web.monitor.client;
051:
052: import java.awt.Font;
053: import java.awt.FontMetrics;
054: import java.awt.Graphics;
055: import javax.swing.BorderFactory;
056: import javax.swing.DefaultCellEditor;
057: import javax.swing.JComboBox;
058: import javax.swing.JTable;
059: import javax.swing.border.Border;
060: import javax.swing.table.TableCellEditor;
061: import javax.swing.table.TableColumn;
062: import javax.swing.table.TableColumnModel;
063: import javax.swing.table.TableModel;
064: import javax.swing.event.TableModelEvent;
065: import javax.swing.event.TableModelListener;
066: import java.awt.Color;
067: import java.awt.Dimension;
068: import java.awt.SystemColor;
069:
070: import org.netbeans.modules.web.monitor.data.Param;
071: import org.openide.util.NbBundle;
072:
073: public class DisplayTable extends JTable {
074:
075: private static final boolean debug = false;
076:
077: // Type of data displayed
078: public static final int UNEDITABLE = 0;
079: public static final int REQUEST = 1;
080: public static final int SERVER = 2;
081: public static final int HEADERS = 3;
082: public static final int PARAMS = 4;
083: public static final int COOKIES = 5;
084:
085: // Sorting states
086: public static final int NEUTRAL = 0;
087: public static final int A2Z = 1;
088: public static final int Z2A = 2;
089:
090: private int numRows = 0;
091: private int numCols = 3;
092:
093: private Object[][] data = null;
094:
095: private TableCellEditor[][] cellEditors = null;
096:
097: // Can we edit the fields?
098: private boolean editableNames = false;
099: private int editable = UNEDITABLE;
100:
101: // Do we sort?
102: private int sort = NEUTRAL;
103: private boolean sortable = false;
104:
105: // Handle resizing for larger fonts
106: boolean fontChanged = true;
107:
108: public DisplayTable(String[] categories) {
109: this (categories, null, UNEDITABLE, false);
110: }
111:
112: public DisplayTable(String[] categories, boolean sortable) {
113: this (categories, null, UNEDITABLE, sortable);
114: }
115:
116: public DisplayTable(String[] categories, int editable) {
117: this (categories, null, editable, false);
118: }
119:
120: public DisplayTable(String[] categories, int editable,
121: boolean sortable) {
122: this (categories, null, editable, sortable);
123: }
124:
125: public DisplayTable(String[] names, String[] values) {
126: this (names, values, UNEDITABLE, false);
127: }
128:
129: public DisplayTable(String[] names, String[] values,
130: boolean sortable) {
131: this (names, values, UNEDITABLE, sortable);
132: }
133:
134: public DisplayTable(String[] names, String[] values, int editable) {
135: this (names, values, editable, false);
136: }
137:
138: public DisplayTable(String[] names, String[] values, int editable,
139: boolean sortable) {
140:
141: super ();
142: numRows = names.length;
143: editableNames = false;
144: this .editable = editable;
145: this .sortable = sortable;
146:
147: data = new Object[numRows][numCols];
148: cellEditors = new TableCellEditor[numRows][numCols];
149: for (int i = 0; i < numRows; ++i) {
150: data[i][0] = names[i];
151: if (values == null) {
152: data[i][1] = new String(""); // NOI18N
153: } else {
154: data[i][1] = values[i];
155: }
156: data[i][2] = NbBundle.getBundle(DisplayTable.class)
157: .getString("MON_Edit_dots"); // NOI18N
158: cellEditors[i][2] = NameValueCellEditor.createCellEditor(
159: (JTable) this , data, false, i, editable);
160: }
161: setMyModel(data, editable > UNEDITABLE);
162: setup();
163: }
164:
165: public DisplayTable(Param[] params) {
166: this (params, UNEDITABLE, false);
167: }
168:
169: public DisplayTable(Param[] params, boolean sortable) {
170: this (params, UNEDITABLE, sortable);
171: }
172:
173: public DisplayTable(Param[] params, int editable) {
174: this (params, editable, false);
175: }
176:
177: public DisplayTable(Param[] params, int editable, boolean sortable) {
178:
179: super ();
180:
181: if (editable < 3)
182: editableNames = false;
183: else
184: editableNames = true;
185:
186: this .editable = editable;
187: this .sortable = sortable;
188:
189: numRows = params.length;
190: data = new Object[numRows][numCols];
191: cellEditors = new TableCellEditor[numRows][numCols];
192: for (int i = 0; i < numRows; ++i) {
193: data[i][0] = params[i].getAttributeValue("name"); // NOI18N
194: data[i][1] = params[i].getAttributeValue("value"); // NOI18N
195: data[i][2] = NbBundle.getBundle(DisplayTable.class)
196: .getString("MON_Edit_dots"); // NOI18N
197: cellEditors[i][2] = NameValueCellEditor.createCellEditor(
198: (JTable) this , data, true, i, editable);
199: }
200: setMyModel(data, editable > UNEDITABLE);
201: setup();
202: }
203:
204: private void setup() {
205: setBorderAndColorScheme();
206: Dimension margins = new Dimension(6, 4);
207: setIntercellSpacing(margins);
208: sort();
209: }
210:
211: /**
212: * Set the border and colors for the table.
213: * Depends on whether the table is ediable or not.
214: */
215: private void setBorderAndColorScheme() {
216: setBorderAndColorScheme(editable != UNEDITABLE);
217: }
218:
219: private void setBorderAndColorScheme(boolean editable) {
220: Color bg;
221: this .setBorder(BorderFactory.createLoweredBevelBorder());
222: if (!editable) {
223: //bg = SystemColor.control;
224: bg = this .getBackground().darker();
225: } else {
226: bg = Color.white;
227: }
228: this .setTableHeader(null);
229: this .setBackground(bg);
230: }
231:
232: /**
233: * Creates a combobox for a cell editor.
234: *
235: * @return the combobox that is used as the editor.
236: */
237: public JComboBox setChoices(int row, int col, String[] choices,
238: boolean editable) {
239: TableCellEditor ed = new ComboBoxTableCellEditor(choices);
240: cellEditors[row][col] = ed;
241:
242: // if the table is editable, we should turn off the [...] editor
243: // when there's a choice on the row.
244: data[row][2] = NbBundle.getBundle(DisplayTable.class)
245: .getString("MON_Editing");
246: cellEditors[row][2] = null;
247:
248: return ((ComboBoxTableCellEditor) ed).getComboBox();
249: }
250:
251: /**
252: * Override the getter for the cell editors, so that customized
253: * cell editors will show up.
254: */
255: public TableCellEditor getCellEditor(int row, int col) {
256: TableCellEditor ed = cellEditors[row][col];
257: if (ed == null) {
258: return super .getCellEditor(row, col);
259: }
260: return ed;
261: }
262:
263: public void setSorting(int state) {
264: sort = state;
265: if (getModel() instanceof DisplayTableSorter)
266: ((DisplayTableSorter) getModel()).sort(sort);
267: }
268:
269: public void sort() {
270: if (getModel() instanceof DisplayTableSorter)
271: ((DisplayTableSorter) getModel()).sort(sort);
272: }
273:
274: private void setMyModel(Object[][] data, boolean canEdit) {
275:
276: DisplayTableModel model = new DisplayTableModel(data, canEdit,
277: editable > 2);
278: if (sortable) {
279: DisplayTableSorter sorter = new DisplayTableSorter(model);
280: setModel(sorter);
281: } else {
282: setModel(model);
283: }
284:
285: // PENDING - the column size does not shrink the way it should
286: TableColumnModel tcm = getColumnModel();
287: if (tcm.getColumnCount() > 0) {
288: TableColumn column = tcm.getColumn(0);
289: column.setPreferredWidth(10);
290: tcm.getColumn(2).setMaxWidth(5);
291: }
292: }
293:
294: public void addTableModelListener(TableModelListener tml) {
295: TableModel tableModel = getModel();
296: if (tableModel != null) {
297: tableModel.addTableModelListener(tml);
298: }
299: }
300:
301: public void removeTableModelListener(TableModelListener tml) {
302: TableModel tableModel = getModel();
303: if (tableModel != null) {
304: tableModel.removeTableModelListener(tml);
305: }
306: }
307:
308: public Object[][] getData() {
309: return data;
310: }
311:
312: public void setFont(Font f) {
313: fontChanged = true;
314: super .setFont(f);
315: }
316:
317: /**
318: * When paint is first invoked, we set the rowheight based on the
319: * size of the font. */
320: public void paint(Graphics g) {
321: if (fontChanged) {
322: Font f = getFont();
323: FontMetrics fm = g.getFontMetrics(f);
324: int rowHeight = fm.getHeight();
325: fontChanged = false;
326: //triggers paint, just return afterwards
327: this .setRowHeight(rowHeight);
328: return;
329: }
330: super .paint(g);
331: }
332:
333: private void log(String s) {
334: System.out.println("DisplayTable::" + s); //NOI18N
335: }
336:
337: } // DisplayTable
|