59 |
import javax.swing.AbstractAction; |
import javax.swing.AbstractAction; |
60 |
import javax.swing.ActionMap; |
import javax.swing.ActionMap; |
61 |
import javax.swing.CellRendererPane; |
import javax.swing.CellRendererPane; |
62 |
|
import javax.swing.DefaultListSelectionModel; |
63 |
import javax.swing.InputMap; |
import javax.swing.InputMap; |
64 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
65 |
import javax.swing.JList; |
import javax.swing.JList; |
229 |
{ |
{ |
230 |
int lead = list.getLeadSelectionIndex(); |
int lead = list.getLeadSelectionIndex(); |
231 |
int max = list.getModel().getSize() - 1; |
int max = list.getModel().getSize() - 1; |
232 |
|
DefaultListSelectionModel selModel = (DefaultListSelectionModel)list.getSelectionModel(); |
233 |
|
String command = e.getActionCommand(); |
234 |
// Do nothing if list is empty |
// Do nothing if list is empty |
235 |
if (max == -1) |
if (max == -1) |
236 |
return; |
return; |
237 |
|
|
238 |
if (e.getActionCommand().equals("selectNextRow")) |
if (command.equals("selectNextRow")) |
239 |
{ |
{ |
240 |
selectNextIndex(); |
selectNextIndex(); |
241 |
} |
} |
242 |
else if (e.getActionCommand().equals("selectPreviousRow")) |
else if (command.equals("selectPreviousRow")) |
243 |
{ |
{ |
244 |
selectPreviousIndex(); |
selectPreviousIndex(); |
245 |
} |
} |
246 |
else if (e.getActionCommand().equals("clearSelection")) |
else if (command.equals("clearSelection")) |
247 |
{ |
{ |
248 |
list.clearSelection(); |
list.clearSelection(); |
249 |
} |
} |
250 |
else if (e.getActionCommand().equals("selectAll")) |
else if (command.equals("selectAll")) |
251 |
{ |
{ |
252 |
list.setSelectionInterval(0, max); |
list.setSelectionInterval(0, max); |
253 |
// this next line is to restore the lead selection index to the old |
// this next line is to restore the lead selection index to the old |
254 |
// position, because select-all should not change the lead index |
// position, because select-all should not change the lead index |
255 |
list.addSelectionInterval(lead, lead); |
list.addSelectionInterval(lead, lead); |
256 |
} |
} |
257 |
else if (e.getActionCommand().equals("selectLastRow")) |
else if (command.equals("selectLastRow")) |
258 |
{ |
{ |
259 |
list.setSelectedIndex(list.getModel().getSize() - 1); |
list.setSelectedIndex(list.getModel().getSize() - 1); |
260 |
} |
} |
261 |
else if (e.getActionCommand().equals("scrollDownExtendSelection")) |
else if (command.equals("selectLastRowChangeLead")) |
262 |
|
{ |
263 |
|
selModel.moveLeadSelectionIndex(list.getModel().getSize() - 1); |
264 |
|
} |
265 |
|
else if (command.equals("scrollDownExtendSelection")) |
266 |
{ |
{ |
267 |
int target; |
int target; |
268 |
if (lead == list.getLastVisibleIndex()) |
if (lead == list.getLastVisibleIndex()) |
273 |
} |
} |
274 |
else |
else |
275 |
target = list.getLastVisibleIndex(); |
target = list.getLastVisibleIndex(); |
276 |
list.getSelectionModel().setLeadSelectionIndex(target); |
selModel.setLeadSelectionIndex(target); |
277 |
} |
} |
278 |
else if (e.getActionCommand().equals("scrollUpExtendSelection")) |
else if (command.equals("scrollDownChangeLead")) |
279 |
|
{ |
280 |
|
int target; |
281 |
|
if (lead == list.getLastVisibleIndex()) |
282 |
|
{ |
283 |
|
target = Math.min |
284 |
|
(max, lead + (list.getLastVisibleIndex() - |
285 |
|
list.getFirstVisibleIndex() + 1)); |
286 |
|
} |
287 |
|
else |
288 |
|
target = list.getLastVisibleIndex(); |
289 |
|
selModel.moveLeadSelectionIndex(target); |
290 |
|
} |
291 |
|
else if (command.equals("scrollUpExtendSelection")) |
292 |
{ |
{ |
293 |
int target; |
int target; |
294 |
if (lead == list.getFirstVisibleIndex()) |
if (lead == list.getFirstVisibleIndex()) |
299 |
} |
} |
300 |
else |
else |
301 |
target = list.getFirstVisibleIndex(); |
target = list.getFirstVisibleIndex(); |
302 |
list.getSelectionModel().setLeadSelectionIndex(target); |
selModel.setLeadSelectionIndex(target); |
303 |
} |
} |
304 |
else if (e.getActionCommand().equals("selectNextRowExtendSelection")) |
else if (command.equals("scrollUpChangeLead")) |
305 |
{ |
{ |
306 |
list.getSelectionModel(). |
int target; |
307 |
setLeadSelectionIndex(Math.min(lead + 1,max)); |
if (lead == list.getFirstVisibleIndex()) |
308 |
|
{ |
309 |
|
target = Math.max |
310 |
|
(0, lead - (list.getLastVisibleIndex() - |
311 |
|
list.getFirstVisibleIndex() + 1)); |
312 |
|
} |
313 |
|
else |
314 |
|
target = list.getFirstVisibleIndex(); |
315 |
|
selModel.moveLeadSelectionIndex(target); |
316 |
|
} |
317 |
|
else if (command.equals("selectNextRowExtendSelection")) |
318 |
|
{ |
319 |
|
selModel.setLeadSelectionIndex(Math.min(lead + 1,max)); |
320 |
} |
} |
321 |
else if (e.getActionCommand().equals("selectFirstRow")) |
else if (command.equals("selectFirstRow")) |
322 |
{ |
{ |
323 |
list.setSelectedIndex(0); |
list.setSelectedIndex(0); |
324 |
} |
} |
325 |
else if (e.getActionCommand().equals("selectFirstRowExtendSelection")) |
else if (command.equals("selectFirstRowChangeLead")) |
326 |
|
{ |
327 |
|
selModel.moveLeadSelectionIndex(0); |
328 |
|
} |
329 |
|
else if (command.equals("selectFirstRowExtendSelection")) |
330 |
{ |
{ |
331 |
list.getSelectionModel().setLeadSelectionIndex(0); |
selModel.setLeadSelectionIndex(0); |
332 |
} |
} |
333 |
else if (e.getActionCommand().equals("selectPreviousRowExtendSelection")) |
else if (command.equals("selectPreviousRowExtendSelection")) |
334 |
{ |
{ |
335 |
list.getSelectionModel().setLeadSelectionIndex(Math.max(0,lead - 1)); |
selModel.setLeadSelectionIndex(Math.max(0,lead - 1)); |
336 |
} |
} |
337 |
else if (e.getActionCommand().equals("scrollUp")) |
else if (command.equals("scrollUp")) |
338 |
{ |
{ |
339 |
int target; |
int target; |
340 |
if (lead == list.getFirstVisibleIndex()) |
if (lead == list.getFirstVisibleIndex()) |
347 |
target = list.getFirstVisibleIndex(); |
target = list.getFirstVisibleIndex(); |
348 |
list.setSelectedIndex(target); |
list.setSelectedIndex(target); |
349 |
} |
} |
350 |
else if (e.getActionCommand().equals("selectLastRowExtendSelection")) |
else if (command.equals("selectLastRowExtendSelection")) |
351 |
{ |
{ |
352 |
list.getSelectionModel(). |
selModel.setLeadSelectionIndex(list.getModel().getSize() - 1); |
|
setLeadSelectionIndex(list.getModel().getSize() - 1); |
|
353 |
} |
} |
354 |
else if (e.getActionCommand().equals("scrollDown")) |
else if (command.equals("scrollDown")) |
355 |
{ |
{ |
356 |
int target; |
int target; |
357 |
if (lead == list.getLastVisibleIndex()) |
if (lead == list.getLastVisibleIndex()) |
364 |
target = list.getLastVisibleIndex(); |
target = list.getLastVisibleIndex(); |
365 |
list.setSelectedIndex(target); |
list.setSelectedIndex(target); |
366 |
} |
} |
367 |
|
else if (command.equals("selectNextRowChangeLead")) |
368 |
|
{ |
369 |
|
if (selModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) |
370 |
|
selectNextIndex(); |
371 |
|
else |
372 |
|
{ |
373 |
|
selModel.moveLeadSelectionIndex(Math.min(max, lead + 1)); |
374 |
|
} |
375 |
|
} |
376 |
|
else if (command.equals("selectPreviousRowChangeLead")) |
377 |
|
{ |
378 |
|
if (selModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) |
379 |
|
selectPreviousIndex(); |
380 |
|
else |
381 |
|
{ |
382 |
|
selModel.moveLeadSelectionIndex(Math.max(0, lead - 1)); |
383 |
|
} |
384 |
|
} |
385 |
|
else if (command.equals("addToSelection")) |
386 |
|
{ |
387 |
|
list.addSelectionInterval(lead, lead); |
388 |
|
} |
389 |
|
else if (command.equals("extendTo")) |
390 |
|
{ |
391 |
|
selModel.setSelectionInterval(selModel.getAnchorSelectionIndex(), |
392 |
|
lead); |
393 |
|
} |
394 |
|
else if (command.equals("toggleAndAnchor")) |
395 |
|
{ |
396 |
|
if (!list.isSelectedIndex(lead)) |
397 |
|
list.addSelectionInterval(lead, lead); |
398 |
|
else |
399 |
|
list.removeSelectionInterval(lead, lead); |
400 |
|
selModel.setAnchorSelectionIndex(lead); |
401 |
|
} |
402 |
else |
else |
403 |
{ |
{ |
404 |
// DEBUG: uncomment the following line to print out |
// DEBUG: uncomment the following line to print out |
426 |
public void mouseClicked(MouseEvent event) |
public void mouseClicked(MouseEvent event) |
427 |
{ |
{ |
428 |
Point click = event.getPoint(); |
Point click = event.getPoint(); |
429 |
int index = BasicListUI.this.locationToIndex(list, click); |
int index = locationToIndex(list, click); |
430 |
if (index == -1) |
if (index == -1) |
431 |
return; |
return; |
432 |
if (event.isShiftDown()) |
if (event.isShiftDown()) |
433 |
{ |
{ |
434 |
if (BasicListUI.this.list.getSelectionMode() == |
if (list.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION) |
435 |
ListSelectionModel.SINGLE_SELECTION) |
list.setSelectedIndex(index); |
436 |
BasicListUI.this.list.setSelectedIndex(index); |
else if (list.getSelectionMode() == |
|
else if (BasicListUI.this.list.getSelectionMode() == |
|
437 |
ListSelectionModel.SINGLE_INTERVAL_SELECTION) |
ListSelectionModel.SINGLE_INTERVAL_SELECTION) |
438 |
// COMPAT: the IBM VM is compatible with the following line of code. |
// COMPAT: the IBM VM is compatible with the following line of code. |
439 |
// However, compliance with Sun's VM would correspond to replacing |
// However, compliance with Sun's VM would correspond to replacing |
440 |
// getAnchorSelectionIndex() with getLeadSelectionIndex().This is |
// getAnchorSelectionIndex() with getLeadSelectionIndex().This is |
441 |
// both unnatural and contradictory to the way they handle other |
// both unnatural and contradictory to the way they handle other |
442 |
// similar UI interactions. |
// similar UI interactions. |
443 |
BasicListUI.this.list.setSelectionInterval |
list.setSelectionInterval(list.getAnchorSelectionIndex(), index); |
|
(BasicListUI.this.list.getAnchorSelectionIndex(), index); |
|
444 |
else |
else |
445 |
// COMPAT: both Sun and IBM are compatible instead with: |
// COMPAT: both Sun and IBM are compatible instead with: |
446 |
// BasicListUI.this.list.setSelectionInterval |
// list.setSelectionInterval |
447 |
// (BasicListUI.this.list.getLeadSelectionIndex(),index); |
// (list.getLeadSelectionIndex(),index); |
448 |
// Note that for IBM this is contradictory to what they did in |
// Note that for IBM this is contradictory to what they did in |
449 |
// the above situation for SINGLE_INTERVAL_SELECTION. |
// the above situation for SINGLE_INTERVAL_SELECTION. |
450 |
// The most natural thing to do is the following: |
// The most natural thing to do is the following: |
451 |
BasicListUI.this.list.getSelectionModel(). |
if (list.isSelectedIndex(list.getAnchorSelectionIndex())) |
452 |
setLeadSelectionIndex(index); |
list.getSelectionModel().setLeadSelectionIndex(index); |
453 |
|
else |
454 |
|
list.addSelectionInterval(list.getAnchorSelectionIndex(), index); |
455 |
} |
} |
456 |
else if (event.isControlDown()) |
else if (event.isControlDown()) |
457 |
{ |
{ |
458 |
if (BasicListUI.this.list.getSelectionMode() == |
if (list.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION) |
459 |
ListSelectionModel.SINGLE_SELECTION) |
list.setSelectedIndex(index); |
460 |
BasicListUI.this.list.setSelectedIndex(index); |
else if (list.isSelectedIndex(index)) |
461 |
else if (BasicListUI.this.list.isSelectedIndex(index)) |
list.removeSelectionInterval(index,index); |
|
BasicListUI.this.list.removeSelectionInterval(index,index); |
|
462 |
else |
else |
463 |
BasicListUI.this.list.addSelectionInterval(index,index); |
list.addSelectionInterval(index,index); |
464 |
} |
} |
465 |
else |
else |
466 |
BasicListUI.this.list.setSelectedIndex(index); |
list.setSelectedIndex(index); |
467 |
|
|
468 |
BasicListUI.this.list.ensureIndexIsVisible |
list.ensureIndexIsVisible(list.getLeadSelectionIndex()); |
|
(BasicListUI.this.list.getLeadSelectionIndex()); |
|
469 |
} |
} |
470 |
|
|
471 |
/** |
/** |