001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.plaf.basic;
022:
023: import java.awt.Rectangle;
024: import java.awt.event.ActionEvent;
025:
026: import javax.swing.AbstractAction;
027: import javax.swing.DefaultListSelectionModel;
028: import javax.swing.JComponent;
029: import javax.swing.JList;
030: import javax.swing.TransferHandler;
031:
032: import org.apache.harmony.x.swing.Utilities;
033:
034: final class BasicListKeyboardActions {
035: private static AbstractAction selectPreviousRowAction = new AbstractAction() {
036: public void actionPerformed(final ActionEvent e) {
037: JList list = (JList) e.getSource();
038:
039: int previousIndex = getPreviousRow(list);
040: if (previousIndex == -1) {
041: return;
042: }
043:
044: list.setSelectedIndex(previousIndex);
045: list.ensureIndexIsVisible(previousIndex);
046: }
047: };
048:
049: private static AbstractAction selectPreviousRowChangeLeadAction = new AbstractAction() {
050: public void actionPerformed(final ActionEvent e) {
051: JList list = (JList) e.getSource();
052: if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
053: return;
054: }
055:
056: int previousIndex = getPreviousRow(list);
057: if (previousIndex == -1) {
058: return;
059: }
060:
061: ((DefaultListSelectionModel) list.getSelectionModel())
062: .moveLeadSelectionIndex(previousIndex);
063: list.ensureIndexIsVisible(previousIndex);
064: }
065: };
066:
067: private static AbstractAction selectPreviousRowExtendSelectionAction = new AbstractAction() {
068: public void actionPerformed(final ActionEvent e) {
069: JList list = (JList) e.getSource();
070:
071: int previousIndex = getPreviousRow(list);
072: if (previousIndex == -1) {
073: return;
074: }
075:
076: if (list.isSelectionEmpty()) {
077: list.setSelectedIndex(previousIndex);
078: } else {
079: list.setValueIsAdjusting(true);
080: list.clearSelection();
081: list.addSelectionInterval(list
082: .getAnchorSelectionIndex(), previousIndex);
083: list.setValueIsAdjusting(false);
084: list.ensureIndexIsVisible(previousIndex);
085: }
086: }
087: };
088:
089: private static AbstractAction selectNextRowAction = new AbstractAction() {
090: public void actionPerformed(final ActionEvent e) {
091: JList list = (JList) e.getSource();
092: int nextIndex = getNextRow(list);
093: if (nextIndex == -1) {
094: return;
095: }
096:
097: list.setSelectedIndex(nextIndex);
098: list.ensureIndexIsVisible(nextIndex);
099: }
100: };
101:
102: private static AbstractAction selectNextRowChangeLeadAction = new AbstractAction() {
103: public void actionPerformed(final ActionEvent e) {
104: JList list = (JList) e.getSource();
105: if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
106: return;
107: }
108:
109: int nextIndex = getNextRow(list);
110: if (nextIndex == -1) {
111: return;
112: }
113:
114: ((DefaultListSelectionModel) list.getSelectionModel())
115: .moveLeadSelectionIndex(nextIndex);
116: list.ensureIndexIsVisible(nextIndex);
117: }
118: };
119:
120: private static AbstractAction selectNextRowExtendSelectionAction = new AbstractAction() {
121: public void actionPerformed(final ActionEvent e) {
122: JList list = (JList) e.getSource();
123: int nextIndex = getNextRow(list);
124: if (nextIndex == -1) {
125: return;
126: }
127:
128: if (list.isSelectionEmpty()) {
129: list.setSelectedIndex(nextIndex);
130: } else {
131: list.setValueIsAdjusting(true);
132: list.clearSelection();
133: list.addSelectionInterval(list
134: .getAnchorSelectionIndex(), nextIndex);
135: list.setValueIsAdjusting(false);
136: list.ensureIndexIsVisible(nextIndex);
137: }
138: }
139: };
140:
141: private static AbstractAction selectPreviousColumnAction = new AbstractAction() {
142: public void actionPerformed(final ActionEvent e) {
143: JList list = (JList) e.getSource();
144: int previousIndex = getPreviousColumn(list);
145: if (previousIndex == -1) {
146: return;
147: }
148:
149: list.setSelectedIndex(previousIndex);
150: list.ensureIndexIsVisible(previousIndex);
151: }
152: };
153:
154: private static AbstractAction selectPreviousColumnChangeLeadAction = new AbstractAction() {
155: public void actionPerformed(final ActionEvent e) {
156: JList list = (JList) e.getSource();
157: if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
158: return;
159: }
160:
161: int previousIndex = getPreviousColumn(list);
162: if (previousIndex == -1) {
163: return;
164: }
165:
166: ((DefaultListSelectionModel) list.getSelectionModel())
167: .moveLeadSelectionIndex(previousIndex);
168: list.ensureIndexIsVisible(previousIndex);
169: }
170: };
171:
172: private static AbstractAction selectPreviousColumnExtendSelectionAction = new AbstractAction() {
173: public void actionPerformed(final ActionEvent e) {
174: JList list = (JList) e.getSource();
175:
176: int previousIndex = getPreviousColumn(list);
177: if (previousIndex == -1) {
178: return;
179: }
180:
181: list.setValueIsAdjusting(true);
182: list.clearSelection();
183: list.addSelectionInterval(list.getAnchorSelectionIndex(),
184: previousIndex);
185: list.setValueIsAdjusting(false);
186: list.ensureIndexIsVisible(previousIndex);
187: }
188: };
189:
190: private static AbstractAction selectNextColumnAction = new AbstractAction() {
191: public void actionPerformed(final ActionEvent e) {
192: JList list = (JList) e.getSource();
193:
194: int nextIndex = getNextColumn(list);
195: if (nextIndex == -1) {
196: return;
197: }
198:
199: list.setSelectedIndex(nextIndex);
200: list.ensureIndexIsVisible(nextIndex);
201: }
202: };
203:
204: private static AbstractAction selectNextColumnChangeLeadAction = new AbstractAction() {
205: public void actionPerformed(final ActionEvent e) {
206: JList list = (JList) e.getSource();
207: if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
208: return;
209: }
210:
211: int nextIndex = getNextColumn(list);
212: if (nextIndex == -1) {
213: return;
214: }
215:
216: ((DefaultListSelectionModel) list.getSelectionModel())
217: .moveLeadSelectionIndex(nextIndex);
218: list.ensureIndexIsVisible(nextIndex);
219: }
220: };
221:
222: private static AbstractAction selectNextColumnExtendSelectionAction = new AbstractAction() {
223: public void actionPerformed(final ActionEvent e) {
224: JList list = (JList) e.getSource();
225:
226: int nextIndex = getNextColumn(list);
227: if (nextIndex == -1) {
228: return;
229: }
230:
231: list.setValueIsAdjusting(true);
232: list.clearSelection();
233: list.addSelectionInterval(list.getAnchorSelectionIndex(),
234: nextIndex);
235: list.setValueIsAdjusting(false);
236: list.ensureIndexIsVisible(nextIndex);
237: }
238: };
239:
240: private static AbstractAction selectFirstRowAction = new AbstractAction() {
241: public void actionPerformed(final ActionEvent e) {
242: JList list = (JList) e.getSource();
243: if (list.getModel().getSize() == 0) {
244: return;
245: }
246:
247: int candidateIndex = 0;
248: BasicListUI ui = (BasicListUI) list.getUI();
249: if (ui.extendedSupportEnabled) {
250: while (candidateIndex < list.getModel().getSize()) {
251: if (ui.isChoosable(candidateIndex)) {
252: break;
253: }
254: candidateIndex++;
255: }
256: if (candidateIndex < list.getModel().getSize()) {
257: return;
258: }
259: }
260: list.setSelectedIndex(candidateIndex);
261: list.ensureIndexIsVisible(candidateIndex);
262: }
263: };
264:
265: private static AbstractAction selectFirstRowChangeLeadAction = new AbstractAction() {
266: public void actionPerformed(final ActionEvent e) {
267: JList list = (JList) e.getSource();
268: if (list.getModel().getSize() == 0) {
269: return;
270: }
271: if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
272: return;
273: }
274:
275: ((DefaultListSelectionModel) list.getSelectionModel())
276: .moveLeadSelectionIndex(0);
277: list.ensureIndexIsVisible(0);
278: }
279: };
280:
281: private static AbstractAction selectFirstRowExtendSelectionAction = new AbstractAction() {
282: public void actionPerformed(final ActionEvent e) {
283: JList list = (JList) e.getSource();
284: if (list.getModel().getSize() == 0) {
285: return;
286: }
287:
288: int beginIndex = list.getAnchorSelectionIndex();
289: if (beginIndex != -1) {
290: list.setValueIsAdjusting(true);
291: list.clearSelection();
292: list.addSelectionInterval(beginIndex, 0);
293: list.setValueIsAdjusting(false);
294: }
295:
296: list.ensureIndexIsVisible(0);
297: }
298: };
299:
300: private static AbstractAction selectLastRowAction = new AbstractAction() {
301: public void actionPerformed(final ActionEvent e) {
302: JList list = (JList) e.getSource();
303: if (list.getModel().getSize() == 0) {
304: return;
305: }
306:
307: int candidateIndex = list.getModel().getSize() - 1;
308: BasicListUI ui = (BasicListUI) list.getUI();
309: if (ui.extendedSupportEnabled) {
310: while (candidateIndex >= 0) {
311: if (ui.isChoosable(candidateIndex)) {
312: break;
313: }
314: candidateIndex--;
315: }
316: if (candidateIndex == -1) {
317: return;
318: }
319: }
320:
321: list.setSelectedIndex(candidateIndex);
322: list.ensureIndexIsVisible(candidateIndex);
323: }
324: };
325:
326: private static AbstractAction selectLastRowChangeLeadAction = new AbstractAction() {
327: public void actionPerformed(final ActionEvent e) {
328: JList list = (JList) e.getSource();
329: if (list.getModel().getSize() == 0) {
330: return;
331: }
332: if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
333: return;
334: }
335:
336: int lastIndex = list.getModel().getSize() - 1;
337: ((DefaultListSelectionModel) list.getSelectionModel())
338: .moveLeadSelectionIndex(lastIndex);
339: list.ensureIndexIsVisible(lastIndex);
340: }
341: };
342:
343: private static AbstractAction selectLastRowExtendSelectionAction = new AbstractAction() {
344: public void actionPerformed(final ActionEvent e) {
345: JList list = (JList) e.getSource();
346: if (list.getModel().getSize() == 0) {
347: return;
348: }
349:
350: int lastIndex = list.getModel().getSize() - 1;
351: int beginIndex = list.getAnchorSelectionIndex();
352: if (beginIndex != -1) {
353: list.setValueIsAdjusting(true);
354: list.clearSelection();
355: list.addSelectionInterval(beginIndex, lastIndex);
356: list.setValueIsAdjusting(false);
357: }
358:
359: list.ensureIndexIsVisible(lastIndex);
360: }
361: };
362:
363: private static AbstractAction scrollUpAction = new AbstractAction() {
364: public void actionPerformed(final ActionEvent e) {
365: JList list = (JList) e.getSource();
366:
367: int upIndex = getScrollUpIndex(list);
368: if (upIndex == -1) {
369: return;
370: }
371:
372: list.setSelectedIndex(upIndex);
373: list.ensureIndexIsVisible(upIndex);
374: }
375: };
376:
377: private static AbstractAction scrollUpChangeLeadAction = new AbstractAction() {
378: public void actionPerformed(final ActionEvent e) {
379: JList list = (JList) e.getSource();
380: if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
381: return;
382: }
383:
384: int upIndex = getScrollUpIndex(list);
385: if (upIndex == -1) {
386: return;
387: }
388:
389: ((DefaultListSelectionModel) list.getSelectionModel())
390: .moveLeadSelectionIndex(upIndex);
391: list.ensureIndexIsVisible(upIndex);
392: }
393: };
394:
395: private static AbstractAction scrollUpExtendSelectionAction = new AbstractAction() {
396: public void actionPerformed(final ActionEvent e) {
397: JList list = (JList) e.getSource();
398:
399: int upIndex = getScrollUpIndex(list);
400: if (upIndex == -1) {
401: return;
402: }
403:
404: int beginIndex = list.getAnchorSelectionIndex();
405: if (beginIndex != -1) {
406: list.setValueIsAdjusting(true);
407: list.clearSelection();
408: list.addSelectionInterval(beginIndex, upIndex);
409: list.setValueIsAdjusting(false);
410: }
411:
412: list.ensureIndexIsVisible(upIndex);
413: }
414: };
415:
416: private static AbstractAction scrollDownAction = new AbstractAction() {
417: public void actionPerformed(final ActionEvent e) {
418: JList list = (JList) e.getSource();
419:
420: int downIndex = getScrollDownIndex(list);
421: if (downIndex == -1) {
422: return;
423: }
424:
425: list.setSelectedIndex(downIndex);
426: list.ensureIndexIsVisible(downIndex);
427: }
428: };
429:
430: private static AbstractAction scrollDownChangeLeadAction = new AbstractAction() {
431: public void actionPerformed(final ActionEvent e) {
432: JList list = (JList) e.getSource();
433: if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
434: return;
435: }
436:
437: int downIndex = getScrollDownIndex(list);
438: if (downIndex == -1) {
439: return;
440: }
441:
442: ((DefaultListSelectionModel) list.getSelectionModel())
443: .moveLeadSelectionIndex(downIndex);
444: list.ensureIndexIsVisible(downIndex);
445: }
446: };
447:
448: private static AbstractAction scrollDownExtendSelectionAction = new AbstractAction() {
449: public void actionPerformed(final ActionEvent e) {
450: JList list = (JList) e.getSource();
451:
452: int downIndex = getScrollDownIndex(list);
453: if (downIndex == -1) {
454: return;
455: }
456:
457: int beginIndex = list.getAnchorSelectionIndex();
458: if (beginIndex != -1) {
459: list.setValueIsAdjusting(true);
460: list.clearSelection();
461: list.addSelectionInterval(beginIndex, downIndex);
462: list.setValueIsAdjusting(false);
463: }
464:
465: list.ensureIndexIsVisible(downIndex);
466: }
467: };
468:
469: private static AbstractAction selectAllAction = new AbstractAction() {
470: public void actionPerformed(final ActionEvent e) {
471: JList list = (JList) e.getSource();
472: if (list.getModel().getSize() == 0) {
473: return;
474: }
475:
476: list.setSelectionInterval(0, list.getModel().getSize() - 1);
477: }
478: };
479:
480: private static AbstractAction clearSelectionAction = new AbstractAction() {
481: public void actionPerformed(final ActionEvent e) {
482: JList list = (JList) e.getSource();
483: if (list.getModel().getSize() == 0) {
484: return;
485: }
486:
487: list.clearSelection();
488: }
489: };
490:
491: private static AbstractAction addToSelectionAction = new AbstractAction() {
492: public void actionPerformed(final ActionEvent e) {
493: JList list = (JList) e.getSource();
494:
495: int addIndex = list.getLeadSelectionIndex();
496: if (addIndex == -1) {
497: return;
498: }
499:
500: list.addSelectionInterval(addIndex, addIndex);
501: }
502: };
503:
504: private static AbstractAction toggleAndAnchorAction = new AbstractAction() {
505: public void actionPerformed(final ActionEvent e) {
506: JList list = (JList) e.getSource();
507:
508: int toggleIndex = list.getLeadSelectionIndex();
509: if (toggleIndex == -1) {
510: return;
511: }
512:
513: if (list.isSelectedIndex(toggleIndex)) {
514: list.removeSelectionInterval(toggleIndex, toggleIndex);
515: } else {
516: list.addSelectionInterval(toggleIndex, toggleIndex);
517: }
518: list.getSelectionModel().setAnchorSelectionIndex(
519: toggleIndex);
520: }
521: };
522:
523: private static AbstractAction moveSelectionToAction = new AbstractAction() {
524: public void actionPerformed(final ActionEvent e) {
525: JList list = (JList) e.getSource();
526:
527: int moveIndex = list.getLeadSelectionIndex();
528: if (moveIndex == -1) {
529: return;
530: }
531:
532: list.setSelectedIndex(moveIndex);
533: }
534: };
535:
536: private static AbstractAction extendToAction = new AbstractAction() {
537: public void actionPerformed(final ActionEvent e) {
538: JList list = (JList) e.getSource();
539: int extendIndex = list.getLeadSelectionIndex();
540: if (extendIndex == -1) {
541: return;
542: }
543:
544: list.setValueIsAdjusting(true);
545: list.clearSelection();
546: list.addSelectionInterval(list.getAnchorSelectionIndex(),
547: extendIndex);
548: list.setValueIsAdjusting(false);
549: }
550: };
551:
552: public static void installKeyboardActions(final JList list) {
553: Utilities.installKeyboardActions(list, JComponent.WHEN_FOCUSED,
554: "List.focusInputMap", "List.focusInputMap.RightToLeft");
555:
556: list.getActionMap().put("selectPreviousRow",
557: selectPreviousRowAction);
558: list.getActionMap().put("selectNextRow", selectNextRowAction);
559: list.getActionMap().put("selectPreviousRowExtendSelection",
560: selectPreviousRowExtendSelectionAction);
561: list.getActionMap().put("selectNextRowExtendSelection",
562: selectNextRowExtendSelectionAction);
563:
564: list.getActionMap().put("selectPreviousRowChangeLead",
565: selectPreviousRowChangeLeadAction);
566: list.getActionMap().put("selectNextRowChangeLead",
567: selectNextRowChangeLeadAction);
568: list.getActionMap().put("selectPreviousColumnChangeLead",
569: selectPreviousColumnChangeLeadAction);
570: list.getActionMap().put("selectNextColumnChangeLead",
571: selectNextColumnChangeLeadAction);
572:
573: list.getActionMap().put("selectPreviousColumn",
574: selectPreviousColumnAction);
575: list.getActionMap().put("selectNextColumn",
576: selectNextColumnAction);
577: list.getActionMap().put("selectPreviousColumnExtendSelection",
578: selectPreviousColumnExtendSelectionAction);
579: list.getActionMap().put("selectNextColumnExtendSelection",
580: selectNextColumnExtendSelectionAction);
581:
582: list.getActionMap().put("selectFirstRow", selectFirstRowAction);
583: list.getActionMap().put("selectFirstRowExtendSelection",
584: selectFirstRowExtendSelectionAction);
585: list.getActionMap().put("selectLastRow", selectLastRowAction);
586: list.getActionMap().put("selectLastRowExtendSelection",
587: selectLastRowExtendSelectionAction);
588:
589: list.getActionMap().put("selectLastRowChangeLead",
590: selectLastRowChangeLeadAction);
591: list.getActionMap().put("selectFirstRowChangeLead",
592: selectFirstRowChangeLeadAction);
593:
594: list.getActionMap().put("scrollUp", scrollUpAction);
595: list.getActionMap().put("scrollUpExtendSelection",
596: scrollUpExtendSelectionAction);
597: list.getActionMap().put("scrollDown", scrollDownAction);
598: list.getActionMap().put("scrollDownExtendSelection",
599: scrollDownExtendSelectionAction);
600:
601: list.getActionMap().put("scrollUpChangeLead",
602: scrollUpChangeLeadAction);
603: list.getActionMap().put("scrollDownChangeLead",
604: scrollDownChangeLeadAction);
605:
606: list.getActionMap().put("selectAll", selectAllAction);
607: list.getActionMap().put("clearSelection", clearSelectionAction);
608:
609: list.getActionMap().put("addToSelection", addToSelectionAction);
610: list.getActionMap().put("toggleAndAnchor",
611: toggleAndAnchorAction);
612: list.getActionMap().put("moveSelectionTo",
613: moveSelectionToAction);
614: list.getActionMap().put("extendTo", extendToAction);
615:
616: list.getActionMap()
617: .put("copy", TransferHandler.getCopyAction());
618: list.getActionMap().put("paste",
619: TransferHandler.getPasteAction());
620: list.getActionMap().put("cut", TransferHandler.getCutAction());
621: }
622:
623: public static void uninstallKeyboardActions(final JList list) {
624: Utilities.uninstallKeyboardActions(list,
625: JComponent.WHEN_FOCUSED);
626: }
627:
628: private static int getNextRow(final JList list) {
629: if (list.getModel().getSize() == 0) {
630: return -1;
631: }
632:
633: BasicListUI ui = (BasicListUI) list.getUI();
634:
635: int currectSelection = list.getLeadSelectionIndex();
636: if (currectSelection == -1
637: || currectSelection >= list.getModel().getSize()) {
638: currectSelection = 0;
639: if (!ui.extendedSupportEnabled
640: || ui.isChoosable(currectSelection)) {
641: return currectSelection;
642: }
643: }
644:
645: ui.maybeUpdateLayoutState();
646: BasicListUI.LayoutStrategy strategy = ui.layouter
647: .getLayoutStrategy();
648:
649: int candidateIndex = currectSelection;
650: while (candidateIndex < list.getModel().getSize()) {
651: int selectedRow = strategy.getRow(candidateIndex);
652: int selectedColumn = strategy.getColumn(candidateIndex);
653: if (selectedRow < strategy.getRowCount() - 1
654: && strategy.getIndex(selectedRow + 1,
655: selectedColumn) < list.getModel().getSize()) {
656: candidateIndex = strategy.getIndex(selectedRow + 1,
657: selectedColumn);
658: } else if (list.getLayoutOrientation() == JList.VERTICAL_WRAP
659: && selectedColumn < strategy.getColumnCount() - 1
660: && strategy.getIndex(0, selectedColumn + 1) < list
661: .getModel().getSize()) {
662: candidateIndex = strategy.getIndex(0,
663: selectedColumn + 1);
664: } else {
665: return -1;
666: }
667:
668: if (ui.isChoosable(candidateIndex)) {
669: return candidateIndex;
670: }
671: }
672:
673: return -1;
674: }
675:
676: private static int getPreviousRow(final JList list) {
677: if (list.getModel().getSize() == 0) {
678: return -1;
679: }
680:
681: BasicListUI ui = (BasicListUI) list.getUI();
682: int currectSelection = list.getLeadSelectionIndex();
683: if (currectSelection == -1
684: || currectSelection >= list.getModel().getSize()) {
685: currectSelection = list.getModel().getSize() - 1;
686: if (!ui.extendedSupportEnabled
687: || ui.isChoosable(currectSelection)) {
688: return currectSelection;
689: }
690: }
691:
692: ui.maybeUpdateLayoutState();
693: BasicListUI.LayoutStrategy strategy = ui.layouter
694: .getLayoutStrategy();
695:
696: int candidateIndex = currectSelection;
697: while (candidateIndex >= 0) {
698: int selectedRow = strategy.getRow(candidateIndex);
699: int selectedColumn = strategy.getColumn(candidateIndex);
700:
701: if (selectedRow > 0) {
702: candidateIndex = strategy.getIndex(selectedRow - 1,
703: selectedColumn);
704: } else if (list.getLayoutOrientation() == JList.VERTICAL_WRAP
705: && selectedColumn > 0) {
706: candidateIndex = strategy.getIndex(strategy
707: .getRowCount() - 1, selectedColumn - 1);
708: } else {
709: return -1;
710: }
711:
712: if (ui.isChoosable(candidateIndex)) {
713: return candidateIndex;
714: }
715: }
716:
717: return -1;
718: }
719:
720: private static int getNextColumn(final JList list) {
721: if (list.getModel().getSize() == 0) {
722: return -1;
723: }
724:
725: int currectSelection = list.getLeadSelectionIndex();
726: if (currectSelection == -1
727: || currectSelection >= list.getModel().getSize()) {
728: list.setSelectedIndex(0);
729: return -1;
730: }
731:
732: BasicListUI ui = (BasicListUI) list.getUI();
733: ui.maybeUpdateLayoutState();
734: BasicListUI.LayoutStrategy strategy = ui.layouter
735: .getLayoutStrategy();
736:
737: int candidateIndex = currectSelection;
738: while (candidateIndex < list.getModel().getSize()) {
739: int selectedRow = strategy.getRow(candidateIndex);
740: int selectedColumn = strategy.getColumn(candidateIndex);
741: if (selectedColumn < strategy.getColumnCount() - 1) {
742: if (strategy.getIndex(selectedRow, selectedColumn + 1) < list
743: .getModel().getSize()) {
744: candidateIndex = strategy.getIndex(selectedRow,
745: selectedColumn + 1);
746: } else {
747: candidateIndex = list.getModel().getSize() - 1;
748: }
749: } else if (list.getLayoutOrientation() == JList.VERTICAL_WRAP
750: && selectedRow < strategy.getRowCount() - 1
751: && strategy.getIndex(selectedRow + 1,
752: selectedColumn) < list.getModel().getSize()) {
753: return strategy.getIndex(selectedRow + 1,
754: selectedColumn);
755: } else {
756: return -1;
757: }
758:
759: if (ui.isChoosable(candidateIndex)) {
760: return candidateIndex;
761: }
762: }
763:
764: return -1;
765: }
766:
767: private static int getPreviousColumn(final JList list) {
768: if (list.getModel().getSize() == 0) {
769: return -1;
770: }
771:
772: int currectSelection = list.getLeadSelectionIndex();
773: if (currectSelection == -1
774: || currectSelection >= list.getModel().getSize()) {
775: list.setSelectedIndex(list.getModel().getSize() - 1);
776: return -1;
777: }
778:
779: BasicListUI ui = (BasicListUI) list.getUI();
780: ui.maybeUpdateLayoutState();
781: BasicListUI.LayoutStrategy strategy = ui.layouter
782: .getLayoutStrategy();
783:
784: int candidateIndex = currectSelection;
785: while (candidateIndex >= 0) {
786: int selectedRow = strategy.getRow(candidateIndex);
787: int selectedColumn = strategy.getColumn(candidateIndex);
788: if (selectedColumn > 0) {
789: candidateIndex = strategy.getIndex(selectedRow,
790: selectedColumn - 1);
791: } else if (list.getLayoutOrientation() == JList.VERTICAL_WRAP
792: && selectedRow > 0) {
793: candidateIndex = strategy.getIndex(selectedRow - 1,
794: selectedColumn);
795: } else {
796: return -1;
797: }
798:
799: if (ui.isChoosable(candidateIndex)) {
800: return candidateIndex;
801: }
802: }
803:
804: return -1;
805: }
806:
807: private static int getScrollDownIndex(final JList list) {
808: if (list.getModel().getSize() == 0) {
809: return -1;
810: }
811:
812: int candidateIndex;
813: int currentSelection = list.getLeadSelectionIndex();
814: int lastVisible = list.getLastVisibleIndex();
815: if (lastVisible != currentSelection
816: || lastVisible == list.getModel().getSize() - 1) {
817: candidateIndex = lastVisible;
818: } else {
819: Rectangle visibleRect = list.getVisibleRect();
820: int i = lastVisible + 1;
821: while (i < list.getModel().getSize() - 1
822: && list.getCellBounds(lastVisible + 1, i).height < visibleRect.height) {
823: i++;
824: }
825:
826: candidateIndex = i;
827: }
828:
829: BasicListUI ui = (BasicListUI) list.getUI();
830: if (!ui.extendedSupportEnabled) {
831: return candidateIndex;
832: }
833: for (int i = candidateIndex; i < list.getModel().getSize(); i++) {
834: if (ui.isChoosable(i)) {
835: return i;
836: }
837: }
838: for (int i = candidateIndex; i > currentSelection; i--) {
839: if (ui.isChoosable(i)) {
840: return i;
841: }
842: }
843:
844: return -1;
845: }
846:
847: private static int getScrollUpIndex(final JList list) {
848: if (list.getModel().getSize() == 0) {
849: return -1;
850: }
851:
852: int candidateIndex;
853: int currentSelection = list.getLeadSelectionIndex();
854:
855: int firstVisible = list.getFirstVisibleIndex();
856: if (firstVisible != currentSelection || firstVisible == 0) {
857: candidateIndex = firstVisible;
858: } else {
859: Rectangle visibleRect = list.getVisibleRect();
860: int i = firstVisible - 1;
861: while (i > 0
862: && list.getCellBounds(firstVisible - 1, i).height < visibleRect.height) {
863: i--;
864: }
865:
866: candidateIndex = i;
867: }
868:
869: BasicListUI ui = (BasicListUI) list.getUI();
870: if (!ui.extendedSupportEnabled) {
871: return candidateIndex;
872: }
873: for (int i = candidateIndex; i >= 0; i--) {
874: if (ui.isChoosable(i)) {
875: return i;
876: }
877: }
878: for (int i = candidateIndex; i < currentSelection; i++) {
879: if (ui.isChoosable(i)) {
880: return i;
881: }
882: }
883:
884: return -1;
885: }
886: }
|