KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > DDTable


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  * DDTable.java
22  *
23  * @author Ana von Klopp
24  * @version
25  */

26 package org.netbeans.modules.web.wizards;
27
28 import java.awt.Color JavaDoc;
29 import java.awt.Component JavaDoc;
30 import java.awt.Dimension JavaDoc;
31 import java.awt.FontMetrics JavaDoc;
32 import java.awt.Graphics JavaDoc;
33 import java.awt.event.KeyEvent JavaDoc;
34 import java.awt.event.KeyListener JavaDoc;
35
36 //import javax.swing.AbstractCellEditor;
37
import javax.swing.DefaultCellEditor JavaDoc;
38 import javax.swing.BorderFactory JavaDoc;
39 import javax.swing.JTable JavaDoc;
40 import javax.swing.JTextField JavaDoc;
41 import javax.swing.ListSelectionModel JavaDoc;
42 import javax.swing.table.AbstractTableModel JavaDoc;
43 import javax.swing.table.TableCellRenderer JavaDoc;
44 import javax.swing.event.TableModelEvent JavaDoc;
45
46 import org.openide.util.NbBundle;
47
48 class DDTable extends JTable JavaDoc implements KeyListener JavaDoc {
49
50     private static final boolean debug = false;
51
52     private String JavaDoc titleKey;
53     private Editable editable;
54     private String JavaDoc[] headers;
55     private final static int margin = 6;
56
57     // Handle resizing for larger fonts
58
private boolean fontChanged = true;
59     private boolean addedRow = true;
60     private int rowHeight = 23;
61     
62     private static final long serialVersionUID = -155464225493968935L;
63     
64     DDTable(String JavaDoc[] headers, String JavaDoc titleKey) {
65     this(headers, titleKey, Editable.BOTH);
66     }
67     
68     DDTable(String JavaDoc[] headers, String JavaDoc titleKey, Editable editable) {
69     
70     super(new Object JavaDoc[0][headers.length], headers);
71
72     this.headers = headers;
73     this.titleKey = titleKey;
74     this.editable = editable;
75
76     setModel(new DDTableModel(headers, editable));
77     setColors(editable);
78     this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
79     setIntercellSpacing(new Dimension JavaDoc(margin, margin));
80     DefaultCellEditor JavaDoc dce = new DefaultCellEditor JavaDoc(new CellText(this));
81     dce.setClickCountToStart(1);
82     getColumnModel().getColumn(0).setCellEditor(dce);
83     getColumnModel().getColumn(1).setCellEditor(dce);
84     }
85
86     void setEditable(Editable editable) {
87     this.editable = editable;
88     setColors(editable);
89     }
90
91     Editable getEditable() {
92     return this.editable;
93     }
94
95     int addRow(String JavaDoc[] values) {
96     int i = ((DDTableModel)getModel()).addRow(values);
97     if(i == 0) fontChanged = true;
98     addedRow = true;
99     this.invalidate();
100     return i;
101     }
102
103     void removeRow(int row) {
104         if (isEditing())
105             getCellEditor().cancelCellEditing();
106         
107     ((DDTableModel)getModel()).removeRow(row);
108     this.invalidate();
109         
110         int maxSelectedRow = getRowCount() - 1;
111         if (getSelectedRow() > maxSelectedRow) {
112             if (maxSelectedRow >= 0)
113                 setRowSelectionInterval(maxSelectedRow, maxSelectedRow);
114             else
115                 clearSelection();
116         }
117     }
118
119     String JavaDoc getColumnKey(int col) {
120     return headers[col];
121     }
122
123     private void setColors(Editable editable) {
124     Color JavaDoc bg;
125     this.setBorder(BorderFactory.createLoweredBevelBorder());
126     if (editable == Editable.NEITHER) {
127         bg = this.getBackground().darker();
128     } else {
129         bg = Color.white;
130     }
131     this.setBackground(bg);
132     }
133
134     /**
135      * Override the getter for the cell editors, so that customized
136      * cell editors will show up.
137
138     public TableCellEditor getCellEditor(int row, int col) {
139     TableCellEditor e = super.getCellEditor(row, col);
140     Component c = e.getTableCellEditorComponent(this,
141                             getValueAt(row, col),
142                             true, row, col);
143     c.addKeyListener(this);
144     return e;
145     }
146      */

147
148
149     /**
150      * Override the getter for the cell editors, so that customized
151      * cell editors will show up.
152      */

153     public TableCellRenderer JavaDoc getCellRenderer(int row, int col) {
154     return super.getCellRenderer(row, col);
155     }
156
157
158     // This method is used by the edit button of the InitParamTable
159
void setData(String JavaDoc name, String JavaDoc value, int row) {
160     if(getEditingRow() == row) {
161         int col = getEditingColumn();
162         getCellEditor(row, col).cancelCellEditing();
163     }
164     ((DDTableModel)getModel()).setData(name, value, row);
165     }
166
167     /**
168      * Checks whether the cells are editable
169      */

170     public boolean isCellEditable(int row, int col) {
171     if(editable == Editable.NEITHER) { return false; }
172     if(editable == Editable.VALUE && col == 0) { return false; }
173     else return true;
174     }
175
176     /**
177      * When paint is first invoked, we set the rowheight based on the
178      * size of the font. */

179     public void paint(Graphics JavaDoc g) {
180
181     if(debug) log("::paint()"); //NOI18N
182

183     if (fontChanged) {
184         
185         if(debug) log("\tFont changed");
186         fontChanged = false;
187
188         int height = 0;
189         if(debug) log("\tGetting font height"); //NOI18N
190
FontMetrics JavaDoc fm = g.getFontMetrics(getFont());
191         // Add 2 for button border
192
// height = fm.getHeight() + 2 + margin;
193
height = fm.getHeight() + margin;
194         if(height > rowHeight) rowHeight = height;
195
196         if(debug) log("\trow height is " + rowHeight); //NOI18N
197

198         //triggers paint, just return afterwards
199
this.setRowHeight(rowHeight);
200         return;
201     }
202
203     if(addedRow) {
204         addedRow = false;
205         if(debug) log("\tAdded row");
206         int row = getModel().getRowCount() - 1;
207         this.editCellAt(row, 0);
208         Component JavaDoc c = getCellEditor(row, 0)
209         .getTableCellEditorComponent(this, getValueAt(row, 0),
210                         true, row, 0);
211         if(c instanceof JTextField JavaDoc) {
212         if(debug) log("\tTrying to request focus");
213         ((JTextField JavaDoc)c).requestFocus();
214         }
215     }
216     super.paint(g);
217     }
218     
219     public void keyPressed(KeyEvent JavaDoc keyEvent) {
220     if(debug) log("\tKey pressed");
221     }
222         
223     public void keyReleased(KeyEvent JavaDoc keyEvent) {
224
225     if(debug) log("::keyReleased()");
226
227     Object JavaDoc o = keyEvent.getSource();
228     String JavaDoc s = null;
229     if(o instanceof JTextField JavaDoc) {
230         if(debug) log("\tFound text field");
231         s = ((JTextField JavaDoc)o).getText().trim();
232     }
233     
234     int row = getEditingRow();
235     int col = getEditingColumn();
236     if(debug) log("\trow=" + row + ", col=" + col);
237
238     setValueAt(s, row, col);
239     }
240         
241     public void keyTyped (KeyEvent JavaDoc keyEvent) {
242     if(debug) log("\tKey typed");
243     }
244
245     private void log(String JavaDoc s) {
246     System.out.println("DDTable" + s); //NOI18N
247
}
248
249
250     class DDTableModel extends AbstractTableModel JavaDoc {
251     
252     private String JavaDoc[] colheaders = null;
253     private Object JavaDoc[][] data = null;
254     private Editable editable;
255     private int numCols;
256     private int numRows=0;
257     
258         private static final long serialVersionUID = -5044296029944667379L;
259         
260     DDTableModel(String JavaDoc[] headers, Editable editable) {
261
262         this.colheaders = headers;
263         this.editable = editable;
264         numCols = colheaders.length;
265         data = new Object JavaDoc[numRows][numCols];
266     }
267     
268     public String JavaDoc getColumnName(int col) {
269         String JavaDoc key = "LBL_".concat(colheaders[col]);
270         return NbBundle.getMessage(DDTable.class, key);
271     }
272     
273     public int getRowCount() { return data.length; }
274     public int getColumnCount() { return numCols; }
275
276     public Object JavaDoc getValueAt(int row, int col) {
277         return data[row][col];
278     }
279
280     public int addRow(String JavaDoc[] values) {
281
282         Object JavaDoc[][] data2 = new Object JavaDoc[numRows+1][numCols];
283         int i=0, j=0;
284
285         if(numRows > 0) {
286         for(j=0; j<numRows; ++j)
287             data2[j] = data[j];
288         }
289
290         for(i=0; i<values.length; ++i)
291         data2[j][i] = values[i];
292
293         data = data2;
294         numRows++;
295         return j;
296     }
297
298     public void removeRow(int row) {
299
300         if(debug) {
301         log("::removeRow()"); //NOI18N
302
log("row is " + row); //NOI18N
303
log("numRows is " + numRows); //NOI18N
304
}
305
306         Object JavaDoc[][] data2 = new Object JavaDoc[numRows-1][numCols];
307         int newRowIndex = 0;
308         for(int i=0; i<numRows; ++i) {
309         if(debug) log("\tExamining row " + i); //NOI18N
310
if(i==row) continue;
311         if(debug) log("\tKeep this row"); //NOI18N
312
data2[newRowIndex]=data[i];
313         newRowIndex++;
314         if(debug) log("\tnewRowIndex is " + newRowIndex); //NOI18N
315
}
316         data = data2;
317         numRows = --numRows;
318     }
319
320     void setData(String JavaDoc name, String JavaDoc value, int row) {
321         data[row][0] = name;
322         data[row][1] = value;
323         fireTableChanged(new TableModelEvent JavaDoc(this, row));
324     }
325
326     public void setValueAt(Object JavaDoc value, int row, int col) {
327
328         if(debug)
329         log("::setValueAt(): value = " + value + //NOI18N
330
" at " + row + ", " + col); //NOI18N
331

332         data[row][col] = value;
333         
334         if(debug) {
335         for(int i=0; i<data.length; ++i) {
336             for(int j=0; j<numCols; ++j) {
337             log("\t" + String.valueOf(i) + "," + //NOI18N
338
String.valueOf(j) + ": " + data[i][j]); //NOI18N
339
}
340         }
341         }
342         // Commenting this out since the value is set twice.
343
fireTableCellUpdated(row, col);
344     }
345
346     private void log(String JavaDoc s) {
347         System.out.println("DDTableModel" + s); //NOI18N
348
}
349
350     } // DDTableModel
351

352
353     class CellText extends JTextField JavaDoc {
354         
355         private static final long serialVersionUID = 2674682216176560005L;
356         
357     public CellText(DDTable table) {
358         super();
359         addKeyListener(table);
360         getAccessibleContext().setAccessibleName(this.getText()); // NOI18N
361
getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DDTable.class, "ACSD_ipcell")); // NOI18N
362
}
363     }
364 }
365
Popular Tags