KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicTableUI


1 /*
2  * @(#)BasicTableUI.java 1.148 06/04/18
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.basic;
9
10 import java.awt.*;
11 import java.awt.datatransfer.*;
12 import java.awt.dnd.*;
13 import java.awt.event.*;
14 import java.util.Enumeration JavaDoc;
15 import java.util.EventObject JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.TooManyListenersException JavaDoc;
18 import javax.swing.*;
19 import javax.swing.event.*;
20 import javax.swing.plaf.*;
21 import javax.swing.text.*;
22 import javax.swing.table.*;
23 import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag JavaDoc;
24 import com.sun.java.swing.SwingUtilities2;
25 import static com.sun.java.swing.SwingUtilities2.DRAG_FIX;
26
27
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30
31 import sun.swing.DefaultLookup;
32 import sun.swing.UIAction;
33
34 /**
35  * BasicTableUI implementation
36  *
37  * @version 1.148 04/18/06
38  * @author Philip Milne
39  * @author Shannon Hickey (improved drag recognition)
40  */

41 public class BasicTableUI extends TableUI
42 {
43
44 //
45
// Instance Variables
46
//
47

48     // The JTable that is delegating the painting to this UI.
49
protected JTable table;
50     protected CellRendererPane rendererPane;
51
52     // Listeners that are attached to the JTable
53
protected KeyListener keyListener;
54     protected FocusListener focusListener;
55     protected MouseInputListener mouseInputListener;
56
57     private Handler handler;
58
59     /**
60      * Local cache of Table's client property "Table.isFileList"
61      */

62     private boolean isFileList = false;
63
64 //
65
// Helper class for keyboard actions
66
//
67

68     private static class Actions extends UIAction {
69         private static final String JavaDoc CANCEL_EDITING = "cancel";
70         private static final String JavaDoc SELECT_ALL = "selectAll";
71         private static final String JavaDoc CLEAR_SELECTION = "clearSelection";
72         private static final String JavaDoc START_EDITING = "startEditing";
73
74         private static final String JavaDoc NEXT_ROW = "selectNextRow";
75         private static final String JavaDoc NEXT_ROW_CELL = "selectNextRowCell";
76     private static final String JavaDoc NEXT_ROW_EXTEND_SELECTION =
77                 "selectNextRowExtendSelection";
78         private static final String JavaDoc NEXT_ROW_CHANGE_LEAD =
79                 "selectNextRowChangeLead";
80         private static final String JavaDoc PREVIOUS_ROW = "selectPreviousRow";
81         private static final String JavaDoc PREVIOUS_ROW_CELL = "selectPreviousRowCell";
82     private static final String JavaDoc PREVIOUS_ROW_EXTEND_SELECTION =
83                 "selectPreviousRowExtendSelection";
84         private static final String JavaDoc PREVIOUS_ROW_CHANGE_LEAD =
85                 "selectPreviousRowChangeLead";
86
87         private static final String JavaDoc NEXT_COLUMN = "selectNextColumn";
88         private static final String JavaDoc NEXT_COLUMN_CELL = "selectNextColumnCell";
89     private static final String JavaDoc NEXT_COLUMN_EXTEND_SELECTION =
90                 "selectNextColumnExtendSelection";
91         private static final String JavaDoc NEXT_COLUMN_CHANGE_LEAD =
92                 "selectNextColumnChangeLead";
93         private static final String JavaDoc PREVIOUS_COLUMN = "selectPreviousColumn";
94         private static final String JavaDoc PREVIOUS_COLUMN_CELL =
95                 "selectPreviousColumnCell";
96     private static final String JavaDoc PREVIOUS_COLUMN_EXTEND_SELECTION =
97                 "selectPreviousColumnExtendSelection";
98         private static final String JavaDoc PREVIOUS_COLUMN_CHANGE_LEAD =
99                 "selectPreviousColumnChangeLead";
100
101         private static final String JavaDoc SCROLL_LEFT_CHANGE_SELECTION =
102                 "scrollLeftChangeSelection";
103         private static final String JavaDoc SCROLL_LEFT_EXTEND_SELECTION =
104                 "scrollLeftExtendSelection";
105         private static final String JavaDoc SCROLL_RIGHT_CHANGE_SELECTION =
106                 "scrollRightChangeSelection";
107         private static final String JavaDoc SCROLL_RIGHT_EXTEND_SELECTION =
108                 "scrollRightExtendSelection";
109
110     private static final String JavaDoc SCROLL_UP_CHANGE_SELECTION =
111                 "scrollUpChangeSelection";
112         private static final String JavaDoc SCROLL_UP_EXTEND_SELECTION =
113                 "scrollUpExtendSelection";
114         private static final String JavaDoc SCROLL_DOWN_CHANGE_SELECTION =
115                 "scrollDownChangeSelection";
116         private static final String JavaDoc SCROLL_DOWN_EXTEND_SELECTION =
117                 "scrollDownExtendSelection";
118
119         private static final String JavaDoc FIRST_COLUMN =
120                 "selectFirstColumn";
121         private static final String JavaDoc FIRST_COLUMN_EXTEND_SELECTION =
122                 "selectFirstColumnExtendSelection";
123         private static final String JavaDoc LAST_COLUMN =
124                 "selectLastColumn";
125         private static final String JavaDoc LAST_COLUMN_EXTEND_SELECTION =
126                 "selectLastColumnExtendSelection";
127
128         private static final String JavaDoc FIRST_ROW =
129                 "selectFirstRow";
130         private static final String JavaDoc FIRST_ROW_EXTEND_SELECTION =
131                 "selectFirstRowExtendSelection";
132         private static final String JavaDoc LAST_ROW =
133                 "selectLastRow";
134         private static final String JavaDoc LAST_ROW_EXTEND_SELECTION =
135                 "selectLastRowExtendSelection";
136
137         // add the lead item to the selection without changing lead or anchor
138
private static final String JavaDoc ADD_TO_SELECTION = "addToSelection";
139
140         // toggle the selected state of the lead item and move the anchor to it
141
private static final String JavaDoc TOGGLE_AND_ANCHOR = "toggleAndAnchor";
142
143         // extend the selection to the lead item
144
private static final String JavaDoc EXTEND_TO = "extendTo";
145
146         // move the anchor to the lead and ensure only that item is selected
147
private static final String JavaDoc MOVE_SELECTION_TO = "moveSelectionTo";
148
149         protected int dx;
150         protected int dy;
151     protected boolean extend;
152     protected boolean inSelection;
153         protected boolean forwards;
154         protected boolean vertically;
155         protected boolean toLimit;
156
157     protected int leadRow;
158     protected int leadColumn;
159
160         Actions(String JavaDoc name) {
161             super(name);
162         }
163
164         Actions(String JavaDoc name, int dx, int dy, boolean extend,
165                 boolean inSelection) {
166             super(name);
167
168             // Actions spcifying true for "inSelection" are
169
// fairly sensitive to bad parameter values. They require
170
// that one of dx and dy be 0 and the other be -1 or 1.
171
// Bogus parameter values could cause an infinite loop.
172
// To prevent any problems we massage the params here
173
// and complain if we get something we can't deal with.
174
if (inSelection) {
175                 this.inSelection = true;
176
177                 // look at the sign of dx and dy only
178
dx = sign(dx);
179                 dy = sign(dy);
180
181                 // make sure one is zero, but not both
182
assert (dx == 0 || dy == 0) && !(dx == 0 && dy == 0);
183             }
184
185             this.dx = dx;
186             this.dy = dy;
187             this.extend = extend;
188         }
189
190         Actions(String JavaDoc name, boolean extend, boolean forwards,
191                 boolean vertically, boolean toLimit) {
192             this(name, 0, 0, extend, false);
193             this.forwards = forwards;
194             this.vertically = vertically;
195             this.toLimit = toLimit;
196         }
197
198     private static int clipToRange(int i, int a, int b) {
199         return Math.min(Math.max(i, a), b-1);
200     }
201
202     private void moveWithinTableRange(JTable table, int dx, int dy) {
203             leadRow = clipToRange(leadRow+dy, 0, table.getRowCount());
204             leadColumn = clipToRange(leadColumn+dx, 0, table.getColumnCount());
205     }
206
207         private static int sign(int num) {
208             return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
209         }
210
211         /**
212          * Called to move within the selected range of the given JTable.
213          * This method uses the table's notion of selection, which is
214          * important to allow the user to navigate between items visually
215          * selected on screen. This notion may or may not be the same as
216          * what could be determined by directly querying the selection models.
217          * It depends on certain table properties (such as whether or not
218          * row or column selection is allowed). When performing modifications,
219          * it is recommended that caution be taken in order to preserve
220          * the intent of this method, especially when deciding whether to
221          * query the selection models or interact with JTable directly.
222          */

223     private boolean moveWithinSelectedRange(JTable table, int dx, int dy,
224                 ListSelectionModel rsm, ListSelectionModel csm) {
225
226             // Note: The Actions constructor ensures that only one of
227
// dx and dy is 0, and the other is either -1 or 1
228

229             // find out how many items the table is showing as selected
230
// and the range of items to navigate through
231
int totalCount;
232             int minX, maxX, minY, maxY;
233
234             boolean rs = table.getRowSelectionAllowed();
235             boolean cs = table.getColumnSelectionAllowed();
236
237             // both column and row selection
238
if (rs && cs) {
239                 totalCount = table.getSelectedRowCount() * table.getSelectedColumnCount();
240                 minX = csm.getMinSelectionIndex();
241                 maxX = csm.getMaxSelectionIndex();
242                 minY = rsm.getMinSelectionIndex();
243                 maxY = rsm.getMaxSelectionIndex();
244             // row selection only
245
} else if (rs) {
246                 totalCount = table.getSelectedRowCount();
247                 minX = 0;
248                 maxX = table.getColumnCount() - 1;
249                 minY = rsm.getMinSelectionIndex();
250                 maxY = rsm.getMaxSelectionIndex();
251             // column selection only
252
} else if (cs) {
253                 totalCount = table.getSelectedColumnCount();
254                 minX = csm.getMinSelectionIndex();
255                 maxX = csm.getMaxSelectionIndex();
256                 minY = 0;
257                 maxY = table.getRowCount() - 1;
258             // no selection allowed
259
} else {
260                 totalCount = 0;
261                 // A bogus assignment to stop javac from complaining
262
// about unitialized values. In this case, these
263
// won't even be used.
264
minX = maxX = minY = maxY = 0;
265             }
266
267             // For some cases, there is no point in trying to stay within the
268
// selected area. Instead, move outside the selection, wrapping at
269
// the table boundaries. The cases are:
270
boolean stayInSelection;
271
272             // - nothing selected
273
if (totalCount == 0 ||
274                     // - one item selected, and the lead is already selected
275
(totalCount == 1 && table.isCellSelected(leadRow, leadColumn))) {
276
277                 stayInSelection = false;
278
279                 maxX = table.getColumnCount() - 1;
280                 maxY = table.getRowCount() - 1;
281
282                 // the mins are calculated like this in case the max is -1
283
minX = Math.min(0, maxX);
284                 minY = Math.min(0, maxY);
285             } else {
286                 stayInSelection = true;
287             }
288
289             // the algorithm below isn't prepared to deal with -1 lead/anchor
290
// so massage appropriately here first
291
if (dy == 1 && leadColumn == -1) {
292                 leadColumn = minX;
293                 leadRow = -1;
294             } else if (dx == 1 && leadRow == -1) {
295                 leadRow = minY;
296                 leadColumn = -1;
297             } else if (dy == -1 && leadColumn == -1) {
298                 leadColumn = maxX;
299                 leadRow = maxY + 1;
300             } else if (dx == -1 && leadRow == -1) {
301                 leadRow = maxY;
302                 leadColumn = maxX + 1;
303             }
304
305             // In cases where the lead is not within the search range,
306
// we need to bring it within one cell for the the search
307
// to work properly. Check these here.
308
leadRow = Math.min(Math.max(leadRow, minY - 1), maxY + 1);
309             leadColumn = Math.min(Math.max(leadColumn, minX - 1), maxX + 1);
310
311             // find the next position, possibly looping until it is selected
312
do {
313                 calcNextPos(dx, minX, maxX, dy, minY, maxY);
314             } while (stayInSelection && !table.isCellSelected(leadRow, leadColumn));
315
316             return stayInSelection;
317     }
318
319         /**
320          * Find the next lead row and column based on the given
321          * dx/dy and max/min values.
322          */

323         private void calcNextPos(int dx, int minX, int maxX,
324                                  int dy, int minY, int maxY) {
325
326             if (dx != 0) {
327                 leadColumn += dx;
328                 if (leadColumn > maxX) {
329                     leadColumn = minX;
330                     leadRow++;
331                     if (leadRow > maxY) {
332                         leadRow = minY;
333                     }
334                 } else if (leadColumn < minX) {
335                     leadColumn = maxX;
336                     leadRow--;
337                     if (leadRow < minY) {
338                         leadRow = maxY;
339                     }
340                 }
341             } else {
342                 leadRow += dy;
343                 if (leadRow > maxY) {
344                     leadRow = minY;
345                     leadColumn++;
346                     if (leadColumn > maxX) {
347                         leadColumn = minX;
348                     }
349                 } else if (leadRow < minY) {
350                     leadRow = maxY;
351                     leadColumn--;
352                     if (leadColumn < minX) {
353                         leadColumn = maxX;
354                     }
355                 }
356             }
357         }
358
359         public void actionPerformed(ActionEvent e) {
360             String JavaDoc key = getName();
361             JTable table = (JTable)e.getSource();
362
363             ListSelectionModel rsm = table.getSelectionModel();
364             leadRow = getAdjustedLead(table, true, rsm);
365
366             ListSelectionModel csm = table.getColumnModel().getSelectionModel();
367             leadColumn = getAdjustedLead(table, false, csm);
368
369             if (!table.getComponentOrientation().isLeftToRight()) {
370                 if (key == SCROLL_LEFT_CHANGE_SELECTION ||
371                         key == SCROLL_LEFT_EXTEND_SELECTION) {
372                     forwards = true;
373                 } else if (key == SCROLL_RIGHT_CHANGE_SELECTION ||
374                         key == SCROLL_RIGHT_EXTEND_SELECTION) {
375                     forwards = false;
376                 }
377             }
378
379             if (key == SCROLL_LEFT_CHANGE_SELECTION || // Paging Actions
380
key == SCROLL_LEFT_EXTEND_SELECTION ||
381                     key == SCROLL_RIGHT_CHANGE_SELECTION ||
382                     key == SCROLL_RIGHT_EXTEND_SELECTION ||
383                 key == SCROLL_UP_CHANGE_SELECTION ||
384                     key == SCROLL_UP_EXTEND_SELECTION ||
385                     key == SCROLL_DOWN_CHANGE_SELECTION ||
386                     key == SCROLL_DOWN_EXTEND_SELECTION ||
387                     key == FIRST_COLUMN ||
388                     key == FIRST_COLUMN_EXTEND_SELECTION ||
389                     key == FIRST_ROW ||
390                     key == FIRST_ROW_EXTEND_SELECTION ||
391                     key == LAST_COLUMN ||
392                     key == LAST_COLUMN_EXTEND_SELECTION ||
393                     key == LAST_ROW ||
394                     key == LAST_ROW_EXTEND_SELECTION) {
395                 if (toLimit) {
396                     if (vertically) {
397                         int rowCount = table.getRowCount();
398                         this.dx = 0;
399                         this.dy = forwards ? rowCount : -rowCount;
400                     }
401                     else {
402                         int colCount = table.getColumnCount();
403                         this.dx = forwards ? colCount : -colCount;
404                         this.dy = 0;
405                     }
406                 }
407                 else {
408                     if (!(table.getParent().getParent() instanceof
409                             JScrollPane)) {
410                         return;
411                     }
412     
413                     Dimension delta = table.getParent().getSize();
414
415                     if (vertically) {
416                         Rectangle r = table.getCellRect(leadRow, 0, true);
417                         r.y += forwards ? delta.height : -delta.height;
418                         this.dx = 0;
419                         int newRow = table.rowAtPoint(r.getLocation());
420                         if (newRow == -1 && forwards) {
421                             newRow = table.getRowCount();
422                         }
423                         this.dy = newRow - leadRow;
424                     }
425                     else {
426                         Rectangle r = table.getCellRect(0, leadColumn, true);
427                         r.x += forwards ? delta.width : -delta.width;
428                         int newColumn = table.columnAtPoint(r.getLocation());
429                         if (newColumn == -1 && forwards) {
430                             newColumn = table.getColumnCount();
431                         }
432                         this.dx = newColumn - leadColumn;
433                         this.dy = 0;
434                     }
435                 }
436             }
437             if (key == NEXT_ROW || // Navigate Actions
438
key == NEXT_ROW_CELL ||
439                     key == NEXT_ROW_EXTEND_SELECTION ||
440                     key == NEXT_ROW_CHANGE_LEAD ||
441                     key == NEXT_COLUMN ||
442                     key == NEXT_COLUMN_CELL ||
443                     key == NEXT_COLUMN_EXTEND_SELECTION ||
444                     key == NEXT_COLUMN_CHANGE_LEAD ||
445                     key == PREVIOUS_ROW ||
446                     key == PREVIOUS_ROW_CELL ||
447                     key == PREVIOUS_ROW_EXTEND_SELECTION ||
448                     key == PREVIOUS_ROW_CHANGE_LEAD ||
449                     key == PREVIOUS_COLUMN ||
450                     key == PREVIOUS_COLUMN_CELL ||
451                     key == PREVIOUS_COLUMN_EXTEND_SELECTION ||
452                     key == PREVIOUS_COLUMN_CHANGE_LEAD ||
453                     // Paging Actions.
454
key == SCROLL_LEFT_CHANGE_SELECTION ||
455                     key == SCROLL_LEFT_EXTEND_SELECTION ||
456                     key == SCROLL_RIGHT_CHANGE_SELECTION ||
457                     key == SCROLL_RIGHT_EXTEND_SELECTION ||
458                 key == SCROLL_UP_CHANGE_SELECTION ||
459                     key == SCROLL_UP_EXTEND_SELECTION ||
460                     key == SCROLL_DOWN_CHANGE_SELECTION ||
461                     key == SCROLL_DOWN_EXTEND_SELECTION ||
462                     key == FIRST_COLUMN ||
463                     key == FIRST_COLUMN_EXTEND_SELECTION ||
464                     key == FIRST_ROW ||
465                     key == FIRST_ROW_EXTEND_SELECTION ||
466                     key == LAST_COLUMN ||
467                     key == LAST_COLUMN_EXTEND_SELECTION ||
468                     key == LAST_ROW ||
469                     key == LAST_ROW_EXTEND_SELECTION) {
470
471                 if (table.isEditing() &&
472                         !table.getCellEditor().stopCellEditing()) {
473                     return;
474                 }
475     
476                 // Unfortunately, this strategy introduces bugs because
477
// of the asynchronous nature of requestFocus() call below.
478
// Introducing a delay with invokeLater() makes this work
479
// in the typical case though race conditions then allow
480
// focus to disappear altogether. The right solution appears
481
// to be to fix requestFocus() so that it queues a request
482
// for the focus regardless of who owns the focus at the
483
// time the call to requestFocus() is made. The optimisation
484
// to ignore the call to requestFocus() when the component
485
// already has focus may ligitimately be made as the
486
// request focus event is dequeued, not before.
487

488                 // boolean wasEditingWithFocus = table.isEditing() &&
489
// table.getEditorComponent().isFocusOwner();
490

491                 boolean changeLead = false;
492                 if (key == NEXT_ROW_CHANGE_LEAD || key == PREVIOUS_ROW_CHANGE_LEAD) {
493                     changeLead = (rsm.getSelectionMode()
494                                      == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
495                 } else if (key == NEXT_COLUMN_CHANGE_LEAD || key == PREVIOUS_COLUMN_CHANGE_LEAD) {
496                     changeLead = (csm.getSelectionMode()
497                                      == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
498                 }
499
500                 if (changeLead) {
501                     moveWithinTableRange(table, dx, dy);
502                     if (dy != 0) {
503                         // casting should be safe since the action is only enabled
504
// for DefaultListSelectionModel
505
((DefaultListSelectionModel)rsm).moveLeadSelectionIndex(leadRow);
506                         if (getAdjustedLead(table, false, csm) == -1
507                                 && table.getColumnCount() > 0) {
508
509                             ((DefaultListSelectionModel)csm).moveLeadSelectionIndex(0);
510                         }
511                     } else {
512                         // casting should be safe since the action is only enabled
513
// for DefaultListSelectionModel
514
((DefaultListSelectionModel)csm).moveLeadSelectionIndex(leadColumn);
515                         if (getAdjustedLead(table, true, rsm) == -1
516                                 && table.getRowCount() > 0) {
517
518                             ((DefaultListSelectionModel)rsm).moveLeadSelectionIndex(0);
519                         }
520                     }
521
522                     Rectangle cellRect = table.getCellRect(leadRow, leadColumn, false);
523                     if (cellRect != null) {
524                         table.scrollRectToVisible(cellRect);
525                     }
526                 } else if (!inSelection) {
527                     moveWithinTableRange(table, dx, dy);
528                     table.changeSelection(leadRow, leadColumn, false, extend);
529                 }
530                 else {
531                     if (table.getRowCount() <= 0 || table.getColumnCount() <= 0) {
532                         // bail - don't try to move selection on an empty table
533
return;
534                     }
535
536                     if (moveWithinSelectedRange(table, dx, dy, rsm, csm)) {
537                         // this is the only way we have to set both the lead
538
// and the anchor without changing the selection
539
if (rsm.isSelectedIndex(leadRow)) {
540                             rsm.addSelectionInterval(leadRow, leadRow);
541                         } else {
542                             rsm.removeSelectionInterval(leadRow, leadRow);
543                         }
544
545                         if (csm.isSelectedIndex(leadColumn)) {
546                             csm.addSelectionInterval(leadColumn, leadColumn);
547                         } else {
548                             csm.removeSelectionInterval(leadColumn, leadColumn);
549                         }
550
551                         Rectangle cellRect = table.getCellRect(leadRow, leadColumn, false);
552                         if (cellRect != null) {
553                             table.scrollRectToVisible(cellRect);
554                         }
555                     }
556                     else {
557                         table.changeSelection(leadRow, leadColumn,
558                                 false, false);
559                     }
560                 }
561     
562                 /*
563                 if (wasEditingWithFocus) {
564                     table.editCellAt(leadRow, leadColumn);
565                     final Component editorComp = table.getEditorComponent();
566                     if (editorComp != null) {
567                         SwingUtilities.invokeLater(new Runnable() {
568                             public void run() {
569                                 editorComp.requestFocus();
570                             }
571                         });
572                     }
573                 }
574                 */

575             } else if (key == CANCEL_EDITING) {
576                 table.removeEditor();
577             } else if (key == SELECT_ALL) {
578                 table.selectAll();
579             } else if (key == CLEAR_SELECTION) {
580                 table.clearSelection();
581             } else if (key == START_EDITING) {
582                 if (!table.hasFocus()) {
583                     CellEditor cellEditor = table.getCellEditor();
584                     if (cellEditor != null && !cellEditor.stopCellEditing()) {
585                         return;
586                     }
587                     table.requestFocus();
588                     return;
589                 }
590                 table.editCellAt(leadRow, leadColumn);
591                 Component editorComp = table.getEditorComponent();
592                 if (editorComp != null) {
593                     editorComp.requestFocus();
594                 }
595             } else if (key == ADD_TO_SELECTION) {
596                 if (!table.isCellSelected(leadRow, leadColumn)) {
597                     int oldAnchorRow = rsm.getAnchorSelectionIndex();
598                     int oldAnchorColumn = csm.getAnchorSelectionIndex();
599                     rsm.setValueIsAdjusting(true);
600                     csm.setValueIsAdjusting(true);
601                     table.changeSelection(leadRow, leadColumn, true, false);
602                     rsm.setAnchorSelectionIndex(oldAnchorRow);
603                     csm.setAnchorSelectionIndex(oldAnchorColumn);
604                     rsm.setValueIsAdjusting(false);
605                     csm.setValueIsAdjusting(false);
606                 }
607             } else if (key == TOGGLE_AND_ANCHOR) {
608                 table.changeSelection(leadRow, leadColumn, true, false);
609             } else if (key == EXTEND_TO) {
610                 table.changeSelection(leadRow, leadColumn, false, true);
611             } else if (key == MOVE_SELECTION_TO) {
612                 table.changeSelection(leadRow, leadColumn, false, false);
613             }
614         }
615
616         public boolean isEnabled(Object JavaDoc sender) {
617             String JavaDoc key = getName();
618
619             if (sender instanceof JTable &&
620                 Boolean.TRUE.equals(((JTable)sender).getClientProperty("Table.isFileList"))) {
621                 if (key == NEXT_COLUMN ||
622                         key == NEXT_COLUMN_CELL ||
623                         key == NEXT_COLUMN_EXTEND_SELECTION ||
624                         key == NEXT_COLUMN_CHANGE_LEAD ||
625                         key == PREVIOUS_COLUMN ||
626                         key == PREVIOUS_COLUMN_CELL ||
627                         key == PREVIOUS_COLUMN_EXTEND_SELECTION ||
628                         key == PREVIOUS_COLUMN_CHANGE_LEAD ||
629                         key == SCROLL_LEFT_CHANGE_SELECTION ||
630                         key == SCROLL_LEFT_EXTEND_SELECTION ||
631                         key == SCROLL_RIGHT_CHANGE_SELECTION ||
632                         key == SCROLL_RIGHT_EXTEND_SELECTION ||
633                         key == FIRST_COLUMN ||
634                         key == FIRST_COLUMN_EXTEND_SELECTION ||
635                         key == LAST_COLUMN ||
636                         key == LAST_COLUMN_EXTEND_SELECTION ||
637                         key == NEXT_ROW_CELL ||
638                         key == PREVIOUS_ROW_CELL) {
639
640                     return false;
641                 }
642             }
643
644             if (key == CANCEL_EDITING && sender instanceof JTable) {
645                 return ((JTable)sender).isEditing();
646             } else if (key == NEXT_ROW_CHANGE_LEAD ||
647                        key == PREVIOUS_ROW_CHANGE_LEAD) {
648                 // discontinuous selection actions are only enabled for
649
// DefaultListSelectionModel
650
return sender != null &&
651                        ((JTable)sender).getSelectionModel()
652                            instanceof DefaultListSelectionModel;
653             } else if (key == NEXT_COLUMN_CHANGE_LEAD ||
654                        key == PREVIOUS_COLUMN_CHANGE_LEAD) {
655                 // discontinuous selection actions are only enabled for
656
// DefaultListSelectionModel
657
return sender != null &&
658                        ((JTable)sender).getColumnModel().getSelectionModel()
659                            instanceof DefaultListSelectionModel;
660             } else if (key == ADD_TO_SELECTION && sender instanceof JTable) {
661                 // This action is typically bound to SPACE.
662
// If the table is already in an editing mode, SPACE should
663
// simply enter a space character into the table, and not
664
// select a cell. Likewise, if the lead cell is already selected
665
// then hitting SPACE should just enter a space character
666
// into the cell and begin editing. In both of these cases
667
// this action will be disabled.
668
JTable table = (JTable)sender;
669                 int leadRow = getAdjustedLead(table, true);
670                 int leadCol = getAdjustedLead(table, false);
671                 return !(table.isEditing() || table.isCellSelected(leadRow, leadCol));
672             }
673
674             return true;
675         }
676     }
677
678
679 //
680
// The Table's Key listener
681
//
682

683     /**
684      * This inner class is marked &quot;public&quot; due to a compiler bug.
685      * This class should be treated as a &quot;protected&quot; inner class.
686      * Instantiate it only within subclasses of BasicTableUI.
687      * <p>As of Java 2 platform v1.3 this class is no longer used.
688      * Instead <code>JTable</code>
689      * overrides <code>processKeyBinding</code> to dispatch the event to
690      * the current <code>TableCellEditor</code>.
691      */

692      public class KeyHandler implements KeyListener {
693         // NOTE: This class exists only for backward compatability. All
694
// its functionality has been moved into Handler. If you need to add
695
// new functionality add it to the Handler, but make sure this
696
// class calls into the Handler.
697
public void keyPressed(KeyEvent e) {
698             getHandler().keyPressed(e);
699         }
700
701         public void keyReleased(KeyEvent e) {
702             getHandler().keyReleased(e);
703         }
704
705         public void keyTyped(KeyEvent e) {
706             getHandler().keyTyped(e);
707         }
708     }
709
710 //
711
// The Table's focus listener
712
//
713

714     /**
715      * This inner class is marked &quot;public&quot; due to a compiler bug.
716      * This class should be treated as a &quot;protected&quot; inner class.
717      * Instantiate it only within subclasses of BasicTableUI.
718      */

719     public class FocusHandler implements FocusListener {
720         // NOTE: This class exists only for backward compatability. All
721
// its functionality has been moved into Handler. If you need to add
722
// new functionality add it to the Handler, but make sure this
723
// class calls into the Handler.
724
public void focusGained(FocusEvent e) {
725             getHandler().focusGained(e);
726         }
727
728         public void focusLost(FocusEvent e) {
729             getHandler().focusLost(e);
730         }
731     }
732
733 //
734
// The Table's mouse and mouse motion listeners
735
//
736

737     /**
738      * This inner class is marked &quot;public&quot; due to a compiler bug.
739      * This class should be treated as a &quot;protected&quot; inner class.
740      * Instantiate it only within subclasses of BasicTableUI.
741      */

742     public class MouseInputHandler implements MouseInputListener {
743         // NOTE: This class exists only for backward compatability. All
744
// its functionality has been moved into Handler. If you need to add
745
// new functionality add it to the Handler, but make sure this
746
// class calls into the Handler.
747
public void mouseClicked(MouseEvent e) {
748             getHandler().mouseClicked(e);
749         }
750
751         public void mousePressed(MouseEvent e) {
752             getHandler().mousePressed(e);
753     }
754
755         public void mouseReleased(MouseEvent e) {
756             getHandler().mouseReleased(e);
757         }
758
759         public void mouseEntered(MouseEvent e) {
760             getHandler().mouseEntered(e);
761         }
762
763         public void mouseExited(MouseEvent e) {
764             getHandler().mouseExited(e);
765         }
766
767         public void mouseMoved(MouseEvent e) {
768             getHandler().mouseMoved(e);
769         }
770
771         public void mouseDragged(MouseEvent e) {
772             getHandler().mouseDragged(e);
773         }
774     }
775
776     private class Handler implements FocusListener, MouseInputListener,
777             PropertyChangeListener JavaDoc {
778
779         // FocusListener
780
private void repaintLeadCell( ) {
781             int lr = getAdjustedLead(table, true);
782             int lc = getAdjustedLead(table, false);
783
784             if (lr < 0 || lc < 0) {
785                 return;
786             }
787
788             Rectangle dirtyRect = table.getCellRect(lr, lc, false);
789             table.repaint(dirtyRect);
790         }
791
792         public void focusGained(FocusEvent e) {
793             repaintLeadCell();
794         }
795
796         public void focusLost(FocusEvent e) {
797             repaintLeadCell();
798         }
799
800
801         // KeyListener
802
public void keyPressed(KeyEvent e) { }
803
804         public void keyReleased(KeyEvent e) { }
805
806         public void keyTyped(KeyEvent e) {
807             KeyStroke keyStroke = KeyStroke.getKeyStroke(e.getKeyChar(),
808                     e.getModifiers());
809
810             // We register all actions using ANCESTOR_OF_FOCUSED_COMPONENT
811
// which means that we might perform the appropriate action
812
// in the table and then forward it to the editor if the editor
813
// had focus. Make sure this doesn't happen by checking our
814
// InputMaps.
815
InputMap map = table.getInputMap(JComponent.WHEN_FOCUSED);
816         if (map != null && map.get(keyStroke) != null) {
817         return;
818         }
819         map = table.getInputMap(JComponent.
820                   WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
821         if (map != null && map.get(keyStroke) != null) {
822         return;
823         }
824
825         keyStroke = KeyStroke.getKeyStrokeForEvent(e);
826
827             // The AWT seems to generate an unconsumed \r event when
828
// ENTER (\n) is pressed.
829
if (e.getKeyChar() == '\r') {
830                 return;
831             }
832
833             int leadRow = getAdjustedLead(table, true);
834             int leadColumn = getAdjustedLead(table, false);
835             if (leadRow != -1 && leadColumn != -1 && !table.isEditing()) {
836                 if (!table.editCellAt(leadRow, leadColumn)) {
837                     return;
838                 }
839             }
840
841             // Forwarding events this way seems to put the component
842
// in a state where it believes it has focus. In reality
843
// the table retains focus - though it is difficult for
844
// a user to tell, since the caret is visible and flashing.
845

846             // Calling table.requestFocus() here, to get the focus back to
847
// the table, seems to have no effect.
848

849             Component editorComp = table.getEditorComponent();
850             if (table.isEditing() && editorComp != null) {
851                 if (editorComp instanceof JComponent) {
852                     JComponent component = (JComponent)editorComp;
853             map = component.getInputMap(JComponent.WHEN_FOCUSED);
854             Object JavaDoc binding = (map != null) ? map.get(keyStroke) : null;
855             if (binding == null) {
856             map = component.getInputMap(JComponent.
857                      WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
858             binding = (map != null) ? map.get(keyStroke) : null;
859             }
860             if (binding != null) {
861             ActionMap am = component.getActionMap();
862             Action action = (am != null) ? am.get(binding) : null;
863             if (action != null && SwingUtilities.
864                 notifyAction(action, keyStroke, e, component,
865                      e.getModifiers())) {
866                 e.consume();
867             }
868             }
869                 }
870             }
871         }
872
873
874         // MouseInputListener
875

876         // Component receiving mouse events during editing.
877
// May not be editorComponent.
878
protected Component dispatchComponent;
879     private boolean selectedOnPress;
880
881         public void mouseClicked(MouseEvent e) {}
882
883         protected void setDispatchComponent(MouseEvent e) {
884             Component editorComponent = table.getEditorComponent();
885             Point p = e.getPoint();
886             Point p2 = SwingUtilities.convertPoint(table, p, editorComponent);
887             dispatchComponent =
888                     SwingUtilities.getDeepestComponentAt(editorComponent,
889                             p2.x, p2.y);
890             SwingUtilities2.setSkipClickCount(dispatchComponent,
891                                               e.getClickCount() - 1);
892         }
893
894         protected boolean repostEvent(MouseEvent e) {
895         // Check for isEditing() in case another event has
896
// caused the editor to be removed. See bug #4306499.
897
if (dispatchComponent == null || !table.isEditing()) {
898                 return false;
899             }
900             MouseEvent e2 = SwingUtilities.convertMouseEvent(table, e,
901                     dispatchComponent);
902             dispatchComponent.dispatchEvent(e2);
903             return true;
904         }
905
906         protected void setValueIsAdjusting(boolean flag) {
907             table.getSelectionModel().setValueIsAdjusting(flag);
908             table.getColumnModel().getSelectionModel().
909                     setValueIsAdjusting(flag);
910         }
911
912     private boolean shouldIgnore0(MouseEvent e) {
913             return e.isConsumed() || SwingUtilities2.shouldIgnore(e, table);
914     }
915
916         public void mousePressed(MouseEvent e) {
917         if (e.isConsumed()) {
918         selectedOnPress = false;
919         return;
920         }
921         selectedOnPress = true;
922         adjustFocusAndSelection(e);
923     }
924
925     void adjustFocusAndSelection(MouseEvent e) {
926         if (shouldIgnore0(e)) {
927             return;
928         }
929
930             Point p = e.getPoint();
931             int row = table.rowAtPoint(p);
932             int column = table.columnAtPoint(p);
933             // Fix for 4835633
934
if (pointOutsidePrefSize(table, row, column, p)) {
935                 // If shift is down in multi-select, we should just return.
936
// For single select or non-shift-click, clear the selection
937
if (e.getID() == MouseEvent.MOUSE_PRESSED &&
938                     (!e.isShiftDown() ||
939                      table.getSelectionModel().getSelectionMode() ==
940                      ListSelectionModel.SINGLE_SELECTION)) {
941                     table.clearSelection();
942                     TableCellEditor tce = table.getCellEditor();
943                     if (tce != null) {
944                         tce.stopCellEditing();
945                     }
946                 }
947                 return;
948             }
949         // The autoscroller can generate drag events outside the
950
// table's range.
951
if ((column == -1) || (row == -1)) {
952                 return;
953             }
954
955             if (table.editCellAt(row, column, e)) {
956                 setDispatchComponent(e);
957                 repostEvent(e);
958             }
959         else {
960                 SwingUtilities2.adjustFocus(table);
961         }
962
963             CellEditor editor = table.getCellEditor();
964             if (editor == null || editor.shouldSelectCell(e)) {
965         boolean adjusting = (e.getID() == MouseEvent.MOUSE_PRESSED) ?
966                         true : false;
967                 setValueIsAdjusting(adjusting);
968                 makeSelectionChange(row, column, e);
969         }
970         }
971
972         protected void makeSelectionChange(int row, int column, MouseEvent e) {
973             boolean ctrl = e.isControlDown();
974
975             // Apply the selection state of the anchor to all cells between it and the
976
// current cell, and then select the current cell.
977
// For mustang, where API changes are allowed, this logic will moved to
978
// JTable.changeSelection()
979
if (ctrl && e.isShiftDown()) {
980                 ListSelectionModel rm = table.getSelectionModel();
981                 ListSelectionModel cm = table.getColumnModel().getSelectionModel();
982                 int anchorRow = rm.getAnchorSelectionIndex();
983                 int anchorCol = cm.getAnchorSelectionIndex();
984
985                 boolean anchorSelected = true;
986                 if (anchorRow == -1 || anchorRow >= table.getRowCount()) {
987                     anchorRow = 0;
988                     anchorSelected = false;
989                 }
990
991                 if (anchorCol == -1 || anchorCol >= table.getColumnCount()) {
992                     anchorCol = 0;
993                     anchorSelected = false;
994                 }
995
996                 if (anchorSelected && table.isCellSelected(anchorRow, anchorCol)) {
997                     rm.addSelectionInterval(anchorRow, row);
998                     cm.addSelectionInterval(anchorCol, column);
999                 } else {
1000                    rm.removeSelectionInterval(anchorRow, row);
1001                    cm.removeSelectionInterval(anchorCol, column);
1002
1003                    // This is only to match the windows explorer behavior in JFileChooser
1004
if (isFileList) {
1005                        rm.addSelectionInterval(row, row);
1006                        rm.setAnchorSelectionIndex(anchorRow);
1007                        cm.addSelectionInterval(column, column);
1008                        cm.setAnchorSelectionIndex(anchorCol);
1009                    }
1010                }
1011            } else {
1012                table.changeSelection(row, column, ctrl, !ctrl && e.isShiftDown());
1013            }
1014        }
1015
1016        public void mouseReleased(MouseEvent e) {
1017        if (selectedOnPress) {
1018        if (shouldIgnore0(e)) {
1019            return;
1020        }
1021
1022        repostEvent(e);
1023        dispatchComponent = null;
1024        setValueIsAdjusting(false);
1025        } else {
1026        adjustFocusAndSelection(e);
1027        }
1028        }
1029
1030
1031        public void mouseEntered(MouseEvent e) {}
1032
1033        public void mouseExited(MouseEvent e) {}
1034
1035        public void mouseMoved(MouseEvent e) {}
1036
1037        public void mouseDragged(MouseEvent e) {
1038        if (shouldIgnore0(e)) {
1039            return;
1040        }
1041
1042            mouseDraggedImpl(e);
1043        }
1044
1045        protected void mouseDraggedImpl(MouseEvent e) {
1046            repostEvent(e);
1047
1048            // Check isFileList:
1049
// Until we support drag-selection, dragging should not change
1050
// the selection (act like single-select).
1051
if (isFileList || table.isEditing()) {
1052                return;
1053            }
1054
1055            Point p = e.getPoint();
1056            int row = table.rowAtPoint(p);
1057            int column = table.columnAtPoint(p);
1058        // The autoscroller can generate drag events outside the
1059
// table's range.
1060
if ((column == -1) || (row == -1)) {
1061                return;
1062            }
1063
1064            if (e.isControlDown()) {
1065                ListSelectionModel cm = table.getColumnModel().getSelectionModel();
1066                ListSelectionModel rm = table.getSelectionModel();
1067                int colAnchor = cm.getAnchorSelectionIndex();
1068                int rowAnchor = rm.getAnchorSelectionIndex();
1069
1070                boolean selected = true;
1071
1072                if (rowAnchor == -1 || rowAnchor >= table.getRowCount()) {
1073                    rowAnchor = 0;
1074                    selected = false;
1075                }
1076
1077                if (colAnchor == -1 || colAnchor >= table.getColumnCount()) {
1078                    colAnchor = 0;
1079                    selected = false;
1080                }
1081
1082                selected = selected && table.isCellSelected(rowAnchor, colAnchor);
1083
1084                changeSelectionModel(cm, colAnchor, selected, column);
1085                changeSelectionModel(rm, rowAnchor, selected, row);
1086
1087                // From JTable.changeSelection():
1088
// Scroll after changing the selection as blit scrolling is immediate,
1089
// so that if we cause the repaint after the scroll we end up painting
1090
// everything!
1091
if (table.getAutoscrolls()) {
1092                    Rectangle cellRect = table.getCellRect(row, column, false);
1093                    if (cellRect != null) {
1094                        table.scrollRectToVisible(cellRect);
1095                    }
1096                }
1097            } else {
1098                table.changeSelection(row, column, false, true);
1099            }
1100        }
1101
1102        private void changeSelectionModel(ListSelectionModel sm,
1103                                          int anchorIndex,
1104                                          boolean anchorSelected,
1105                                          int index) {
1106
1107            if (anchorSelected) {
1108                sm.addSelectionInterval(anchorIndex, index);
1109            } else {
1110                sm.removeSelectionInterval(anchorIndex, index);
1111            }
1112        }
1113
1114
1115        // PropertyChangeListener
1116
public void propertyChange(PropertyChangeEvent JavaDoc event) {
1117        String JavaDoc changeName = event.getPropertyName();
1118
1119        if ("componentOrientation" == changeName) {
1120        JTableHeader header = table.getTableHeader();
1121        if (header != null) {
1122            header.setComponentOrientation(
1123                            (ComponentOrientation)event.getNewValue());
1124        }
1125            } else if ("transferHandler" == changeName) {
1126                DropTarget dropTarget = table.getDropTarget();
1127                if (dropTarget instanceof UIResource) {
1128                    if (defaultDropTargetListener == null) {
1129                        defaultDropTargetListener =
1130                            new TableDropTargetListener();
1131                    }
1132                    try {
1133                        dropTarget.addDropTargetListener(
1134                                defaultDropTargetListener);
1135                    } catch (TooManyListenersException JavaDoc tmle) {
1136                        // should not happen... swing drop target is multicast
1137
}
1138                }
1139            } else if ("Table.isFileList" == changeName) {
1140                isFileList = Boolean.TRUE.equals(table.getClientProperty("Table.isFileList"));
1141                table.revalidate();
1142                table.repaint();
1143            }
1144    }
1145    }
1146
1147
1148    private class DragFixHandler extends Handler implements ListSelectionListener,
1149                                                            ActionListener,
1150                                                            BeforeDrag {
1151
1152        // The row and column where the press occurred and the
1153
// press event itself
1154
private int pressedRow;
1155        private int pressedCol;
1156        private MouseEvent pressedEvent;
1157
1158        // Whether or not the mouse press (which is being considered as part
1159
// of a drag sequence) also caused the selection change to be fully
1160
// processed.
1161
private boolean dragPressDidSelection;
1162        
1163        // Set to true when a drag gesture has been fully recognized and DnD
1164
// begins. Use this to ignore further mouse events which could be
1165
// delivered if DnD is cancelled (via ESCAPE for example)
1166
private boolean dragStarted;
1167
1168        // Whether or not we should start the editing timer on release
1169
private boolean shouldStartTimer;
1170
1171        // To cache the return value of pointOutsidePrefSize since we use
1172
// it multiple times.
1173
private boolean outsidePrefSize;
1174
1175        // Used to delay the start of editing.
1176
private Timer timer = null;
1177
1178        private boolean canStartDrag() {
1179            if (pressedRow == -1 || pressedCol == -1) {
1180                return false;
1181            }
1182
1183            if (isFileList) {
1184                return !outsidePrefSize;
1185            }
1186
1187            // if this is a single selection table
1188
if ((table.getSelectionModel().getSelectionMode() ==
1189                     ListSelectionModel.SINGLE_SELECTION) &&
1190                (table.getColumnModel().getSelectionModel().getSelectionMode() ==
1191                     ListSelectionModel.SINGLE_SELECTION)) {
1192
1193                return true;
1194            }
1195
1196            return table.isCellSelected(pressedRow, pressedCol);
1197        }
1198
1199        public void mousePressed(MouseEvent e) {
1200            if (SwingUtilities2.shouldIgnore(e, table)) {
1201                return;
1202            }
1203
1204            if (table.isEditing() && !table.getCellEditor().stopCellEditing()) {
1205                Component editorComponent = table.getEditorComponent();
1206                if (editorComponent != null && !editorComponent.hasFocus()) {
1207                    BasicLookAndFeel.compositeRequestFocus(editorComponent);
1208                }
1209                return;
1210            }
1211
1212            Point p = e.getPoint();
1213            pressedRow = table.rowAtPoint(p);
1214            pressedCol = table.columnAtPoint(p);
1215            outsidePrefSize = pointOutsidePrefSize(table, pressedRow, pressedCol, p);
1216
1217            if (isFileList) {
1218                shouldStartTimer =
1219                    table.isCellSelected(pressedRow, pressedCol) &&
1220                    !e.isShiftDown() &&
1221                    !e.isControlDown() &&
1222                    !outsidePrefSize;
1223            }
1224
1225            if (table.getDragEnabled()) {
1226                mousePressedDND(e);
1227            } else {
1228                SwingUtilities2.adjustFocus(table);
1229                if (!isFileList) {
1230                    setValueIsAdjusting(true);
1231                }
1232                adjustSelection(e);
1233            }
1234        }
1235
1236        private void mousePressedDND(MouseEvent e) {
1237            pressedEvent = e;
1238            boolean grabFocus = true;
1239            dragStarted = false;
1240
1241            if (canStartDrag() && DragRecognitionSupport.mousePressed(e)) {
1242
1243                dragPressDidSelection = false;
1244
1245                if (e.isControlDown() && isFileList) {
1246                    // do nothing for control - will be handled on release
1247
// or when drag starts
1248
return;
1249                } else if (!e.isShiftDown() && table.isCellSelected(pressedRow, pressedCol)) {
1250                    // clicking on something that's already selected
1251
// and need to make it the lead now
1252
table.getSelectionModel().addSelectionInterval(pressedRow,
1253                                                                   pressedRow);
1254                    table.getColumnModel().getSelectionModel().
1255                        addSelectionInterval(pressedCol, pressedCol);
1256
1257                    return;
1258                }
1259
1260                dragPressDidSelection = true;
1261
1262                // could be a drag initiating event - don't grab focus
1263
grabFocus = false;
1264            } else if (!isFileList) {
1265                // When drag can't happen, mouse drags might change the selection in the table
1266
// so we want the isAdjusting flag to be set
1267
setValueIsAdjusting(true);
1268            }
1269
1270            if (grabFocus) {
1271                SwingUtilities2.adjustFocus(table);
1272            }
1273
1274            adjustSelection(e);
1275        }
1276
1277        private void adjustSelection(MouseEvent e) {
1278            // Fix for 4835633
1279
if (outsidePrefSize) {
1280                // If shift is down in multi-select, we should just return.
1281
// For single select or non-shift-click, clear the selection
1282
if (e.getID() == MouseEvent.MOUSE_PRESSED &&
1283                    (!e.isShiftDown() ||
1284                     table.getSelectionModel().getSelectionMode() ==
1285                     ListSelectionModel.SINGLE_SELECTION)) {
1286                    table.clearSelection();
1287                    TableCellEditor tce = table.getCellEditor();
1288                    if (tce != null) {
1289                        tce.stopCellEditing();
1290                    }
1291                }
1292                return;
1293            }
1294            // The autoscroller can generate drag events outside the
1295
// table's range.
1296
if ((pressedCol == -1) || (pressedRow == -1)) {
1297                return;
1298            }
1299
1300            boolean dragEnabled = table.getDragEnabled();
1301
1302            if (!dragEnabled && !isFileList && table.editCellAt(pressedRow, pressedCol, e)) {
1303                setDispatchComponent(e);
1304                repostEvent(e);
1305            }
1306
1307            CellEditor editor = table.getCellEditor();
1308            if (dragEnabled || editor == null || editor.shouldSelectCell(e)) {
1309                makeSelectionChange(pressedRow, pressedCol, e);
1310            }
1311        }
1312
1313        public void valueChanged(ListSelectionEvent e) {
1314            if (timer != null) {
1315                timer.stop();
1316                timer = null;
1317            }
1318        }
1319
1320        public void actionPerformed(ActionEvent ae) {
1321            table.editCellAt(pressedRow, pressedCol, null);
1322            Component editorComponent = table.getEditorComponent();
1323            if (editorComponent != null && !editorComponent.hasFocus()) {
1324                BasicLookAndFeel.compositeRequestFocus(editorComponent);
1325            }
1326            return;
1327        }
1328
1329        private void maybeStartTimer() {
1330            if (!shouldStartTimer) {
1331                return;
1332            }
1333
1334            if (timer == null) {
1335                timer = new Timer(1200, this);
1336                timer.setRepeats(false);
1337            }
1338
1339            timer.start();
1340        }
1341
1342        public void mouseReleased(MouseEvent e) {
1343            if (SwingUtilities2.shouldIgnore(e, table)) {
1344                return;
1345            }
1346
1347            if (table.getDragEnabled()) {
1348                mouseReleasedDND(e);
1349            } else {
1350                if (isFileList) {
1351                    maybeStartTimer();
1352                }
1353            }
1354
1355            pressedEvent = null;
1356            repostEvent(e);
1357            dispatchComponent = null;
1358            setValueIsAdjusting(false);
1359        }
1360
1361        private void mouseReleasedDND(MouseEvent e) {
1362            MouseEvent me = DragRecognitionSupport.mouseReleased(e);
1363            if (me != null) {
1364                SwingUtilities2.adjustFocus(table);
1365                if (!dragPressDidSelection) {
1366                    adjustSelection(me);
1367                }
1368            }
1369
1370            if (!dragStarted) {
1371                if (isFileList) {
1372                    maybeStartTimer();
1373                    return;
1374                }
1375                
1376                Point p = e.getPoint();
1377
1378                if (pressedEvent != null &&
1379                        table.rowAtPoint(p) == pressedRow &&
1380                        table.columnAtPoint(p) == pressedCol &&
1381                        table.editCellAt(pressedRow, pressedCol, pressedEvent)) {
1382
1383                    setDispatchComponent(pressedEvent);
1384                    repostEvent(pressedEvent);
1385
1386                    // This may appear completely odd, but must be done for backward
1387
// compatibility reasons. Developers have been known to rely on
1388
// a call to shouldSelectCell after editing has begun.
1389
CellEditor ce = table.getCellEditor();
1390                    if (ce != null) {
1391                        ce.shouldSelectCell(pressedEvent);
1392                    }
1393                }
1394            }
1395        }
1396
1397        public void dragStarting(MouseEvent me) {
1398            dragStarted = true;
1399
1400            if (me.isControlDown() && isFileList) {
1401                table.getSelectionModel().addSelectionInterval(pressedRow,
1402                                                               pressedRow);
1403                table.getColumnModel().getSelectionModel().
1404                    addSelectionInterval(pressedCol, pressedCol);
1405            }
1406
1407            pressedEvent = null;
1408        }
1409
1410        public void mouseDragged(MouseEvent e) {
1411            if (SwingUtilities2.shouldIgnore(e, table)) {
1412                return;
1413            }
1414
1415            if (table.getDragEnabled() &&
1416                    (DragRecognitionSupport.mouseDragged(e, this) || dragStarted)) {
1417
1418                return;
1419            }
1420
1421            mouseDraggedImpl(e);
1422        }
1423
1424        public void propertyChange(PropertyChangeEvent JavaDoc event) {
1425            super.propertyChange(event);
1426
1427            String JavaDoc changeName = event.getPropertyName();
1428
1429            if ("Table.isFileList" == changeName) {
1430                if (isFileList) {
1431                    table.getSelectionModel().addListSelectionListener(getDragFixHandler());
1432                } else {
1433                    table.getSelectionModel().removeListSelectionListener(getDragFixHandler());
1434                    timer = null;
1435                }
1436            } else if ("selectionModel" == changeName) {
1437                if (isFileList) {
1438                    ListSelectionModel old = (ListSelectionModel)event.getOldValue();
1439                    old.removeListSelectionListener(getDragFixHandler());
1440                    table.getSelectionModel().addListSelectionListener(getDragFixHandler());
1441                }
1442            }
1443        }
1444    }
1445
1446
1447    /*
1448     * Returns true if the given point is outside the preferredSize of the
1449     * item at the given row of the table. (Column must be 0).
1450     * Returns false if the "Table.isFileList" client property is not set.
1451     */

1452    private static boolean pointOutsidePrefSize(JTable table,
1453                                                int row, int column, Point p) {
1454        if (!Boolean.TRUE.equals(table.getClientProperty("Table.isFileList"))) {
1455            return false;
1456        }
1457
1458        return SwingUtilities2.pointOutsidePrefSize(table, row, column, p);
1459    }
1460
1461//
1462
// Factory methods for the Listeners
1463
//
1464

1465    private Handler getHandler() {
1466        if (handler == null) {
1467            handler = DRAG_FIX ? new DragFixHandler() : new Handler();
1468        }
1469        return handler;
1470    }
1471
1472    private DragFixHandler getDragFixHandler() {
1473        // this only called by code that's enabled when DRAG_FIX is on
1474
assert DRAG_FIX;
1475        return (DragFixHandler)handler;
1476    }
1477
1478    /**
1479     * Creates the key listener for handling keyboard navigation in the JTable.
1480     */

1481    protected KeyListener createKeyListener() {
1482    return null;
1483    }
1484
1485    /**
1486     * Creates the focus listener for handling keyboard navigation in the JTable.
1487     */

1488    protected FocusListener createFocusListener() {
1489        return getHandler();
1490    }
1491
1492    /**
1493     * Creates the mouse listener for the JTable.
1494     */

1495    protected MouseInputListener createMouseInputListener() {
1496        return getHandler();
1497    }
1498
1499//
1500
// The installation/uninstall procedures and support
1501
//
1502

1503    public static ComponentUI createUI(JComponent c) {
1504        return new BasicTableUI JavaDoc();
1505    }
1506
1507// Installation
1508

1509    public void installUI(JComponent c) {
1510        table = (JTable)c;
1511
1512        rendererPane = new CellRendererPane();
1513        table.add(rendererPane);
1514        installDefaults();
1515        installDefaults2();
1516        installListeners();
1517        installKeyboardActions();
1518    }
1519
1520    /**
1521     * Initialize JTable properties, e.g. font, foreground, and background.
1522     * The font, foreground, and background properties are only set if their
1523     * current value is either null or a UIResource, other properties are set
1524     * if the current value is null.
1525     *
1526     * @see #installUI
1527     */

1528    protected void installDefaults() {
1529        LookAndFeel.installColorsAndFont(table, "Table.background",
1530                                         "Table.foreground", "Table.font");
1531        // JTable's original row height is 16. To correctly display the
1532
// contents on Linux we should have set it to 18, Windows 19 and
1533
// Solaris 20. As these values vary so much it's too hard to
1534
// be backward compatable and try to update the row height, we're
1535
// therefor NOT going to adjust the row height based on font. If the
1536
// developer changes the font, it's there responsability to update
1537
// the row height.
1538

1539        LookAndFeel.installProperty(table, "opaque", Boolean.TRUE);
1540
1541        Color sbg = table.getSelectionBackground();
1542        if (sbg == null || sbg instanceof UIResource) {
1543            table.setSelectionBackground(UIManager.getColor("Table.selectionBackground"));
1544        }
1545
1546        Color sfg = table.getSelectionForeground();
1547        if (sfg == null || sfg instanceof UIResource) {
1548            table.setSelectionForeground(UIManager.getColor("Table.selectionForeground"));
1549        }
1550
1551        Color gridColor = table.getGridColor();
1552        if (gridColor == null || gridColor instanceof UIResource) {
1553            table.setGridColor(UIManager.getColor("Table.gridColor"));
1554        }
1555
1556        // install the scrollpane border
1557
Container parent = table.getParent(); // should be viewport
1558
if (parent != null) {
1559            parent = parent.getParent(); // should be the scrollpane
1560
if (parent != null && parent instanceof JScrollPane) {
1561                LookAndFeel.installBorder((JScrollPane)parent, "Table.scrollPaneBorder");
1562            }
1563        }
1564
1565        isFileList = Boolean.TRUE.equals(table.getClientProperty("Table.isFileList"));
1566    }
1567
1568    private void installDefaults2() {
1569    TransferHandler th = table.getTransferHandler();
1570    if (th == null || th instanceof UIResource) {
1571        table.setTransferHandler(defaultTransferHandler);
1572    }
1573    DropTarget dropTarget = table.getDropTarget();
1574    if (dropTarget instanceof UIResource) {
1575            if (defaultDropTargetListener == null) {
1576                defaultDropTargetListener =
1577                    new TableDropTargetListener();
1578            }
1579        try {
1580        dropTarget.addDropTargetListener(defaultDropTargetListener);
1581        } catch (TooManyListenersException JavaDoc tmle) {
1582        // should not happen... swing drop target is multicast
1583
}
1584    }
1585    }
1586
1587    /**
1588     * Attaches listeners to the JTable.
1589     */

1590    protected void installListeners() {
1591        focusListener = createFocusListener();
1592        keyListener = createKeyListener();
1593        mouseInputListener = createMouseInputListener();
1594
1595        table.addFocusListener(focusListener);
1596        table.addKeyListener(keyListener);
1597        if (!DRAG_FIX) {
1598            table.addMouseListener(defaultDragRecognizer);
1599            table.addMouseMotionListener(defaultDragRecognizer);
1600        }
1601        table.addMouseListener(mouseInputListener);
1602        table.addMouseMotionListener(mouseInputListener);
1603        table.addPropertyChangeListener(getHandler());
1604        if (DRAG_FIX && isFileList) {
1605            table.getSelectionModel().addListSelectionListener(getDragFixHandler());
1606        }
1607    }
1608
1609    /**
1610     * Register all keyboard actions on the JTable.
1611     */

1612    protected void installKeyboardActions() {
1613        LazyActionMap.installLazyActionMap(table, BasicTableUI JavaDoc.class,
1614                "Table.actionMap");
1615
1616    InputMap inputMap = getInputMap(JComponent.
1617                    WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
1618    SwingUtilities.replaceUIInputMap(table,
1619                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
1620                inputMap);
1621    }
1622
1623    InputMap getInputMap(int condition) {
1624        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
1625            InputMap keyMap =
1626                (InputMap)DefaultLookup.get(table, this,
1627                                            "Table.ancestorInputMap");
1628            return keyMap;
1629        }
1630        return null;
1631    }
1632
1633    static void loadActionMap(LazyActionMap JavaDoc map) {
1634        // IMPORTANT: There is a very close coupling between the parameters
1635
// passed to the Actions constructor. Only certain parameter
1636
// combinations are supported. For example, the following Action would
1637
// not work as expected:
1638
// new Actions(Actions.NEXT_ROW_CELL, 1, 4, false, true)
1639
// Actions which move within the selection only (having a true
1640
// inSelection parameter) require that one of dx or dy be
1641
// zero and the other be -1 or 1. The point of this warning is
1642
// that you should be very careful about making sure a particular
1643
// combination of parameters is supported before changing or
1644
// adding anything here.
1645

1646        map.put(new Actions(Actions.NEXT_COLUMN, 1, 0,
1647                false, false));
1648        map.put(new Actions(Actions.NEXT_COLUMN_CHANGE_LEAD, 1, 0,
1649                false, false));
1650        map.put(new Actions(Actions.PREVIOUS_COLUMN, -1, 0,
1651                false, false));
1652        map.put(new Actions(Actions.PREVIOUS_COLUMN_CHANGE_LEAD, -1, 0,
1653                false, false));
1654        map.put(new Actions(Actions.NEXT_ROW, 0, 1,
1655                false, false));
1656        map.put(new Actions(Actions.NEXT_ROW_CHANGE_LEAD, 0, 1,
1657                false, false));
1658        map.put(new Actions(Actions.PREVIOUS_ROW, 0, -1,
1659                false, false));
1660        map.put(new Actions(Actions.PREVIOUS_ROW_CHANGE_LEAD, 0, -1,
1661                false, false));
1662        map.put(new Actions(Actions.NEXT_COLUMN_EXTEND_SELECTION,
1663                1, 0, true, false));
1664        map.put(new Actions(Actions.PREVIOUS_COLUMN_EXTEND_SELECTION,
1665                -1, 0, true, false));
1666        map.put(new Actions(Actions.NEXT_ROW_EXTEND_SELECTION,
1667                0, 1, true, false));
1668        map.put(new Actions(Actions.PREVIOUS_ROW_EXTEND_SELECTION,
1669                0, -1, true, false));
1670        map.put(new Actions(Actions.SCROLL_UP_CHANGE_SELECTION,
1671            false, false, true, false));
1672        map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_SELECTION,
1673            false, true, true, false));
1674        map.put(new Actions(Actions.FIRST_COLUMN,
1675            false, false, false, true));
1676        map.put(new Actions(Actions.LAST_COLUMN,
1677            false, true, false, true));
1678
1679        map.put(new Actions(Actions.SCROLL_UP_EXTEND_SELECTION,
1680        true, false, true, false));
1681        map.put(new Actions(Actions.SCROLL_DOWN_EXTEND_SELECTION,
1682                true, true, true, false));
1683        map.put(new Actions(Actions.FIRST_COLUMN_EXTEND_SELECTION,
1684                true, false, false, true));
1685        map.put(new Actions(Actions.LAST_COLUMN_EXTEND_SELECTION,
1686                true, true, false, true));
1687
1688    map.put(new Actions(Actions.FIRST_ROW, false, false, true, true));
1689    map.put(new Actions(Actions.LAST_ROW, false, true, true, true));
1690
1691    map.put(new Actions(Actions.FIRST_ROW_EXTEND_SELECTION,
1692                true, false, true, true));
1693    map.put(new Actions(Actions.LAST_ROW_EXTEND_SELECTION,
1694                true, true, true, true));
1695
1696    map.put(new Actions(Actions.NEXT_COLUMN_CELL,
1697                1, 0, false, true));
1698    map.put(new Actions(Actions.PREVIOUS_COLUMN_CELL,
1699                -1, 0, false, true));
1700    map.put(new Actions(Actions.NEXT_ROW_CELL, 0, 1, false, true));
1701    map.put(new Actions(Actions.PREVIOUS_ROW_CELL,
1702                0, -1, false, true));
1703
1704    map.put(new Actions(Actions.SELECT_ALL));
1705        map.put(new Actions(Actions.CLEAR_SELECTION));
1706    map.put(new Actions(Actions.CANCEL_EDITING));
1707    map.put(new Actions(Actions.START_EDITING));
1708
1709        map.put(TransferHandler.getCutAction().getValue(Action.NAME),
1710                TransferHandler.getCutAction());
1711        map.put(TransferHandler.getCopyAction().getValue(Action.NAME),
1712                TransferHandler.getCopyAction());
1713        map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
1714                TransferHandler.getPasteAction());
1715
1716    map.put(new Actions(Actions.SCROLL_LEFT_CHANGE_SELECTION,
1717                false, false, false, false));
1718    map.put(new Actions(Actions.SCROLL_RIGHT_CHANGE_SELECTION,
1719                false, true, false, false));
1720    map.put(new Actions(Actions.SCROLL_LEFT_EXTEND_SELECTION,
1721                true, false, false, false));
1722    map.put(new Actions(Actions.SCROLL_RIGHT_EXTEND_SELECTION,
1723                true, true, false, false));
1724
1725        map.put(new Actions(Actions.ADD_TO_SELECTION));
1726        map.put(new Actions(Actions.TOGGLE_AND_ANCHOR));
1727        map.put(new Actions(Actions.EXTEND_TO));
1728        map.put(new Actions(Actions.MOVE_SELECTION_TO));
1729    }
1730
1731// Uninstallation
1732

1733    public void uninstallUI(JComponent c) {
1734        uninstallDefaults();
1735        uninstallListeners();
1736        uninstallKeyboardActions();
1737
1738        table.remove(rendererPane);
1739        rendererPane = null;
1740        table = null;
1741    }
1742
1743    protected void uninstallDefaults() {
1744    if (table.getTransferHandler() instanceof UIResource) {
1745        table.setTransferHandler(null);
1746    }
1747    }
1748
1749    protected void uninstallListeners() {
1750        table.removeFocusListener(focusListener);
1751        table.removeKeyListener(keyListener);
1752        if (!DRAG_FIX) {
1753            table.removeMouseListener(defaultDragRecognizer);
1754            table.removeMouseMotionListener(defaultDragRecognizer);
1755        }
1756        table.removeMouseListener(mouseInputListener);
1757        table.removeMouseMotionListener(mouseInputListener);
1758        table.removePropertyChangeListener(getHandler());
1759        if (DRAG_FIX && isFileList) {
1760            table.getSelectionModel().removeListSelectionListener(getDragFixHandler());
1761        }
1762
1763        focusListener = null;
1764        keyListener = null;
1765        mouseInputListener = null;
1766        handler = null;
1767    }
1768
1769    protected void uninstallKeyboardActions() {
1770    SwingUtilities.replaceUIInputMap(table, JComponent.
1771                   WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
1772        SwingUtilities.replaceUIActionMap(table, null);
1773    }
1774
1775//
1776
// Size Methods
1777
//
1778

1779    private Dimension createTableSize(long width) {
1780    int height = 0;
1781    int rowCount = table.getRowCount();
1782    if (rowCount > 0 && table.getColumnCount() > 0) {
1783        Rectangle r = table.getCellRect(rowCount-1, 0, true);
1784        height = r.y + r.height;
1785    }
1786    // Width is always positive. The call to abs() is a workaround for
1787
// a bug in the 1.1.6 JIT on Windows.
1788
long tmp = Math.abs(width);
1789        if (tmp > Integer.MAX_VALUE) {
1790            tmp = Integer.MAX_VALUE;
1791        }
1792    return new Dimension((int)tmp, height);
1793    }
1794
1795    /**
1796     * Return the minimum size of the table. The minimum height is the
1797     * row height times the number of rows.
1798     * The minimum width is the sum of the minimum widths of each column.
1799     */

1800    public Dimension getMinimumSize(JComponent c) {
1801        long width = 0;
1802        Enumeration JavaDoc enumeration = table.getColumnModel().getColumns();
1803        while (enumeration.hasMoreElements()) {
1804            TableColumn aColumn = (TableColumn)enumeration.nextElement();
1805            width = width + aColumn.getMinWidth();
1806        }
1807        return createTableSize(width);
1808    }
1809
1810    /**
1811     * Return the preferred size of the table. The preferred height is the
1812     * row height times the number of rows.
1813     * The preferred width is the sum of the preferred widths of each column.
1814     */

1815    public Dimension getPreferredSize(JComponent c) {
1816        long width = 0;
1817        Enumeration JavaDoc enumeration = table.getColumnModel().getColumns();
1818        while (enumeration.hasMoreElements()) {
1819            TableColumn aColumn = (TableColumn)enumeration.nextElement();
1820            width = width + aColumn.getPreferredWidth();
1821        }
1822        return createTableSize(width);
1823    }
1824
1825    /**
1826     * Return the maximum size of the table. The maximum height is the
1827     * row heighttimes the number of rows.
1828     * The maximum width is the sum of the maximum widths of each column.
1829     */

1830    public Dimension getMaximumSize(JComponent c) {
1831        long width = 0;
1832        Enumeration JavaDoc enumeration = table.getColumnModel().getColumns();
1833        while (enumeration.hasMoreElements()) {
1834            TableColumn aColumn = (TableColumn)enumeration.nextElement();
1835            width = width + aColumn.getMaxWidth();
1836        }
1837        return createTableSize(width);
1838    }
1839
1840//
1841
// Paint methods and support
1842
//
1843

1844    /** Paint a representation of the <code>table</code> instance
1845     * that was set in installUI().
1846     */

1847    public void paint(Graphics g, JComponent c) {
1848        Rectangle clip = g.getClipBounds();
1849
1850        Rectangle bounds = table.getBounds();
1851        // account for the fact that the graphics has already been translated
1852
// into the table's bounds
1853
bounds.x = bounds.y = 0;
1854
1855    if (table.getRowCount() <= 0 || table.getColumnCount() <= 0 ||
1856                // this check prevents us from painting the entire table
1857
// when the clip doesn't intersect our bounds at all
1858
!bounds.intersects(clip)) {
1859
1860        return;
1861    }
1862
1863    Point upperLeft = clip.getLocation();
1864    Point lowerRight = new Point(clip.x + clip.width - 1, clip.y + clip.height - 1);
1865        int rMin = table.rowAtPoint(upperLeft);
1866        int rMax = table.rowAtPoint(lowerRight);
1867        // This should never happen (as long as our bounds intersect the clip,
1868
// which is why we bail above if that is the case).
1869
if (rMin == -1) {
1870        rMin = 0;
1871        }
1872        // If the table does not have enough rows to fill the view we'll get -1.
1873
// (We could also get -1 if our bounds don't intersect the clip,
1874
// which is why we bail above if that is the case).
1875
// Replace this with the index of the last row.
1876
if (rMax == -1) {
1877        rMax = table.getRowCount()-1;
1878        }
1879
1880        boolean ltr = table.getComponentOrientation().isLeftToRight();
1881        int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight);
1882        int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft);
1883        // This should never happen.
1884
if (cMin == -1) {
1885        cMin = 0;
1886        }
1887    // If the table does not have enough columns to fill the view we'll get -1.
1888
// Replace this with the index of the last column.
1889
if (cMax == -1) {
1890        cMax = table.getColumnCount()-1;
1891        }
1892
1893        // Paint the grid.
1894
paintGrid(g, rMin, rMax, cMin, cMax);
1895
1896        // Paint the cells.
1897
paintCells(g, rMin, rMax, cMin, cMax);
1898    }
1899
1900    /*
1901     * Paints the grid lines within <I>aRect</I>, using the grid
1902     * color set with <I>setGridColor</I>. Paints vertical lines
1903     * if <code>getShowVerticalLines()</code> returns true and paints
1904     * horizontal lines if <code>getShowHorizontalLines()</code>
1905     * returns true.
1906     */

1907    private void paintGrid(Graphics g, int rMin, int rMax, int cMin, int cMax) {
1908        g.setColor(table.getGridColor());
1909
1910    Rectangle minCell = table.getCellRect(rMin, cMin, true);
1911    Rectangle maxCell = table.getCellRect(rMax, cMax, true);
1912        Rectangle damagedArea = minCell.union( maxCell );
1913
1914        if (table.getShowHorizontalLines()) {
1915        int tableWidth = damagedArea.x + damagedArea.width;
1916        int y = damagedArea.y;
1917        for (int row = rMin; row <= rMax; row++) {
1918        y += table.getRowHeight(row);
1919        g.drawLine(damagedArea.x, y - 1, tableWidth - 1, y - 1);
1920        }
1921    }
1922        if (table.getShowVerticalLines()) {
1923        TableColumnModel cm = table.getColumnModel();
1924        int tableHeight = damagedArea.y + damagedArea.height;
1925        int x;
1926        if (table.getComponentOrientation().isLeftToRight()) {
1927        x = damagedArea.x;
1928        for (int column = cMin; column <= cMax; column++) {
1929            int w = cm.getColumn(column).getWidth();
1930            x += w;
1931            g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
1932        }
1933        } else {
1934        x = damagedArea.x + damagedArea.width;
1935        for (int column = cMin; column < cMax; column++) {
1936            int w = cm.getColumn(column).getWidth();
1937            x -= w;
1938            g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
1939        }
1940        x -= cm.getColumn(cMax).getWidth();
1941        g.drawLine(x, 0, x, tableHeight - 1);
1942        }
1943    }
1944    }
1945
1946    private int viewIndexForColumn(TableColumn aColumn) {
1947        TableColumnModel cm = table.getColumnModel();
1948        for (int column = 0; column < cm.getColumnCount(); column++) {
1949            if (cm.getColumn(column) == aColumn) {
1950                return column;
1951            }
1952        }
1953        return -1;
1954    }
1955
1956    private void paintCells(Graphics g, int rMin, int rMax, int cMin, int cMax) {
1957    JTableHeader header = table.getTableHeader();
1958    TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn();
1959
1960    TableColumnModel cm = table.getColumnModel();
1961    int columnMargin = cm.getColumnMargin();
1962
1963        Rectangle cellRect;
1964    TableColumn aColumn;
1965    int columnWidth;
1966    if (table.getComponentOrientation().isLeftToRight()) {
1967        for(int row = rMin; row <= rMax; row++) {
1968        cellRect = table.getCellRect(row, cMin, false);
1969                for(int column = cMin; column <= cMax; column++) {
1970                    aColumn = cm.getColumn(column);
1971                    columnWidth = aColumn.getWidth();
1972                    cellRect.width = columnWidth - columnMargin;
1973                    if (aColumn != draggedColumn) {
1974                        paintCell(g, cellRect, row, column);
1975                    }
1976                    cellRect.x += columnWidth;
1977            }
1978        }
1979    } else {
1980        for(int row = rMin; row <= rMax; row++) {
1981                cellRect = table.getCellRect(row, cMin, false);
1982                aColumn = cm.getColumn(cMin);
1983                if (aColumn != draggedColumn) {
1984                    columnWidth = aColumn.getWidth();
1985                    cellRect.width = columnWidth - columnMargin;
1986                    paintCell(g, cellRect, row, cMin);
1987                }
1988                for(int column = cMin+1; column <= cMax; column++) {
1989                    aColumn = cm.getColumn(column);
1990                    columnWidth = aColumn.getWidth();
1991                    cellRect.width = columnWidth - columnMargin;
1992                    cellRect.x -= columnWidth;
1993                    if (aColumn != draggedColumn) {
1994                        paintCell(g, cellRect, row, column);
1995                    }
1996            }
1997        }
1998    }
1999
2000        // Paint the dragged column if we are dragging.
2001
if (draggedColumn != null) {
2002        paintDraggedArea(g, rMin, rMax, draggedColumn, header.getDraggedDistance());
2003    }
2004
2005    // Remove any renderers that may be left in the rendererPane.
2006
rendererPane.removeAll();
2007    }
2008
2009    private void paintDraggedArea(Graphics g, int rMin, int rMax, TableColumn draggedColumn, int distance) {
2010        int draggedColumnIndex = viewIndexForColumn(draggedColumn);
2011
2012        Rectangle minCell = table.getCellRect(rMin, draggedColumnIndex, true);
2013    Rectangle maxCell = table.getCellRect(rMax, draggedColumnIndex, true);
2014
2015    Rectangle vacatedColumnRect = minCell.union(maxCell);
2016
2017    // Paint a gray well in place of the moving column.
2018
g.setColor(table.getParent().getBackground());
2019    g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y,
2020           vacatedColumnRect.width, vacatedColumnRect.height);
2021
2022    // Move to the where the cell has been dragged.
2023
vacatedColumnRect.x += distance;
2024
2025    // Fill the background.
2026
g.setColor(table.getBackground());
2027    g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y,
2028           vacatedColumnRect.width, vacatedColumnRect.height);
2029
2030    // Paint the vertical grid lines if necessary.
2031
if (table.getShowVerticalLines()) {
2032        g.setColor(table.getGridColor());
2033        int x1 = vacatedColumnRect.x;
2034        int y1 = vacatedColumnRect.y;
2035        int x2 = x1 + vacatedColumnRect.width - 1;
2036        int y2 = y1 + vacatedColumnRect.height - 1;
2037        // Left
2038
g.drawLine(x1-1, y1, x1-1, y2);
2039        // Right
2040
g.drawLine(x2, y1, x2, y2);
2041    }
2042
2043    for(int row = rMin; row <= rMax; row++) {
2044        // Render the cell value
2045
Rectangle r = table.getCellRect(row, draggedColumnIndex, false);
2046        r.x += distance;
2047        paintCell(g, r, row, draggedColumnIndex);
2048
2049        // Paint the (lower) horizontal grid line if necessary.
2050
if (table.getShowHorizontalLines()) {
2051        g.setColor(table.getGridColor());
2052        Rectangle rcr = table.getCellRect(row, draggedColumnIndex, true);
2053        rcr.x += distance;
2054        int x1 = rcr.x;
2055        int y1 = rcr.y;
2056        int x2 = x1 + rcr.width - 1;
2057        int y2 = y1 + rcr.height - 1;
2058        g.drawLine(x1, y2, x2, y2);
2059        }
2060    }
2061    }
2062
2063    private void paintCell(Graphics g, Rectangle cellRect, int row, int column) {
2064        if (table.isEditing() && table.getEditingRow()==row &&
2065                                 table.getEditingColumn()==column) {
2066            Component component = table.getEditorComponent();
2067        component.setBounds(cellRect);
2068            component.validate();
2069        }
2070        else {
2071            TableCellRenderer renderer = table.getCellRenderer(row, column);
2072            Component component = table.prepareRenderer(renderer, row, column);
2073            rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y,
2074                                        cellRect.width, cellRect.height, true);
2075        }
2076    }
2077
2078    private static int getAdjustedLead(JTable table,
2079                                       boolean row,
2080                                       ListSelectionModel model) {
2081
2082        int index = model.getLeadSelectionIndex();
2083        int compare = row ? table.getRowCount() : table.getColumnCount();
2084        return index < compare ? index : -1;
2085    }
2086
2087    private static int getAdjustedLead(JTable table, boolean row) {
2088        return row ? getAdjustedLead(table, row, table.getSelectionModel())
2089                   : getAdjustedLead(table, row, table.getColumnModel().getSelectionModel());
2090    }
2091
2092
2093    private static final TableDragGestureRecognizer defaultDragRecognizer =
2094        DRAG_FIX ? null : new TableDragGestureRecognizer();
2095
2096    /**
2097     * Drag gesture recognizer for JTable components
2098     */

