1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.awt.*; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.MouseAdapter ; 28 import java.awt.event.MouseEvent ; 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeListener ; 31 import java.text.MessageFormat ; 32 import javax.swing.*; 33 import javax.swing.border.LineBorder ; 34 import javax.swing.event.*; 35 import javax.swing.plaf.BorderUIResource ; 36 import javax.swing.plaf.BorderUIResource.BevelBorderUIResource; 37 import javax.swing.plaf.basic.BasicHTML ; 38 import javax.swing.table.*; 39 40 import org.openide.DialogDescriptor; 41 import org.openide.NotifyDescriptor; 42 import org.openide.DialogDisplayer; 43 import org.openide.util.NbBundle; 44 import org.openide.util.WeakListeners; 45 import org.openide.windows.TopComponent; 46 47 48 53 public class BundleEditPanel extends JPanel implements PropertyChangeListener { 54 55 56 private PropertiesDataObject obj; 57 58 59 private DocumentListener listener; 60 61 62 private static TableViewSettings settings; 63 64 65 static final long serialVersionUID =-843810329041244483L; 66 67 68 69 public BundleEditPanel(final PropertiesDataObject obj, PropertiesTableModel propTableModel) { 70 this.obj = obj; 71 72 initComponents(); 73 initAccessibility(); 74 initSettings(); 75 76 table.setColumnModel(new TableViewColumnModel()); 78 79 table.setModel(propTableModel); 81 82 JTextField textField = new JTextField(); 84 textField.getDocument().putProperty("filterNewlines", Boolean.FALSE); textField.setBorder(new LineBorder (Color.black)); 89 textField.getAccessibleContext().setAccessibleName(NbBundle.getBundle(BundleEditPanel.class).getString("ACSN_CellEditor")); 90 textField.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(BundleEditPanel.class).getString("ACSD_CellEditor")); 91 listener = new ModifiedListener(); 92 table.setDefaultEditor(PropertiesTableModel.StringPair.class, 93 new PropertiesTableCellEditor(textField, textComment, textValue, valueLabel, listener)); 94 95 table.setDefaultRenderer(PropertiesTableModel.StringPair.class, new TableViewRenderer()); 97 98 updateAddButton(); 99 100 table.addPropertyChangeListener(new PropertyChangeListener () { 102 public void propertyChange(PropertyChangeEvent evt) { 103 if (evt.getPropertyName().equals("tableCellEditor")) { updateEnabled(); 105 } else if (evt.getPropertyName().equals("model")) { updateAddButton(); 107 } 108 } 109 }); 110 111 table.getTableHeader().addMouseListener(new MouseAdapter () { 113 public void mouseClicked(MouseEvent e) { 114 TableColumnModel colModel = table.getColumnModel(); 115 int columnModelIndex = colModel.getColumnIndexAtX(e.getX()); 116 if(columnModelIndex < 0) 118 return; 119 int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex(); 120 if (modelIndex < 0) 122 return; 123 obj.getBundleStructure().sort(modelIndex); 124 } 125 }); 126 127 128 table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { 129 public void valueChanged(ListSelectionEvent evt) { 130 SwingUtilities.invokeLater(new Runnable () { 131 public void run() { 132 updateSelection(); 133 } 134 }); 135 } 136 }); 137 138 } 140 141 142 protected void stopEditing() { 143 if (!table.isEditing()) return; 144 TableCellEditor cellEdit = table.getCellEditor(); 145 if (cellEdit != null) 146 cellEdit.stopCellEditing(); 147 } 148 149 150 private void updateEnabled() { 151 textValue.setEditable(table.isEditing()); 153 textValue.setEnabled(table.isEditing()); 154 if (table.isEditing()) { 156 PropertiesTableModel.StringPair sp = (PropertiesTableModel.StringPair)table.getCellEditor().getCellEditorValue(); 157 textComment.setEditable(sp.isCommentEditable()); 158 textComment.setEnabled(sp.isCommentEditable()); 159 } else { 160 textComment.setEditable(false); 161 textComment.setEnabled(false); 162 } 163 } 164 165 private void updateSelection() { 166 int row = table.getSelectedRow(); 167 int column = table.getSelectedColumn(); 168 BundleStructure structure = obj.getBundleStructure(); 169 removeButton.setEnabled((row >= 0) && (!structure.isReadOnly())); 170 String value; 171 String comment; 172 if (column == -1) { 173 value = ""; comment = ""; } else if (column == 0) { 176 Element.ItemElem elem = structure.getItem(0, row); 177 value = structure.keyAt(row); 178 comment = (elem != null) ? elem.getComment() : ""; } else { 180 Element.ItemElem elem = structure.getItem(column-1, row); 181 if (elem != null) { 182 value = elem.getValue(); 183 comment = elem.getComment(); 184 } else { 185 value = ""; comment = ""; } 188 } 189 textValue.getDocument().removeDocumentListener(listener); 190 textComment.getDocument().removeDocumentListener(listener); 191 textValue.setText(value); 192 textComment.setText(comment); 193 textValue.getDocument().addDocumentListener(listener); 194 textComment.getDocument().addDocumentListener(listener); 195 } 196 197 private void updateAddButton() { 198 addButton.setEnabled(!obj.getBundleStructure().isReadOnly()); 199 } 200 201 202 public JTable getTable() { 203 return table; 204 } 205 206 207 208 private void initSettings() { 209 settings = TableViewSettings.getDefault(); 210 211 settings.addPropertyChangeListener( 213 WeakListeners.propertyChange(this, settings) 214 ); 215 } 216 217 220 public void propertyChange(PropertyChangeEvent evt) { 221 BundleEditPanel.this.repaint(); 223 } 224 225 private void initAccessibility() { 226 this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(BundleEditPanel.class).getString("ACS_BundleEditPanel")); 227 228 table.getAccessibleContext().setAccessibleName(NbBundle.getBundle(BundleEditPanel.class).getString("ACSN_CTL_Table")); 229 table.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(BundleEditPanel.class).getString("ACSD_CTL_Table")); 230 textValue.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(BundleEditPanel.class).getString("ACS_CTL_TEXTVALUE")); 231 addButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(BundleEditPanel.class).getString("ACS_LBL_AddPropertyButton")); 232 textComment.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(BundleEditPanel.class).getString("ACS_CTL_TEXTCOMMENT")); 233 autoResizeCheck.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(BundleEditPanel.class).getString("ACS_CTL_AutoResize")); 234 removeButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(BundleEditPanel.class).getString("ACS_LBL_RemovePropertyButton")); 235 } 236 237 public boolean requestFocusInWindow() { 238 return table.requestFocusInWindow(); 239 } 240 241 246 private void initComponents() { 248 java.awt.GridBagConstraints gridBagConstraints; 249 250 tablePanel = new javax.swing.JPanel (); 251 scrollPane = new javax.swing.JScrollPane (); 252 table = new BundleTable(); 253 valuePanel = new javax.swing.JPanel (); 254 commentLabel = new javax.swing.JLabel (); 255 jScrollPane2 = new javax.swing.JScrollPane (); 256 textComment = new javax.swing.JTextArea (); 257 valueLabel = new javax.swing.JLabel (); 258 jScrollPane3 = new javax.swing.JScrollPane (); 259 textValue = new javax.swing.JTextArea (); 260 buttonPanel = new javax.swing.JPanel (); 261 addButton = new javax.swing.JButton (); 262 removeButton = new javax.swing.JButton (); 263 autoResizeCheck = new javax.swing.JCheckBox (); 264 265 setFocusCycleRoot(true); 266 setLayout(new java.awt.GridBagLayout ()); 267 268 tablePanel.setLayout(new java.awt.GridBagLayout ()); 269 270 table.setCellSelectionEnabled(true); 271 scrollPane.setViewportView(table); 272 273 gridBagConstraints = new java.awt.GridBagConstraints (); 274 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 275 gridBagConstraints.weightx = 1.0; 276 gridBagConstraints.weighty = 1.0; 277 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 11); 278 tablePanel.add(scrollPane, gridBagConstraints); 279 280 gridBagConstraints = new java.awt.GridBagConstraints (); 281 gridBagConstraints.gridwidth = 2; 282 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 283 gridBagConstraints.weightx = 1.0; 284 gridBagConstraints.weighty = 1.0; 285 add(tablePanel, gridBagConstraints); 286 287 valuePanel.setLayout(new java.awt.GridBagLayout ()); 288 289 commentLabel.setLabelFor(textComment); 290 org.openide.awt.Mnemonics.setLocalizedText(commentLabel, NbBundle.getBundle(BundleEditPanel.class).getString("LBL_CommentLabel")); gridBagConstraints = new java.awt.GridBagConstraints (); 292 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 293 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 294 gridBagConstraints.insets = new java.awt.Insets (11, 11, 0, 0); 295 valuePanel.add(commentLabel, gridBagConstraints); 296 297 textComment.setEditable(false); 298 textComment.setLineWrap(true); 299 textComment.setRows(3); 300 textComment.setEnabled(false); 301 jScrollPane2.setViewportView(textComment); 302 303 gridBagConstraints = new java.awt.GridBagConstraints (); 304 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 305 gridBagConstraints.weightx = 1.0; 306 gridBagConstraints.weighty = 1.0; 307 gridBagConstraints.insets = new java.awt.Insets (11, 11, 0, 0); 308 valuePanel.add(jScrollPane2, gridBagConstraints); 309 310 valueLabel.setLabelFor(textValue); 311 org.openide.awt.Mnemonics.setLocalizedText(valueLabel, NbBundle.getBundle(BundleEditPanel.class).getString("LBL_ValueLabel")); gridBagConstraints = new java.awt.GridBagConstraints (); 313 gridBagConstraints.gridx = 0; 314 gridBagConstraints.gridy = 1; 315 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 316 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 317 gridBagConstraints.insets = new java.awt.Insets (11, 11, 11, 0); 318 valuePanel.add(valueLabel, gridBagConstraints); 319 320 textValue.setEditable(false); 321 textValue.setLineWrap(true); 322 textValue.setRows(3); 323 textValue.setEnabled(false); 324 jScrollPane3.setViewportView(textValue); 325 326 gridBagConstraints = new java.awt.GridBagConstraints (); 327 gridBagConstraints.gridx = 1; 328 gridBagConstraints.gridy = 1; 329 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 330 gridBagConstraints.weightx = 1.0; 331 gridBagConstraints.weighty = 1.0; 332 gridBagConstraints.insets = new java.awt.Insets (7, 11, 11, 0); 333 valuePanel.add(jScrollPane3, gridBagConstraints); 334 335 gridBagConstraints = new java.awt.GridBagConstraints (); 336 gridBagConstraints.gridx = 0; 337 gridBagConstraints.gridy = 1; 338 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 339 gridBagConstraints.weightx = 1.0; 340 add(valuePanel, gridBagConstraints); 341 342 buttonPanel.setLayout(new java.awt.GridBagLayout ()); 343 344 org.openide.awt.Mnemonics.setLocalizedText(addButton, NbBundle.getBundle(BundleEditPanel.class).getString("LBL_AddPropertyButton")); addButton.addActionListener(new java.awt.event.ActionListener () { 346 public void actionPerformed(java.awt.event.ActionEvent evt) { 347 addButtonActionPerformed(evt); 348 } 349 }); 350 gridBagConstraints = new java.awt.GridBagConstraints (); 351 gridBagConstraints.gridx = 0; 352 gridBagConstraints.gridy = 1; 353 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 354 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH; 355 gridBagConstraints.weighty = 1.0; 356 gridBagConstraints.insets = new java.awt.Insets (11, 11, 0, 11); 357 buttonPanel.add(addButton, gridBagConstraints); 358 359 org.openide.awt.Mnemonics.setLocalizedText(removeButton, NbBundle.getBundle(BundleEditPanel.class).getString("LBL_RemovePropertyButton")); removeButton.setEnabled(false); 361 removeButton.addActionListener(new java.awt.event.ActionListener () { 362 public void actionPerformed(java.awt.event.ActionEvent evt) { 363 removeButtonActionPerformed(evt); 364 } 365 }); 366 gridBagConstraints = new java.awt.GridBagConstraints (); 367 gridBagConstraints.gridx = 0; 368 gridBagConstraints.gridy = 2; 369 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 370 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH; 371 gridBagConstraints.insets = new java.awt.Insets (5, 11, 11, 11); 372 buttonPanel.add(removeButton, gridBagConstraints); 373 374 autoResizeCheck.setSelected(true); 375 org.openide.awt.Mnemonics.setLocalizedText(autoResizeCheck, NbBundle.getBundle(BundleEditPanel.class).getString("CTL_AutoResize")); autoResizeCheck.addActionListener(new java.awt.event.ActionListener () { 377 public void actionPerformed(java.awt.event.ActionEvent evt) { 378 autoResizeCheckActionPerformed(evt); 379 } 380 }); 381 gridBagConstraints = new java.awt.GridBagConstraints (); 382 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 383 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 11); 384 buttonPanel.add(autoResizeCheck, gridBagConstraints); 385 386 gridBagConstraints = new java.awt.GridBagConstraints (); 387 gridBagConstraints.gridx = 1; 388 gridBagConstraints.gridy = 1; 389 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 390 add(buttonPanel, gridBagConstraints); 391 } 393 private void autoResizeCheckActionPerformed(java.awt.event.ActionEvent evt) { if(autoResizeCheck.isSelected()) 395 table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 396 else 397 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 398 } 400 private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) { int selectedRow = table.getSelectedRow(); 402 403 if(selectedRow == -1) 404 return; 405 406 stopEditing(); 407 String key = ((PropertiesTableModel.StringPair)table.getModel().getValueAt(selectedRow, 0)).getValue(); 408 409 if(key == null) 411 return; 412 413 NotifyDescriptor.Confirmation msg = new NotifyDescriptor.Confirmation( 414 MessageFormat.format( 415 NbBundle.getBundle(BundleEditPanel.class).getString("MSG_DeleteKeyQuestion"), 416 new Object [] { key } 417 ), 418 NotifyDescriptor.OK_CANCEL_OPTION 419 ); 420 421 if (DialogDisplayer.getDefault().notify(msg).equals(NotifyDescriptor.OK_OPTION)) { 422 try { 423 obj.getOpenSupport().atomicUndoRedoFlag = new Object (); 425 426 for (int i=0; i < obj.getBundleStructure().getEntryCount(); i++) { 427 PropertiesFileEntry entry = obj.getBundleStructure().getNthEntry(i); 428 if (entry != null) { 429 PropertiesStructure ps = entry.getHandler().getStructure(); 430 if (ps != null) { 431 ps.deleteItem(key); 432 } 433 } 434 } 435 } finally { 436 obj.getOpenSupport().atomicUndoRedoFlag = null; 438 } 439 } 440 } 442 private void addButtonActionPerformed (java.awt.event.ActionEvent evt) { stopEditing(); 444 445 final Dialog[] dialog = new Dialog[1]; 446 final Element.ItemElem item = new Element.ItemElem( 447 null, 448 new Element.KeyElem(null, ""), new Element.ValueElem(null, ""), new Element.CommentElem(null, "") ); 452 final JPanel panel = new PropertyPanel(item); 453 454 DialogDescriptor dd = new DialogDescriptor( 455 panel, 456 NbBundle.getBundle(BundleEditPanel.class).getString("CTL_NewPropertyTitle"), 457 true, 458 DialogDescriptor.OK_CANCEL_OPTION, 459 DialogDescriptor.OK_OPTION, 460 new ActionListener () { 461 public void actionPerformed(ActionEvent evt2) { 462 if(evt2.getSource() == DialogDescriptor.OK_OPTION) { 464 dialog[0].setVisible(false); 465 dialog[0].dispose(); 466 467 final String key = item.getKey(); 468 String value = item.getValue(); 469 String comment = item.getComment(); 470 471 boolean keyAdded = false; 472 473 try { 474 obj.getOpenSupport().atomicUndoRedoFlag = new Object (); 476 477 for (int i=0; i < obj.getBundleStructure().getEntryCount(); i++) { 479 PropertiesFileEntry entry = obj.getBundleStructure().getNthEntry(i); 480 481 if (entry != null && !entry.getHandler().getStructure().addItem(key, value, comment)) { 482 NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 483 MessageFormat.format( 484 NbBundle.getBundle(BundleEditPanel.class).getString("MSG_KeyExists"), 485 new Object [] { 486 item.getKey(), 487 Util.getLocaleLabel(entry) 488 } 489 ), 490 NotifyDescriptor.ERROR_MESSAGE); 491 DialogDisplayer.getDefault().notify(msg); 492 } else { 493 keyAdded = true; 494 } 495 } 496 } finally { 497 obj.getOpenSupport().atomicUndoRedoFlag = null; 499 } 500 501 if(keyAdded) { 502 PropertiesRequestProcessor.getInstance().post(new Runnable () { 508 public void run() { 509 int rowIndex = obj.getBundleStructure().getKeyIndexByName(key); 511 512 if((rowIndex != -1)) { 513 final int row = rowIndex; 514 final int column = 1; 516 SwingUtilities.invokeLater(new Runnable () { 517 public void run() { 518 if(table.getAutoscrolls()) { 520 Rectangle cellRect = table.getCellRect(row, column, false); 521 if (cellRect != null) { 522 table.scrollRectToVisible(cellRect); 523 } 524 } 525 526 table.getColumnModel().getSelectionModel().setSelectionInterval(column, column); 528 table.getSelectionModel().setSelectionInterval(row, row); 529 530 table.requestFocusInWindow(); 531 table.editCellAt(row, column); 532 } 533 }); 534 } 535 } 536 }); 537 } 538 539 } else if (evt2.getSource() == DialogDescriptor.CANCEL_OPTION) { 541 dialog[0].setVisible(false); 542 dialog[0].dispose(); 543 } 544 } 545 } 546 ); 547 548 dialog[0] = DialogDisplayer.getDefault().createDialog(dd); 549 dialog[0].setVisible(true); 550 } 552 553 private javax.swing.JButton addButton; 555 private javax.swing.JCheckBox autoResizeCheck; 556 private javax.swing.JPanel buttonPanel; 557 private javax.swing.JLabel commentLabel; 558 private javax.swing.JScrollPane jScrollPane2; 559 private javax.swing.JScrollPane jScrollPane3; 560 private javax.swing.JButton removeButton; 561 private javax.swing.JScrollPane scrollPane; 562 private javax.swing.JTable table; 563 private javax.swing.JPanel tablePanel; 564 private javax.swing.JTextArea textComment; 565 private javax.swing.JTextArea textValue; 566 private javax.swing.JLabel valueLabel; 567 private javax.swing.JPanel valuePanel; 568 570 571 572 private class TableViewHeaderRenderer extends DefaultTableCellRenderer { 573 574 private int column; 575 576 577 public Component getTableCellRendererComponent(JTable table, Object value, 578 boolean isSelected, boolean hasFocus, int row, int column) { 579 580 this.column = column; 581 582 if (table != null) { 583 JTableHeader header = table.getTableHeader(); 584 if (header != null) { 585 this.setForeground(header.getForeground()); 586 this.setBackground(header.getBackground()); 587 this.setFont(header.getFont()); 588 } 589 } 590 591 setText((value == null) ? "" : value.toString()); this.setBorder(UIManager.getBorder("TableHeader.cellBorder")); return this; 594 } 595 596 597 protected void paintComponent(Graphics g) { 598 super.paintComponent(g); 599 600 int sortIndex = table.convertColumnIndexToView(obj.getBundleStructure().getSortIndex()); 601 602 if(column == sortIndex ) { 604 605 Color oldColor = g.getColor(); 606 607 FontMetrics fm = g.getFontMetrics(); 608 Rectangle space = fm.getStringBounds(" ", g).getBounds(); Rectangle mark = fm.getStringBounds("\u25B2", g).getBounds(); Rectangle bounds = this.getBounds(); 611 612 Insets insets = this.getInsets(); 613 614 BevelBorderUIResource bevelUI = (BevelBorderUIResource)BorderUIResource.getLoweredBevelBorderUIResource(); 615 616 boolean ascending = obj.getBundleStructure().getSortOrder(); 617 618 int x1, x2, x3, y1, y2, y3; 619 620 if(ascending) { 621 x1 = space.width + mark.width/2; 623 x2 = space.width; 624 x3 = space.width + mark.width; 625 626 y1 = bounds.y + insets.top+2; 627 y2 = bounds.y + bounds.height - insets.bottom-2; 628 y3 = y2; 629 } else { 630 x1 = space.width; 632 x2 = space.width + mark.width; 633 x3 = space.width + mark.width/2; 634 635 y1 = bounds.y + insets.top + 2; 636 y2 = y1; 637 y3 = bounds.y + bounds.height - insets.bottom - 2; 638 } 639 640 g.setColor(bevelUI.getShadowOuterColor(this)); 643 if(ascending) 644 g.drawLine(x1, y1, x2, y2); 645 else 646 g.drawPolyline(new int[] {x2, x1, x3}, new int[] {y2, y1, y3}, 3); 647 648 g.setColor(bevelUI.getShadowInnerColor(this)); 650 if(ascending) 651 g.drawLine(x1, y1+1, x2+1, y2-1); 652 else 653 g.drawPolyline(new int[] {x2-1, x1+1, x3}, new int[] {y2+1, y1+1, y3-1}, 3); 654 655 g.setColor(bevelUI.getHighlightOuterColor(this)); 657 if(ascending) 658 g.drawPolyline(new int[] {x1, x3, x2}, new int[] {y1, y3, y2}, 3); 659 else 660 g.drawLine(x2, y2, x3, y3); 661 662 g.setColor(bevelUI.getHighlightInnerColor(this)); 664 if(ascending) 665 g.drawPolyline(new int[] {x1, x3-1, x2+1}, new int[] {y1+1, y3-1, y2-1}, 3); 666 else 667 g.drawLine(x2-1, y2+1, x3, y3-1); 668 669 g.setColor(oldColor); 670 } 671 } 672 } 674 675 679 private class TableViewColumnModel extends DefaultTableColumnModel { 680 681 private AncestorListener ancestorListener; 682 683 684 private final TableCellRenderer headerRenderer = new TableViewHeaderRenderer(); 685 686 687 public void addColumn(TableColumn aColumn) { 688 if (aColumn == null) { 689 throw new IllegalArgumentException ("Object is null"); } 691 692 tableColumns.addElement(aColumn); 693 aColumn.addPropertyChangeListener(this); 694 695 adjustColumnWidths(); 697 698 aColumn.setHeaderRenderer(headerRenderer); 702 703 fireColumnAdded(new TableColumnModelEvent(this, 0, 705 getColumnCount() - 1)); 706 } 707 708 710 private void adjustColumnWidths() { 711 Rectangle screenBounds = org.openide.util.Utilities.getUsableScreenBounds(); 713 int columnWidth = screenBounds.width / 10; 714 715 int totalWidth = 0; 717 TopComponent tc = (TopComponent)SwingUtilities.getAncestorOfClass(TopComponent.class, table); 718 if(tc != null) { 719 totalWidth = tc.getBounds().width; 720 } else { 721 if(ancestorListener == null) { 722 table.addAncestorListener(ancestorListener = new AncestorListener() { 723 724 public void ancestorAdded(AncestorEvent evt) { 725 if(evt.getAncestor() instanceof TopComponent) { 726 adjustColumnWidths(); 727 table.removeAncestorListener(ancestorListener); 728 ancestorListener = null; 729 } 730 } 731 732 733 public void ancestorMoved(AncestorEvent evt) { 734 } 735 736 737 public void ancestorRemoved(AncestorEvent evt) { 738 } 739 }); 740 } 741 } 742 743 totalWidth -= scrollPane.getInsets().left + scrollPane.getInsets().right + 12 + 11; 747 748 int remainder = 0; 750 751 if(totalWidth > 0) { 754 int computedColumnWidth = totalWidth / table.getColumnCount(); 755 if(computedColumnWidth > columnWidth) { 756 columnWidth = computedColumnWidth - table.getColumnModel().getColumnMargin(); 757 remainder = totalWidth % table.getColumnCount(); 758 } 759 } 760 761 for (int i = 0; i < table.getColumnCount(); i++) { 763 TableColumn column = table.getColumnModel().getColumn(i); 764 765 if(i==0) { 767 column.setPreferredWidth(columnWidth + remainder); 769 column.setWidth(columnWidth + remainder); 770 } else { 771 column.setPreferredWidth(columnWidth); 773 column.setWidth(columnWidth); 774 } 775 } 776 777 recalcWidthCache(); 779 780 table.revalidate(); 782 783 table.getTableHeader().repaint(); 785 } 786 } 788 789 790 private class TableViewRenderer extends DefaultTableCellRenderer { 791 792 public Component getTableCellRendererComponent(JTable table, 793 Object value, boolean isSelected, boolean hasFocus, int row, int column) { 794 795 if(value==null) return this; 796 797 PropertiesTableModel.StringPair sp = (PropertiesTableModel.StringPair)value; 798 799 setFont(settings.getFont()); 800 801 if(hasFocus) { 802 setBorder(UIManager.getBorder("Table.focusCellHighlightBorder") ); } else { 804 setBorder(noFocusBorder); 805 } 806 807 String text = null; 808 809 if(sp.getValue() != null) { 810 text = sp.getValue(); 811 } 812 813 if(BasicHTML.isHTMLString(text)) { text = " " + text; } 817 818 setValue(text == null ? "" : text); 820 if(sp.isKeyType()) 822 setBackground(settings.getKeyBackground()); 823 else { 824 if( sp.getValue() != null) 825 setBackground(settings.getValueBackground()); 826 else 827 setBackground(settings.getShadowColor()); 828 } 829 830 if(sp.isKeyType()) 832 setForeground(settings.getKeyColor()); 833 else 834 setForeground(settings.getValueColor()); 835 836 Color back = getBackground(); 838 boolean colorMatch = (back != null) && (back.equals(table.getBackground()) ) && table.isOpaque(); 839 setOpaque(!colorMatch); 840 841 return this; 842 } 843 844 845 protected void paintComponent(Graphics g) { 846 super.paintComponent(g); 847 848 if(FindPerformer.getFindPerformer(BundleEditPanel.this.table).isHighlightSearch()) { 850 String text = getText(); 851 String findString = FindPerformer.getFindPerformer(BundleEditPanel.this.table).getFindString(); 852 853 if(text != null && text.length()>0 && findString != null && findString.length()>0) { 855 int index = 0; 856 int width = (int)g.getFontMetrics().getStringBounds(findString, g).getWidth(); 857 858 Color oldColor = g.getColor(); 859 while((index = text.indexOf(findString, index)) >= 0) { 861 862 int x = (int)g.getFontMetrics().getStringBounds(text.substring(0, index), g).getWidth()+this.getInsets().left; 863 864 g.setColor(settings.getHighlightBackground()); 865 g.fillRect(x, 0, width, g.getClipBounds().height); 866 867 g.setColor(settings.getHighlightColor()); 868 g.drawString(findString, x, -(int)g.getFontMetrics().getStringBounds(findString, g).getY()); 869 870 index += findString.length(); 871 } 872 g.setColor(oldColor); 874 } 875 } 876 } 877 } 879 880 881 883 static class BundleTable extends JTable { 884 885 public BundleTable(){ 886 super(); 887 this.setRowHeight(getCellFontHeight() + 1); 888 } 889 890 895 public void removeEditorSilent() { 896 TableCellEditor editor = getCellEditor(); 897 if(editor != null) { 898 editor.removeCellEditorListener(this); 899 900 if (editorComp != null) { 902 remove(editorComp); 903 } 904 905 Rectangle cellRect = getCellRect(editingRow, editingColumn, false); 906 907 setCellEditor(null); 908 setEditingColumn(-1); 909 setEditingRow(-1); 910 editorComp = null; 911 912 repaint(cellRect); 913 } 914 } 915 916 private int getCellFontHeight() { 917 Font cellFont = UIManager.getFont("TextField.font"); 918 if (cellFont != null) { 919 FontMetrics fm = getFontMetrics(cellFont); 920 if (fm != null) { 921 return fm.getHeight(); 922 } 923 } 924 return 14; 925 } 926 927 } 929 private class ModifiedListener implements DocumentListener { 930 931 public void changedUpdate(DocumentEvent e) { 932 documentModified(); 933 } 934 935 public void insertUpdate(DocumentEvent e) { 936 documentModified(); 937 } 938 939 public void removeUpdate(DocumentEvent e) { 940 documentModified(); 941 } 942 943 private void documentModified() { 944 obj.setModified(true); 945 } 946 947 } 948 949 } 950 | Popular Tags |