KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > editors2 > CustomTableModelEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.editors2;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.util.ResourceBundle JavaDoc;
25
26
27 import javax.swing.*;
28 import javax.swing.border.EmptyBorder JavaDoc;
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 /** A custom property editor for TableModel.
38 * @author Jan Jancura, Ian Formanek
39 * @version 1.00, 06 Oct 1998
40 */

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; // holds default table values
54
private TableModelEditor.NbTableModel titleModel; // holds metadata
55

56     private int stSelectedRow;
57     private int stSelectedColumn;
58     private int defSelectedRow;
59     private int defSelectedColumn;
60
61     // buttons
62
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 JavaDoc 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 JavaDoc(6, 6, 6, 6));
93         
94         tabbedPane = new JTabbedPane();
95
96         JLabel titleLabel = new JLabel();
97         Mnemonics.setLocalizedText(titleLabel, bundle.getString("CTL_TableModelTitle")); // NOI18N
98
titleLabel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_TableModelTitle")); // NOI18N
99
titleLabel.setLabelFor(tabbedPane);
100         add(titleLabel, BorderLayout.NORTH);
101
102         // first tab (table settings)
103
JPanel tab = new JPanel();
104         tab.setLayout(new BorderLayout(6, 6));
105         tab.setBorder(new EmptyBorder JavaDoc(6, 2, 0, 2));
106         
107         settingsTable = new CustomJTable();
108
109         JLabel titleLabel1 = new JLabel();
110         Mnemonics.setLocalizedText(titleLabel1, bundle.getString("CTL_Title1")); // NOI18N
111
titleLabel1.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Title1")); // NOI18N
112
titleLabel1.setLabelFor(settingsTable);
113         tab.add(titleLabel1,BorderLayout.NORTH);
114
115         titleModel = new TableModelEditor.NbTableModel(
116                          new String JavaDoc[] {
117                              bundle.getString("CTL_Column"), // NOI18N
118
bundle.getString("CTL_Title"), // NOI18N
119
bundle.getString("CTL_Type"), // NOI18N
120
bundle.getString("CTL_Editable") // NOI18N
121
},
122                          new Class JavaDoc[] {
123                              String JavaDoc.class, String JavaDoc.class, String JavaDoc.class, Boolean JavaDoc.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"); // NOI18N
134
comboBox.addItem("String"); // NOI18N
135
comboBox.addItem("Boolean"); // NOI18N
136
comboBox.addItem("Integer"); // NOI18N
137
comboBox.addItem("Byte"); // NOI18N
138
comboBox.addItem("Short"); // NOI18N
139
comboBox.addItem("Long"); // NOI18N
140
comboBox.addItem("Float"); // NOI18N
141
comboBox.addItem("Double"); // NOI18N
142
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         // panel with buttons on the right (for settings table)
157
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")); // NOI18N
162
insertSColBtn.setToolTipText(bundle.getString("CTL_HINT_InsertColumn")); // NOI18N
163
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")); // NOI18N
172
deleteSColBtn.setToolTipText(bundle.getString("CTL_HINT_DeleteColumn")); // NOI18N
173
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")); // NOI18N
182
moveSColUpBtn.setToolTipText(bundle.getString("CTL_HINT_MoveColumnUp")); // NOI18N
183
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")); // NOI18N
192
moveSColDownBtn.setToolTipText(bundle.getString("CTL_HINT_MoveColumnDown")); // NOI18N
193
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); // NOI18N
206

207         // second tab (default-values table)
208
tab = new JPanel();
209         tab.setLayout(new BorderLayout(6, 6));
210         tab.setBorder(new EmptyBorder JavaDoc(6, 2, 0, 2));
211
212         defaultValuesTable = new CustomJTable();
213
214         JLabel titleLabel2 = new JLabel();
215         Mnemonics.setLocalizedText(titleLabel2, bundle.getString("CTL_DefaultTableValues")); // NOI18N
216
titleLabel2.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_DefaultTableValues")); // NOI18N
217
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); // NOI18N
231