2099    static class TableDragGestureRecognizer extends BasicDragGestureRecognizer JavaDoc {
2100
2101    /**
2102     * Determines if the following are true:
2103     * <ul>
2104     * <li>the press event is located over a selection
2105     * <li>the dragEnabled property is true
2106     * <li>A TranferHandler is installed
2107     * </ul>
2108     * <p>
2109     * This is implemented to perform the superclass behavior
2110     * followed by a check if the dragEnabled
2111     * property is set and if the location picked is selected.
2112     */

2113        protected boolean isDragPossible(MouseEvent e) {
2114        if (super.isDragPossible(e)) {
2115        JTable table = (JTable) this.getComponent(e);
2116        if (table.getDragEnabled()) {
2117            Point p = e.getPoint();
2118            int row = table.rowAtPoint(p);
2119            int column = table.columnAtPoint(p);
2120            // For 4835633. Otherwise, you can drag a file by clicking below
2121
// it.
2122
if (pointOutsidePrefSize(table, row, column, p)) {
2123                return false;
2124            }
2125            if ((column != -1) && (row != -1) && table.isCellSelected(row, column)) {
2126            return true;
2127            }
2128        }
2129        }
2130        return false;
2131    }
2132    }
2133
2134    private static DropTargetListener defaultDropTargetListener = null;
2135
2136    /**
2137     * A DropTargetListener to extend the default Swing handling of drop operations
2138     * by moving the tree selection to the nearest location to the mouse pointer.
2139     * Also adds autoscroll capability.
2140     */

