1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.io.Serializable ; 25 import javax.swing.event.TableModelEvent ; 26 import javax.swing.table.AbstractTableModel ; 27 import javax.swing.table.TableColumn ; 28 import javax.swing.JTable ; 29 30 import org.openide.util.NbBundle; 31 import org.openide.util.WeakListeners; 32 33 34 43 public class PropertiesTableModel extends AbstractTableModel { 44 45 46 static final long serialVersionUID = -7882925922830244768L; 47 48 49 private BundleStructure structure; 50 51 52 private PropertyBundleListener bundleListener; 53 54 58 public PropertiesTableModel(BundleStructure structure) { 59 super(); 60 this.structure = structure; 61 62 bundleListener = new TablePropertyBundleListener(); 64 65 structure.addPropertyBundleListener( 66 (PropertyBundleListener) WeakListeners.create(PropertyBundleListener.class, bundleListener, structure) 67 ); 68 69 } 70 71 72 public Class getColumnClass(int columnIndex) { 73 return StringPair.class; 74 } 75 76 77 public int getRowCount() { 78 return structure.getKeyCount(); 79 } 80 81 82 public int getColumnCount() { 83 return structure.getEntryCount() + 1; 84 } 85 86 87 public Object getValueAt(int row, int column) { 88 BundleStructure bs = structure; 89 90 if(column == 0) 91 return stringPairForKey(row); else { 94 Element.ItemElem item; 96 try { 97 item = bs.getItem(column - 1, bs.keyAt(row)); 98 } catch (ArrayIndexOutOfBoundsException aie) { 99 item = null; 100 } 101 return stringPairForValue(item); 102 } 103 } 104 105 106 private StringPair stringPairForKey(int row) { 107 BundleStructure bs = structure; 108 Element.ItemElem item = bs.getItem(0, bs.keyAt(row)); 109 StringPair sp; 110 if (item == null) 111 sp = new StringPair("", bs.keyAt(row), true); else 113 sp = new StringPair(item.getComment(), bs.keyAt(row), true); 114 115 if (structure.getEntryCount() > 1) 116 sp.setCommentEditable(false); 117 118 return sp; 119 } 120 121 122 private StringPair stringPairForValue(Element.ItemElem item) { 123 if (item == null) 124 return new StringPair(null, null); 126 else 127 return new StringPair(item.getComment(), item.getValue()); 128 } 129 130 133 public String getColumnName(int column) { 134 String leading; 135 136 if(column == structure.getSortIndex()) 138 leading = " "; else 141 leading = " "; 143 if(column == 0) 144 return leading+NbBundle.getBundle(PropertiesTableModel.class).getString("LAB_KeyColumnLabel"); 145 else { 146 if(structure.getEntryCount() == 1) 147 return leading+NbBundle.getBundle(PropertiesTableModel.class).getString("LBL_ColumnValue"); 148 else { 149 PropertiesFileEntry entry = structure.getNthEntry(column - 1); 150 return entry == null ? "" : leading+Util.getLocaleLabel(entry); } 152 } 153 } 154 155 156 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 157 if (aValue.equals(getValueAt(rowIndex, columnIndex))) { 159 return; 160 } 161 162 if (columnIndex == 0) { 165 BundleStructure bs = structure; 166 String oldValue = (String )bs.keyAt(rowIndex); 167 if (oldValue == null) { 168 return; 169 } 170 String newValue = ((StringPair)aValue).getValue(); 171 if (newValue == null) { return; 174 } else { 175 for (int i=0; i < structure.getEntryCount(); i++) { 177 PropertiesFileEntry entry = structure.getNthEntry(i); 178 if (entry != null) { 179 PropertiesStructure ps = entry.getHandler().getStructure(); 180 if (ps != null) { 181 if (!oldValue.equals(newValue)) { 183 ps.renameItem(oldValue, newValue); 184 structure.sort(-1); 187 } 188 if (i == 0) { 190 Element.ItemElem item = ps.getItem(newValue); 191 if (item != null && ((StringPair)aValue).isCommentEditable()) { 192 if (!item.getComment().equals(((StringPair)aValue).getComment())) 194 item.setComment(((StringPair)aValue).getComment()); 195 } 196 } 197 } 198 } 199 } 200 } 201 } else { 202 PropertiesFileEntry entry = structure.getNthEntry(columnIndex - 1); 204 String key = structure.keyAt(rowIndex); 205 if (entry != null && key != null) { 206 PropertiesStructure ps = entry.getHandler().getStructure(); 207 if (ps != null) { 208 Element.ItemElem item = ps.getItem(key); 209 if (item != null) { 210 item.setValue(((StringPair)aValue).getValue()); 211 item.setComment(((StringPair)aValue).getComment()); 212 structure.sort(-1); 215 } else { 216 if ((((StringPair)aValue).getValue().length() > 0) || (((StringPair)aValue).getComment().length() > 0)) { 217 ps.addItem(key, ((StringPair)aValue).getValue(), ((StringPair)aValue).getComment()); 218 structure.sort(-1); 221 } 222 } 223 } 224 } 225 } 226 } 227 228 230 public boolean isCellEditable(int rowIndex, int columnIndex) { 231 if (columnIndex == 0) { 232 return !structure.isReadOnly(); 233 } else { 234 PropertiesFileEntry entry = structure.getNthEntry(columnIndex-1); 235 return entry.getFile().canWrite(); 236 } 237 } 238 239 240 public void fireTableColumnChanged(int index) { 241 int columnModelIndex = index; 242 243 Object list[] = listenerList.getListenerList(); 245 for (int i = 0; i < list.length; i++) { 246 if (list[i] instanceof JTable ) { 247 JTable jt = (JTable )list[i]; 248 try { 249 TableColumn column = jt.getColumnModel().getColumn(index); 250 columnModelIndex = column.getModelIndex(); 251 column.setHeaderValue(jt.getModel().getColumnName(columnModelIndex)); 252 } catch (ArrayIndexOutOfBoundsException abe) { 253 } 255 jt.getTableHeader().repaint(); 256 } 257 } 258 fireTableChanged(new TableModelEvent (this, 0, getRowCount() - 1, columnModelIndex)); 259 } 260 261 262 public String toString() { 263 StringBuffer result = new StringBuffer (); 264 result.append("------------------------------ TABLE MODEL DUMP -----------------------\n"); for (int row = 0; row < getRowCount(); row ++) { 266 for (int column = 0; column < getColumnCount(); column ++) { 267 StringPair sp = (StringPair)getValueAt(row, column); 268 result.append("[" + sp.getValue() + "]"); if (column == 0) 270 result.append(" : "); else 272 if (column == getColumnCount() - 1) 273 result.append("\n"); else 275 result.append(","); } 277 } 278 result.append("---------------------------- END TABLE MODEL DUMP ---------------------\n"); return result.toString(); 280 } 281 282 283 private void cancelEditingInTables(CancelSelector can) { 284 285 Object list[] = listenerList.getListenerList(); 286 for(int i = 0; i < list.length; i++) { 287 if(list[i] instanceof BundleEditPanel.BundleTable) { 288 BundleEditPanel.BundleTable jt = (BundleEditPanel.BundleTable)list[i]; 289 if (can.doCancelEditing(jt.getEditingRow(), jt.getEditingColumn())) { 290 jt.removeEditorSilent(); 291 } 292 } 293 } 294 } 295 296 297 private CancelSelector getDefaultCancelSelector() { 298 return new CancelSelector() { 299 300 public boolean doCancelEditing(int row, int column) { 301 return (row >= 0 && row < getRowCount() && column >= 0 && column < getColumnCount()); 302 } 303 }; 304 } 305 306 307 308 private static interface CancelSelector { 309 310 public boolean doCancelEditing(int row, int column); 311 } 313 314 315 private class TablePropertyBundleListener implements PropertyBundleListener { 316 public void bundleChanged(final PropertyBundleEvent evt) { 317 if (java.awt.EventQueue.isDispatchThread()) { 320 doBundleChanged(evt); 321 } 322 else { 323 java.awt.EventQueue.invokeLater(new Runnable () { 324 public void run() { 325 doBundleChanged(evt); 326 } 327 }); 328 } 329 } 330 331 private void doBundleChanged(PropertyBundleEvent evt) { 332 int changeType = evt.getChangeType(); 333 334 if(changeType == PropertyBundleEvent.CHANGE_STRUCT) { 335 337 340 343 Object [] list = PropertiesTableModel.super.listenerList.getListenerList(); 344 for(int i = 0; i < list.length; i++) { 345 if(list[i] instanceof JTable ) { 346 ((JTable )list[i]).setModel(new PropertiesTableModel(PropertiesTableModel.this.structure)); 349 } 350 } 351 } else if(changeType == PropertyBundleEvent.CHANGE_ALL) { 352 cancelEditingInTables(getDefaultCancelSelector()); 354 355 Object [] list = PropertiesTableModel.super.listenerList.getListenerList(); 357 for (int i = 0; i < list.length; i++) { 358 if (list[i] instanceof JTable ) { 359 JTable jt = (JTable )list[i]; 360 361 for (int j=0 ; j < jt.getColumnModel().getColumnCount(); j++) { 362 TableColumn column = jt.getColumnModel().getColumn(j); 363 column.setHeaderValue(jt.getModel().getColumnName(column.getModelIndex())); 364 } 365 } 366 } 367 368 fireTableDataChanged(); 369 } else if(changeType == PropertyBundleEvent.CHANGE_FILE) { 370 final int index = structure.getEntryIndexByFileName(evt.getEntryName()); 372 if (index == -1) { 373 if (Boolean.getBoolean("netbeans.debug.exceptions")) (new Exception ("Changed file not found")).printStackTrace(); return; 376 } 377 378 cancelEditingInTables(new CancelSelector() { 379 public boolean doCancelEditing(int row, int column) { 380 if (!(row >= 0 && row < getRowCount() && column >= 0 && column < getColumnCount())) 381 return false; 382 return (column == index + 1); 383 } 384 }); 385 386 fireTableColumnChanged(index + 1); 387 } else if(changeType == PropertyBundleEvent.CHANGE_ITEM) { 388 final int index2 = structure.getEntryIndexByFileName(evt.getEntryName()); 390 final int keyIndex = structure.getKeyIndexByName(evt.getItemName()); 391 392 if(index2 == -1 || keyIndex == -1) { 393 if(Boolean.getBoolean("netbeans.debug.exceptions")) (new Exception ("Changed file not found")).printStackTrace(); 396 return; 397 } 398 399 cancelEditingInTables(new CancelSelector() { 400 public boolean doCancelEditing(int row, int column) { 401 if (!(row >= 0 && row < getRowCount() && column >= 0 && column < getColumnCount())) 402 return false; 403 return (column == index2 + 1 && row == keyIndex); 404 } 405 }); 406 407 fireTableCellUpdated(keyIndex, index2 + 1); 408 } 409 } 410 } 412 413 417 static class StringPair implements Serializable { 418 419 420 private String comment; 421 422 423 private String value; 424 425 426 private boolean keyType; 427 428 429 private boolean commentEditable; 430 431 432 static final long serialVersionUID =-463968846283787181L; 433 434 435 436 public StringPair() { 437 this (null, "", false); } 439 440 441 public StringPair(String v) { 442 this (null, v, true); 443 } 444 445 446 public StringPair(String c, String v) { 447 this (c, v, false); 448 } 449 450 451 public StringPair(String c, String v, boolean kt) { 452 comment = c; 453 value = v; 454 keyType = kt; 455 commentEditable = true; 456 } 457 458 459 460 public String getComment() { 461 return comment; 462 } 463 464 465 public String getValue() { 466 return value; 467 } 468 469 470 public boolean equals(Object obj) { 471 if(obj == null || !(obj instanceof StringPair)) 472 return false; 473 474 StringPair compared = (StringPair)obj; 475 476 478 if(comment == null && compared.getComment() != null) 480 return false; 481 482 String str1 = comment; 483 String str2 = compared.getComment(); 484 485 if(!str1.equals(str2)) 486 return false; 487 488 if(value == null && compared.getValue() != null) 490 return false; 491 492 str1 = value; 493 str2 = compared.getValue(); 494 495 return str1.equals(str2); 496 } 497 498 499 public String toString() { 500 return value; 501 } 502 503 504 public boolean isKeyType () { 505 return keyType; 506 } 507 508 509 public boolean isCommentEditable() { 510 return commentEditable; 511 } 512 513 514 public void setCommentEditable(boolean newEditable) { 515 commentEditable = newEditable; 516 } 517 } 519 } 520 | Popular Tags |