-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRXTable.java
More file actions
297 lines (262 loc) · 8.73 KB
/
RXTable.java
File metadata and controls
297 lines (262 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
/**
* The RXTable provides some extensions to the default JTable
*
* 1) Select All editing - when a text related cell is placed in editing mode
* the text is selected. Controlled by invoking a "setSelectAll..." method.
*
* 2) reorderColumns - static convenience method for reodering table columns
*/
public class RXTable extends JTable
{
private boolean isSelectAllForMouseEvent = false;
private boolean isSelectAllForActionEvent = false;
private boolean isSelectAllForKeyEvent = false;
//
// Constructors
//
/**
* Constructs a default <code>RXTable</code> that is initialized with a default
* data model, a default column model, and a default selection
* model.
*/
public RXTable()
{
this(null, null, null);
}
/**
* Constructs a <code>RXTable</code> that is initialized with
* <code>dm</code> as the data model, a default column model,
* and a default selection model.
*
* @param dm the data model for the table
*/
public RXTable(TableModel dm)
{
this(dm, null, null);
}
/**
* Constructs a <code>RXTable</code> that is initialized with
* <code>dm</code> as the data model, <code>cm</code>
* as the column model, and a default selection model.
*
* @param dm the data model for the table
* @param cm the column model for the table
*/
public RXTable(TableModel dm, TableColumnModel cm)
{
this(dm, cm, null);
}
/**
* Constructs a <code>RXTable</code> that is initialized with
* <code>dm</code> as the data model, <code>cm</code> as the
* column model, and <code>sm</code> as the selection model.
* If any of the parameters are <code>null</code> this method
* will initialize the table with the corresponding default model.
* The <code>autoCreateColumnsFromModel</code> flag is set to false
* if <code>cm</code> is non-null, otherwise it is set to true
* and the column model is populated with suitable
* <code>TableColumns</code> for the columns in <code>dm</code>.
*
* @param dm the data model for the table
* @param cm the column model for the table
* @param sm the row selection model for the table
*/
public RXTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm)
{
super(dm, cm, sm);
}
/**
* Constructs a <code>RXTable</code> with <code>numRows</code>
* and <code>numColumns</code> of empty cells using
* <code>DefaultTableModel</code>. The columns will have
* names of the form "A", "B", "C", etc.
*
* @param numRows the number of rows the table holds
* @param numColumns the number of columns the table holds
*/
public RXTable(int numRows, int numColumns)
{
this(new DefaultTableModel(numRows, numColumns));
}
/**
* Constructs a <code>RXTable</code> to display the values in the
* <code>Vector</code> of <code>Vectors</code>, <code>rowData</code>,
* with column names, <code>columnNames</code>. The
* <code>Vectors</code> contained in <code>rowData</code>
* should contain the values for that row. In other words,
* the value of the cell at row 1, column 5 can be obtained
* with the following code:
* <p>
* <pre>((Vector)rowData.elementAt(1)).elementAt(5);</pre>
* <p>
* @param rowData the data for the new table
* @param columnNames names of each column
*/
public RXTable(Vector rowData, Vector columnNames)
{
this(new DefaultTableModel(rowData, columnNames));
}
/**
* Constructs a <code>RXTable</code> to display the values in the two dimensional array,
* <code>rowData</code>, with column names, <code>columnNames</code>.
* <code>rowData</code> is an array of rows, so the value of the cell at row 1,
* column 5 can be obtained with the following code:
* <p>
* <pre> rowData[1][5]; </pre>
* <p>
* All rows must be of the same length as <code>columnNames</code>.
* <p>
* @param rowData the data for the new table
* @param columnNames names of each column
*/
public RXTable(final Object[][] rowData, final Object[] columnNames)
{
super(rowData, columnNames);
}
//
// Overridden methods
//
/*
* Override to provide Select All editing functionality
*/
@Override
public boolean editCellAt(int row, int column, EventObject e)
{
// Only valid KeyEvents should start editing of a cell
if (e instanceof KeyEvent
&& ! validKeyEvent(e))
return false;
// Start editing normally
boolean result = super.editCellAt(row, column, e);
if (isSelectAllForMouseEvent
|| isSelectAllForActionEvent
|| isSelectAllForKeyEvent)
{
selectAll(e);
}
return result;
}
private boolean validKeyEvent(EventObject e)
{
KeyEvent ke = (KeyEvent)e;
// F2 is used to start the editor
// if (ke.getKeyCode() == KeyEvent.VK_F2
// && ke.getModifiers() == 0)
// return true;
// The Shift key is only valid when used with another character
if (ke.getModifiersEx() != 0
&& ke.getModifiersEx() != KeyEvent.SHIFT_DOWN_MASK)
return false;
// Make sure the key char is a valid unicode character
return ke.getKeyChar() != KeyEvent.CHAR_UNDEFINED;
}
/*
* Select the text when editing on a text related cell is started
*/
private void selectAll(EventObject e)
{
final Component editor = getEditorComponent();
if (editor == null
|| ! (editor instanceof JTextComponent))
return;
if (e == null)
{
((JTextComponent)editor).selectAll();
return;
}
// Typing in the cell was used to activate the editor
if (e instanceof KeyEvent && isSelectAllForKeyEvent)
{
((JTextComponent)editor).selectAll();
return;
}
// F2 was used to activate the editor
if (e instanceof ActionEvent && isSelectAllForActionEvent)
{
((JTextComponent)editor).selectAll();
return;
}
// A mouse click was used to activate the editor.
// Generally this is a double click and the second mouse click is
// passed to the editor which would remove the text selection unless
// we use the invokeLater()
if (e instanceof MouseEvent && isSelectAllForMouseEvent)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
((JTextComponent)editor).selectAll();
}
});
}
}
//
// Newly added methods
//
/*
* Sets the Select All property for for all event types
*/
public void setSelectAllForEdit(boolean isSelectAllForEdit)
{
setSelectAllForMouseEvent( isSelectAllForEdit );
setSelectAllForActionEvent( isSelectAllForEdit );
setSelectAllForKeyEvent( isSelectAllForEdit );
}
/*
* Set the Select All property when editing is invoked by the mouse
*/
public void setSelectAllForMouseEvent(boolean isSelectAllForMouseEvent)
{
this.isSelectAllForMouseEvent = isSelectAllForMouseEvent;
}
/*
* Set the Select All property when editing is invoked by the "F2" key
*/
public void setSelectAllForActionEvent(boolean isSelectAllForActionEvent)
{
this.isSelectAllForActionEvent = isSelectAllForActionEvent;
}
/*
* Set the Select All property when editing is invoked by
* typing directly into the cell
*/
public void setSelectAllForKeyEvent(boolean isSelectAllForKeyEvent)
{
this.isSelectAllForKeyEvent = isSelectAllForKeyEvent;
}
//
// Static, convenience methods
//
/**
* Convenience method to order the table columns of a table. The columns
* are ordered based on the column names specified in the array. If the
* column name is not found then no column is moved. This means you can
* specify a null value to preserve the current order of a given column.
*
* @param table the table containing the columns to be sorted
* @param columnNames an array containing the column names in the
* order they should be displayed
*/
public static void reorderColumns(JTable table, Object... columnNames)
{
TableColumnModel model = table.getColumnModel();
for (int newIndex = 0; newIndex < columnNames.length; newIndex++)
{
try
{
Object columnName = columnNames[newIndex];
int index = model.getColumnIndex(columnName);
model.moveColumn(index, newIndex);
}
catch(IllegalArgumentException e) {}
}
}
} // End of Class RXTable