232         tabbedPane.addTab(bundle.getString("CTL_DefaultValues"), tab); // NOI18N
233

234         // panel with buttons on the right (for default-values table)
235
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")); // NOI18N
240
colLabel.setLabelFor(colButtonsPanel);
241         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
242         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
243         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(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 JavaDoc();
249         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(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")); // NOI18N
256
insertColBtn.setToolTipText(bundle.getString("CTL_HINT_InsertColumn")); // NOI18N
257
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")); // NOI18N
266
deleteColBtn.setToolTipText(bundle.getString("CTL_HINT_DeleteColumn")); // NOI18N
267
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")); // NOI18N
276
moveColLeftBtn.setToolTipText(bundle.getString("CTL_HINT_MoveColumnLeft")); // NOI18N
277
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")); // NOI18N
286
moveColRightBtn.setToolTipText(bundle.getString("CTL_HINT_MoveColumnRight")); // NOI18N
287
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")); // NOI18N
296
rowLabel.setLabelFor(rowButtonsPanel);
297         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
298         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(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 JavaDoc();
304         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(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")); // NOI18N
311
insertRowBtn.setToolTipText(bundle.getString("CTL_HINT_InsertRow")); // NOI18N
312
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")); // NOI18N
321
deleteRowBtn.setToolTipText(bundle.getString("CTL_HINT_DeleteRow")); // NOI18N
322
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")); // NOI18N
331
moveRowUpBtn.setToolTipText(bundle.getString("CTL_HINT_MoveRowUp")); // NOI18N
332
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")); // NOI18N
341
moveRowDownBtn.setToolTipText(bundle.getString("CTL_HINT_MoveRowDown")); // NOI18N
342
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 JavaDoc();
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         // lower panel with size info & buttons
361
JPanel sizePanel = new JPanel();
362         sizePanel.setBorder(new EmptyBorder JavaDoc(8,2,0,2));
363         sizePanel.setLayout(new java.awt.GridBagLayout JavaDoc());
364
365         rowsField = new JTextField(3);
366
367         JLabel rowsLabel = new JLabel();
368         Mnemonics.setLocalizedText(rowsLabel, bundle.getString("CTL_Rows")); // NOI18N
369
rowsLabel.setLabelFor(rowsField);
370         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
371         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
372         sizePanel.add(rowsLabel, gridBagConstraints1);
373
374         rowsField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Rows")); // NOI18N
375
gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
376         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 4, 0, 0);
377         sizePanel.add(rowsField, gridBagConstraints1);
378
379         addRowBtn = new JButton(" "+bundle.getString("CTL_CountPlus")+" "); // NOI18N
380
addRowBtn.setMnemonic(bundle.getString("CTL_CountPlusRow_Mnemonic").charAt(0)); // NOI18N
381
addRowBtn.setToolTipText(bundle.getString("CTL_HINT_AddRow")); // NOI18N
382
addRowBtn.setMargin(new Insets(-1,1,-2,0));
383         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
384         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(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")+" "); // NOI18N
393
removeRowBtn.setMnemonic(bundle.getString("CTL_CountMinusRow_Mnemonic").charAt(0)); // NOI18N
394
removeRowBtn.setToolTipText(bundle.getString("CTL_HINT_RemoveRow")); // NOI18N
395
removeRowBtn.setMargin(new Insets(-2,1,-1,1));
396         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
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")); // NOI18N
408
columnsLabel.setLabelFor(columnsField);
409         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
410         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
411         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 20, 0, 0);
412         sizePanel.add(columnsLabel, gridBagConstraints1);
413
414         columnsField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Columns")); // NOI18N
415
gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
416         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 4, 0, 0);
417         sizePanel.add(columnsField, gridBagConstraints1);
418
419         addColBtn = new JButton(" "+bundle.getString("CTL_CountPlus")+" "); // NOI18N
420
addColBtn.setMnemonic(bundle.getString("CTL_CountPlusColumn_Mnemonic").charAt(0)); // NOI18N
421
addColBtn.setToolTipText(bundle.getString("CTL_HINT_AddColumn")); // NOI18N
422
addColBtn.setMargin(new Insets(-1,1,-2,0));
423         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
424         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(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")+" "); // NOI18N
433
removeColBtn.setMnemonic(bundle.getString("CTL_CountMinusColumn_Mnemonic").charAt(0)); // NOI18N
434
removeColBtn.setToolTipText(bundle.getString("CTL_HINT_RemoveColumn")); // NOI18N
435
removeColBtn.setMargin(new Insets(-2,1,-1,1));
436         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc();
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         // set textfields values and listeners
455
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         // listen to changes in the settings table - update the default-values table
483
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         // listen to default table's ColumnModel and change column order when user does it visually
504
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         // enable/disable buttons on any selection change
526
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     // enables/disables control buttons according to selected rows and columns
535
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     // adds one row to the default-values table
579
private void addRow() {
580         model.setRowCount(model.getRowCount()+1);
581         rowsField.setText(Integer.toString(model.getRowCount()));
582         defaultValuesTable.clearSelection();
583     }
584
585     // removes one row from the default-values table
586
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     // inserts one row to the default-values table (at current position)
596
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     // removes one row from the default-values table (from current position)
610
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     // sets number of rows for the default-values table from given text
625
private void updateRows(String JavaDoc text) {
626         int n = 0;
627         try {
628             n = Integer.parseInt(text);
629         } catch(NumberFormatException JavaDoc e) {
630             return;
631         }
632         if (n >= 0) {
633             model.setRowCount(n);
634             //defaultValuesTable.clearSelection();
635
}
636     }
637
638     // moves selected row up one row
639
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     // moves selected row down one row
651
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     // adds one column to the default-values table (a row to the settings table)
663
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     // removes last column from the default-values table (last row from the settings table)
673
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     // inserts one column to the default-values table at the current position (row in settings table)
685
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     // removes currently selected column from the default-values table (row from the settings table)
705
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     // sets number of columns from given text
730
private void updateColumns(String JavaDoc text) {
731         int n = 0;
732         try {
733             n = Integer.parseInt(text);
734         } catch(NumberFormatException JavaDoc e) {
735             return;
736         }
737         if (n >= 0) {
738             model.setColumnCount(n);
739             //defaultValuesTable.clearSelection();
740
updateSettingsTable();
741             //settingsTable.clearSelection();
742
}
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     // called when a column is moved in the default-values table (by user)
786
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     // helper methods ------------------
816
private void examineSelections() {
817         // This method is called before action that changes the table model
818
// (no. of rows or columns) => stop editing of cells (see issue 64441).
819
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     // updates cell selection in tables (settings and default-values)
832
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     // ------------------
855

856     // updates the default-values-table ('model') according to settings-table ('titleModel')
857
void updateDefaultTable() {
858         if (model == null || isChangingTableModel) return; //at component creation
859

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 JavaDoc) titleModel.getValueAt(i,1));
866             Class JavaDoc type;
867             try {
868                 type = Class.forName("java.lang."+((String JavaDoc) titleModel.getValueAt(i,2))); // NOI18N
869
} catch(Exception JavaDoc e) {
870                 type = Object JavaDoc.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 JavaDoc) titleModel.getValueAt(i,3)).booleanValue());
879         }
880
881         if (typeChanged)
882             defaultValuesTable.createDefaultColumnsFromModel();
883
884         model.fireTableStructureChanged();
885     }
886
887     // updates the settings table ('titleModel') according to default-values-table ('model')
888
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 JavaDoc 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     /**
910     * @return Returns the property value that is result of the CustomPropertyEditor.
911     * @exception InvalidStateException when the custom property editor does not represent valid property value
912     * (and thus it should not be set)
913     */

914     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
915         updateDefaultTable();
916         return new TableModelEditor.NbTableModel(model);
917     }
918
919     //
920
// -------------------------------------------------------------------------
921

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     // a custom JTable class solving some accessibility issues of JTable
935
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 JavaDoc.class);
983             initEditor(Number JavaDoc.class);
984         }
985         
986         private void initEditor(Class JavaDoc 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