1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82:
83:
87: public class BasicListUI extends ListUI
88: {
89:
90:
94: private class ComponentHandler extends ComponentAdapter {
95:
96:
100: public void componentResized(ComponentEvent ev) {
101: BasicListUI.this.damageLayout();
102: }
103: }
104:
105:
109: public class FocusHandler implements FocusListener
110: {
111:
116: public void focusGained(FocusEvent e)
117: {
118: repaintCellFocus();
119: }
120:
121:
126: public void focusLost(FocusEvent e)
127: {
128: repaintCellFocus();
129: }
130:
131:
135: protected void repaintCellFocus()
136: {
137:
138: }
139: }
140:
141:
147: public class ListDataHandler implements ListDataListener
148: {
149:
155: public void contentsChanged(ListDataEvent e)
156: {
157: BasicListUI.this.damageLayout();
158: }
159:
160:
165: public void intervalAdded(ListDataEvent e)
166: {
167: BasicListUI.this.damageLayout();
168: }
169:
170:
175: public void intervalRemoved(ListDataEvent e)
176: {
177: BasicListUI.this.damageLayout();
178: }
179: }
180:
181:
185: public class ListSelectionHandler implements ListSelectionListener
186: {
187:
192: public void valueChanged(ListSelectionEvent e)
193: {
194:
195: }
196: }
197:
198:
205: private static class ActionListenerProxy
206: extends AbstractAction
207: {
208: ActionListener target;
209: String bindingCommandName;
210:
211: public ActionListenerProxy(ActionListener li,
212: String cmd)
213: {
214: target = li;
215: bindingCommandName = cmd;
216: }
217:
218: public void actionPerformed(ActionEvent e)
219: {
220: ActionEvent derivedEvent = new ActionEvent(e.getSource(),
221: e.getID(),
222: bindingCommandName,
223: e.getModifiers());
224: target.actionPerformed(derivedEvent);
225: }
226: }
227:
228: class ListAction extends AbstractAction
229: {
230: public void actionPerformed (ActionEvent e)
231: {
232: int lead = list.getLeadSelectionIndex();
233: int max = list.getModel().getSize() - 1;
234: DefaultListSelectionModel selModel = (DefaultListSelectionModel)list.getSelectionModel();
235: String command = e.getActionCommand();
236:
237: if (max == -1)
238: return;
239:
240: if (command.equals("selectNextRow"))
241: {
242: selectNextIndex();
243: }
244: else if (command.equals("selectPreviousRow"))
245: {
246: selectPreviousIndex();
247: }
248: else if (command.equals("clearSelection"))
249: {
250: list.clearSelection();
251: }
252: else if (command.equals("selectAll"))
253: {
254: list.setSelectionInterval(0, max);
255:
256:
257: list.addSelectionInterval(lead, lead);
258: }
259: else if (command.equals("selectLastRow"))
260: {
261: list.setSelectedIndex(list.getModel().getSize() - 1);
262: }
263: else if (command.equals("selectLastRowChangeLead"))
264: {
265: selModel.moveLeadSelectionIndex(list.getModel().getSize() - 1);
266: }
267: else if (command.equals("scrollDownExtendSelection"))
268: {
269: int target;
270: if (lead == list.getLastVisibleIndex())
271: {
272: target = Math.min
273: (max, lead + (list.getLastVisibleIndex() -
274: list.getFirstVisibleIndex() + 1));
275: }
276: else
277: target = list.getLastVisibleIndex();
278: selModel.setLeadSelectionIndex(target);
279: }
280: else if (command.equals("scrollDownChangeLead"))
281: {
282: int target;
283: if (lead == list.getLastVisibleIndex())
284: {
285: target = Math.min
286: (max, lead + (list.getLastVisibleIndex() -
287: list.getFirstVisibleIndex() + 1));
288: }
289: else
290: target = list.getLastVisibleIndex();
291: selModel.moveLeadSelectionIndex(target);
292: }
293: else if (command.equals("scrollUpExtendSelection"))
294: {
295: int target;
296: if (lead == list.getFirstVisibleIndex())
297: {
298: target = Math.max
299: (0, lead - (list.getLastVisibleIndex() -
300: list.getFirstVisibleIndex() + 1));
301: }
302: else
303: target = list.getFirstVisibleIndex();
304: selModel.setLeadSelectionIndex(target);
305: }
306: else if (command.equals("scrollUpChangeLead"))
307: {
308: int target;
309: if (lead == list.getFirstVisibleIndex())
310: {
311: target = Math.max
312: (0, lead - (list.getLastVisibleIndex() -
313: list.getFirstVisibleIndex() + 1));
314: }
315: else
316: target = list.getFirstVisibleIndex();
317: selModel.moveLeadSelectionIndex(target);
318: }
319: else if (command.equals("selectNextRowExtendSelection"))
320: {
321: selModel.setLeadSelectionIndex(Math.min(lead + 1,max));
322: }
323: else if (command.equals("selectFirstRow"))
324: {
325: list.setSelectedIndex(0);
326: }
327: else if (command.equals("selectFirstRowChangeLead"))
328: {
329: selModel.moveLeadSelectionIndex(0);
330: }
331: else if (command.equals("selectFirstRowExtendSelection"))
332: {
333: selModel.setLeadSelectionIndex(0);
334: }
335: else if (command.equals("selectPreviousRowExtendSelection"))
336: {
337: selModel.setLeadSelectionIndex(Math.max(0,lead - 1));
338: }
339: else if (command.equals("scrollUp"))
340: {
341: int target;
342: if (lead == list.getFirstVisibleIndex())
343: {
344: target = Math.max
345: (0, lead - (list.getLastVisibleIndex() -
346: list.getFirstVisibleIndex() + 1));
347: }
348: else
349: target = list.getFirstVisibleIndex();
350: list.setSelectedIndex(target);
351: }
352: else if (command.equals("selectLastRowExtendSelection"))
353: {
354: selModel.setLeadSelectionIndex(list.getModel().getSize() - 1);
355: }
356: else if (command.equals("scrollDown"))
357: {
358: int target;
359: if (lead == list.getLastVisibleIndex())
360: {
361: target = Math.min
362: (max, lead + (list.getLastVisibleIndex() -
363: list.getFirstVisibleIndex() + 1));
364: }
365: else
366: target = list.getLastVisibleIndex();
367: list.setSelectedIndex(target);
368: }
369: else if (command.equals("selectNextRowChangeLead"))
370: {
371: if (selModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
372: selectNextIndex();
373: else
374: {
375: selModel.moveLeadSelectionIndex(Math.min(max, lead + 1));
376: }
377: }
378: else if (command.equals("selectPreviousRowChangeLead"))
379: {
380: if (selModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
381: selectPreviousIndex();
382: else
383: {
384: selModel.moveLeadSelectionIndex(Math.max(0, lead - 1));
385: }
386: }
387: else if (command.equals("addToSelection"))
388: {
389: list.addSelectionInterval(lead, lead);
390: }
391: else if (command.equals("extendTo"))
392: {
393: selModel.setSelectionInterval(selModel.getAnchorSelectionIndex(),
394: lead);
395: }
396: else if (command.equals("toggleAndAnchor"))
397: {
398: if (!list.isSelectedIndex(lead))
399: list.addSelectionInterval(lead, lead);
400: else
401: list.removeSelectionInterval(lead, lead);
402: selModel.setAnchorSelectionIndex(lead);
403: }
404: else
405: {
406:
407:
408:
409:
410: }
411:
412: list.ensureIndexIsVisible(list.getLeadSelectionIndex());
413: }
414: }
415:
416:
420: public class MouseInputHandler implements MouseInputListener
421: {
422:
428: public void mouseClicked(MouseEvent event)
429: {
430: Point click = event.getPoint();
431: int index = locationToIndex(list, click);
432: if (index == -1)
433: return;
434: if (event.isShiftDown())
435: {
436: if (list.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION)
437: list.setSelectedIndex(index);
438: else if (list.getSelectionMode() ==
439: ListSelectionModel.SINGLE_INTERVAL_SELECTION)
440:
441:
442:
443:
444:
445: list.setSelectionInterval(list.getAnchorSelectionIndex(), index);
446: else
447:
448:
449:
450:
451:
452:
453: if (list.isSelectedIndex(list.getAnchorSelectionIndex()))
454: list.getSelectionModel().setLeadSelectionIndex(index);
455: else
456: list.addSelectionInterval(list.getAnchorSelectionIndex(), index);
457: }
458: else if (event.isControlDown())
459: {
460: if (list.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION)
461: list.setSelectedIndex(index);
462: else if (list.isSelectedIndex(index))
463: list.removeSelectionInterval(index,index);
464: else
465: list.addSelectionInterval(index,index);
466: }
467: else
468: list.setSelectedIndex(index);
469:
470: list.ensureIndexIsVisible(list.getLeadSelectionIndex());
471: }
472:
473:
479: public void mousePressed(MouseEvent event)
480: {
481:
482: }
483:
484:
490: public void mouseReleased(MouseEvent event)
491: {
492:
493: }
494:
495:
501: public void mouseEntered(MouseEvent event)
502: {
503:
504: }
505:
506:
512: public void mouseExited(MouseEvent event)
513: {
514:
515: }
516:
517:
523: public void mouseDragged(MouseEvent event)
524: {
525:
526: }
527:
528:
534: public void mouseMoved(MouseEvent event)
535: {
536:
537: }
538: }
539:
540:
544: public class PropertyChangeHandler implements PropertyChangeListener
545: {
546:
551: public void propertyChange(PropertyChangeEvent e)
552: {
553: if (e.getSource() == BasicListUI.this.list)
554: {
555: if (e.getOldValue() != null && e.getOldValue() instanceof ListModel)
556: ((ListModel) e.getOldValue()).removeListDataListener(BasicListUI.this.listDataListener);
557:
558: if (e.getNewValue() != null && e.getNewValue() instanceof ListModel)
559: ((ListModel) e.getNewValue()).addListDataListener(BasicListUI.this.listDataListener);
560: }
561:
562: if (e.getPropertyName().equals("model"))
563: updateLayoutStateNeeded += modelChanged;
564: else if (e.getPropertyName().equals("selectionModel"))
565: updateLayoutStateNeeded += selectionModelChanged;
566: else if (e.getPropertyName().equals("font"))
567: updateLayoutStateNeeded += fontChanged;
568: else if (e.getPropertyName().equals("fixedCellWidth"))
569: updateLayoutStateNeeded += fixedCellWidthChanged;
570: else if (e.getPropertyName().equals("fixedCellHeight"))
571: updateLayoutStateNeeded += fixedCellHeightChanged;
572: else if (e.getPropertyName().equals("prototypeCellValue"))
573: updateLayoutStateNeeded += prototypeCellValueChanged;
574: else if (e.getPropertyName().equals("cellRenderer"))
575: updateLayoutStateNeeded += cellRendererChanged;
576:
577: BasicListUI.this.damageLayout();
578: }
579: }
580:
581:
584: protected static final int modelChanged = 1;
585:
586:
589: protected static final int selectionModelChanged = 2;
590:
591:
594: protected static final int fontChanged = 4;
595:
596:
599: protected static final int fixedCellWidthChanged = 8;
600:
601:
604: protected static final int fixedCellHeightChanged = 16;
605:
606:
609: protected static final int prototypeCellValueChanged = 32;
610:
611:
614: protected static final int cellRendererChanged = 64;
615:
616:
623: public static ComponentUI createUI(final JComponent c)
624: {
625: return new BasicListUI();
626: }
627:
628:
629: protected FocusListener focusListener;
630:
631:
632: protected ListDataListener listDataListener;
633:
634:
635: protected ListSelectionListener listSelectionListener;
636:
637:
638: protected MouseInputListener mouseInputListener;
639:
640:
641: protected PropertyChangeListener propertyChangeListener;
642:
643:
644:
646: private ComponentListener componentListener;
647:
648:
649: protected JList list;
650:
651:
652: protected int cellHeight;
653:
654:
655: protected int cellWidth;
656:
657:
661: protected int[] cellHeights;
662:
663:
677: protected int updateLayoutStateNeeded;
678:
679:
682: protected CellRendererPane rendererPane;
683:
684:
685: ListAction action;
686:
687:
697: protected int getRowHeight(int row)
698: {
699: if (row < 0 || row >= cellHeights.length)
700: return -1;
701: else if (cellHeight != -1)
702: return cellHeight;
703: else
704: return cellHeights[row];
705: }
706:
707:
718: public Rectangle getCellBounds(JList l, int index1, int index2)
719: {
720: maybeUpdateLayoutState();
721:
722: if (l != list || cellWidth == -1)
723: return null;
724:
725: int minIndex = Math.min(index1, index2);
726: int maxIndex = Math.max(index1, index2);
727: Point loc = indexToLocation(list, minIndex);
728: Rectangle bounds = new Rectangle(loc.x, loc.y, cellWidth,
729: getRowHeight(minIndex));
730:
731: for (int i = minIndex + 1; i <= maxIndex; i++)
732: {
733: Point hiLoc = indexToLocation(list, i);
734: Rectangle hibounds = new Rectangle(hiLoc.x, hiLoc.y, cellWidth,
735: getRowHeight(i));
736: bounds = bounds.union(hibounds);
737: }
738:
739: return bounds;
740: }
741:
742:
752: protected int convertRowToY(int row)
753: {
754: int y = 0;
755: for (int i = 0; i < row; ++i)
756: {
757: int h = getRowHeight(i);
758: if (h == -1)
759: return -1;
760: y += h;
761: }
762: return y;
763: }
764:
765:
775: protected int convertYToRow(int y0)
776: {
777: for (int row = 0; row < cellHeights.length; ++row)
778: {
779: int h = getRowHeight(row);
780:
781: if (y0 < h)
782: return row;
783: y0 -= h;
784: }
785: return -1;
786: }
787:
788:
793: protected void updateLayoutState()
794: {
795: int nrows = list.getModel().getSize();
796: cellHeight = -1;
797: cellWidth = -1;
798: if (cellHeights == null || cellHeights.length != nrows)
799: cellHeights = new int[nrows];
800: if (list.getFixedCellHeight() == -1 || list.getFixedCellWidth() == -1)
801: {
802: ListCellRenderer rend = list.getCellRenderer();
803: for (int i = 0; i < nrows; ++i)
804: {
805: Component flyweight = rend.getListCellRendererComponent(list,
806: list.getModel()
807: .getElementAt(i),
808: 0, false,
809: false);
810: Dimension dim = flyweight.getPreferredSize();
811: cellHeights[i] = dim.height;
812:
813: cellHeight = (cellHeight * i + cellHeights[i]) / (i + 1);
814: cellWidth = Math.max(cellWidth, dim.width);
815: if (list.getLayoutOrientation() == JList.VERTICAL)
816: cellWidth = Math.max(cellWidth, list.getSize().width);
817: }
818: }
819: else
820: {
821: cellHeight = list.getFixedCellHeight();
822: cellWidth = list.getFixedCellWidth();
823: }
824: }
825:
826:
833: void damageLayout()
834: {
835: updateLayoutStateNeeded = 1;
836: }
837:
838:
842: protected void maybeUpdateLayoutState()
843: {
844: if (updateLayoutStateNeeded != 0)
845: {
846: updateLayoutState();
847: updateLayoutStateNeeded = 0;
848: }
849: }
850:
851:
854: public BasicListUI()
855: {
856: updateLayoutStateNeeded = 1;
857: rendererPane = new CellRendererPane();
858: }
859:
860:
866: protected void installDefaults()
867: {
868: LookAndFeel.installColorsAndFont(list, "List.background",
869: "List.foreground", "List.font");
870: list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));
871: list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));
872: list.setOpaque(true);
873: }
874:
875:
879: protected void uninstallDefaults()
880: {
881: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
882: list.setForeground(null);
883: list.setBackground(null);
884: list.setSelectionForeground(null);
885: list.setSelectionBackground(null);
886: }
887:
888:
894: protected void installListeners()
895: {
896: if (focusListener == null)
897: focusListener = createFocusListener();
898: list.addFocusListener(focusListener);
899: if (listDataListener == null)
900: listDataListener = createListDataListener();
901: list.getModel().addListDataListener(listDataListener);
902: if (listSelectionListener == null)
903: listSelectionListener = createListSelectionListener();
904: list.addListSelectionListener(listSelectionListener);
905: if (mouseInputListener == null)
906: mouseInputListener = createMouseInputListener();
907: list.addMouseListener(mouseInputListener);
908: list.addMouseMotionListener(mouseInputListener);
909: if (propertyChangeListener == null)
910: propertyChangeListener = createPropertyChangeListener();
911: list.addPropertyChangeListener(propertyChangeListener);
912:
913:
914:
915: list.addComponentListener(componentListener);
916: componentListener = new ComponentHandler();
917:
918: }
919:
920:
923: protected void uninstallListeners()
924: {
925: list.removeFocusListener(focusListener);
926: list.getModel().removeListDataListener(listDataListener);
927: list.removeListSelectionListener(listSelectionListener);
928: list.removeMouseListener(mouseInputListener);
929:
930: list.removeMouseMotionListener(mouseInputListener);
931: list.removePropertyChangeListener(propertyChangeListener);
932: }
933:
934:
937: protected void installKeyboardActions()
938: {
939: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
940: InputMap focusInputMap = (InputMap)defaults.get("List.focusInputMap");
941: InputMapUIResource parentInputMap = new InputMapUIResource();
942:
943: ActionMap parentActionMap = new ActionMap();
944: action = new ListAction();
945: Object keys[] = focusInputMap.allKeys();
946:
947: for (int i = 0; i < keys.length; i++)
948: {
949: KeyStroke stroke = (KeyStroke)keys[i];
950: String actionString = (String) focusInputMap.get(stroke);
951: parentInputMap.put(KeyStroke.getKeyStroke(stroke.getKeyCode(),
952: stroke.getModifiers()),
953: actionString);
954:
955: parentActionMap.put (actionString,
956: new ActionListenerProxy(action, actionString));
957: }
958:
959:
960: parentInputMap.setParent(list.getInputMap().getParent());
961: parentActionMap.setParent(list.getActionMap().getParent());
962: list.getInputMap().setParent(parentInputMap);
963: list.getActionMap().setParent(parentActionMap);
964: }
965:
966:
969: protected void uninstallKeyboardActions()
970: {
971:
972: }
973:
974:
982: public void installUI(final JComponent c)
983: {
984: super.installUI(c);
985: list = (JList) c;
986: installDefaults();
987: installListeners();
988: installKeyboardActions();
989: maybeUpdateLayoutState();
990: }
991:
992:
1000: public void uninstallUI(final JComponent c)
1001: {
1002: uninstallKeyboardActions();
1003: uninstallListeners();
1004: uninstallDefaults();
1005: list = null;
1006: }
1007:
1008:
1016: public Dimension getPreferredSize(JComponent c)
1017: {
1018: int size = list.getModel().getSize();
1019: if (size == 0)
1020: return new Dimension(0, 0);
1021: int visibleRows = list.getVisibleRowCount();
1022: int layoutOrientation = list.getLayoutOrientation();
1023: Rectangle bounds = getCellBounds(list, 0, list.getModel().getSize() - 1);
1024: Dimension retVal = bounds.getSize();
1025: Component parent = list.getParent();
1026: if ((visibleRows == -1) && (parent instanceof JViewport))
1027: {
1028: JViewport viewport = (JViewport) parent;
1029:
1030: if (layoutOrientation == JList.HORIZONTAL_WRAP)
1031: {
1032: int h = viewport.getSize().height;
1033: int cellsPerCol = h / cellHeight;
1034: int w = size / cellsPerCol * cellWidth;
1035: retVal = new Dimension(w, h);
1036: }
1037: else if (layoutOrientation == JList.VERTICAL_WRAP)
1038: {
1039: int w = viewport.getSize().width;
1040: int cellsPerRow = Math.max(w / cellWidth, 1);
1041: int h = size / cellsPerRow * cellHeight;
1042: retVal = new Dimension(w, h);
1043: }
1044: }
1045: return retVal;
1046: }
1047:
1048:
1055: private void paintBackground(Graphics g, JComponent c)
1056: {
1057: Dimension size = getPreferredSize(c);
1058: Color save = g.getColor();
1059: g.setColor(c.getBackground());
1060: g.fillRect(0, 0, size.width, size.height);
1061: g.setColor(save);
1062: }
1063:
1064:
1077: protected void paintCell(Graphics g, int row, Rectangle bounds,
1078: ListCellRenderer rend, ListModel data,
1079: ListSelectionModel sel, int lead)
1080: {
1081: boolean isSel = list.isSelectedIndex(row);
1082: boolean hasFocus = (list.getLeadSelectionIndex() == row) && BasicListUI.this.list.hasFocus();
1083: Component comp = rend.getListCellRendererComponent(list,
1084: data.getElementAt(row),
1085: 0, isSel, hasFocus);
1086:
1087:
1088: rendererPane.paintComponent(g, comp, list, bounds);
1089: }
1090:
1091:
1098: public void paint(Graphics g, JComponent c)
1099: {
1100: int nrows = list.getModel().getSize();
1101: if (nrows == 0)
1102: return;
1103:
1104: maybeUpdateLayoutState();
1105: ListCellRenderer render = list.getCellRenderer();
1106: ListModel model = list.getModel();
1107: ListSelectionModel sel = list.getSelectionModel();
1108: int lead = sel.getLeadSelectionIndex();
1109: Rectangle clip = g.getClipBounds();
1110: paintBackground(g, list);
1111:
1112: for (int row = 0; row < nrows; ++row)
1113: {
1114: Rectangle bounds = getCellBounds(list, row, row);
1115: if (bounds.intersects(clip))
1116: paintCell(g, row, bounds, render, model, sel, lead);
1117: }
1118: }
1119:
1120:
1129: public int locationToIndex(JList list, Point location)
1130: {
1131: int layoutOrientation = list.getLayoutOrientation();
1132: int index = -1;
1133: switch (layoutOrientation)
1134: {
1135: case JList.VERTICAL:
1136: index = convertYToRow(location.y);
1137: break;
1138: case JList.HORIZONTAL_WRAP:
1139:
1140: int visibleRows = list.getVisibleRowCount();
1141: int cellsPerRow = -1;
1142: int numberOfItems = list.getModel().getSize();
1143: Dimension listDim = list.getSize();
1144: if (visibleRows <= 0)
1145: {
1146: try
1147: {
1148: cellsPerRow = listDim.width / cellWidth;
1149: }
1150: catch (ArithmeticException ex)
1151: {
1152: cellsPerRow = 1;
1153: }
1154: }
1155: else
1156: {
1157: cellsPerRow = numberOfItems / visibleRows + 1;
1158: }
1159:
1160:
1161: int cellsPerColumn = numberOfItems / cellsPerRow + 1;
1162: int gridX = Math.min(location.x / cellWidth, cellsPerRow - 1);
1163: int gridY = Math.min(location.y / cellHeight, cellsPerColumn);
1164: index = gridX + gridY * cellsPerRow;
1165: break;
1166: case JList.VERTICAL_WRAP:
1167:
1168: int visibleRows2 = list.getVisibleRowCount();
1169: if (visibleRows2 <= 0)
1170: {
1171: Dimension listDim2 = list.getSize();
1172: visibleRows2 = listDim2.height / cellHeight;
1173: }
1174: int numberOfItems2 = list.getModel().getSize();
1175: int cellsPerRow2 = numberOfItems2 / visibleRows2 + 1;
1176:
1177: Dimension listDim2 = list.getSize();
1178: int gridX2 = Math.min(location.x / cellWidth, cellsPerRow2 - 1);
1179: int gridY2 = Math.min(location.y / cellHeight, visibleRows2);
1180: index = gridY2 + gridX2 * visibleRows2;
1181: break;
1182: }
1183: return index;
1184: }
1185:
1186: public Point indexToLocation(JList list, int index)
1187: {
1188: int layoutOrientation = list.getLayoutOrientation();
1189: Point loc = null;
1190: switch (layoutOrientation)
1191: {
1192: case JList.VERTICAL:
1193: loc = new Point(0, convertRowToY(index));
1194: break;
1195: case JList.HORIZONTAL_WRAP:
1196:
1197: int visibleRows = list.getVisibleRowCount();
1198: int numberOfCellsPerRow = -1;
1199: if (visibleRows <= 0)
1200: {
1201: Dimension listDim = list.getSize();
1202: numberOfCellsPerRow = Math.max(listDim.width / cellWidth, 1);
1203: }
1204: else
1205: {
1206: int numberOfItems = list.getModel().getSize();
1207: numberOfCellsPerRow = numberOfItems / visibleRows + 1;
1208: }
1209:
1210: int gridX = index % numberOfCellsPerRow;
1211: int gridY = index / numberOfCellsPerRow;
1212: int locX = gridX * cellWidth;
1213: int locY = gridY * cellHeight;
1214: loc = new Point(locX, locY);
1215: break;
1216: case JList.VERTICAL_WRAP:
1217:
1218: int visibleRows2 = list.getVisibleRowCount();
1219: if (visibleRows2 <= 0)
1220: {
1221: Dimension listDim2 = list.getSize();
1222: visibleRows2 = listDim2.height / cellHeight;
1223: }
1224:
1225: if (visibleRows2 > 0)
1226: {
1227: int gridY2 = index % visibleRows2;
1228: int gridX2 = index / visibleRows2;
1229: int locX2 = gridX2 * cellWidth;
1230: int locY2 = gridY2 * cellHeight;
1231: loc = new Point(locX2, locY2);
1232: }
1233: else
1234: loc = new Point(0, convertRowToY(index));
1235: break;
1236: }
1237: return loc;
1238: }
1239:
1240:
1245: protected FocusListener createFocusListener()
1246: {
1247: return new FocusHandler();
1248: }
1249:
1250:
1255: protected ListDataListener createListDataListener()
1256: {
1257: return new ListDataHandler();
1258: }
1259:
1260:
1265: protected ListSelectionListener createListSelectionListener()
1266: {
1267: return new ListSelectionHandler();
1268: }
1269:
1270:
1275: protected MouseInputListener createMouseInputListener()
1276: {
1277: return new MouseInputHandler();
1278: }
1279:
1280:
1285: protected PropertyChangeListener createPropertyChangeListener()
1286: {
1287: return new PropertyChangeHandler();
1288: }
1289:
1290:
1293: protected void selectNextIndex()
1294: {
1295: int index = list.getSelectionModel().getLeadSelectionIndex();
1296: if (index < list.getModel().getSize() - 1)
1297: {
1298: index++;
1299: list.setSelectedIndex(index);
1300: }
1301: list.ensureIndexIsVisible(index);
1302: }
1303:
1304:
1307: protected void selectPreviousIndex()
1308: {
1309: int index = list.getSelectionModel().getLeadSelectionIndex();
1310: if (index > 0)
1311: {
1312: index--;
1313: list.setSelectedIndex(index);
1314: }
1315: list.ensureIndexIsVisible(index);
1316: }
1317: }