1 19 20 26 package org.netbeans.modules.web.wizards; 27 28 import java.awt.Color ; 29 import java.awt.Component ; 30 import java.awt.Dimension ; 31 import java.awt.FontMetrics ; 32 import java.awt.Graphics ; 33 import java.awt.event.KeyEvent ; 34 import java.awt.event.KeyListener ; 35 36 import javax.swing.DefaultCellEditor ; 38 import javax.swing.BorderFactory ; 39 import javax.swing.JTable ; 40 import javax.swing.JTextField ; 41 import javax.swing.ListSelectionModel ; 42 import javax.swing.table.AbstractTableModel ; 43 import javax.swing.table.TableCellRenderer ; 44 import javax.swing.event.TableModelEvent ; 45 46 import org.openide.util.NbBundle; 47 48 class DDTable extends JTable implements KeyListener { 49 50 private static final boolean debug = false; 51 52 private String titleKey; 53 private Editable editable; 54 private String [] headers; 55 private final static int margin = 6; 56 57 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 [] headers, String titleKey) { 65 this(headers, titleKey, Editable.BOTH); 66 } 67 68 DDTable(String [] headers, String titleKey, Editable editable) { 69 70 super(new Object [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 (margin, margin)); 80 DefaultCellEditor dce = new DefaultCellEditor (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 [] 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 getColumnKey(int col) { 120 return headers[col]; 121 } 122 123 private void setColors(Editable editable) { 124 Color 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 147 148 149 153 public TableCellRenderer getCellRenderer(int row, int col) { 154 return super.getCellRenderer(row, col); 155 } 156 157 158 void setData(String name, String 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 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 179 public void paint(Graphics g) { 180 181 if(debug) log("::paint()"); 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"); FontMetrics fm = g.getFontMetrics(getFont()); 191 height = fm.getHeight() + margin; 194 if(height > rowHeight) rowHeight = height; 195 196 if(debug) log("\trow height is " + rowHeight); 198 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 c = getCellEditor(row, 0) 209 .getTableCellEditorComponent(this, getValueAt(row, 0), 210 true, row, 0); 211 if(c instanceof JTextField ) { 212 if(debug) log("\tTrying to request focus"); 213 ((JTextField )c).requestFocus(); 214 } 215 } 216 super.paint(g); 217 } 218 219 public void keyPressed(KeyEvent keyEvent) { 220 if(debug) log("\tKey pressed"); 221 } 222 223 public void keyReleased(KeyEvent keyEvent) { 224 225 if(debug) log("::keyReleased()"); 226 227 Object o = keyEvent.getSource(); 228 String s = null; 229 if(o instanceof JTextField ) { 230 if(debug) log("\tFound text field"); 231 s = ((JTextField )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 keyEvent) { 242 if(debug) log("\tKey typed"); 243 } 244 245 private void log(String s) { 246 System.out.println("DDTable" + s); } 248 249 250 class DDTableModel extends AbstractTableModel { 251 252 private String [] colheaders = null; 253 private Object [][] 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 [] headers, Editable editable) { 261 262 this.colheaders = headers; 263 this.editable = editable; 264 numCols = colheaders.length; 265 data = new Object [numRows][numCols]; 266 } 267 268 public String getColumnName(int col) { 269 String 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 getValueAt(int row, int col) { 277 return data[row][col]; 278 } 279 280 public int addRow(String [] values) { 281 282 Object [][] data2 = new Object [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()"); log("row is " + row); log("numRows is " + numRows); } 305 306 Object [][] data2 = new Object [numRows-1][numCols]; 307 int newRowIndex = 0; 308 for(int i=0; i<numRows; ++i) { 309 if(debug) log("\tExamining row " + i); if(i==row) continue; 311 if(debug) log("\tKeep this row"); data2[newRowIndex]=data[i]; 313 newRowIndex++; 314 if(debug) log("\tnewRowIndex is " + newRowIndex); } 316 data = data2; 317 numRows = --numRows; 318 } 319 320 void setData(String name, String value, int row) { 321 data[row][0] = name; 322 data[row][1] = value; 323 fireTableChanged(new TableModelEvent (this, row)); 324 } 325 326 public void setValueAt(Object value, int row, int col) { 327 328 if(debug) 329 log("::setValueAt(): value = " + value + " at " + row + ", " + col); 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) + "," + String.valueOf(j) + ": " + data[i][j]); } 340 } 341 } 342 fireTableCellUpdated(row, col); 344 } 345 346 private void log(String s) { 347 System.out.println("DDTableModel" + s); } 349 350 } 352 353 class CellText extends JTextField { 354 355 private static final long serialVersionUID = 2674682216176560005L; 356 357 public CellText(DDTable table) { 358 super(); 359 addKeyListener(table); 360 getAccessibleContext().setAccessibleName(this.getText()); getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DDTable.class, "ACSD_ipcell")); } 363 } 364 } 365 | Popular Tags |