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
226: = (DefaultListSelectionModel) list.getSelectionModel();
227: String command = e.getActionCommand();
228:
229: if (max == -1)
230: return;
231:
232: if (command.equals("selectNextRow"))
233: {
234: selectNextIndex();
235: }
236: else if (command.equals("selectPreviousRow"))
237: {
238: selectPreviousIndex();
239: }
240: else if (command.equals("clearSelection"))
241: {
242: list.clearSelection();
243: }
244: else if (command.equals("selectAll"))
245: {
246: list.setSelectionInterval(0, max);
247:
248:
249: list.addSelectionInterval(lead, lead);
250: }
251: else if (command.equals("selectLastRow"))
252: {
253: list.setSelectedIndex(list.getModel().getSize() - 1);
254: }
255: else if (command.equals("selectLastRowChangeLead"))
256: {
257: selModel.moveLeadSelectionIndex(list.getModel().getSize() - 1);
258: }
259: else if (command.equals("scrollDownExtendSelection"))
260: {
261: int target;
262: if (lead == list.getLastVisibleIndex())
263: {
264: target = Math.min(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(max, lead + (list.getLastVisibleIndex()
277: - list.getFirstVisibleIndex() + 1));
278: }
279: else
280: target = list.getLastVisibleIndex();
281: selModel.moveLeadSelectionIndex(target);
282: }
283: else if (command.equals("scrollUpExtendSelection"))
284: {
285: int target;
286: if (lead == list.getFirstVisibleIndex())
287: {
288: target = Math.max(0, lead - (list.getLastVisibleIndex()
289: - list.getFirstVisibleIndex() + 1));
290: }
291: else
292: target = list.getFirstVisibleIndex();
293: selModel.setLeadSelectionIndex(target);
294: }
295: else if (command.equals("scrollUpChangeLead"))
296: {
297: int target;
298: if (lead == list.getFirstVisibleIndex())
299: {
300: target = Math.max(0, lead - (list.getLastVisibleIndex()
301: - list.getFirstVisibleIndex() + 1));
302: }
303: else
304: target = list.getFirstVisibleIndex();
305: selModel.moveLeadSelectionIndex(target);
306: }
307: else if (command.equals("selectNextRowExtendSelection"))
308: {
309: selModel.setLeadSelectionIndex(Math.min(lead + 1, max));
310: }
311: else if (command.equals("selectFirstRow"))
312: {
313: list.setSelectedIndex(0);
314: }
315: else if (command.equals("selectFirstRowChangeLead"))
316: {
317: selModel.moveLeadSelectionIndex(0);
318: }
319: else if (command.equals("selectFirstRowExtendSelection"))
320: {
321: selModel.setLeadSelectionIndex(0);
322: }
323: else if (command.equals("selectPreviousRowExtendSelection"))
324: {
325: selModel.setLeadSelectionIndex(Math.max(0, lead - 1));
326: }
327: else if (command.equals("scrollUp"))
328: {
329: int target;
330: if (lead == list.getFirstVisibleIndex())
331: {
332: target = Math.max(0, lead - (list.getLastVisibleIndex()
333: - list.getFirstVisibleIndex() + 1));
334: }
335: else
336: target = list.getFirstVisibleIndex();
337: list.setSelectedIndex(target);
338: }
339: else if (command.equals("selectLastRowExtendSelection"))
340: {
341: selModel.setLeadSelectionIndex(list.getModel().getSize() - 1);
342: }
343: else if (command.equals("scrollDown"))
344: {
345: int target;
346: if (lead == list.getLastVisibleIndex())
347: {
348: target = Math.min(max, lead + (list.getLastVisibleIndex()
349: - list.getFirstVisibleIndex() + 1));
350: }
351: else
352: target = list.getLastVisibleIndex();
353: list.setSelectedIndex(target);
354: }
355: else if (command.equals("selectNextRowChangeLead"))
356: {
357: if (selModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
358: selectNextIndex();
359: else
360: {
361: selModel.moveLeadSelectionIndex(Math.min(max, lead + 1));
362: }
363: }
364: else if (command.equals("selectPreviousRowChangeLead"))
365: {
366: if (selModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
367: selectPreviousIndex();
368: else
369: {
370: selModel.moveLeadSelectionIndex(Math.max(0, lead - 1));
371: }
372: }
373: else if (command.equals("addToSelection"))
374: {
375: list.addSelectionInterval(lead, lead);
376: }
377: else if (command.equals("extendTo"))
378: {
379: selModel.setSelectionInterval(selModel.getAnchorSelectionIndex(),
380: lead);
381: }
382: else if (command.equals("toggleAndAnchor"))
383: {
384: if (!list.isSelectedIndex(lead))
385: list.addSelectionInterval(lead, lead);
386: else
387: list.removeSelectionInterval(lead, lead);
388: selModel.setAnchorSelectionIndex(lead);
389: }
390: else
391: {
392:
393:
394:
395:
396: }
397:
398: list.ensureIndexIsVisible(list.getLeadSelectionIndex());
399: }
400: }
401:
402:
406: public class MouseInputHandler implements MouseInputListener
407: {
408:
414: public void mouseClicked(MouseEvent event)
415: {
416: Point click = event.getPoint();
417: int index = locationToIndex(list, click);
418: if (index == -1)
419: return;
420: if (event.isShiftDown())
421: {
422: if (list.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION)
423: list.setSelectedIndex(index);
424: else if (list.getSelectionMode() ==
425: ListSelectionModel.SINGLE_INTERVAL_SELECTION)
426:
427:
428:
429:
430:
431: list.setSelectionInterval(list.getAnchorSelectionIndex(), index);
432: else
433:
434:
435:
436:
437:
438:
439: if (list.isSelectedIndex(list.getAnchorSelectionIndex()))
440: list.getSelectionModel().setLeadSelectionIndex(index);
441: else
442: list.addSelectionInterval(list.getAnchorSelectionIndex(), index);
443: }
444: else if (event.isControlDown())
445: {
446: if (list.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION)
447: list.setSelectedIndex(index);
448: else if (list.isSelectedIndex(index))
449: list.removeSelectionInterval(index, index);
450: else
451: list.addSelectionInterval(index, index);
452: }
453: else
454: list.setSelectedIndex(index);
455:
456: list.ensureIndexIsVisible(list.getLeadSelectionIndex());
457: }
458:
459:
465: public void mousePressed(MouseEvent event)
466: {
467:
468: }
469:
470:
476: public void mouseReleased(MouseEvent event)
477: {
478:
479: }
480:
481:
487: public void mouseEntered(MouseEvent event)
488: {
489:
490: }
491:
492:
498: public void mouseExited(MouseEvent event)
499: {
500:
501: }
502:
503:
509: public void mouseDragged(MouseEvent event)
510: {
511: Point click = event.getPoint();
512: int index = locationToIndex(list, click);
513: if (index == -1)
514: return;
515: if (!event.isShiftDown() && !event.isControlDown())
516: list.setSelectedIndex(index);
517:
518: list.ensureIndexIsVisible(list.getLeadSelectionIndex());
519: }
520:
521:
527: public void mouseMoved(MouseEvent event)
528: {
529:
530: }
531: }
532:
533:
537: public class PropertyChangeHandler implements PropertyChangeListener
538: {
539:
544: public void propertyChange(PropertyChangeEvent e)
545: {
546: if (e.getPropertyName().equals("model"))
547: {
548: if (e.getOldValue() != null && e.getOldValue() instanceof ListModel)
549: {
550: ListModel oldModel = (ListModel) e.getOldValue();
551: oldModel.removeListDataListener(listDataListener);
552: }
553: if (e.getNewValue() != null && e.getNewValue() instanceof ListModel)
554: {
555: ListModel newModel = (ListModel) e.getNewValue();
556: newModel.addListDataListener(BasicListUI.this.listDataListener);
557: }
558:
559: updateLayoutStateNeeded |= modelChanged;
560: }
561: else if (e.getPropertyName().equals("selectionModel"))
562: updateLayoutStateNeeded |= selectionModelChanged;
563: else if (e.getPropertyName().equals("font"))
564: updateLayoutStateNeeded |= fontChanged;
565: else if (e.getPropertyName().equals("fixedCellWidth"))
566: updateLayoutStateNeeded |= fixedCellWidthChanged;
567: else if (e.getPropertyName().equals("fixedCellHeight"))
568: updateLayoutStateNeeded |= fixedCellHeightChanged;
569: else if (e.getPropertyName().equals("prototypeCellValue"))
570: updateLayoutStateNeeded |= prototypeCellValueChanged;
571: else if (e.getPropertyName().equals("cellRenderer"))
572: updateLayoutStateNeeded |= cellRendererChanged;
573: }
574: }
575:
576:
579: protected static final int modelChanged = 1;
580:
581:
584: protected static final int selectionModelChanged = 2;
585:
586:
589: protected static final int fontChanged = 4;
590:
591:
594: protected static final int fixedCellWidthChanged = 8;
595:
596:
599: protected static final int fixedCellHeightChanged = 16;
600:
601:
604: protected static final int prototypeCellValueChanged = 32;
605:
606:
609: protected static final int cellRendererChanged = 64;
610:
611:
618: public static ComponentUI createUI(final JComponent c)
619: {
620: return new BasicListUI();
621: }
622:
623:
624: protected FocusListener focusListener;
625:
626:
627: protected ListDataListener listDataListener;
628:
629:
630: protected ListSelectionListener listSelectionListener;
631:
632:
633: protected MouseInputListener mouseInputListener;
634:
635:
636: protected PropertyChangeListener propertyChangeListener;
637:
638:
639: protected JList list;
640:
641:
646: protected int cellHeight;
647:
648:
649: protected int cellWidth;
650:
651:
657: protected int[] cellHeights;
658:
659:
673: protected int updateLayoutStateNeeded;
674:
675:
678: protected CellRendererPane rendererPane;
679:
680:
681: ListAction action;
682:
683:
693: protected int getRowHeight(int row)
694: {
695: int height;
696: if (cellHeights == null)
697: height = cellHeight;
698: else
699: {
700: if (row < 0 || row >= cellHeights.length)
701: height = -1;
702: else
703: height = cellHeights[row];
704: }
705: return height;
706: }
707:
708:
720: public Rectangle getCellBounds(JList l, int index1, int index2)
721: {
722: maybeUpdateLayoutState();
723:
724: if (l != list || cellWidth == -1)
725: return null;
726:
727: int minIndex = Math.min(index1, index2);
728: int maxIndex = Math.max(index1, index2);
729: Point loc = indexToLocation(list, minIndex);
730:
731:
732:
733: int width = cellWidth;
734: if (l.getLayoutOrientation() == JList.VERTICAL)
735: width = l.getWidth();
736:
737: Rectangle bounds = new Rectangle(loc.x, loc.y, width,
738: getCellHeight(minIndex));
739: for (int i = minIndex + 1; i <= maxIndex; i++)
740: {
741: Point hiLoc = indexToLocation(list, i);
742: bounds = SwingUtilities.computeUnion(hiLoc.x, hiLoc.y, width,
743: getCellHeight(i), bounds);
744: }
745:
746: return bounds;
747: }
748:
749:
756: private int getCellHeight(int index)
757: {
758: int height = cellHeight;
759: if (height <= 0)
760: {
761: if (list.getLayoutOrientation() == JList.VERTICAL)
762: height = getRowHeight(index);
763: else
764: {
765: for (int j = 0; j < cellHeights.length; j++)
766: height = Math.max(height, cellHeights[j]);
767: }
768: }
769: return height;
770: }
771:
772:
782: protected int convertRowToY(int row)
783: {
784: int y = 0;
785: for (int i = 0; i < row; ++i)
786: {
787: int h = getRowHeight(i);
788: if (h == -1)
789: return -1;
790: y += h;
791: }
792: return y;
793: }
794:
795:
812: protected int convertYToRow(int y0)
813: {
814: if (list.getModel().getSize() == 0)
815: return -1;
816:
817:
818:
819: if (y0 < 0)
820: return list.getModel().getSize() - 1;
821:
822:
823: maybeUpdateLayoutState();
824:
825: int index = list.getModel().getSize() - 1;
826:
827:
828: if (cellHeight > 0)
829: index = Math.min(y0 / cellHeight, index);
830:
831:
832: else
833: {
834: int h = 0;
835: for (int row = 0; row < cellHeights.length; ++row)
836: {
837: h += cellHeights[row];
838: if (y0 < h)
839: {
840: index = row;
841: break;
842: }
843: }
844: }
845: return index;
846: }
847:
848:
853: protected void updateLayoutState()
854: {
855: int nrows = list.getModel().getSize();
856: cellHeight = -1;
857: cellWidth = -1;
858: if (cellHeights == null || cellHeights.length != nrows)
859: cellHeights = new int[nrows];
860: ListCellRenderer rend = list.getCellRenderer();
861:
862: int fixedCellHeight = list.getFixedCellHeight();
863: if (fixedCellHeight > 0)
864: {
865: cellHeight = fixedCellHeight;
866: cellHeights = null;
867: }
868: else
869: {
870: cellHeight = -1;
871: for (int i = 0; i < nrows; ++i)
872: {
873: Component flyweight =
874: rend.getListCellRendererComponent(list,
875: list.getModel().getElementAt(i),
876: i, list.isSelectedIndex(i),
877: list.getSelectionModel().getAnchorSelectionIndex() == i);
878: Dimension dim = flyweight.getPreferredSize();
879: cellHeights[i] = dim.height;
880: }
881: }
882:
883:
884: int fixedCellWidth = list.getFixedCellWidth();
885: if (fixedCellWidth > 0)
886: cellWidth = fixedCellWidth;
887: else
888: {
889: for (int i = 0; i < nrows; ++i)
890: {
891: Component flyweight =
892: rend.getListCellRendererComponent(list,
893: list.getModel().getElementAt(i),
894: i, list.isSelectedIndex(i),
895: list.getSelectionModel().getAnchorSelectionIndex() == i);
896: Dimension dim = flyweight.getPreferredSize();
897: cellWidth = Math.max(cellWidth, dim.width);
898: }
899: }
900: }
901:
902:
906: protected void maybeUpdateLayoutState()
907: {
908: if (updateLayoutStateNeeded != 0)
909: {
910: updateLayoutState();
911: updateLayoutStateNeeded = 0;
912: }
913: }
914:
915:
918: public BasicListUI()
919: {
920: updateLayoutStateNeeded = 1;
921: rendererPane = new CellRendererPane();
922: }
923:
924:
930: protected void installDefaults()
931: {
932: LookAndFeel.installColorsAndFont(list, "List.background",
933: "List.foreground", "List.font");
934: list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));
935: list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));
936: list.setOpaque(true);
937: }
938:
939:
943: protected void uninstallDefaults()
944: {
945: list.setForeground(null);
946: list.setBackground(null);
947: list.setSelectionForeground(null);
948: list.setSelectionBackground(null);
949: }
950:
951:
957: protected void installListeners()
958: {
959: if (focusListener == null)
960: focusListener = createFocusListener();
961: list.addFocusListener(focusListener);
962: if (listDataListener == null)
963: listDataListener = createListDataListener();
964: list.getModel().addListDataListener(listDataListener);
965: if (listSelectionListener == null)
966: listSelectionListener = createListSelectionListener();
967: list.addListSelectionListener(listSelectionListener);
968: if (mouseInputListener == null)
969: mouseInputListener = createMouseInputListener();
970: list.addMouseListener(mouseInputListener);
971: list.addMouseMotionListener(mouseInputListener);
972: if (propertyChangeListener == null)
973: propertyChangeListener = createPropertyChangeListener();
974: list.addPropertyChangeListener(propertyChangeListener);
975: }
976:
977:
980: protected void uninstallListeners()
981: {
982: list.removeFocusListener(focusListener);
983: list.getModel().removeListDataListener(listDataListener);
984: list.removeListSelectionListener(listSelectionListener);
985: list.removeMouseListener(mouseInputListener);
986: list.removeMouseMotionListener(mouseInputListener);
987: list.removePropertyChangeListener(propertyChangeListener);
988: }
989:
990:
993: protected void installKeyboardActions()
994: {
995: InputMap focusInputMap = (InputMap) UIManager.get("List.focusInputMap");
996: InputMapUIResource parentInputMap = new InputMapUIResource();
997:
998: ActionMap parentActionMap = new ActionMapUIResource();
999: action = new ListAction();
1000: Object keys[] = focusInputMap.allKeys();
1001:
1002: for (int i = 0; i < keys.length; i++)
1003: {
1004: KeyStroke stroke = (KeyStroke) keys[i];
1005: String actionString = (String) focusInputMap.get(stroke);
1006: parentInputMap.put(KeyStroke.getKeyStroke(stroke.getKeyCode(),
1007: stroke.getModifiers()),
1008: actionString);
1009:
1010: parentActionMap.put(actionString,
1011: new ActionListenerProxy(action, actionString));
1012: }
1013:
1014:
1015: parentInputMap.setParent(list.getInputMap().getParent());
1016: parentActionMap.setParent(list.getActionMap().getParent());
1017: list.getInputMap().setParent(parentInputMap);
1018: list.getActionMap().setParent(parentActionMap);
1019: }
1020:
1021:
1024: protected void uninstallKeyboardActions()
1025: throws NotImplementedException
1026: {
1027:
1028: }
1029:
1030:
1038: public void installUI(final JComponent c)
1039: {
1040: super.installUI(c);
1041: list = (JList) c;
1042: installDefaults();
1043: installListeners();
1044: installKeyboardActions();
1045: maybeUpdateLayoutState();
1046: }
1047:
1048:
1056: public void uninstallUI(final JComponent c)
1057: {
1058: uninstallKeyboardActions();
1059: uninstallListeners();
1060: uninstallDefaults();
1061: list = null;
1062: }
1063:
1064:
1072: public Dimension getPreferredSize(JComponent c)
1073: {
1074: maybeUpdateLayoutState();
1075: int size = list.getModel().getSize();
1076: int visibleRows = list.getVisibleRowCount();
1077: int layoutOrientation = list.getLayoutOrientation();
1078:
1079: int h;
1080: int w;
1081: int maxCellHeight = cellHeight;
1082: if (maxCellHeight <= 0)
1083: {
1084: for (int i = 0; i < cellHeights.length; i++)
1085: maxCellHeight = Math.max(maxCellHeight, cellHeights[i]);
1086: }
1087: if (layoutOrientation == JList.HORIZONTAL_WRAP)
1088: {
1089: if (visibleRows > 0)
1090: {
1091:
1092: double modelSize = size;
1093: int neededColumns = (int) Math.ceil(modelSize / visibleRows);
1094: int adjustedRows = (int) Math.ceil(modelSize / neededColumns);
1095: h = maxCellHeight * adjustedRows;
1096: w = cellWidth * neededColumns;
1097: }
1098: else
1099: {
1100: int neededColumns = Math.min(1, list.getWidth() / cellWidth);
1101: h = size / neededColumns * maxCellHeight;
1102: w = neededColumns * cellWidth;
1103: }
1104: }
1105: else if (layoutOrientation == JList.VERTICAL_WRAP)
1106: {
1107: if (visibleRows > 0)
1108: h = visibleRows * maxCellHeight;
1109: else
1110: h = Math.max(list.getHeight(), maxCellHeight);
1111: int neededColumns = h / maxCellHeight;
1112: w = cellWidth * neededColumns;
1113: }
1114: else
1115: {
1116: if (list.getFixedCellWidth() > 0)
1117: w = list.getFixedCellWidth();
1118: else
1119: w = cellWidth;
1120: if (list.getFixedCellHeight() > 0)
1121:
1122:
1123: h = list.getFixedCellHeight() * size;
1124: else
1125: h = maxCellHeight * size;
1126: }
1127: Insets insets = list.getInsets();
1128: Dimension retVal = new Dimension(w + insets.left + insets.right,
1129: h + insets.top + insets.bottom);
1130: return retVal;
1131: }
1132:
1133:
1146: protected void paintCell(Graphics g, int row, Rectangle bounds,
1147: ListCellRenderer rend, ListModel data,
1148: ListSelectionModel sel, int lead)
1149: {
1150: boolean isSel = list.isSelectedIndex(row);
1151: boolean hasFocus = (list.getLeadSelectionIndex() == row) && BasicListUI.this.list.hasFocus();
1152: Component comp = rend.getListCellRendererComponent(list,
1153: data.getElementAt(row),
1154: 0, isSel, hasFocus);
1155: rendererPane.paintComponent(g, comp, list, bounds);
1156: }
1157:
1158:
1165: public void paint(Graphics g, JComponent c)
1166: {
1167: int nrows = list.getModel().getSize();
1168: if (nrows == 0)
1169: return;
1170:
1171: maybeUpdateLayoutState();
1172: ListCellRenderer render = list.getCellRenderer();
1173: ListModel model = list.getModel();
1174: ListSelectionModel sel = list.getSelectionModel();
1175: int lead = sel.getLeadSelectionIndex();
1176: Rectangle clip = g.getClipBounds();
1177:
1178: int startIndex = locationToIndex(list, new Point(clip.x, clip.y));
1179: int endIndex = locationToIndex(list, new Point(clip.x + clip.width,
1180: clip.y + clip.height));
1181:
1182: for (int row = startIndex; row <= endIndex; ++row)
1183: {
1184: Rectangle bounds = getCellBounds(list, row, row);
1185: if (bounds != null && bounds.intersects(clip))
1186: paintCell(g, row, bounds, render, model, sel, lead);
1187: }
1188: }
1189:
1190:
1201: public int locationToIndex(JList l, Point location)
1202: {
1203: int layoutOrientation = list.getLayoutOrientation();
1204: int index = -1;
1205: switch (layoutOrientation)
1206: {
1207: case JList.VERTICAL:
1208: index = convertYToRow(location.y);
1209: break;
1210: case JList.HORIZONTAL_WRAP:
1211:
1212: int maxCellHeight = getCellHeight(0);
1213: int visibleRows = list.getHeight() / maxCellHeight;
1214: int cellsPerRow = -1;
1215: int numberOfItems = list.getModel().getSize();
1216: cellsPerRow = numberOfItems / visibleRows + 1;
1217:
1218:
1219: int cellsPerColumn = numberOfItems / cellsPerRow + 1;
1220: int gridX = Math.min(location.x / cellWidth, cellsPerRow - 1);
1221: int gridY = Math.min(location.y / maxCellHeight, cellsPerColumn);
1222: index = gridX + gridY * cellsPerRow;
1223: break;
1224: case JList.VERTICAL_WRAP:
1225:
1226: int maxCellHeight2 = getCellHeight(0);
1227: int visibleRows2 = list.getHeight() / maxCellHeight2;
1228: int numberOfItems2 = list.getModel().getSize();
1229: int cellsPerRow2 = numberOfItems2 / visibleRows2 + 1;
1230:
1231: int gridX2 = Math.min(location.x / cellWidth, cellsPerRow2 - 1);
1232: int gridY2 = Math.min(location.y / maxCellHeight2, visibleRows2);
1233: index = gridY2 + gridX2 * visibleRows2;
1234: break;
1235: }
1236: return index;
1237: }
1238:
1239: public Point indexToLocation(JList l, int index)
1240: {
1241: int layoutOrientation = list.getLayoutOrientation();
1242: Point loc = null;
1243: switch (layoutOrientation)
1244: {
1245: case JList.VERTICAL:
1246: loc = new Point(0, convertRowToY(index));
1247: break;
1248: case JList.HORIZONTAL_WRAP:
1249:
1250: int maxCellHeight = getCellHeight(0);
1251: int visibleRows = list.getHeight() / maxCellHeight;
1252: int numberOfCellsPerRow = -1;
1253: int numberOfItems = list.getModel().getSize();
1254: numberOfCellsPerRow = numberOfItems / visibleRows + 1;
1255:
1256:
1257: int gridX = index % numberOfCellsPerRow;
1258: int gridY = index / numberOfCellsPerRow;
1259: int locX = gridX * cellWidth;
1260: int locY;
1261: locY = gridY * maxCellHeight;
1262: loc = new Point(locX, locY);
1263: break;
1264: case JList.VERTICAL_WRAP:
1265:
1266: int maxCellHeight2 = getCellHeight(0);
1267: int visibleRows2 = list.getHeight() / maxCellHeight2;
1268:
1269: if (visibleRows2 > 0)
1270: {
1271: int gridY2 = index % visibleRows2;
1272: int gridX2 = index / visibleRows2;
1273: int locX2 = gridX2 * cellWidth;
1274: int locY2 = gridY2 * maxCellHeight2;
1275: loc = new Point(locX2, locY2);
1276: }
1277: else
1278: loc = new Point(0, convertRowToY(index));
1279: break;
1280: }
1281: return loc;
1282: }
1283:
1284:
1289: protected FocusListener createFocusListener()
1290: {
1291: return new FocusHandler();
1292: }
1293:
1294:
1299: protected ListDataListener createListDataListener()
1300: {
1301: return new ListDataHandler();
1302: }
1303:
1304:
1309: protected ListSelectionListener createListSelectionListener()
1310: {
1311: return new ListSelectionHandler();
1312: }
1313:
1314:
1319: protected MouseInputListener createMouseInputListener()
1320: {
1321: return new MouseInputHandler();
1322: }
1323:
1324:
1329: protected PropertyChangeListener createPropertyChangeListener()
1330: {
1331: return new PropertyChangeHandler();
1332: }
1333:
1334:
1337: protected void selectNextIndex()
1338: {
1339: int index = list.getSelectionModel().getLeadSelectionIndex();
1340: if (index < list.getModel().getSize() - 1)
1341: {
1342: index++;
1343: list.setSelectedIndex(index);
1344: }
1345: list.ensureIndexIsVisible(index);
1346: }
1347:
1348:
1351: protected void selectPreviousIndex()
1352: {
1353: int index = list.getSelectionModel().getLeadSelectionIndex();
1354: if (index > 0)
1355: {
1356: index--;
1357: list.setSelectedIndex(index);
1358: }
1359: list.ensureIndexIsVisible(index);
1360: }
1361: }