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: * DDTable.java
044: *
045: * @author Ana von Klopp
046: * @version
047: */package org.netbeans.modules.web.wizards;
048:
049: import java.awt.Color;
050: import java.awt.Component;
051: import java.awt.Dimension;
052: import java.awt.FontMetrics;
053: import java.awt.Graphics;
054: import java.awt.event.KeyEvent;
055: import java.awt.event.KeyListener;
056:
057: //import javax.swing.AbstractCellEditor;
058: import javax.swing.DefaultCellEditor;
059: import javax.swing.BorderFactory;
060: import javax.swing.JTable;
061: import javax.swing.JTextField;
062: import javax.swing.ListSelectionModel;
063: import javax.swing.table.AbstractTableModel;
064: import javax.swing.table.TableCellRenderer;
065: import javax.swing.event.TableModelEvent;
066:
067: import org.openide.util.NbBundle;
068:
069: class DDTable extends JTable implements KeyListener {
070:
071: private static final boolean debug = false;
072:
073: private String titleKey;
074: private Editable editable;
075: private String[] headers;
076: private final static int margin = 6;
077:
078: // Handle resizing for larger fonts
079: private boolean fontChanged = true;
080: private boolean addedRow = true;
081: private int rowHeight = 23;
082:
083: private static final long serialVersionUID = -155464225493968935L;
084:
085: DDTable(String[] headers, String titleKey) {
086: this (headers, titleKey, Editable.BOTH);
087: }
088:
089: DDTable(String[] headers, String titleKey, Editable editable) {
090:
091: super (new Object[0][headers.length], headers);
092:
093: this .headers = headers;
094: this .titleKey = titleKey;
095: this .editable = editable;
096:
097: setModel(new DDTableModel(headers, editable));
098: setColors(editable);
099: this .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
100: setIntercellSpacing(new Dimension(margin, margin));
101: DefaultCellEditor dce = new DefaultCellEditor(
102: new CellText(this ));
103: dce.setClickCountToStart(1);
104: getColumnModel().getColumn(0).setCellEditor(dce);
105: getColumnModel().getColumn(1).setCellEditor(dce);
106: }
107:
108: void setEditable(Editable editable) {
109: this .editable = editable;
110: setColors(editable);
111: }
112:
113: Editable getEditable() {
114: return this .editable;
115: }
116:
117: int addRow(String[] values) {
118: int i = ((DDTableModel) getModel()).addRow(values);
119: if (i == 0)
120: fontChanged = true;
121: addedRow = true;
122: this .invalidate();
123: return i;
124: }
125:
126: void removeRow(int row) {
127: if (isEditing())
128: getCellEditor().cancelCellEditing();
129:
130: ((DDTableModel) getModel()).removeRow(row);
131: this .invalidate();
132:
133: int maxSelectedRow = getRowCount() - 1;
134: if (getSelectedRow() > maxSelectedRow) {
135: if (maxSelectedRow >= 0)
136: setRowSelectionInterval(maxSelectedRow, maxSelectedRow);
137: else
138: clearSelection();
139: }
140: }
141:
142: String getColumnKey(int col) {
143: return headers[col];
144: }
145:
146: private void setColors(Editable editable) {
147: Color bg;
148: this .setBorder(BorderFactory.createLoweredBevelBorder());
149: if (editable == Editable.NEITHER) {
150: bg = this .getBackground().darker();
151: } else {
152: bg = Color.white;
153: }
154: this .setBackground(bg);
155: }
156:
157: /**
158: * Override the getter for the cell editors, so that customized
159: * cell editors will show up.
160:
161: public TableCellEditor getCellEditor(int row, int col) {
162: TableCellEditor e = super.getCellEditor(row, col);
163: Component c = e.getTableCellEditorComponent(this,
164: getValueAt(row, col),
165: true, row, col);
166: c.addKeyListener(this);
167: return e;
168: }
169: */
170:
171: /**
172: * Override the getter for the cell editors, so that customized
173: * cell editors will show up.
174: */
175: public TableCellRenderer getCellRenderer(int row, int col) {
176: return super .getCellRenderer(row, col);
177: }
178:
179: // This method is used by the edit button of the InitParamTable
180: void setData(String name, String value, int row) {
181: if (getEditingRow() == row) {
182: int col = getEditingColumn();
183: getCellEditor(row, col).cancelCellEditing();
184: }
185: ((DDTableModel) getModel()).setData(name, value, row);
186: }
187:
188: /**
189: * Checks whether the cells are editable
190: */
191: public boolean isCellEditable(int row, int col) {
192: if (editable == Editable.NEITHER) {
193: return false;
194: }
195: if (editable == Editable.VALUE && col == 0) {
196: return false;
197: } else
198: return true;
199: }
200:
201: /**
202: * When paint is first invoked, we set the rowheight based on the
203: * size of the font. */
204: public void paint(Graphics g) {
205:
206: if (debug)
207: log("::paint()"); //NOI18N
208:
209: if (fontChanged) {
210:
211: if (debug)
212: log("\tFont changed");
213: fontChanged = false;
214:
215: int height = 0;
216: if (debug)
217: log("\tGetting font height"); //NOI18N
218: FontMetrics fm = g.getFontMetrics(getFont());
219: // Add 2 for button border
220: // height = fm.getHeight() + 2 + margin;
221: height = fm.getHeight() + margin;
222: if (height > rowHeight)
223: rowHeight = height;
224:
225: if (debug)
226: log("\trow height is " + rowHeight); //NOI18N
227:
228: //triggers paint, just return afterwards
229: this .setRowHeight(rowHeight);
230: return;
231: }
232:
233: if (addedRow) {
234: addedRow = false;
235: if (debug)
236: log("\tAdded row");
237: int row = getModel().getRowCount() - 1;
238: this .editCellAt(row, 0);
239: Component c = getCellEditor(row, 0)
240: .getTableCellEditorComponent(this ,
241: getValueAt(row, 0), true, row, 0);
242: if (c instanceof JTextField) {
243: if (debug)
244: log("\tTrying to request focus");
245: ((JTextField) c).requestFocus();
246: }
247: }
248: super .paint(g);
249: }
250:
251: public void keyPressed(KeyEvent keyEvent) {
252: if (debug)
253: log("\tKey pressed");
254: }
255:
256: public void keyReleased(KeyEvent keyEvent) {
257:
258: if (debug)
259: log("::keyReleased()");
260:
261: Object o = keyEvent.getSource();
262: String s = null;
263: if (o instanceof JTextField) {
264: if (debug)
265: log("\tFound text field");
266: s = ((JTextField) o).getText().trim();
267: }
268:
269: int row = getEditingRow();
270: int col = getEditingColumn();
271: if (debug)
272: log("\trow=" + row + ", col=" + col);
273:
274: setValueAt(s, row, col);
275: }
276:
277: public void keyTyped(KeyEvent keyEvent) {
278: if (debug)
279: log("\tKey typed");
280: }
281:
282: private void log(String s) {
283: System.out.println("DDTable" + s); //NOI18N
284: }
285:
286: class DDTableModel extends AbstractTableModel {
287:
288: private String[] colheaders = null;
289: private Object[][] data = null;
290: private Editable editable;
291: private int numCols;
292: private int numRows = 0;
293:
294: private static final long serialVersionUID = -5044296029944667379L;
295:
296: DDTableModel(String[] headers, Editable editable) {
297:
298: this .colheaders = headers;
299: this .editable = editable;
300: numCols = colheaders.length;
301: data = new Object[numRows][numCols];
302: }
303:
304: public String getColumnName(int col) {
305: String key = "LBL_".concat(colheaders[col]);
306: return NbBundle.getMessage(DDTable.class, key);
307: }
308:
309: public int getRowCount() {
310: return data.length;
311: }
312:
313: public int getColumnCount() {
314: return numCols;
315: }
316:
317: public Object getValueAt(int row, int col) {
318: return data[row][col];
319: }
320:
321: public int addRow(String[] values) {
322:
323: Object[][] data2 = new Object[numRows + 1][numCols];
324: int i = 0, j = 0;
325:
326: if (numRows > 0) {
327: for (j = 0; j < numRows; ++j)
328: data2[j] = data[j];
329: }
330:
331: for (i = 0; i < values.length; ++i)
332: data2[j][i] = values[i];
333:
334: data = data2;
335: numRows++;
336: return j;
337: }
338:
339: public void removeRow(int row) {
340:
341: if (debug) {
342: log("::removeRow()"); //NOI18N
343: log("row is " + row); //NOI18N
344: log("numRows is " + numRows); //NOI18N
345: }
346:
347: Object[][] data2 = new Object[numRows - 1][numCols];
348: int newRowIndex = 0;
349: for (int i = 0; i < numRows; ++i) {
350: if (debug)
351: log("\tExamining row " + i); //NOI18N
352: if (i == row)
353: continue;
354: if (debug)
355: log("\tKeep this row"); //NOI18N
356: data2[newRowIndex] = data[i];
357: newRowIndex++;
358: if (debug)
359: log("\tnewRowIndex is " + newRowIndex); //NOI18N
360: }
361: data = data2;
362: numRows = --numRows;
363: }
364:
365: void setData(String name, String value, int row) {
366: data[row][0] = name;
367: data[row][1] = value;
368: fireTableChanged(new TableModelEvent(this , row));
369: }
370:
371: public void setValueAt(Object value, int row, int col) {
372:
373: if (debug)
374: log("::setValueAt(): value = " + value + //NOI18N
375: " at " + row + ", " + col); //NOI18N
376:
377: data[row][col] = value;
378:
379: if (debug) {
380: for (int i = 0; i < data.length; ++i) {
381: for (int j = 0; j < numCols; ++j) {
382: log("\t" + String.valueOf(i) + "," + //NOI18N
383: String.valueOf(j) + ": " + data[i][j]); //NOI18N
384: }
385: }
386: }
387: // Commenting this out since the value is set twice.
388: fireTableCellUpdated(row, col);
389: }
390:
391: private void log(String s) {
392: System.out.println("DDTableModel" + s); //NOI18N
393: }
394:
395: } // DDTableModel
396:
397: class CellText extends JTextField {
398:
399: private static final long serialVersionUID = 2674682216176560005L;
400:
401: public CellText(DDTable table) {
402: super ();
403: addKeyListener(table);
404: getAccessibleContext().setAccessibleName(this .getText()); // NOI18N
405: getAccessibleContext().setAccessibleDescription(
406: NbBundle.getMessage(DDTable.class, "ACSD_ipcell")); // NOI18N
407: }
408: }
409: }
|