1 19 20 package org.netbeans.modules.form.editors2; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.util.ResourceBundle ; 25 26 27 import javax.swing.*; 28 import javax.swing.border.EmptyBorder ; 29 import javax.swing.event.*; 30 import javax.swing.table.*; 31 32 import org.openide.awt.Mnemonics; 33 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor; 34 import org.openide.util.NbBundle; 35 36 37 41 public class CustomTableModelEditor extends JPanel implements EnhancedCustomPropertyEditor { 42 43 private boolean isChangingTableModel = false; 44 45 private ChangeListener changeListener = new TabChangeListener(); 46 private ListSelectionListener selectionListener = new SelectionListener(); 47 48 private JTabbedPane tabbedPane; 49 private JTable settingsTable; 50 private JTable defaultValuesTable; 51 private JTextField rowsField; 52 private JTextField columnsField; 53 private TableModelEditor.NbTableModel model; private TableModelEditor.NbTableModel titleModel; 56 private int stSelectedRow; 57 private int stSelectedColumn; 58 private int defSelectedRow; 59 private int defSelectedColumn; 60 61 private JButton insertSColBtn; 63 private JButton deleteSColBtn; 64 private JButton moveSColUpBtn; 65 private JButton moveSColDownBtn; 66 67 private JButton insertColBtn; 68 private JButton deleteColBtn; 69 private JButton moveColLeftBtn; 70 private JButton moveColRightBtn; 71 private JButton insertRowBtn; 72 private JButton deleteRowBtn; 73 private JButton moveRowUpBtn; 74 private JButton moveRowDownBtn; 75 76 private JButton addRowBtn; 77 private JButton removeRowBtn; 78 private JButton addColBtn; 79 private JButton removeColBtn; 80 81 static final int SETTINGS_TAB = 0; 82 static final int DEFAULT_TAB = 1; 83 84 static final long serialVersionUID =8002510111948803668L; 85 86 public CustomTableModelEditor(TableModelEditor editor) { 87 ResourceBundle bundle = NbBundle.getBundle(CustomTableModelEditor.class); 88 89 model = new TableModelEditor.NbTableModel((TableModel)editor.getValue()); 90 91 setLayout(new BorderLayout(0, 2)); 92 setBorder(new EmptyBorder (6, 6, 6, 6)); 93 94 tabbedPane = new JTabbedPane(); 95 96 JLabel titleLabel = new JLabel(); 97 Mnemonics.setLocalizedText(titleLabel, bundle.getString("CTL_TableModelTitle")); titleLabel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_TableModelTitle")); titleLabel.setLabelFor(tabbedPane); 100 add(titleLabel, BorderLayout.NORTH); 101 102 JPanel tab = new JPanel(); 104 tab.setLayout(new BorderLayout(6, 6)); 105 tab.setBorder(new EmptyBorder (6, 2, 0, 2)); 106 107 settingsTable = new CustomJTable(); 108 109 JLabel titleLabel1 = new JLabel(); 110 Mnemonics.setLocalizedText(titleLabel1, bundle.getString("CTL_Title1")); titleLabel1.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Title1")); titleLabel1.setLabelFor(settingsTable); 113 tab.add(titleLabel1,BorderLayout.NORTH); 114 115 titleModel = new TableModelEditor.NbTableModel( 116 new String [] { 117 bundle.getString("CTL_Column"), bundle.getString("CTL_Title"), bundle.getString("CTL_Type"), bundle.getString("CTL_Editable") }, 122 new Class [] { 123 String .class, String .class, String .class, Boolean .class 124 }, 125 new boolean[] { 126 false, true, true, true 127 }, 128 model.getColumnCount() 129 ); 130 settingsTable.setModel(titleModel); 131 132 JComboBox comboBox = new JComboBox(); 133 comboBox.addItem("Object"); comboBox.addItem("String"); comboBox.addItem("Boolean"); comboBox.addItem("Integer"); comboBox.addItem("Byte"); comboBox.addItem("Short"); comboBox.addItem("Long"); comboBox.addItem("Float"); comboBox.addItem("Double"); comboBox.setMaximumRowCount(9); 143 comboBox.setSelectedIndex(0); 144 145 TableColumn typeColumn = settingsTable.getColumn(bundle.getString("CTL_Type")); 146 typeColumn.setCellEditor(new DefaultCellEditor(comboBox)); 147 148 JScrollPane jscrollpane = new JScrollPane(settingsTable); 149 settingsTable.setSelectionMode(0); 150 settingsTable.setCellSelectionEnabled(true); 151 settingsTable.setRowSelectionAllowed(true); 152 settingsTable.setColumnSelectionAllowed(true); 153 settingsTable.setPreferredScrollableViewportSize(new Dimension(450, 200)); 154 tab.add(jscrollpane, BorderLayout.CENTER); 155 156 JPanel buttonsPanel = new JPanel(); 158 buttonsPanel.setLayout(new GridLayout(4, 0, 0, 3)); 159 160 insertSColBtn = new JButton(); 161 Mnemonics.setLocalizedText(insertSColBtn, bundle.getString("CTL_InsertColumn")); insertSColBtn.setToolTipText(bundle.getString("CTL_HINT_InsertColumn")); buttonsPanel.add(insertSColBtn); 164 insertSColBtn.addActionListener(new ActionListener() { 165 public void actionPerformed(ActionEvent evt) { 166 insertColumn(); 167 } 168 }); 169 170 deleteSColBtn = new JButton(); 171 Mnemonics.setLocalizedText(deleteSColBtn, bundle.getString("CTL_DeleteColumn")); deleteSColBtn.setToolTipText(bundle.getString("CTL_HINT_DeleteColumn")); buttonsPanel.add(deleteSColBtn); 174 deleteSColBtn.addActionListener(new ActionListener() { 175 public void actionPerformed(ActionEvent evt) { 176 removeColumn(); 177 } 178 }); 179 180 moveSColUpBtn = new JButton(); 181 Mnemonics.setLocalizedText(moveSColUpBtn, bundle.getString("CTL_MoveRowUp")); moveSColUpBtn.setToolTipText(bundle.getString("CTL_HINT_MoveColumnUp")); buttonsPanel.add(moveSColUpBtn); 184 moveSColUpBtn.addActionListener(new ActionListener() { 185 public void actionPerformed(ActionEvent evt) { 186 moveColumnLeft(); 187 } 188 }); 189 190 moveSColDownBtn = new JButton(); 191 Mnemonics.setLocalizedText(moveSColDownBtn, bundle.getString("CTL_MoveRowDown")); moveSColDownBtn.setToolTipText(bundle.getString("CTL_HINT_MoveColumnDown")); buttonsPanel.add(moveSColDownBtn); 194 moveSColDownBtn.addActionListener(new ActionListener() { 195 public void actionPerformed(ActionEvent evt) { 196 moveColumnRight(); 197 } 198 }); 199 200 JPanel padding = new JPanel(new BorderLayout()); 201 padding.add(buttonsPanel, BorderLayout.NORTH); 202 203 tab.add(padding, BorderLayout.EAST); 204 205 tabbedPane.addTab(bundle.getString("CTL_Title2"), tab); 207 tab = new JPanel(); 209 tab.setLayout(new BorderLayout(6, 6)); 210 tab.setBorder(new EmptyBorder (6, 2, 0, 2)); 211 212 defaultValuesTable = new CustomJTable(); 213 214 JLabel titleLabel2 = new JLabel(); 215 Mnemonics.setLocalizedText(titleLabel2, bundle.getString("CTL_DefaultTableValues")); titleLabel2.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_DefaultTableValues")); titleLabel2.setLabelFor(defaultValuesTable); 218 tab.add(titleLabel2,BorderLayout.NORTH); 219 220 defaultValuesTable.setModel(model); 221 model.alwaysEditable = true; 222 jscrollpane = new JScrollPane(defaultValuesTable); 223 defaultValuesTable.setSelectionMode(0); 224 defaultValuesTable.setCellSelectionEnabled(true); 225 defaultValuesTable.setRowSelectionAllowed(true); 226 defaultValuesTable.setColumnSelectionAllowed(true); 227 defaultValuesTable.setPreferredScrollableViewportSize(new Dimension(450, 80)); 228 defaultValuesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 229 230 tab.add(jscrollpane,BorderLayout.CENTER); 232 tabbedPane.addTab(bundle.getString("CTL_DefaultValues"), tab); 234 buttonsPanel = new JPanel(); 236 buttonsPanel.setLayout(new GridBagLayout()); 237 238 JPanel colButtonsPanel = new JPanel(new GridLayout(4, 0, 0, 3)); 239 JLabel colLabel = new JLabel(bundle.getString("CTL_Columns")); colLabel.setLabelFor(colButtonsPanel); 241 java.awt.GridBagConstraints gridBagConstraints1; 242 gridBagConstraints1 = new java.awt.GridBagConstraints (); 243 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 2, 0); 244 gridBagConstraints1.anchor = GridBagConstraints.WEST; 245 gridBagConstraints1.gridx = 0; 246 buttonsPanel.add(colLabel, gridBagConstraints1); 247 248 gridBagConstraints1 = new java.awt.GridBagConstraints (); 249 gridBagConstraints1.insets = new java.awt.Insets (0, 12, 5, 0); 250 gridBagConstraints1.anchor = GridBagConstraints.WEST; 251 gridBagConstraints1.gridx = 0; 252 buttonsPanel.add(colButtonsPanel, gridBagConstraints1); 253 254 insertColBtn = new JButton(); 255 Mnemonics.setLocalizedText(insertColBtn, bundle.getString("CTL_InsertColumn")); insertColBtn.setToolTipText(bundle.getString("CTL_HINT_InsertColumn")); colButtonsPanel.add(insertColBtn); 258 insertColBtn.addActionListener(new ActionListener() { 259 public void actionPerformed(ActionEvent evt) { 260 insertColumn(); 261 } 262 }); 263 264 deleteColBtn = new JButton(); 265 Mnemonics.setLocalizedText(deleteColBtn, bundle.getString("CTL_DeleteColumn")); deleteColBtn.setToolTipText(bundle.getString("CTL_HINT_DeleteColumn")); colButtonsPanel.add(deleteColBtn); 268 deleteColBtn.addActionListener(new ActionListener() { 269 public void actionPerformed(ActionEvent evt) { 270 removeColumn(); 271 } 272 }); 273 274 moveColLeftBtn = new JButton(); 275 Mnemonics.setLocalizedText(moveColLeftBtn, bundle.getString("CTL_MoveColumnLeft")); moveColLeftBtn.setToolTipText(bundle.getString("CTL_HINT_MoveColumnLeft")); colButtonsPanel.add(moveColLeftBtn); 278 moveColLeftBtn.addActionListener(new ActionListener() { 279 public void actionPerformed(ActionEvent evt) { 280 moveColumnLeft(); 281 } 282 }); 283 284 moveColRightBtn = new JButton(); 285 Mnemonics.setLocalizedText(moveColRightBtn, bundle.getString("CTL_MoveColumnRight")); moveColRightBtn.setToolTipText(bundle.getString("CTL_HINT_MoveColumnRight")); colButtonsPanel.add(moveColRightBtn); 288 moveColRightBtn.addActionListener(new ActionListener() { 289 public void actionPerformed(ActionEvent evt) { 290 moveColumnRight(); 291 } 292 }); 293 294 JPanel rowButtonsPanel = new JPanel(new GridLayout(4, 0, 0, 3)); 295 JLabel rowLabel = new JLabel(bundle.getString("CTL_Rows")); rowLabel.setLabelFor(rowButtonsPanel); 297 gridBagConstraints1 = new java.awt.GridBagConstraints (); 298 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 2, 0); 299 gridBagConstraints1.anchor = GridBagConstraints.WEST; 300 gridBagConstraints1.gridx = 0; 301 buttonsPanel.add(rowLabel, gridBagConstraints1); 302 303 gridBagConstraints1 = new java.awt.GridBagConstraints (); 304 gridBagConstraints1.insets = new java.awt.Insets (0, 12, 0, 0); 305 gridBagConstraints1.anchor = GridBagConstraints.WEST; 306 gridBagConstraints1.gridx = 0; 307 buttonsPanel.add(rowButtonsPanel, gridBagConstraints1); 308 309 insertRowBtn = new JButton(); 310 Mnemonics.setLocalizedText(insertRowBtn, bundle.getString("CTL_InsertRow")); insertRowBtn.setToolTipText(bundle.getString("CTL_HINT_InsertRow")); rowButtonsPanel.add(insertRowBtn); 313 insertRowBtn.addActionListener(new ActionListener() { 314 public void actionPerformed(ActionEvent evt) { 315 insertRow(); 316 } 317 }); 318 319 deleteRowBtn = new JButton(); 320 Mnemonics.setLocalizedText(deleteRowBtn, bundle.getString("CTL_DeleteRow")); deleteRowBtn.setToolTipText(bundle.getString("CTL_HINT_DeleteRow")); rowButtonsPanel.add(deleteRowBtn); 323 deleteRowBtn.addActionListener(new ActionListener() { 324 public void actionPerformed(ActionEvent evt) { 325 removeRow(); 326 } 327 }); 328 329 moveRowUpBtn = new JButton(); 330 Mnemonics.setLocalizedText(moveRowUpBtn, bundle.getString("CTL_MoveRowUp")); moveRowUpBtn.setToolTipText(bundle.getString("CTL_HINT_MoveRowUp")); rowButtonsPanel.add(moveRowUpBtn); 333 moveRowUpBtn.addActionListener(new ActionListener() { 334 public void actionPerformed(ActionEvent evt) { 335 moveRowUp(); 336 } 337 }); 338 339 moveRowDownBtn = new JButton(); 340 Mnemonics.setLocalizedText(moveRowDownBtn, bundle.getString("CTL_MoveRowDown")); moveRowDownBtn.setToolTipText(bundle.getString("CTL_HINT_MoveRowDown")); rowButtonsPanel.add(moveRowDownBtn); 343 moveRowDownBtn.addActionListener(new ActionListener() { 344 public void actionPerformed(ActionEvent evt) { 345 moveRowDown(); 346 } 347 }); 348 349 padding = new JPanel(); 350 gridBagConstraints1 = new java.awt.GridBagConstraints (); 351 gridBagConstraints1.gridx = 0; 352 gridBagConstraints1.weighty = 1.0; 353 gridBagConstraints1.fill = java.awt.GridBagConstraints.VERTICAL; 354 buttonsPanel.add(padding, gridBagConstraints1); 355 356 tab.add(buttonsPanel, BorderLayout.EAST); 357 358 add(tabbedPane, BorderLayout.CENTER); 359 360 JPanel sizePanel = new JPanel(); 362 sizePanel.setBorder(new EmptyBorder (8,2,0,2)); 363 sizePanel.setLayout(new java.awt.GridBagLayout ()); 364 365 rowsField = new JTextField(3); 366 367 JLabel rowsLabel = new JLabel(); 368 Mnemonics.setLocalizedText(rowsLabel, bundle.getString("CTL_Rows")); rowsLabel.setLabelFor(rowsField); 370 gridBagConstraints1 = new java.awt.GridBagConstraints (); 371 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 372 sizePanel.add(rowsLabel, gridBagConstraints1); 373 374 rowsField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Rows")); gridBagConstraints1 = new java.awt.GridBagConstraints (); 376 gridBagConstraints1.insets = new java.awt.Insets (0, 4, 0, 0); 377 sizePanel.add(rowsField, gridBagConstraints1); 378 379 addRowBtn = new JButton(" "+bundle.getString("CTL_CountPlus")+" "); addRowBtn.setMnemonic(bundle.getString("CTL_CountPlusRow_Mnemonic").charAt(0)); addRowBtn.setToolTipText(bundle.getString("CTL_HINT_AddRow")); addRowBtn.setMargin(new Insets(-1,1,-2,0)); 383 gridBagConstraints1 = new java.awt.GridBagConstraints (); 384 gridBagConstraints1.insets = new java.awt.Insets (0, 4, 0, 0); 385 sizePanel.add(addRowBtn, gridBagConstraints1); 386 addRowBtn.addActionListener(new ActionListener() { 387 public void actionPerformed(ActionEvent evt) { 388 addRow(); 389 } 390 }); 391 392 removeRowBtn = new JButton(" "+bundle.getString("CTL_CountMinus")+" "); removeRowBtn.setMnemonic(bundle.getString("CTL_CountMinusRow_Mnemonic").charAt(0)); removeRowBtn.setToolTipText(bundle.getString("CTL_HINT_RemoveRow")); removeRowBtn.setMargin(new Insets(-2,1,-1,1)); 396 gridBagConstraints1 = new java.awt.GridBagConstraints (); 397 sizePanel.add(removeRowBtn, gridBagConstraints1); 398 removeRowBtn.addActionListener(new ActionListener() { 399 public void actionPerformed(ActionEvent evt) { 400 removeLastRow(); 401 } 402 }); 403 404 columnsField = new JTextField(3); 405 406 JLabel columnsLabel = new JLabel(); 407 Mnemonics.setLocalizedText(columnsLabel, bundle.getString("CTL_Columns")); columnsLabel.setLabelFor(columnsField); 409 gridBagConstraints1 = new java.awt.GridBagConstraints (); 410 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 411 gridBagConstraints1.insets = new java.awt.Insets (0, 20, 0, 0); 412 sizePanel.add(columnsLabel, gridBagConstraints1); 413 414 columnsField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Columns")); gridBagConstraints1 = new java.awt.GridBagConstraints (); 416 gridBagConstraints1.insets = new java.awt.Insets (0, 4, 0, 0); 417 sizePanel.add(columnsField, gridBagConstraints1); 418 419 addColBtn = new JButton(" "+bundle.getString("CTL_CountPlus")+" "); addColBtn.setMnemonic(bundle.getString("CTL_CountPlusColumn_Mnemonic").charAt(0)); addColBtn.setToolTipText(bundle.getString("CTL_HINT_AddColumn")); addColBtn.setMargin(new Insets(-1,1,-2,0)); 423 gridBagConstraints1 = new java.awt.GridBagConstraints (); 424 gridBagConstraints1.insets = new java.awt.Insets (0, 4, 0, 0); 425 sizePanel.add(addColBtn, gridBagConstraints1); 426 addColBtn.addActionListener(new ActionListener() { 427 public void actionPerformed(ActionEvent evt) { 428 addColumn(); 429 } 430 }); 431 432 removeColBtn = new JButton(" "+bundle.getString("CTL_CountMinus")+" "); removeColBtn.setMnemonic(bundle.getString("CTL_CountMinusColumn_Mnemonic").charAt(0)); removeColBtn.setToolTipText(bundle.getString("CTL_HINT_RemoveColumn")); removeColBtn.setMargin(new Insets(-2,1,-1,1)); 436 gridBagConstraints1 = new java.awt.GridBagConstraints (); 437 sizePanel.add(removeColBtn, gridBagConstraints1); 438 removeColBtn.addActionListener(new ActionListener() { 439 public void actionPerformed(ActionEvent evt) { 440 removeLastColumn(); 441 } 442 }); 443 444 padding = new JPanel(); 445 446 gridBagConstraints1 = new java.awt.GridBagConstraints (); 447 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 448 gridBagConstraints1.weightx = 1.0; 449 sizePanel.add(padding, gridBagConstraints1); 450 451 add(sizePanel, BorderLayout.SOUTH); 452 453 454 rowsField.setText(String.valueOf(model.getRowCount())); 456 columnsField.setText(String.valueOf(model.getColumnCount())); 457 rowsField.addFocusListener(new FocusAdapter() { 458 public void focusLost(FocusEvent evt) { 459 updateRows(rowsField.getText()); 460 } 461 }); 462 rowsField.addActionListener(new ActionListener() { 463 public void actionPerformed(ActionEvent evt) { 464 updateRows(rowsField.getText()); 465 } 466 }); 467 columnsField.addFocusListener(new FocusAdapter() { 468 public void focusLost(FocusEvent evt) { 469 updateColumns(columnsField.getText()); 470 } 471 }); 472 columnsField.addActionListener(new ActionListener() { 473 public void actionPerformed(ActionEvent evt) { 474 updateColumns(columnsField.getText()); 475 } 476 }); 477 478 479 updateSettingsTable(); 480 enableButtons(); 481 482 titleModel.addTableModelListener(new TableModelListener() { 484 public void tableChanged(TableModelEvent evt) { 485 if (evt.getType() == TableModelEvent.UPDATE) { 486 int selRow = defaultValuesTable.getSelectedRow(), 487 selCol = defaultValuesTable.getSelectedColumn(); 488 updateDefaultTable(); 489 refreshSelection(defaultValuesTable, selRow, selCol); 490 } 491 else 492 enableButtons(); 493 } 494 }); 495 496 model.addTableModelListener(new TableModelListener() { 497 public void tableChanged(TableModelEvent evt) { 498 if (evt.getType() != TableModelEvent.UPDATE) 499 enableButtons(); 500 } 501 }); 502 503 defaultValuesTable.getColumnModel().addColumnModelListener(new TableColumnModelListener() { 505 public void columnAdded(TableColumnModelEvent e) { 506 } 507 public void columnRemoved(TableColumnModelEvent e) { 508 } 509 public void columnMoved(TableColumnModelEvent e) { 510 int fromI = e.getFromIndex(), 511 toI = e.getToIndex(); 512 if (fromI != toI) 513 moveColumn(fromI, toI); 514 else { 515 defSelectedRow = defaultValuesTable.getSelectedRow(); 516 defSelectedColumn = defaultValuesTable.getSelectedColumn(); 517 } 518 } 519 public void columnMarginChanged(ChangeEvent e) { 520 } 521 public void columnSelectionChanged(ListSelectionEvent e) { 522 } 523 }); 524 525 tabbedPane.addChangeListener(changeListener); 527 settingsTable.getSelectionModel().addListSelectionListener(selectionListener); 528 settingsTable.getColumnModel().getSelectionModel().addListSelectionListener(selectionListener); 529 defaultValuesTable.getSelectionModel().addListSelectionListener(selectionListener); 530 defaultValuesTable.getColumnModel().getSelectionModel().addListSelectionListener(selectionListener); 531 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TableCustomEditor")); 532 } 533 534 private void enableButtons() { 536 int rowCount = model.getRowCount(); 537 boolean anyRow = rowCount > 0; 538 539 int columnCount = model.getColumnCount(); 540 boolean anyColumn = columnCount > 0; 541 542 int selectedColumn = -1; 543 if (anyColumn) 544 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) 545 selectedColumn = defaultValuesTable.getSelectedColumn(); 546 else if (tabbedPane.getSelectedIndex() == SETTINGS_TAB) 547 selectedColumn = settingsTable.getSelectedRow(); 548 boolean columnSelected = selectedColumn >= 0 && selectedColumn < columnCount; 549 550 int selectedRow = -1; 551 if (anyRow && tabbedPane.getSelectedIndex() == DEFAULT_TAB) { 552 selectedRow = defaultValuesTable.getSelectedRow(); 553 columnSelected &= selectedRow >= 0 && selectedRow < rowCount; 554 } 555 boolean rowSelected = selectedRow >= 0 && selectedRow < rowCount; 556 rowSelected &= columnSelected; 557 558 insertSColBtn.setEnabled(columnSelected); 559 deleteSColBtn.setEnabled(columnSelected); 560 moveSColUpBtn.setEnabled(selectedColumn > 0 && selectedColumn < columnCount); 561 moveSColDownBtn.setEnabled(columnSelected && selectedColumn < columnCount-1); 562 563 insertColBtn.setEnabled(columnSelected); 564 deleteColBtn.setEnabled(columnSelected); 565 moveColLeftBtn.setEnabled(selectedColumn > 0 && selectedColumn < columnCount && rowSelected); 566 moveColRightBtn.setEnabled(columnSelected && selectedColumn < columnCount-1); 567 insertRowBtn.setEnabled(rowSelected); 568 deleteRowBtn.setEnabled(rowSelected); 569 moveRowUpBtn.setEnabled(selectedRow > 0 && selectedRow < rowCount && columnSelected); 570 moveRowDownBtn.setEnabled(rowSelected && selectedRow < rowCount-1); 571 572 addRowBtn.setEnabled(true); 573 removeRowBtn.setEnabled(anyRow); 574 addColBtn.setEnabled(true); 575 removeColBtn.setEnabled(anyColumn); 576 } 577 578 private void addRow() { 580 model.setRowCount(model.getRowCount()+1); 581 rowsField.setText(Integer.toString(model.getRowCount())); 582 defaultValuesTable.clearSelection(); 583 } 584 585 private void removeLastRow() { 587 int n = model.getRowCount(); 588 if (n > 0) { 589 model.setRowCount(n-1); 590 rowsField.setText(Integer.toString(model.getRowCount())); 591 defaultValuesTable.clearSelection(); 592 } 593 } 594 595 private void insertRow() { 597 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) { 598 examineSelections(); 599 if (defSelectedRow >= 0) { 600 model.addRow(defSelectedRow); 601 rowsField.setText(Integer.toString(model.getRowCount())); 602 updateSelections(true); 603 } 604 else if (model.getRowCount() == 0) 605 addRow(); 606 } 607 } 608 609 private void removeRow() { 611 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB && model.getRowCount() > 0) { 612 examineSelections(); 613 if (defSelectedRow >= 0) { 614 model.removeRow(defSelectedRow); 615 rowsField.setText(Integer.toString(model.getRowCount())); 616 617 if (defSelectedRow == model.getRowCount() && defSelectedRow > 0) 618 defSelectedRow--; 619 updateSelections(true); 620 } 621 } 622 } 623 624 private void updateRows(String text) { 626 int n = 0; 627 try { 628 n = Integer.parseInt(text); 629 } catch(NumberFormatException e) { 630 return; 631 } 632 if (n >= 0) { 633 model.setRowCount(n); 634 } 636 } 637 638 private void moveRowUp() { 640 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB && model.getRowCount() > 0) { 641 examineSelections(); 642 if (defSelectedRow > 0) { 643 model.moveRow(defSelectedRow, defSelectedRow-1); 644 defSelectedRow--; 645 updateSelections(true); 646 } 647 } 648 } 649 650 private void moveRowDown() { 652 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB && model.getRowCount() > 0) { 653 examineSelections(); 654 if (defSelectedRow >= 0 && defSelectedRow < model.getRowCount()-1) { 655 model.moveRow(defSelectedRow, defSelectedRow+1); 656 defSelectedRow++; 657 updateSelections(true); 658 } 659 } 660 } 661 662 private void addColumn() { 664 int n = model.getColumnCount(); 665 model.setColumnCount(n+1); 666 defaultValuesTable.clearSelection(); 667 updateSettingsTable(); 668 settingsTable.clearSelection(); 669 columnsField.setText(Integer.toString(model.getColumnCount())); 670 } 671 672 private void removeLastColumn() { 674 int n = model.getColumnCount(); 675 if (n > 0) { 676 model.setColumnCount(n-1); 677 defaultValuesTable.clearSelection(); 678 updateSettingsTable(); 679 settingsTable.clearSelection(); 680 columnsField.setText(Integer.toString(model.getColumnCount())); 681 } 682 } 683 684 private void insertColumn() { 686 int ci; 687 examineSelections(); 688 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) 689 ci = defSelectedColumn; 690 else if (tabbedPane.getSelectedIndex() == SETTINGS_TAB) 691 ci = stSelectedRow; 692 else ci = -1; 693 694 if (ci >= 0) { 695 model.addColumn(ci); 696 updateSettingsTable(); 697 columnsField.setText(Integer.toString(model.getColumnCount())); 698 updateSelections(true); 699 } 700 else if (model.getColumnCount() == 0) 701 addColumn(); 702 } 703 704 private void removeColumn() { 706 if (model.getColumnCount() > 0) { 707 int ci; 708 examineSelections(); 709 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) 710 ci = defSelectedColumn; 711 else if (tabbedPane.getSelectedIndex() == SETTINGS_TAB) 712 ci = stSelectedRow; 713 else ci = -1; 714 715 if (ci >= 0) { 716 model.removeColumn(ci); 717 updateSettingsTable(); 718 columnsField.setText(Integer.toString(model.getColumnCount())); 719 720 if (ci == model.getColumnCount() && ci > 0) ci--; 721 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) 722 defSelectedColumn = ci; 723 else stSelectedRow = ci; 724 updateSelections(true); 725 } 726 } 727 } 728 729 private void updateColumns(String text) { 731 int n = 0; 732 try { 733 n = Integer.parseInt(text); 734 } catch(NumberFormatException e) { 735 return; 736 } 737 if (n >= 0) { 738 model.setColumnCount(n); 739 updateSettingsTable(); 741 } 743 } 744 745 private void moveColumnLeft() { 746 int ci; 747 examineSelections(); 748 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) 749 ci = defSelectedColumn; 750 else if (tabbedPane.getSelectedIndex() == SETTINGS_TAB) 751 ci = stSelectedRow; 752 else ci = -1; 753 754 if (ci > 0) { 755 model.moveColumn(ci, ci-1); 756 updateSettingsTable(); 757 758 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) 759 defSelectedColumn = ci-1; 760 else stSelectedRow = ci-1; 761 updateSelections(true); 762 } 763 } 764 765 private void moveColumnRight() { 766 int ci; 767 examineSelections(); 768 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) 769 ci = defSelectedColumn; 770 else if (tabbedPane.getSelectedIndex() == SETTINGS_TAB) 771 ci = stSelectedRow; 772 else ci = -1; 773 774 if (ci >= 0 && ci < model.getColumnCount()-1) { 775 model.moveColumn(ci, ci+1); 776 updateSettingsTable(); 777 778 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) 779 defSelectedColumn = ci+1; 780 else stSelectedRow = ci+1; 781 updateSelections(true); 782 } 783 } 784 785 private void moveColumn(int fromIndex, int toIndex) { 787 model.moveColumn(fromIndex, toIndex); 788 updateSettingsTable(); 789 790 int selCol; 791 if (defSelectedRow >= 0) { 792 if (defSelectedColumn == fromIndex) 793 selCol = toIndex; 794 else if (defSelectedColumn == toIndex) 795 selCol = fromIndex; 796 else 797 selCol = defSelectedColumn; 798 } 799 else selCol = -1; 800 801 if (selCol >= 0) { 802 defaultValuesTable.setRowSelectionInterval(defSelectedRow,defSelectedRow); 803 defaultValuesTable.setColumnSelectionInterval(selCol, selCol); 804 } 805 else { 806 defaultValuesTable.clearSelection(); 807 defaultValuesTable.getParent().requestFocus(); 808 } 809 810 if (defaultValuesTable.getTableHeader().getDraggedColumn() != null) 811 defaultValuesTable.getTableHeader().setDraggedColumn( 812 defaultValuesTable.getColumnModel().getColumn(toIndex)); 813 } 814 815 private void examineSelections() { 817 if (settingsTable.isEditing()) { 820 settingsTable.removeEditor(); 821 } 822 if (defaultValuesTable.isEditing()) { 823 defaultValuesTable.removeEditor(); 824 } 825 stSelectedRow = settingsTable.getSelectedRow(); 826 stSelectedColumn = settingsTable.getSelectedColumn(); 827 defSelectedRow = defaultValuesTable.getSelectedRow(); 828 defSelectedColumn = defaultValuesTable.getSelectedColumn(); 829 } 830 831 private void updateSelections(boolean focusTable) { 833 if (tabbedPane.getSelectedIndex() == DEFAULT_TAB) { 834 refreshSelection(settingsTable, defSelectedColumn, stSelectedColumn); 835 if (focusTable && refreshSelection(defaultValuesTable, defSelectedRow, defSelectedColumn)) 836 defaultValuesTable.requestFocus(); 837 } 838 else if (tabbedPane.getSelectedIndex() == SETTINGS_TAB) { 839 refreshSelection(defaultValuesTable, defSelectedRow, stSelectedRow); 840 if (focusTable && refreshSelection(settingsTable, stSelectedRow, stSelectedColumn)) 841 settingsTable.requestFocus(); 842 } 843 } 844 845 private static boolean refreshSelection(JTable table, int row, int column) { 846 table.clearSelection(); 847 if (row >= 0 && row < table.getRowCount() && column >= 0 && column < table.getColumnCount()) { 848 table.setRowSelectionInterval(row, row); 849 table.setColumnSelectionInterval(column, column); 850 return true; 851 } 852 return false; 853 } 854 856 void updateDefaultTable() { 858 if (model == null || isChangingTableModel) return; 860 int cols = model.getColumnCount(), 861 rows = model.getRowCount(); 862 boolean typeChanged = false; 863 864 for (int i=0; i < cols; i++) { 865 model.setColumnName(i, (String ) titleModel.getValueAt(i,1)); 866 Class type; 867 try { 868 type = Class.forName("java.lang."+((String ) titleModel.getValueAt(i,2))); } catch(Exception e) { 870 type = Object .class; 871 } 872 if (!type.equals(model.getColumnClass(i))) { 873 typeChanged = true; 874 model.setColumnClass(i, type); 875 for (int j=0; j < rows; j++) 876 model.setValueAt(null, j,i); 877 } 878 model.setColumnEditable(i, ((Boolean ) titleModel.getValueAt(i,3)).booleanValue()); 879 } 880 881 if (typeChanged) 882 defaultValuesTable.createDefaultColumnsFromModel(); 883 884 model.fireTableStructureChanged(); 885 } 886 887 void updateSettingsTable() { 889 isChangingTableModel = true; 890 int cols = model.getColumnCount(); 891 if (cols != titleModel.getRowCount()) { 892 titleModel.setRowCount(cols); 893 } 894 895 for (int i=0; i < cols; i++) { 896 titleModel.setValueAt(Integer.toString(i+1), i,0); 897 titleModel.setValueAt(model.getColumnName(i), i,1); 898 String type = model.getColumnClass(i).getName(); 899 int lastDot = type.lastIndexOf('.'); 900 if (lastDot >= 0) 901 type = type.substring(lastDot+1, type.length()); 902 titleModel.setValueAt(type, i,2); 903 titleModel.setValueAt(model.isColumnEditable(i) ? Boolean.TRUE : Boolean.FALSE, i,3); 904 } 905 906 isChangingTableModel = false; 907 } 908 909 914 public Object getPropertyValue() throws IllegalStateException { 915 updateDefaultTable(); 916 return new TableModelEditor.NbTableModel(model); 917 } 918 919 922 private class SelectionListener implements ListSelectionListener { 923 public void valueChanged(ListSelectionEvent e) { 924 enableButtons(); 925 } 926 } 927 928 private class TabChangeListener implements ChangeListener { 929 public void stateChanged(ChangeEvent e) { 930 enableButtons(); 931 } 932 } 933 934 private static class CustomJTable extends JTable { 936 Component edComp; 937 938 public CustomJTable() { 939 super(); 940 initEditors(); 941 } 942 943 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, 944 int condition, boolean pressed) 945 { 946 if (e != null && e.getID() == KeyEvent.KEY_PRESSED 947 && (e.getModifiers() & (InputEvent.CTRL_MASK|InputEvent.ALT_MASK)) != 0) 948 { 949 return false; 950 } 951 else return super.processKeyBinding(ks, e, condition, pressed); 952 } 953 954 protected void processKeyEvent(KeyEvent e) { 955 if (e.getID() == KeyEvent.KEY_PRESSED) { 956 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 957 if (!isEditing()) 958 return; 959 } 960 else if (e.getKeyCode() == KeyEvent.VK_SPACE) { 961 int anchorRow = getSelectionModel().getAnchorSelectionIndex(); 962 int anchorColumn = getColumnModel().getSelectionModel(). 963 getAnchorSelectionIndex(); 964 if (anchorRow != -1 && anchorColumn != -1 && !isEditing()) { 965 super.processKeyEvent(e); 966 e.consume(); 967 if (edComp != null) 968 edComp.requestFocus(); 969 return; 970 } 971 } 972 } 973 super.processKeyEvent(e); 974 } 975 976 public Component prepareEditor(TableCellEditor editor, int row, int column) { 977 edComp = super.prepareEditor(editor, row, column); 978 return edComp; 979 } 980 981 private void initEditors() { 982 initEditor(Object .class); 983 initEditor(Number .class); 984 } 985 986 private void initEditor(Class type) { 987 TableCellEditor cellEditor = getDefaultEditor(type); 988 if(cellEditor instanceof DefaultCellEditor) { 989 final DefaultCellEditor defaultEditor = (DefaultCellEditor) cellEditor; 990 Component editorComponent = defaultEditor.getComponent(); 991 if(editorComponent instanceof JTextField) { 992 editorComponent.addFocusListener(new FocusListener() { 993 public void focusLost(FocusEvent e) { 994 defaultEditor.stopCellEditing(); 995 } 996 public void focusGained(FocusEvent e) {} 997 }); 998 } 999 } 1000 } 1001 } 1002} 1003 | Popular Tags |