| 1 7 8 package javax.swing; 9 10 import java.awt.event.*; 11 import java.awt.*; 12 13 import java.util.Vector ; 14 import java.util.Locale ; 15 16 import java.beans.*; 17 18 import javax.swing.event.*; 19 import javax.accessibility.*; 20 import javax.swing.plaf.*; 21 import javax.swing.text.Position ; 22 23 import java.io.ObjectOutputStream ; 24 import java.io.ObjectInputStream ; 25 import java.io.IOException ; 26 import java.io.Serializable ; 27 28 29 224 public class JList extends JComponent implements Scrollable , Accessible 225 { 226 230 private static final String uiClassID = "ListUI"; 231 232 237 public static final int VERTICAL = 0; 238 239 245 public static final int VERTICAL_WRAP = 1; 246 247 253 public static final int HORIZONTAL_WRAP = 2; 254 255 private int fixedCellWidth = -1; 256 private int fixedCellHeight = -1; 257 private int horizontalScrollIncrement = -1; 258 private Object prototypeCellValue; 259 private int visibleRowCount = 8; 260 private Color selectionForeground; 261 private Color selectionBackground; 262 private boolean dragEnabled; 263 264 private ListSelectionModel selectionModel; 265 private ListModel dataModel; 266 private ListCellRenderer cellRenderer; 267 private ListSelectionListener selectionListener; 268 269 272 private int layoutOrientation; 273 274 283 public JList(ListModel dataModel) 284 { 285 if (dataModel == null) { 286 throw new IllegalArgumentException ("dataModel must be non null"); 287 } 288 289 ToolTipManager toolTipManager = ToolTipManager.sharedInstance(); 292 toolTipManager.registerComponent(this); 293 294 layoutOrientation = VERTICAL; 295 296 this.dataModel = dataModel; 297 selectionModel = createSelectionModel(); 298 setAutoscrolls(true); 299 setOpaque(true); 300 updateUI(); 301 } 302 303 304 311 public JList(final Object [] listData) 312 { 313 this ( 314 new AbstractListModel () { 315 public int getSize() { return listData.length; } 316 public Object getElementAt(int i) { return listData[i]; } 317 } 318 ); 319 } 320 321 322 330 public JList(final Vector <?> listData) { 331 this ( 332 new AbstractListModel () { 333 public int getSize() { return listData.size(); } 334 public Object getElementAt(int i) { return listData.elementAt(i); } 335 } 336 ); 337 } 338 339 340 343 public JList() { 344 this ( 345 new AbstractListModel () { 346 public int getSize() { return 0; } 347 public Object getElementAt(int i) { return "No Data Model"; } 348 } 349 ); 350 } 351 352 353 358 public ListUI getUI() { 359 return (ListUI)ui; 360 } 361 362 363 374 public void setUI(ListUI ui) { 375 super.setUI(ui); 376 } 377 378 379 384 public void updateUI() { 385 setUI((ListUI)UIManager.getUI(this)); 386 invalidate(); 387 } 388 389 390 398 public String getUIClassID() { 399 return uiClassID; 400 } 401 402 403 414 private void updateFixedCellSize() 415 { 416 ListCellRenderer cr = getCellRenderer(); 417 Object value = getPrototypeCellValue(); 418 419 if ((cr != null) && (value != null)) { 420 Component c = cr.getListCellRendererComponent(this, value, 0, false, false); 421 422 428 Font f = c.getFont(); 429 c.setFont(getFont()); 430 431 Dimension d = c.getPreferredSize(); 432 fixedCellWidth = d.width; 433 fixedCellHeight = d.height; 434 435 c.setFont(f); 436 } 437 } 438 439 440 448 public Object getPrototypeCellValue() { 449 return prototypeCellValue; 450 } 451 452 485 public void setPrototypeCellValue(Object prototypeCellValue) { 486 Object oldValue = this.prototypeCellValue; 487 this.prototypeCellValue = prototypeCellValue; 488 489 492 493 if ((prototypeCellValue != null) && !prototypeCellValue.equals(oldValue)) { 494 updateFixedCellSize(); 495 } 496 497 firePropertyChange("prototypeCellValue", oldValue, prototypeCellValue); 498 } 499 500 501 509 public int getFixedCellWidth() { 510 return fixedCellWidth; 511 } 512 513 531 public void setFixedCellWidth(int width) { 532 int oldValue = fixedCellWidth; 533 fixedCellWidth = width; 534 firePropertyChange("fixedCellWidth", oldValue, fixedCellWidth); 535 } 536 537 538 546 public int getFixedCellHeight() { 547 return fixedCellHeight; 548 } 549 550 570 public void setFixedCellHeight(int height) { 571 int oldValue = fixedCellHeight; 572 fixedCellHeight = height; 573 firePropertyChange("fixedCellHeight", oldValue, fixedCellHeight); 574 } 575 576 577 583 public ListCellRenderer getCellRenderer() { 584 return cellRenderer; 585 } 586 587 610 public void setCellRenderer(ListCellRenderer cellRenderer) { 611 ListCellRenderer oldValue = this.cellRenderer; 612 this.cellRenderer = cellRenderer; 613 614 617 if ((cellRenderer != null) && !cellRenderer.equals(oldValue)) { 618 updateFixedCellSize(); 619 } 620 621 firePropertyChange("cellRenderer", oldValue, cellRenderer); 622 } 623 624 625 632 public Color getSelectionForeground() { 633 return selectionForeground; 634 } 635 636 637 659 public void setSelectionForeground(Color selectionForeground) { 660 Color oldValue = this.selectionForeground; 661 this.selectionForeground = selectionForeground; 662 firePropertyChange("selectionForeground", oldValue, selectionForeground); 663 } 664 665 666 674 public Color getSelectionBackground() { 675 return selectionBackground; 676 } 677 678 679 700 public void setSelectionBackground(Color selectionBackground) { 701 Color oldValue = this.selectionBackground; 702 this.selectionBackground = selectionBackground; 703 firePropertyChange("selectionBackground", oldValue, selectionBackground); 704 } 705 706 707 714 public int getVisibleRowCount() { 715 return visibleRowCount; 716 } 717 718 739 public void setVisibleRowCount(int visibleRowCount) { 740 int oldValue = this.visibleRowCount; 741 this.visibleRowCount = Math.max(0, visibleRowCount); 742 firePropertyChange("visibleRowCount", oldValue, visibleRowCount); 743 } 744 745 746 758 public int getLayoutOrientation() { 759 return layoutOrientation; 760 } 761 762 763 825 public void setLayoutOrientation(int layoutOrientation) { 826 int oldValue = this.layoutOrientation; 827 switch (layoutOrientation) { 828 case VERTICAL: 829 case VERTICAL_WRAP: 830 case HORIZONTAL_WRAP: 831 this.layoutOrientation = layoutOrientation; 832 firePropertyChange("layoutOrientation", oldValue, layoutOrientation); 833 break; 834 default: 835 throw new IllegalArgumentException ("layoutOrientation must be one of: VERTICAL, HORIZONTAL_WRAP or VERTICAL_WRAP"); 836 } 837 } 838 839 840 854 public int getFirstVisibleIndex() { 855 Rectangle r = getVisibleRect(); 856 int first; 857 if (this.getComponentOrientation().isLeftToRight()) { 858 first = locationToIndex(r.getLocation()); 859 } else { 860 first = locationToIndex(new Point((r.x + r.width) - 1, r.y)); 861 } 862 if (first != -1) { 863 Rectangle bounds = getCellBounds(first, first); 864 if (bounds != null) { 865 SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, bounds); 866 if (bounds.width == 0 || bounds.height == 0) { 867 first = -1; 868 } 869 } 870 } 871 return first; 872 } 873 874 875 889 public int getLastVisibleIndex() { 890 boolean leftToRight = this.getComponentOrientation().isLeftToRight(); 891 Rectangle r = getVisibleRect(); 892 Point lastPoint; 893 if (leftToRight) { 894 lastPoint = new Point((r.x + r.width) - 1, (r.y + r.height) - 1); 895 } else { 896 lastPoint = new Point(r.x, (r.y + r.height) - 1); 897 } 898 int location = locationToIndex(lastPoint); 899 900 if (location != -1) { 901 Rectangle bounds = getCellBounds(location, location); 902 903 if (bounds != null) { 904 SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, bounds); 905 if (bounds.width == 0 || bounds.height == 0) { 906 Point visibleLL = new Point(r.x, lastPoint.y); 909 int last; 910 int llIndex = -1; 911 int lrIndex = location; 912 location = -1; 913 914 do { 915 last = llIndex; 916 llIndex = locationToIndex(visibleLL); 917 918 if (llIndex != -1) { 919 bounds = getCellBounds(llIndex, llIndex); 920 if (llIndex != lrIndex && bounds != null && 921 bounds.contains(visibleLL)) { 922 location = llIndex; 923 visibleLL.x = bounds.x + bounds.width + 1; 924 if (visibleLL.x >= lastPoint.x) { 925 last = llIndex; 927 } 928 } 929 else { 930 last = llIndex; 931 } 932 } 933 } while (llIndex != -1 && last != llIndex); 934 } 935 } 936 } 937 return location; 938 } 939 940 941 950 public void ensureIndexIsVisible(int index) { 951 Rectangle cellBounds = getCellBounds(index, index); 952 if (cellBounds != null) { 953 scrollRectToVisible(cellBounds); 954 } 955 } 956 957 1001 public void setDragEnabled(boolean b) { 1002 if (b && GraphicsEnvironment.isHeadless()) { 1003 throw new HeadlessException(); 1004 } 1005 dragEnabled = b; 1006 } 1007 1008 1015 public boolean getDragEnabled() { 1016 return dragEnabled; 1017 } 1018 1019 1033 public int getNextMatch(String prefix, int startIndex, Position.Bias bias) { 1034 ListModel model = getModel(); 1035 int max = model.getSize(); 1036 if (prefix == null) { 1037 throw new IllegalArgumentException (); 1038 } 1039 if (startIndex < 0 || startIndex >= max) { 1040 throw new IllegalArgumentException (); 1041 } 1042 prefix = prefix.toUpperCase(); 1043 1044 int increment = (bias == Position.Bias.Forward) ? 1 : -1; 1046 int index = startIndex; 1047 do { 1048 Object o = model.getElementAt(index); 1049 1050 if (o != null) { 1051 String string; 1052 1053 if (o instanceof String ) { 1054 string = ((String )o).toUpperCase(); 1055 } 1056 else { 1057 string = o.toString(); 1058 &nb
|