1:
37:
38:
39: package ;
40:
41: import ;
42:
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:
57: import ;
58: import ;
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:
82:
86: public class BasicListUI extends ListUI
87: {
88:
89:
93: public class FocusHandler implements FocusListener
94: {
95:
100: public void focusGained(FocusEvent e)
101: {
102: repaintCellFocus();
103: }
104:
105:
110: public void focusLost(FocusEvent e)
111: {
112: repaintCellFocus();
113: }
114:
115:
119: protected void repaintCellFocus()
120: {
121:
122: }
123: }
124:
125:
131: public class ListDataHandler implements ListDataListener
132: {
133:
139: public void contentsChanged(ListDataEvent e)
140: {
141: updateLayoutStateNeeded |= modelChanged;
142: list.revalidate();
143: }
144:
145:
150: public void intervalAdded(ListDataEvent e)
151: {
152: updateLayoutStateNeeded |= modelChanged;
153: list.revalidate();
154: }
155:
156:
161: public void intervalRemoved(ListDataEvent e)
162: {
163: updateLayoutStateNeeded |= modelChanged;
164: list.revalidate();
165: }
166: }
167:
168:
172: public class ListSelectionHandler implements ListSelectionListener
173: {
174:
179: public void valueChanged(ListSelectionEvent e)
180: {
181: int index1 = e.getFirstIndex();
182: int index2 = e.getLastIndex();
183: Rectangle damaged = getCellBounds(list, index1, index2);
184: if (damaged != null)
185: list.repaint(damaged);
186: }
187: }
188:
189:
196: private static class ActionListenerProxy
197: extends AbstractAction
198: {
199: ActionListener target;
200: String bindingCommandName;
201:
202: public ActionListenerProxy(ActionListener li,
203: String cmd)
204: {
205: target = li;
206: bindingCommandName = cmd;
207: }
208:
209: public void actionPerformed(ActionEvent e)
210: {
211: ActionEvent derivedEvent = new ActionEvent(e.getSource(),
212: e.getID(),
213: bindingCommandName,
214: e.getModifiers());
215: target.actionPerformed(derivedEvent);
216: }
217: }
218:
219: class ListAction extends AbstractAction
220: {
221: public void actionPerformed (ActionEvent e)
222: {
223: int lead = list.getLeadSelectionIndex();
224: int max = list.getModel().getSize() - 1;
225: DefaultListSelectionModel selModel = (DefaultListSelectionModel)list.getSelectionModel();
226: String command = e.getActionCommand();
227:
228: if (max == -1)
229: return;
230:
231: if (command.equals("selectNextRow"))
232: {
233: selectNextIndex();
234: }
235: else if (command.equals("selectPreviousRow"))
236: {
237: selectPreviousIndex();
238: }
239: else if (command.equals("clearSelection"))
240: {
241: list.clearSelection();
242: }
243: else if (command.equals("selectAll"))
244: {
245: list.setSelectionInterval(0, max);
246:
247:
248: list.addSelectionInterval(lead, lead);
249: }
250: else if (command.equals("selectLastRow"))
251: {
252: list.setSelectedIndex(list.getModel().getSize() - 1);
253: }
254: else if (command.equals("selectLastRowChangeLead"))
255: {
256: selModel.moveLeadSelectionIndex(list.getModel().getSize() - 1);
257: }
258: else if (command.equals("scrollDownExtendSelection"))
259: {
260: int target;
261: if (lead == list.getLastVisibleIndex())
262: {
263: target = Math.min
264: (max, lead + (list.getLastVisibleIndex() -
265: list.getFirstVisibleIndex() + 1));
266: }
267: else
268: target = list.getLastVisibleIndex();
269: selModel.setLeadSelectionIndex(target);
270: }
271: else if (command.equals("scrollDownChangeLead"))
272: {
273: int target;
274: if (lead == list.getLastVisibleIndex())
275: {
276: target = Math.min
277: (max, lead + (list.getLastVisibleIndex() -
278: list.getFirstVisibleIndex() + 1));
279: }
280: else
281: target = list.getLastVisibleIndex();
282: selModel.moveLeadSelectionIndex(target);
283: }
284: else if (command.equals("scrollUpExtendSelection"))
285: {
286: int target;
287: if (lead == list.getFirstVisibleIndex())
288: {
289: target = Math.max
290: (0, lead - (list.getLastVisibleIndex() -
291: list.getFirstVisibleIndex() + 1));
292: }
293: else
294: target = list.getFirstVisibleIndex();
295: selModel.setLeadSelectionIndex(target);
296: }
297: else if (command.equals("scrollUpChangeLead"))
298: {
299: int target;
300: if (lead == list.getFirstVisibleIndex())
301: {
302: target = Math.max
303: (0, lead - (list.getLastVisibleIndex() -
304: list.getFirstVisibleIndex() + 1));
305: }
306: else
307: target = list.getFirstVisibleIndex();
308: selModel.moveLeadSelectionIndex(target);
309: }
310: else if (command.equals("selectNextRowExtendSelection"))
311: {
312: selModel.setLeadSelectionIndex(Math.min(lead + 1,max));
313: }
314: else if (command.equals("selectFirstRow"))
315: {
316: list.setSelectedIndex(0);
317: }
318: else if (command.equals("selectFirstRowChangeLead"))
319: {
320: selModel.moveLeadSelectionIndex(0);
321: }
322: else if (command.equals("selectFirstRowExtendSelection"))
323: {
324: selModel.setLeadSelectionIndex(0);
325: }
326: else if (command.equals("selectPreviousRowExtendSelection"))
327: {
328: selModel.setLeadSelectionIndex(Math.max(0,lead - 1));
329: }
330: else if (command.equals("scrollUp"))
331: {
332: int target;
333: if (lead == list.getFirstVisibleIndex())
334: {
335: target = Math.max
336: (0, lead - (list.getLastVisibleIndex() -
337: list.getFirstVisibleIndex() + 1));
338: }
339: else
340: target = list.getFirstVisibleIndex();
341: list.setSelectedIndex(target);
342: }
343: else if (command.equals("selectLastRowExtendSelection"))
344: {
345: selModel.setLeadSelectionIndex(list.getModel().getSize() - 1);
346: }
347: else if (command.equals("scrollDown"))
348: {
349: int target;
350: if (lead == list.getLastVisibleIndex())
351: {
352: target = Math.min
353: (max, lead + (list.getLastVisibleIndex() -
354: list.getFirstVisibleIndex() + 1));
355: }
356: else
357: target = list.getLastVisibleIndex();
358: list.setSelectedIndex(target);
359: }
360: else if (command.equals("selectNextRowChangeLead"))
361: {
362: if (selModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
363: selectNextIndex();
364: else
365: {
366: selModel.moveLeadSelectionIndex(Math.min(max, lead + 1));
367: }
368: }
369: else if (command.equals("selectPreviousRowChangeLead"))
370: {
371: if (selModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
372: selectPreviousIndex();
373: else
374: {
375: selModel.moveLeadSelectionIndex(Math.max(0, lead - 1));
376: }
377: }
378: else if (command.equals("addToSelection"))
379: {
380: list.addSelectionInterval(lead, lead);
381: }
382: else if (command.equals("extendTo"))
383: {
384: selModel.setSelectionInterval(selModel.getAnchorSelectionIndex(),
385: lead);
386: }
387: else if (command.equals("toggleAndAnchor"))
388: {
389: if (!list.isSelectedIndex(lead))
390: list.addSelectionInterval(lead, lead);
391: else
392: list.removeSelectionInterval(lead, lead);
393: selModel.setAnchorSelectionIndex(lead);
394: }
395: else
396: {
397:
398:
399:
400:
401: }
402:
403: list.ensureIndexIsVisible(list.getLeadSelectionIndex());
404: }
405: }
406:
407:
411: public class MouseInputHandler implements MouseInputListener
412: {
413:
419: public void mouseClicked(MouseEvent event)
420: {
421: Point click = event.getPoint();
422: int index = locationToIndex(list, click);
423: if (index == -1)
424: return;
425: if (event.isShiftDown())
426: {
427: if (list.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION)
428: list.setSelectedIndex(index);
429: else if (list.getSelectionMode() ==
430: ListSelectionModel.SINGLE_INTERVAL_SELECTION)
431:
432:
433:
434:
435:
436: list.setSelectionInterval(list.getAnchorSelectionIndex(), index);
437: else
438:
439:
440:
441:
442:
443:
444: if (list.isSelectedIndex(list.getAnchorSelectionIndex()))
445: list.getSelectionModel().setLeadSelectionIndex(index);
446: else
447: list.addSelectionInterval(list.getAnchorSelectionIndex(), index);
448: }
449: else if (event.isControlDown())
450: {
451: if (list.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION)
452: list.setSelectedIndex(index);
453: else if (list.isSelectedIndex(index))
454: list.removeSelectionInterval(index,index);
455: else
456: list.addSelectionInterval(index,index);
457: }
458: else
459: list.setSelectedIndex(index);
460:
461: list.ensureIndexIsVisible(list.getLeadSelectionIndex());
462: }
463:
464:
470: public void mousePressed(MouseEvent event)
471: {
472:
473: }
474:
475:
481: public void mouseReleased(MouseEvent event)
482: {
483:
484: }
485:
486:
492: public void mouseEntered(MouseEvent event)
493: {
494:
495: }
496:
497:
503: public void mouseExited(MouseEvent event)
504: {
505:
506: }
507:
508:
514: public void mouseDragged(MouseEvent event)
515: {
516: Point click = event.getPoint();
517: int index = locationToIndex(list, click);
518: if (index == -1)
519: return;
520: if (!event.isShiftDown() && !event.isControlDown())
521: list.setSelectedIndex(index);
522:
523: list.ensureIndexIsVisible(list.getLeadSelectionIndex());
524: }
525:
526:
532: public void mouseMoved(MouseEvent event)
533: {
534:
535: }
536: }
537:
538:
542: public class PropertyChangeHandler implements PropertyChangeListener
543: {
544:
549: public void propertyChange(PropertyChangeEvent e)
550: {
551: if (e.getPropertyName().equals("model"))
552: {
553: if (e.getOldValue() != null && e.getOldValue() instanceof ListModel)
554: {
555: ListModel oldModel = (ListModel) e.getOldValue();
556: oldModel.removeListDataListener(listDataListener);
557: }
558: if (e.getNewValue() != null && e.getNewValue() instanceof ListModel)
559: {
560: ListModel newModel = (ListModel) e.getNewValue();
561: newModel.addListDataListener(BasicListUI.this.listDataListener);
562: }
563:
564: updateLayoutStateNeeded |= modelChanged;
565: }
566: else if (e.getPropertyName().equals("selectionModel"))
567: updateLayoutStateNeeded |= selectionModelChanged;
568: else if (e.getPropertyName().equals("font"))
569: updateLayoutStateNeeded |= fontChanged;
570: else if (e.getPropertyName().equals("fixedCellWidth"))
571: updateLayoutStateNeeded |= fixedCellWidthChanged;
572: else if (e.getPropertyName().equals("fixedCellHeight"))
573: updateLayoutStateNeeded |= fixedCellHeightChanged;
574: else if (e.getPropertyName().equals("prototypeCellValue"))
575: updateLayoutStateNeeded |= prototypeCellValueChanged;
576: else if (e.getPropertyName().equals("cellRenderer"))
577: updateLayoutStateNeeded |= cellRendererChanged;
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: protected JList list;
645:
646:
651: protected int cellHeight;
652:
653:
654: protected int cellWidth;
655:
656:
662: protected int[] cellHeights;
663:
664:
678: protected int updateLayoutStateNeeded;
679:
680:
683: protected CellRendererPane rendererPane;
684:
685:
686: ListAction action;
687:
688:
698: protected int getRowHeight(int row)
699: {
700: int height;
701: if (cellHeights == null)
702: height = cellHeight;
703: else
704: {
705: if (row < 0 || row >= cellHeights.length)
706: height = -1;
707: else
708: height = cellHeights[row];
709: }
710: return height;
711: }
712:
713:
725: public Rectangle getCellBounds(JList l, int index1, int index2)
726: {
727: maybeUpdateLayoutState();
728:
729: if (l != list || cellWidth == -1)
730: return null;
731:
732: int minIndex = Math.min(index1, index2);
733: int maxIndex = Math.max(index1, index2);
734: Point loc = indexToLocation(list, minIndex);
735:
736:
737:
738: int width = cellWidth;
739: if (l.getLayoutOrientation() == JList.VERTICAL)
740: width = l.getWidth();
741:
742: Rectangle bounds = new Rectangle(loc.x, loc.y, width,
743: getCellHeight(minIndex));
744: for (int i = minIndex + 1; i <= maxIndex; i++)
745: {
746: Point hiLoc = indexToLocation(list, i);
747: bounds = SwingUtilities.computeUnion(hiLoc.x, hiLoc.y, width,
748: getCellHeight(i), bounds);
749: }
750:
751: return bounds;
752: }
753:
754:
761: private int getCellHeight(int index)
762: {
763: int height = cellHeight;
764: if (height <= 0)
765: {
766: if (list.getLayoutOrientation() == JList.VERTICAL)
767: height = getRowHeight(index);
768: else
769: {
770: for (int j = 0; j < cellHeights.length; j++)
771: height = Math.max(height, cellHeights[j]);
772: }
773: }
774: return height;
775: }
776:
777:
787: protected int convertRowToY(int row)
788: {
789: int y = 0;
790: for (int i = 0; i < row; ++i)
791: {
792: int h = getRowHeight(i);
793: if (h == -1)
794: return -1;
795: y += h;
796: }
797: return y;
798: }
799:
800:
817: protected int convertYToRow(int y0)
818: {
819: if (list.getModel().getSize() == 0)
820: return -1;
821:
822:
823:
824: if (y0 < 0)
825: return list.getModel().getSize() - 1;
826:
827:
828: maybeUpdateLayoutState();
829:
830: int index = list.getModel().getSize() - 1;
831:
832:
833: if (cellHeight > 0)
834: index = Math.min(y0 / cellHeight, index);
835:
836:
837: else
838: {
839: int h = 0;
840: for (int row = 0; row < cellHeights.length; ++row)
841: {
842: h += cellHeights[row];
843: if (y0 < h)
844: {
845: index = row;
846: break;
847: }
848: }
849: }
850: return index;
851: }
852:
853:
858: protected void updateLayoutState()
859: {
860: int nrows = list.getModel().getSize();
861: cellHeight = -1;
862: cellWidth = -1;
863: if (cellHeights == null || cellHeights.length != nrows)
864: cellHeights = new int[nrows];
865: ListCellRenderer rend = list.getCellRenderer();
866:
867: int fixedCellHeight = list.getFixedCellHeight();
868: if (fixedCellHeight > 0)
869: {
870: cellHeight = fixedCellHeight;
871: cellHeights = null;
872: }
873: else
874: {
875: cellHeight = -1;
876: for (int i = 0; i < nrows; ++i)
877: {
878: Component flyweight =
879: rend.getListCellRendererComponent(list,
880: list.getModel().getElementAt(i),
881: i, list.isSelectedIndex(i),
882: list.getSelectionModel().getAnchorSelectionIndex() == i);
883: Dimension dim = flyweight.getPreferredSize();
884: cellHeights[i] = dim.height;
885: }
886: }
887:
888:
889: int fixedCellWidth = list.getFixedCellWidth();
890: if (fixedCellWidth > 0)
891: cellWidth = fixedCellWidth;
892: else
893: {
894: for (int i = 0; i < nrows; ++i)
895: {
896: Component flyweight =
897: rend.getListCellRendererComponent(list,
898: list.getModel().getElementAt(i),
899: i, list.isSelectedIndex(i),
900: list.getSelectionModel().getAnchorSelectionIndex() == i);
901: Dimension dim = flyweight.getPreferredSize();
902: cellWidth = Math.max(cellWidth, dim.width);
903: }
904: }
905: }
906:
907:
911: protected void maybeUpdateLayoutState()
912: {
913: if (updateLayoutStateNeeded != 0)
914: {
915: updateLayoutState();
916: updateLayoutStateNeeded = 0;
917: }
918: }
919:
920:
923: public BasicListUI()
924: {
925: updateLayoutStateNeeded = 1;
926: rendererPane = new CellRendererPane();
927: }
928:
929:
935: protected void installDefaults()
936: {
937: LookAndFeel.installColorsAndFont(list, "List.background",
938: "List.foreground", "List.font");
939: list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));
940: list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));
941: list.setOpaque(true);
942: }
943:
944:
948: protected void uninstallDefaults()
949: {
950: list.setForeground(null);
951: list.setBackground(null);
952: list.setSelectionForeground(null);
953: list.setSelectionBackground(null);
954: }
955:
956:
962: protected void installListeners()
963: {
964: if (focusListener == null)
965: focusListener = createFocusListener();
966: list.addFocusListener(focusListener);
967: if (listDataListener == null)
968: listDataListener = createListDataListener();
969: list.getModel().addListDataListener(listDataListener);
970: if (listSelectionListener == null)
971: listSelectionListener = createListSelectionListener();
972: list.addListSelectionListener(listSelectionListener);
973: if (mouseInputListener == null)
974: mouseInputListener = createMouseInputListener();
975: list.addMouseListener(mouseInputListener);
976: list.addMouseMotionListener(mouseInputListener);
977: if (propertyChangeListener == null)
978: propertyChangeListener = createPropertyChangeListener();
979: list.addPropertyChangeListener(propertyChangeListener);
980: }
981:
982:
985: protected void uninstallListeners()
986: {
987: list.removeFocusListener(focusListener);
988: list.getModel().removeListDataListener(listDataListener);
989: list.removeListSelectionListener(listSelectionListener);
990: list.removeMouseListener(mouseInputListener);
991: list.removeMouseMotionListener(mouseInputListener);
992: list.removePropertyChangeListener(propertyChangeListener);
993: }
994:
995:
998: protected void installKeyboardActions()
999: {
1000: InputMap focusInputMap = (InputMap) UIManager.get("List.focusInputMap");
1001: InputMapUIResource parentInputMap = new InputMapUIResource();
1002:
1003: ActionMap parentActionMap = new ActionMapUIResource();
1004: action = new ListAction();
1005: Object keys[] = focusInputMap.allKeys();
1006:
1007: for (int i = 0; i < keys.length; i++)
1008: {
1009: KeyStroke stroke = (KeyStroke)keys[i];
1010: String actionString = (String) focusInputMap.get(stroke);
1011: parentInputMap.put(KeyStroke.getKeyStroke(stroke.getKeyCode(),
1012: stroke.getModifiers()),
1013: actionString);
1014:
1015: parentActionMap.put (actionString,
1016: new ActionListenerProxy(action, actionString));
1017: }
1018:
1019:
1020: parentInputMap.setParent(list.getInputMap().getParent());
1021: parentActionMap.setParent(list.getActionMap().getParent());
1022: list.getInputMap().setParent(parentInputMap);
1023: list.getActionMap().setParent(parentActionMap);
1024: }
1025:
1026:
1029: protected void uninstallKeyboardActions()
1030: throws NotImplementedException
1031: {
1032:
1033: }
1034:
1035:
1043: public void installUI(final JComponent c)
1044: {
1045: super.installUI(c);
1046: list = (JList) c;
1047: installDefaults();
1048: installListeners();
1049: installKeyboardActions();
1050: maybeUpdateLayoutState();
1051: }
1052:
1053:
1061: public void uninstallUI(final JComponent c)
1062: {
1063: uninstallKeyboardActions();
1064: uninstallListeners();
1065: uninstallDefaults();
1066: list = null;
1067: }
1068:
1069:
1077: public Dimension getPreferredSize(JComponent c)
1078: {
1079: maybeUpdateLayoutState();
1080: int size = list.getModel().getSize();
1081: int visibleRows = list.getVisibleRowCount();
1082: int layoutOrientation = list.getLayoutOrientation();
1083:
1084: int h;
1085: int w;
1086: int maxCellHeight = cellHeight;
1087: if (maxCellHeight <= 0)
1088: {
1089: for (int i = 0; i < cellHeights.length; i++)
1090: maxCellHeight = Math.max(maxCellHeight, cellHeights[i]);
1091: }
1092: if (layoutOrientation == JList.HORIZONTAL_WRAP)
1093: {
1094: if (visibleRows > 0)
1095: {
1096:
1097: double modelSize = size;
1098: int neededColumns = (int) Math.ceil(modelSize / visibleRows);
1099: int adjustedRows = (int) Math.ceil(modelSize / neededColumns);
1100: h = maxCellHeight * adjustedRows;
1101: w = cellWidth * neededColumns;
1102: }
1103: else
1104: {
1105: int neededColumns = Math.min(1, list.getWidth() / cellWidth);
1106: h = size / neededColumns * maxCellHeight;
1107: w = neededColumns * cellWidth;
1108: }
1109: }
1110: else if (layoutOrientation == JList.VERTICAL_WRAP)
1111: {
1112: if (visibleRows > 0)
1113: h = visibleRows * maxCellHeight;
1114: else
1115: h = Math.max(list.getHeight(), maxCellHeight);
1116: int neededColumns = h / maxCellHeight;
1117: w = cellWidth * neededColumns;
1118: }
1119: else
1120: {
1121: if (list.getFixedCellWidth() > 0)
1122: w = list.getFixedCellWidth();
1123: else
1124: w = cellWidth;
1125: if (list.getFixedCellHeight() > 0)
1126:
1127:
1128: h = list.getFixedCellHeight() * size;
1129: else
1130: h = maxCellHeight * size;
1131: }
1132: Insets insets = list.getInsets();
1133: Dimension retVal = new Dimension(w + insets.left + insets.right,
1134: h + insets.top + insets.bottom);
1135: return retVal;
1136: }
1137:
1138:
1151: protected void paintCell(Graphics g, int row, Rectangle bounds,
1152: ListCellRenderer rend, ListModel data,
1153: ListSelectionModel sel, int lead)
1154: {
1155: boolean isSel = list.isSelectedIndex(row);
1156: boolean hasFocus = (list.getLeadSelectionIndex() == row) && BasicListUI.this.list.hasFocus();
1157: Component comp = rend.getListCellRendererComponent(list,
1158: data.getElementAt(row),
1159: 0, isSel, hasFocus);
1160: rendererPane.paintComponent(g, comp, list, bounds);
1161: }
1162:
1163:
1170: public void paint(Graphics g, JComponent c)
1171: {
1172: int nrows = list.getModel().getSize();
1173: if (nrows == 0)
1174: return;
1175:
1176: maybeUpdateLayoutState();
1177: ListCellRenderer render = list.getCellRenderer();
1178: ListModel model = list.getModel();
1179: ListSelectionModel sel = list.getSelectionModel();
1180: int lead = sel.getLeadSelectionIndex();
1181: Rectangle clip = g.getClipBounds();
1182:
1183: int startIndex = locationToIndex(list, new Point(clip.x, clip.y));
1184: int endIndex = locationToIndex(list, new Point(clip.x + clip.width,
1185: clip.y + clip.height));
1186:
1187: for (int row = startIndex; row <= endIndex; ++row)
1188: {
1189: Rectangle bounds = getCellBounds(list, row, row);
1190: if (bounds != null && bounds.intersects(clip))
1191: paintCell(g, row, bounds, render, model, sel, lead);
1192: }
1193: }
1194:
1195:
1206: public int locationToIndex(JList l, Point location)
1207: {
1208: int layoutOrientation = list.getLayoutOrientation();
1209: int index = -1;
1210: switch (layoutOrientation)
1211: {
1212: case JList.VERTICAL:
1213: index = convertYToRow(location.y);
1214: break;
1215: case JList.HORIZONTAL_WRAP:
1216:
1217: int maxCellHeight = getCellHeight(0);
1218: int visibleRows = list.getHeight() / maxCellHeight;
1219: int cellsPerRow = -1;
1220: int numberOfItems = list.getModel().getSize();
1221: cellsPerRow = numberOfItems / visibleRows + 1;
1222:
1223:
1224: int cellsPerColumn = numberOfItems / cellsPerRow + 1;
1225: int gridX = Math.min(location.x / cellWidth, cellsPerRow - 1);
1226: int gridY = Math.min(location.y / maxCellHeight, cellsPerColumn);
1227: index = gridX + gridY * cellsPerRow;
1228: break;
1229: case JList.VERTICAL_WRAP:
1230:
1231: int maxCellHeight2 = getCellHeight(0);
1232: int visibleRows2 = list.getHeight() / maxCellHeight2;
1233: int numberOfItems2 = list.getModel().getSize();
1234: int cellsPerRow2 = numberOfItems2 / visibleRows2 + 1;
1235:
1236: int gridX2 = Math.min(location.x / cellWidth, cellsPerRow2 - 1);
1237: int gridY2 = Math.min(location.y / maxCellHeight2, visibleRows2);
1238: index = gridY2 + gridX2 * visibleRows2;
1239: break;
1240: }
1241: return index;
1242: }
1243:
1244: public Point indexToLocation(JList l, int index)
1245: {
1246: int layoutOrientation = list.getLayoutOrientation();
1247: Point loc = null;
1248: switch (layoutOrientation)
1249: {
1250: case JList.VERTICAL:
1251: loc = new Point(0, convertRowToY(index));
1252: break;
1253: case JList.HORIZONTAL_WRAP:
1254:
1255: int maxCellHeight = getCellHeight(0);
1256: int visibleRows = list.getHeight() / maxCellHeight;
1257: int numberOfCellsPerRow = -1;
1258: int numberOfItems = list.getModel().getSize();
1259: numberOfCellsPerRow = numberOfItems / visibleRows + 1;
1260:
1261:
1262: int gridX = index % numberOfCellsPerRow;
1263: int gridY = index / numberOfCellsPerRow;
1264: int locX = gridX * cellWidth;
1265: int locY;
1266: locY = gridY * maxCellHeight;
1267: loc = new Point(locX, locY);
1268: break;
1269: case JList.VERTICAL_WRAP:
1270:
1271: int maxCellHeight2 = getCellHeight(0);
1272: int visibleRows2 = list.getHeight() / maxCellHeight2;
1273:
1274: if (visibleRows2 > 0)
1275: {
1276: int gridY2 = index % visibleRows2;
1277: int gridX2 = index / visibleRows2;
1278: int locX2 = gridX2 * cellWidth;
1279: int locY2 = gridY2 * maxCellHeight2;
1280: loc = new Point(locX2, locY2);
1281: }
1282: else
1283: loc = new Point(0, convertRowToY(index));
1284: break;
1285: }
1286: return loc;
1287: }
1288:
1289:
1294: protected FocusListener createFocusListener()
1295: {
1296: return new FocusHandler();
1297: }
1298:
1299:
1304: protected ListDataListener createListDataListener()
1305: {
1306: return new ListDataHandler();
1307: }
1308:
1309:
1314: protected ListSelectionListener createListSelectionListener()
1315: {
1316: return new ListSelectionHandler();
1317: }
1318:
1319:
1324: protected MouseInputListener createMouseInputListener()
1325: {
1326: return new MouseInputHandler();
1327: }
1328:
1329:
1334: protected PropertyChangeListener createPropertyChangeListener()
1335: {
1336: return new PropertyChangeHandler();
1337: }
1338:
1339:
1342: protected void selectNextIndex()
1343: {
1344: int index = list.getSelectionModel().getLeadSelectionIndex();
1345: if (index < list.getModel().getSize() - 1)
1346: {
1347: index++;
1348: list.setSelectedIndex(index);
1349: }
1350: list.ensureIndexIsVisible(index);
1351: }
1352:
1353:
1356: protected void selectPreviousIndex()
1357: {
1358: int index = list.getSelectionModel().getLeadSelectionIndex();
1359: if (index > 0)
1360: {
1361: index--;
1362: list.setSelectedIndex(index);
1363: }
1364: list.ensureIndexIsVisible(index);
1365: }
1366: }