52 |
import java.awt.event.KeyEvent; |
import java.awt.event.KeyEvent; |
53 |
import java.awt.event.KeyListener; |
import java.awt.event.KeyListener; |
54 |
import java.awt.event.MouseEvent; |
import java.awt.event.MouseEvent; |
55 |
|
import java.beans.PropertyChangeEvent; |
56 |
|
import java.beans.PropertyChangeListener; |
57 |
|
|
58 |
import javax.swing.AbstractAction; |
import javax.swing.AbstractAction; |
59 |
import javax.swing.ActionMap; |
import javax.swing.ActionMap; |
|
import javax.swing.BorderFactory; |
|
60 |
import javax.swing.CellRendererPane; |
import javax.swing.CellRendererPane; |
61 |
import javax.swing.DefaultListSelectionModel; |
import javax.swing.DefaultListSelectionModel; |
62 |
import javax.swing.InputMap; |
import javax.swing.InputMap; |
95 |
/** The normal cell border. */ |
/** The normal cell border. */ |
96 |
Border cellBorder; |
Border cellBorder; |
97 |
|
|
|
/** The cell border for selected/highlighted cells. */ |
|
|
Border highlightCellBorder; |
|
|
|
|
98 |
/** The action bound to KeyStrokes. */ |
/** The action bound to KeyStrokes. */ |
99 |
TableAction action; |
TableAction action; |
100 |
|
|
101 |
/** |
/** |
102 |
|
* Listens for changes to the tables properties. |
103 |
|
*/ |
104 |
|
private PropertyChangeListener propertyChangeListener; |
105 |
|
|
106 |
|
/** |
107 |
* Handles key events for the JTable. Key events should be handled through |
* Handles key events for the JTable. Key events should be handled through |
108 |
* the InputMap/ActionMap mechanism since JDK1.3. This class is only there |
* the InputMap/ActionMap mechanism since JDK1.3. This class is only there |
109 |
* for backwards compatibility. |
* for backwards compatibility. |
201 |
|
|
202 |
public void mouseDragged(MouseEvent e) |
public void mouseDragged(MouseEvent e) |
203 |
{ |
{ |
204 |
curr = new Point(e.getX(), e.getY()); |
if (table.isEnabled()) |
205 |
updateSelection(e.isControlDown()); |
{ |
206 |
|
curr = new Point(e.getX(), e.getY()); |
207 |
|
updateSelection(e.isControlDown()); |
208 |
|
} |
209 |
} |
} |
210 |
|
|
211 |
public void mouseEntered(MouseEvent e) |
public void mouseEntered(MouseEvent e) |
225 |
|
|
226 |
public void mousePressed(MouseEvent e) |
public void mousePressed(MouseEvent e) |
227 |
{ |
{ |
228 |
ListSelectionModel rowModel = table.getSelectionModel(); |
if (table.isEnabled()) |
229 |
ListSelectionModel colModel = table.getColumnModel().getSelectionModel(); |
{ |
230 |
int rowLead = rowModel.getLeadSelectionIndex(); |
ListSelectionModel rowModel = table.getSelectionModel(); |
231 |
int colLead = colModel.getLeadSelectionIndex(); |
ListSelectionModel colModel = table.getColumnModel().getSelectionModel(); |
232 |
|
int rowLead = rowModel.getLeadSelectionIndex(); |
233 |
|
int colLead = colModel.getLeadSelectionIndex(); |
234 |
|
|
235 |
|
begin = new Point(e.getX(), e.getY()); |
236 |
|
curr = new Point(e.getX(), e.getY()); |
237 |
|
//if control is pressed and the cell is already selected, deselect it |
238 |
|
if (e.isControlDown() && table. |
239 |
|
isCellSelected(table.rowAtPoint(begin),table.columnAtPoint(begin))) |
240 |
|
{ |
241 |
|
table.getSelectionModel(). |
242 |
|
removeSelectionInterval(table.rowAtPoint(begin), |
243 |
|
table.rowAtPoint(begin)); |
244 |
|
table.getColumnModel().getSelectionModel(). |
245 |
|
removeSelectionInterval(table.columnAtPoint(begin), |
246 |
|
table.columnAtPoint(begin)); |
247 |
|
} |
248 |
|
else |
249 |
|
updateSelection(e.isControlDown()); |
250 |
|
|
251 |
begin = new Point(e.getX(), e.getY()); |
// If we were editing, but the moved to another cell, stop editing |
252 |
curr = new Point(e.getX(), e.getY()); |
if (rowLead != rowModel.getLeadSelectionIndex() || |
253 |
//if control is pressed and the cell is already selected, deselect it |
colLead != colModel.getLeadSelectionIndex()) |
254 |
if (e.isControlDown() && table. |
if (table.isEditing()) |
255 |
isCellSelected(table.rowAtPoint(begin),table.columnAtPoint(begin))) |
table.editingStopped(new ChangeEvent(e)); |
|
{ |
|
|
table.getSelectionModel(). |
|
|
removeSelectionInterval(table.rowAtPoint(begin), |
|
|
table.rowAtPoint(begin)); |
|
|
table.getColumnModel().getSelectionModel(). |
|
|
removeSelectionInterval(table.columnAtPoint(begin), |
|
|
table.columnAtPoint(begin)); |
|
256 |
} |
} |
|
else |
|
|
updateSelection(e.isControlDown()); |
|
|
|
|
|
// If we were editing, but the moved to another cell, stop editing |
|
|
if (rowLead != rowModel.getLeadSelectionIndex() || |
|
|
colLead != colModel.getLeadSelectionIndex()) |
|
|
if (table.isEditing()) |
|
|
table.editingStopped(new ChangeEvent(e)); |
|
257 |
} |
} |
258 |
|
|
259 |
public void mouseReleased(MouseEvent e) |
public void mouseReleased(MouseEvent e) |
260 |
{ |
{ |
261 |
begin = null; |
if (table.isEnabled()) |
262 |
curr = null; |
{ |
263 |
|
begin = null; |
264 |
|
curr = null; |
265 |
|
} |
266 |
|
} |
267 |
|
} |
268 |
|
|
269 |
|
/** |
270 |
|
* Listens for changes to the model property of the JTable and adjusts some |
271 |
|
* settings. |
272 |
|
* |
273 |
|
* @author Roman Kennke (kennke@aicas.com) |
274 |
|
*/ |
275 |
|
private class PropertyChangeHandler implements PropertyChangeListener |
276 |
|
{ |
277 |
|
/** |
278 |
|
* Receives notification if one of the JTable's properties changes. |
279 |
|
* |
280 |
|
* @param ev the property change event |
281 |
|
*/ |
282 |
|
public void propertyChange(PropertyChangeEvent ev) |
283 |
|
{ |
284 |
|
String propName = ev.getPropertyName(); |
285 |
|
if (propName.equals("model")) |
286 |
|
{ |
287 |
|
ListSelectionModel rowSel = table.getSelectionModel(); |
288 |
|
rowSel.clearSelection(); |
289 |
|
ListSelectionModel colSel = table.getColumnModel().getSelectionModel(); |
290 |
|
colSel.clearSelection(); |
291 |
|
TableModel model = table.getModel(); |
292 |
|
|
293 |
|
// Adjust lead and anchor selection indices of the row and column |
294 |
|
// selection models. |
295 |
|
if (model.getRowCount() > 0) |
296 |
|
{ |
297 |
|
rowSel.setAnchorSelectionIndex(0); |
298 |
|
rowSel.setLeadSelectionIndex(0); |
299 |
|
} |
300 |
|
else |
301 |
|
{ |
302 |
|
rowSel.setAnchorSelectionIndex(-1); |
303 |
|
rowSel.setLeadSelectionIndex(-1); |
304 |
|
} |
305 |
|
if (model.getColumnCount() > 0) |
306 |
|
{ |
307 |
|
colSel.setAnchorSelectionIndex(0); |
308 |
|
colSel.setLeadSelectionIndex(0); |
309 |
|
} |
310 |
|
else |
311 |
|
{ |
312 |
|
colSel.setAnchorSelectionIndex(-1); |
313 |
|
colSel.setLeadSelectionIndex(-1); |
314 |
|
} |
315 |
|
} |
316 |
} |
} |
317 |
} |
} |
318 |
|
|
392 |
table.setSelectionForeground(UIManager.getColor("Table.selectionForeground")); |
table.setSelectionForeground(UIManager.getColor("Table.selectionForeground")); |
393 |
table.setSelectionBackground(UIManager.getColor("Table.selectionBackground")); |
table.setSelectionBackground(UIManager.getColor("Table.selectionBackground")); |
394 |
table.setOpaque(true); |
table.setOpaque(true); |
|
|
|
|
highlightCellBorder = UIManager.getBorder("Table.focusCellHighlightBorder"); |
|
|
cellBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1); |
|
395 |
rendererPane = new CellRendererPane(); |
rendererPane = new CellRendererPane(); |
396 |
} |
} |
397 |
|
|
1121 |
mouseInputListener = createMouseInputListener(); |
mouseInputListener = createMouseInputListener(); |
1122 |
table.addMouseListener(mouseInputListener); |
table.addMouseListener(mouseInputListener); |
1123 |
table.addMouseMotionListener(mouseInputListener); |
table.addMouseMotionListener(mouseInputListener); |
1124 |
|
if (propertyChangeListener == null) |
1125 |
|
propertyChangeListener = new PropertyChangeHandler(); |
1126 |
|
table.addPropertyChangeListener(propertyChangeListener); |
1127 |
} |
} |
1128 |
|
|
1129 |
protected void uninstallDefaults() |
protected void uninstallDefaults() |
1157 |
table.removeKeyListener(keyListener); |
table.removeKeyListener(keyListener); |
1158 |
table.removeMouseListener(mouseInputListener); |
table.removeMouseListener(mouseInputListener); |
1159 |
table.removeMouseMotionListener(mouseInputListener); |
table.removeMouseMotionListener(mouseInputListener); |
1160 |
|
table.removePropertyChangeListener(propertyChangeListener); |
1161 |
|
propertyChangeListener = null; |
1162 |
} |
} |
1163 |
|
|
1164 |
public void installUI(JComponent comp) |
public void installUI(JComponent comp) |
1194 |
TableCellRenderer rend, TableModel data, |
TableCellRenderer rend, TableModel data, |
1195 |
int rowLead, int colLead) |
int rowLead, int colLead) |
1196 |
{ |
{ |
1197 |
boolean isSel = table.isCellSelected(row, col); |
boolean rowSelAllowed = table.getRowSelectionAllowed(); |
1198 |
boolean hasFocus = (table.getSelectionModel().getLeadSelectionIndex() == row) && table.hasFocus(); |
boolean colSelAllowed = table.getColumnSelectionAllowed(); |
1199 |
|
boolean isSel = false; |
1200 |
|
if (rowSelAllowed && colSelAllowed || !rowSelAllowed && !colSelAllowed) |
1201 |
|
isSel = table.isCellSelected(row, col); |
1202 |
|
else |
1203 |
|
isSel = table.isRowSelected(row) && table.getRowSelectionAllowed() |
1204 |
|
|| table.isColumnSelected(col) && table.getColumnSelectionAllowed(); |
1205 |
|
|
1206 |
|
// Determine the focused cell. The focused cell is the cell at the |
1207 |
|
// leadSelectionIndices of the row and column selection model. |
1208 |
|
ListSelectionModel rowSel = table.getSelectionModel(); |
1209 |
|
ListSelectionModel colSel = table.getColumnModel().getSelectionModel(); |
1210 |
|
boolean hasFocus = table.hasFocus() && table.isEnabled() |
1211 |
|
&& rowSel.getLeadSelectionIndex() == row |
1212 |
|
&& colSel.getLeadSelectionIndex() == col; |
1213 |
|
|
1214 |
Component comp = rend.getTableCellRendererComponent(table, |
Component comp = rend.getTableCellRendererComponent(table, |
1215 |
data.getValueAt(row, col), |
data.getValueAt(row, col), |
1216 |
isSel, hasFocus, row, col); |
isSel, hasFocus, row, col); |
1217 |
|
|
|
// If the cell is the lead selection then highlight its border |
|
|
if (table.getSelectionModel().getLeadSelectionIndex() == row |
|
|
&& table.getColumnModel().getSelectionModel(). |
|
|
getLeadSelectionIndex() == col) |
|
|
((JComponent) comp).setBorder(highlightCellBorder); |
|
|
else |
|
|
((JComponent) comp).setBorder(cellBorder); |
|
|
|
|
1218 |
rendererPane.paintComponent(g, comp, table, bounds); |
rendererPane.paintComponent(g, comp, table, bounds); |
1219 |
|
|
1220 |
// FIXME: this is manual painting of the Caret, why doesn't the |
// FIXME: this is manual painting of the Caret, why doesn't the |
1255 |
y = y0; |
y = y0; |
1256 |
TableColumn col = cols.getColumn(c); |
TableColumn col = cols.getColumn(c); |
1257 |
int width = col.getWidth(); |
int width = col.getWidth(); |
1258 |
|
int halfGapWidth = gap.width / 2; |
1259 |
|
int halfGapHeight = gap.height / 2; |
1260 |
for (int r = 0; r < nrows && y < ymax; ++r) |
for (int r = 0; r < nrows && y < ymax; ++r) |
1261 |
{ |
{ |
1262 |
Rectangle bounds = new Rectangle(x, y, width, height); |
Rectangle bounds = new Rectangle(x + halfGapWidth, |
1263 |
if (bounds.intersects(clip)) |
y + halfGapHeight + 1, |
1264 |
|
width - gap.width + 1, |
1265 |
|
height - gap.height); |
1266 |
|
if (bounds.intersects(clip)) |
1267 |
{ |
{ |
1268 |
paintCell( |
paintCell(gfx, r, c, bounds, table.getCellRenderer(r, c), |
|
gfx, |
|
|
r, |
|
|
c, |
|
|
bounds, |
|
|
table.getCellRenderer(r, c), |
|
1269 |
table.getModel(), |
table.getModel(), |
1270 |
table.getSelectionModel().getLeadSelectionIndex(), |
table.getSelectionModel().getLeadSelectionIndex(), |
1271 |
table.getColumnModel().getSelectionModel().getLeadSelectionIndex()); |
table.getColumnModel().getSelectionModel().getLeadSelectionIndex()); |
1272 |
} |
} |
1273 |
y += height; |
y += height; |
1274 |
} |
} |
1275 |
x += width; |
x += width; |
1276 |
} |
} |
1281 |
|
|
1282 |
Color grid = table.getGridColor(); |
Color grid = table.getGridColor(); |
1283 |
|
|
1284 |
// paint vertical grid lines |
// paint vertical grid lines |
1285 |
if (grid != null && table.getShowVerticalLines()) |
if (grid != null && table.getShowVerticalLines()) |
1286 |
{ |
{ |
1287 |
x = x0; |
x = x0; |
1291 |
for (int c = 0; c < ncols && x < xmax; ++c) |
for (int c = 0; c < ncols && x < xmax; ++c) |
1292 |
{ |
{ |
1293 |
x += cols.getColumn(c).getWidth(); |
x += cols.getColumn(c).getWidth(); |
1294 |
gfx.drawLine(x - gap.width, y0, x - gap.width, ymax); |
gfx.drawLine(x, y0, x, ymax); |
1295 |
paintedLine = true; |
paintedLine = true; |
1296 |
} |
} |
1297 |
gfx.setColor(save); |
gfx.setColor(save); |