2141    static class TableDropTargetListener extends BasicDropTargetListener JavaDoc {
2142
2143    /**
2144     * called to save the state of a component in case it needs to
2145     * be restored because a drop is not performed.
2146     */

2147        protected void saveComponentState(JComponent comp) {
2148        JTable table = (JTable) comp;
2149        rows = table.getSelectedRows();
2150        cols = table.getSelectedColumns();
2151    }
2152
2153    /**
2154     * called to restore the state of a component
2155     * because a drop was not performed.
2156     */

2157        protected void restoreComponentState(JComponent comp) {
2158        JTable table = (JTable) comp;
2159        table.clearSelection();
2160        for (int i = 0; i < rows.length; i++) {
2161        table.addRowSelectionInterval(rows[i], rows[i]);
2162        }
2163        for (int i = 0; i < cols.length; i++) {
2164        table.addColumnSelectionInterval(cols[i], cols[i]);
2165        }
2166    }
2167
2168    /**
2169     * called to set the insertion location to match the current
2170     * mouse pointer coordinates.
2171     */

2172        protected void updateInsertionLocation(JComponent comp, Point p) {
2173        JTable table = (JTable) comp;
2174            int row = table.rowAtPoint(p);
2175            int col = table.columnAtPoint(p);
2176        if (row != -1) {
2177        table.setRowSelectionInterval(row, row);
2178        }
2179        if (col != -1) {
2180        table.setColumnSelectionInterval(col, col);
2181        }
2182    }
2183
2184    private int[] rows;
2185    private int[] cols;
2186    }
2187
2188    private static final TransferHandler defaultTransferHandler = new TableTransferHandler();
2189
2190    static class TableTransferHandler extends TransferHandler implements UIResource {
2191
2192    /**
2193     * Create a Transferable to use as the source for a data transfer.
2194     *
2195     * @param c The component holding the data to be transfered. This
2196     * argument is provided to enable sharing of TransferHandlers by
2197     * multiple components.
2198     * @return The representation of the data to be transfered.
2199     *
2200     */

2201        protected Transferable createTransferable(JComponent c) {
2202        if (c instanceof JTable) {
2203        JTable table = (JTable) c;
2204        int[] rows;
2205        int[] cols;
2206        
2207        if (!table.getRowSelectionAllowed() && !table.getColumnSelectionAllowed()) {
2208            return null;
2209        }
2210        
2211                if (!table.getRowSelectionAllowed()) {
2212                    int rowCount = table.getRowCount();
2213
2214                    rows = new int[rowCount];
2215                    for (int counter = 0; counter < rowCount; counter++) {
2216                        rows[counter] = counter;
2217                    }
2218                } else {
2219            rows = table.getSelectedRows();
2220        }
2221        
2222                if (!table.getColumnSelectionAllowed()) {
2223                    int colCount = table.getColumnCount();
2224
2225                    cols = new int[colCount];
2226                    for (int counter = 0; counter < colCount; counter++) {
2227                        cols[counter] = counter;
2228                    }
2229                } else {
2230            cols = table.getSelectedColumns();
2231        }
2232                
2233        if (rows == null || cols == null || rows.length == 0 || cols.length == 0) {
2234            return null;
2235        }
2236                
2237                StringBuffer JavaDoc plainBuf = new StringBuffer JavaDoc();
2238                StringBuffer JavaDoc htmlBuf = new StringBuffer JavaDoc();
2239                
2240                htmlBuf.append("<html>\n<body>\n<table>\n");
2241                
2242                for (int row = 0; row < rows.length; row++) {
2243                    htmlBuf.append("<tr>\n");
2244                    for (int col = 0; col < cols.length; col++) {
2245                        Object JavaDoc obj = table.getValueAt(rows[row], cols[col]);
2246                        String JavaDoc val = ((obj == null) ? "" : obj.toString());
2247                        plainBuf.append(val + "\t");
2248                        htmlBuf.append(" <td>" + val + "</td>\n");
2249                    }
2250                    // we want a newline at the end of each line and not a tab
2251
plainBuf.deleteCharAt(plainBuf.length() - 1).append("\n");
2252                    htmlBuf.append("</tr>\n");
2253                }
2254
2255                // remove the last newline
2256
plainBuf.deleteCharAt(plainBuf.length() - 1);
2257                htmlBuf.append("</table>\n</body>\n</html>");
2258                
2259                return new BasicTransferable JavaDoc(plainBuf.toString(), htmlBuf.toString());
2260        }
2261
2262        return null;
2263    }
2264
2265        public int getSourceActions(JComponent c) {
2266        return COPY;
2267    }
2268
2269    }
2270} // End of Class BasicTableUI
2271
Popular Tags