1 19 package org.netbeans.modules.xml.tax.beans.editor; 20 21 import java.awt.Component ; 22 import java.awt.event.KeyAdapter ; 23 import java.awt.event.MouseAdapter ; 24 import java.awt.event.MouseEvent ; 25 import java.awt.event.KeyEvent ; 26 import java.beans.Customizer ; 27 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyChangeListener ; 29 import javax.swing.event.ListSelectionEvent ; 30 import javax.swing.event.TableModelEvent ; 31 import javax.swing.event.ListSelectionListener ; 32 import javax.swing.JPanel ; 33 import javax.swing.JLabel ; 34 import javax.swing.UIManager ; 35 import javax.swing.JTable ; 36 import javax.swing.ListSelectionModel ; 37 import javax.swing.DefaultCellEditor ; 38 import javax.swing.JTextField ; 39 import javax.swing.table.TableColumn ; 40 import javax.swing.table.JTableHeader ; 41 import javax.swing.table.AbstractTableModel ; 42 import javax.swing.table.TableCellRenderer ; 43 import javax.swing.table.TableColumnModel ; 44 45 import org.netbeans.tax.TreeNamedObjectMap; 46 import org.netbeans.tax.TreeAttribute; 47 import org.netbeans.tax.TreeName; 48 import org.netbeans.tax.TreeException; 49 50 import org.netbeans.modules.xml.tax.beans.TreeObjectListProxyListener; 51 import org.netbeans.modules.xml.tax.beans.Lib; 52 import org.netbeans.modules.xml.tax.util.TAXUtil; 53 54 62 public class TreeElementAttributeListCustomizer extends JPanel implements Customizer , PropertyChangeListener { 63 64 65 private static final long serialVersionUID = 1071471854210683733L; 66 67 private String headerToolTip; 68 69 private final int COL_NAME = 0; 70 private final int COL_VALUE = 1; 71 private final int COL_COUNT = 2; 72 73 77 78 79 public TreeElementAttributeListCustomizer() { 80 81 headerToolTip = Util.THIS.getString("PROP_headerTooltip"); 82 initComponents (); 83 upButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_up")); downButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_down")); removeButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_remove")); addButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_add")); initAccessibility(); 89 90 attrTable.getTableHeader().setDefaultRenderer(new HeaderRenderer()); 92 93 final JTextField editorComponent = new JTextField (); 95 editorComponent.getCaret().setVisible(true); 96 final DefaultCellEditor singleClickEditor = new DefaultCellEditor (editorComponent); 97 singleClickEditor.setClickCountToStart(1); 98 attrTable.setDefaultEditor(String .class, singleClickEditor); 99 100 attrTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 102 ListSelectionModel rowsm = attrTable.getSelectionModel(); 103 rowsm.addListSelectionListener (new ListSelectionListener (){ 104 public void valueChanged (ListSelectionEvent e) { 105 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\n#=- TreeElementAttributeListCustomizer::ListSelectionListener.valueChanged: event = " + e); 106 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("#=- event.getValueIsAdjusting() = " + e.getValueIsAdjusting()); 107 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("#=- event.getFirstIndex() = " + e.getFirstIndex()); 108 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("#=- event.getLastIndex() = " + e.getLastIndex()); 109 110 if (e.getValueIsAdjusting()) 111 return; 112 ListSelectionModel lsm = (ListSelectionModel )e.getSource(); 113 114 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("#=- event.getSource() = " + lsm); 115 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("#=- selectionModel.isSelectionEmpty() = " + lsm.isSelectionEmpty()); 116 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("#=- attrTable.getSelectedRow() = " + attrTable.getSelectedRow()); 117 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("#=- attrTable.getRowCount() = " + attrTable.getRowCount()); 118 119 if ( lsm.isSelectionEmpty() || 120 ( attrTable.getRowCount() == 0 ) ) { 121 upButton.setEnabled (false); 122 downButton.setEnabled (false); 123 removeButton.setEnabled (false); 124 } else { 125 upButton.setEnabled (attrTable.getSelectedRow() > 0); 126 downButton.setEnabled (attrTable.getSelectedRow() < (numRows() - 1)); 127 removeButton.setEnabled (true); 128 } 129 } 130 }); 131 132 attrTable.getTableHeader().addMouseListener(new MouseAdapter () { 134 public void mouseClicked(MouseEvent e) { 135 TableColumnModel colModel = attrTable.getColumnModel(); 136 int columnIndex = colModel.getColumnIndexAtX(e.getX()); 137 if(columnIndex < 0) return; 139 int modelIndex = colModel.getColumn(columnIndex).getModelIndex(); 140 if (modelIndex < 0) return; 142 AttlistTableModel tm = (AttlistTableModel) attrTable.getModel(); 144 tm.sortByColumn(columnIndex); 145 } 146 }); 147 148 } 149 150 151 private int numRows(){ 153 return attrTable.getModel().getRowCount(); 154 } 155 156 private int numCols(){ 158 return attrTable.getModel().getColumnCount(); 159 } 160 161 165 170 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 172 173 tableScrollPane = new javax.swing.JScrollPane (); 174 attrTable = new javax.swing.JTable (); 175 upButton = new javax.swing.JButton (); 176 downButton = new javax.swing.JButton (); 177 addButton = new javax.swing.JButton (); 178 removeButton = new javax.swing.JButton (); 179 180 setLayout(new java.awt.GridBagLayout ()); 181 182 setPreferredSize(new java.awt.Dimension (350, 230)); 183 attrTable.setPreferredScrollableViewportSize(new java.awt.Dimension (200, 150)); 184 tableScrollPane.setViewportView(attrTable); 185 186 gridBagConstraints = new java.awt.GridBagConstraints (); 187 gridBagConstraints.gridx = 0; 188 gridBagConstraints.gridy = 0; 189 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 190 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 191 gridBagConstraints.weightx = 1.0; 192 gridBagConstraints.weighty = 1.0; 193 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 194 add(tableScrollPane, gridBagConstraints); 195 196 upButton.setText(Util.THIS.getString ("TEXT_element_attributelist_up")); 197 upButton.setEnabled(false); 198 upButton.addActionListener(new java.awt.event.ActionListener () { 199 public void actionPerformed(java.awt.event.ActionEvent evt) { 200 upButtonActionPerformed(evt); 201 } 202 }); 203 204 gridBagConstraints = new java.awt.GridBagConstraints (); 205 gridBagConstraints.gridx = 1; 206 gridBagConstraints.gridy = 0; 207 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 208 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 209 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 210 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 11); 211 add(upButton, gridBagConstraints); 212 213 downButton.setText(Util.THIS.getString ("TEXT_element_attributelist_down")); 214 downButton.setEnabled(false); 215 downButton.addActionListener(new java.awt.event.ActionListener () { 216 public void actionPerformed(java.awt.event.ActionEvent evt) { 217 downButtonActionPerformed(evt); 218 } 219 }); 220 221 gridBagConstraints = new java.awt.GridBagConstraints (); 222 gridBagConstraints.gridx = 1; 223 gridBagConstraints.gridy = 1; 224 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 225 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 226 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 227 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 11); 228 add(downButton, gridBagConstraints); 229 230 addButton.setText(Util.THIS.getString ("TEXT_element_attributelist_add")); 231 addButton.addActionListener(new java.awt.event.ActionListener () { 232 public void actionPerformed(java.awt.event.ActionEvent evt) { 233 addButtonActionPerformed(evt); 234 } 235 }); 236 237 gridBagConstraints = new java.awt.GridBagConstraints (); 238 gridBagConstraints.gridx = 1; 239 gridBagConstraints.gridy = 2; 240 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 241 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 242 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 243 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 11); 244 add(addButton, gridBagConstraints); 245 246 removeButton.setText(Util.THIS.getString ("TEXT_element_attributelist_remove")); 247 removeButton.setEnabled(false); 248 removeButton.addActionListener(new java.awt.event.ActionListener () { 249 public void actionPerformed(java.awt.event.ActionEvent evt) { 250 removeButtonActionPerformed(evt); 251 } 252 }); 253 254 gridBagConstraints = new java.awt.GridBagConstraints (); 255 gridBagConstraints.gridx = 1; 256 gridBagConstraints.gridy = 3; 257 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 258 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 259 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 260 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 261 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 11); 262 add(removeButton, gridBagConstraints); 263 264 } 266 269 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { 271 AttlistTableModel tm = (AttlistTableModel) attrTable.getModel(); 272 tm.addRow(); 273 int actualIndex = numRows() - 1; 274 attrTable.getSelectionModel().setSelectionInterval(actualIndex, actualIndex); 275 } 277 280 private void upButtonActionPerformed(java.awt.event.ActionEvent evt) { 282 AttlistTableModel tm = (AttlistTableModel) attrTable.getModel(); 283 int index = attrTable.getSelectedRow(); 284 if (index > 0) { 285 tm.moveRow(index, index - 1); 286 attrTable.getSelectionModel().setSelectionInterval(index - 1,index - 1); 287 } 288 } 290 293 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { 295 AttlistTableModel tm = (AttlistTableModel) attrTable.getModel(); 296 297 int sel = attrTable.getSelectedRow(); 298 if (sel > -1) { 299 tm.removeRow(sel); 300 if (numRows() > 0) { 301 if (sel <= numRows() - 1) 302 attrTable.getSelectionModel().setSelectionInterval(sel,sel); 303 else 304 attrTable.getSelectionModel().setSelectionInterval(sel - 1, sel - 1); 305 } else removeButton.setEnabled(false); 306 } 307 } 309 312 private void downButtonActionPerformed(java.awt.event.ActionEvent evt) { 314 AttlistTableModel tm = (AttlistTableModel) attrTable.getModel(); 315 int index = attrTable.getSelectedRow(); 316 if (index > -1 && index < numRows() - 1) { 317 tm.moveRow(index, index + 1); 318 attrTable.getSelectionModel().setSelectionInterval(index + 1,index + 1); 319 } 320 } 322 323 private javax.swing.JTable attrTable; 325 private javax.swing.JButton addButton; 326 private javax.swing.JScrollPane tableScrollPane; 327 private javax.swing.JButton downButton; 328 private javax.swing.JButton upButton; 329 private javax.swing.JButton removeButton; 330 332 333 private TreeNamedObjectMap peer; 334 private AttlistTableModel tableModel; 335 336 338 public void setObject (Object obj) { 339 peer = (TreeNamedObjectMap) obj; 340 tableModel = new AttlistTableModel(); 341 attrTable.setModel(tableModel); 342 345 346 TableColumn column = null; 347 for (int i = 0; i < COL_COUNT; i++) { 348 column = attrTable.getColumnModel().getColumn (i); 349 } 351 352 updateView(); 353 354 TreeObjectListProxyListener proxy = new TreeObjectListProxyListener(peer); 355 proxy.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, proxy)); 356 } 357 358 359 360 public void propertyChange(final PropertyChangeEvent e) { 361 if (e.getSource() == null) 362 return; 363 364 updateView(); 365 } 366 367 368 private void updateView () { 369 tableModel.fireTableDataChanged(); 370 } 371 372 376 380 private class RowKeyListener extends KeyAdapter { 381 382 383 private JTable table; 384 385 386 390 public RowKeyListener (JTable table) { 391 this.table = table; 392 } 393 394 395 399 401 public void keyReleased (KeyEvent e) { 402 if (e.getKeyCode() == KeyEvent.VK_DELETE) { 404 tableModel.removeRow (table.getSelectedRow()); 405 } 408 } 409 } 410 411 412 416 419 private class AttlistTableModel extends AbstractTableModel { 420 421 422 private static final long serialVersionUID = 1567143493464383838L; 423 424 boolean ascending = true; 425 int SortedColumn = -1; 426 int indexes[]; 427 428 432 437 public AttlistTableModel () { 438 super(); 439 } 440 441 442 public int getRowCount () { 443 return peer.size(); 444 } 445 446 447 448 public int getColumnCount () { 449 return COL_COUNT; 450 } 451 452 453 public Class getColumnClass (int index) { 454 return String .class; 455 } 456 457 458 472 474 public void addRow () { 475 476 TreeAttribute attr = (TreeAttribute) Lib.createAttributeDialog(true); 477 if (attr != null) { 478 boolean toSet = true; 479 TreeAttribute oldAttribute = (TreeAttribute) peer.get (attr.getTreeName()); 480 if ( oldAttribute != null ) { 481 toSet = Lib.confirmAction 482 (Util.THIS.getString ("MSG_replace_attribute", attr.getQName())); 483 } 484 if ( toSet ) { 485 peer.add (attr); 486 SortedColumn = -1; 487 fireTableStructureChanged(); 488 } 489 } 490 499 } 500 501 503 public void removeRow (int row) { 504 peer.remove(row); 505 fireTableStructureChanged(); 506 } 507 508 510 public void moveRow (int fromIndex, int toIndex) { 511 try { 512 peer.switchObjects (fromIndex, toIndex); 513 514 SortedColumn = -1; 515 fireTableStructureChanged(); 516 } catch (TreeException exc) { 517 TAXUtil.notifyTreeException (exc); 518 } 519 }; 520 521 523 public Object getValueAt (int row, int column) { 524 TreeAttribute attr = (TreeAttribute) peer.get(row); 525 switch (column) { 526 case COL_NAME: 527 return attr.getQName(); 528 case COL_VALUE: 529 return attr.getValue(); 530 default: 531 return null; 532 } 533 } 534 535 537 public void setValueAt (Object val, int row, int column) { 538 TreeAttribute attr = (TreeAttribute) peer.get (row); 539 try { 540 if (column == COL_NAME) { 541 542 String attrName = (String ) val; 543 544 boolean toSet = true; 545 TreeAttribute oldAttribute = (TreeAttribute) peer.get (new TreeName (attrName)); 546 if ( attr != oldAttribute ) { 547 if ( oldAttribute != null ) { 548 toSet = Lib.confirmAction 549 (Util.THIS.getString ("MSG_replace_attribute", attrName)); 550 } 551 } 552 if ( toSet ) { 553 attr.setQName (attrName); 554 } 555 556 } else if (column == COL_VALUE) { 557 attr.setValue ((String ) val); 558 } 559 } catch (TreeException exc) { 560 TAXUtil.notifyTreeException (exc); 561 } 562 } 563 564 566 public String getColumnName (int column) { 567 switch (column) { 568 case COL_NAME: 569 return Util.THIS.getString ("NAME_column_name"); 570 case COL_VALUE: 571 return Util.THIS.getString ("NAME_column_value"); 572 default: 573 return ""; } 575 } 576 577 580 public boolean isCellEditable (int rowIndex, int columnIndex) { 581 return true; 582 } 583 584 586 public void sortByColumn (int column) { 587 588 if (SortedColumn == column) 589 ascending = !ascending; 590 else { 591 SortedColumn = column; 592 ascending = true; 593 } 594 595 indexes = new int[getRowCount()]; 596 for (int row = 0; row < getRowCount(); row++) { 597 indexes[row] = row; 598 } 599 600 n2sort(column); 601 602 int[] indx = new int[indexes.length]; 603 for (int row = 0; row < indx.length; row++) { 604 indx[indexes[row]] = row; 605 } 606 607 try { 608 peer.reorder (indx); 609 fireTableChanged(new TableModelEvent (this)); 610 } catch (TreeException exc) { 611 TAXUtil.notifyTreeException (exc); 612 } 613 } 614 615 617 private void n2sort (int col) { 618 for (int i = 0; i < getRowCount(); i++) { 619 for (int j = i + 1; j < getRowCount(); j++) { 620 if (compare(indexes[i], indexes[j], col) == -1) { 621 int tmp = indexes[i]; 623 indexes[i] = indexes[j]; 624 indexes[j] = tmp; 625 } 626 } 627 } 628 } 629 630 632 private int compare (int row1, int row2, int col) { 633 int result = compareRowsByColumn(row1, row2, col); 634 if (result != 0) { 635 return ascending ? -result : result; 636 } 637 return 0; 638 } 639 640 642 private int compareRowsByColumn (int row1, int row2, int column) { 643 644 Class type = getColumnClass(column); 645 646 Object o1 = getValueAt(row1, column); 647 Object o2 = getValueAt(row2, column); 648 649 650 652 if (o1 == null && o2 == null) { 653 return 0; 654 } else if (o1 == null) { 655 return -1; 656 } else if (o2 == null) { 657 return 1; 658 } 659 660 662 if (type == String .class) { 663 String s1 = (String ) getValueAt(row1, column); 664 String s2 = (String ) getValueAt(row2, column); 665 int result = s1.compareTo(s2); 666 667 if (result < 0) { 668 return -1; 669 } else if (result > 0) { 670 return 1; 671 } else { 672 return 0; 673 } 674 } 675 return 0; 676 } 677 678 } 680 681 685 private class HeaderRenderer extends JLabel implements TableCellRenderer { 686 687 688 private static final long serialVersionUID =-3658206203140258583L; 689 690 public HeaderRenderer() { 691 super(); 692 setHorizontalAlignment(JLabel.LEFT); 693 setBorder(UIManager.getBorder("TableHeader.cellBorder")); setToolTipText(headerToolTip); 695 } 696 697 public Component getTableCellRendererComponent (JTable table, Object value, 698 boolean isSelected, boolean hasFocus,int row, int column) { 699 700 if (table != null) { 701 JTableHeader header = table.getTableHeader(); 702 if (header != null) { 703 setForeground(header.getForeground()); 704 setBackground(header.getBackground()); 705 setFont(header.getFont()); 706 } 707 } 708 709 AttlistTableModel tm = (AttlistTableModel)table.getModel(); 710 if (column == tm.SortedColumn) 711 setIcon(tm.ascending ? new javax.swing.ImageIcon (getClass().getResource("down.gif")): new javax.swing.ImageIcon (getClass().getResource("up.gif"))); else setIcon(new javax.swing.ImageIcon (getClass().getResource("no.gif"))); 715 setText((value == null) ? "" : value.toString()); return this; 717 } 718 } 720 722 public void initAccessibility(){ 723 724 this.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_TreeElementAttributeListCustomizer")); 725 726 addButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_addButton")); 727 removeButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_removeButton")); 728 upButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_upButton")); 729 downButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_downButton")); 730 731 attrTable.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_attrTable")); 732 attrTable.getAccessibleContext().setAccessibleName(Util.THIS.getString("ACSN_attrTable")); 733 } 734 735 } 736 | Popular Tags |