1 14 package org.wings; 15 16 import org.wings.plaf.ListCG; 17 import org.wings.style.CSSSelector; 18 import org.wings.style.CSSStyleSheet; 19 import org.wings.style.CSSProperty; 20 import org.wings.style.CSSAttributeSet; 21 22 import javax.swing.event.EventListenerList ; 23 import javax.swing.event.ListDataListener ; 24 import javax.swing.event.ListSelectionEvent ; 25 import javax.swing.event.ListSelectionListener ; 26 import javax.swing.*; 27 import java.io.Serializable ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.awt.*; 31 32 61 public class SList extends SComponent implements Scrollable, LowLevelEventListener, ListDataListener { 62 63 66 public static final String ORDERED_LIST = "ol"; 67 68 71 public static final String UNORDERED_LIST = "ul"; 72 73 76 public static final String MENU_LIST = "menu"; 77 78 81 public static final String DIR_LIST = "dir"; 82 83 86 public static final String [] ORDER_TYPE_CIRCLE = {"ul", "circle"}; 87 90 public static final String [] ORDER_TYPE_SQUARE = {"ul", "square"}; 91 94 public static final String [] ORDER_TYPE_DISC = {"ul", "disc"}; 95 98 public static final String [] ORDER_TYPE_BIG_ALPHA = {"ol", "A"}; 99 102 public static final String [] ORDER_TYPE_SMALL_ALPHA = {"ol", "a"}; 103 106 public static final String [] ORDER_TYPE_NUMBER = {"ol", null}; 107 110 public static final String [] ORDER_TYPE_NORMAL = {"ul", null}; 111 114 public static final String [] ORDER_TYPE_BIG_ROMAN = {"ol", "I"}; 115 118 public static final String [] ORDER_TYPE_SMALL_ROMAN = {"ol", "i"}; 119 120 123 public static final int NO_SELECTION = SListSelectionModel.NO_SELECTION; 124 127 public static final int SINGLE_SELECTION = SListSelectionModel.SINGLE_SELECTION; 128 131 public static final int SINGLE_INTERVAL_SELECTION = SListSelectionModel.SINGLE_INTERVAL_SELECTION; 132 135 public static final int MULTIPLE_SELECTION = SListSelectionModel.MULTIPLE_INTERVAL_SELECTION; 136 139 public static final int MULTIPLE_INTERVAL_SELECTION = SListSelectionModel.MULTIPLE_INTERVAL_SELECTION; 140 141 144 public static final CSSSelector SELECTOR_SELECTION = new CSSSelector.Pseudo("SELECTION"); 145 146 149 private int visibleRowCount = 8; 150 151 152 private SListSelectionModel selectionModel; 153 154 155 private ListModel dataModel; 156 157 158 private SListCellRenderer cellRenderer; 159 160 161 private ListSelectionHandler selectionHandler; 162 163 166 private Rectangle viewport = null; 167 168 171 private boolean epochCheckEnabled = true; 172 173 176 protected String type = UNORDERED_LIST; 177 178 181 protected String orderType = null; 182 183 186 protected int start = 0; 187 188 191 public SList(ListModel dataModel) { 192 if (dataModel == null) { 193 throw new IllegalArgumentException ("dataModel must not be null"); 194 } 195 196 if (this.dataModel != null) this.dataModel.removeListDataListener(this); 197 this.dataModel = dataModel; 198 this.dataModel.addListDataListener(this); 199 selectionModel = createSelectionModel(); 200 } 201 202 203 207 public SList(final Object [] listData) { 208 this(new AbstractListModel() { 209 public int getSize() { 210 return listData.length; 211 } 212 213 public Object getElementAt(int i) { 214 return listData[i]; 215 } 216 }); 217 } 218 219 220 224 public SList(final List listData) { 225 this(new AbstractListModel() { 226 public int getSize() { 227 return listData.size(); 228 } 229 230 public Object getElementAt(int i) { 231 return listData.get(i); 232 } 233 }); 234 } 235 236 237 240 public SList() { 241 this(new AbstractListModel() { 242 public int getSize() { 243 return 0; 244 } 245 246 public Object getElementAt(int i) { 247 return "No Data Model"; 248 } 249 }); 250 } 251 252 258 public final SListCellRenderer getCellRenderer() { 259 return cellRenderer; 260 } 261 262 269 public void setCellRenderer(SListCellRenderer cellRenderer) { 270 SListCellRenderer oldValue = this.cellRenderer; 271 this.cellRenderer = cellRenderer; 272 reloadIfChange(oldValue, cellRenderer); 273 } 274 275 276 281 public Color getSelectionBackground() { 282 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getBackground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION)); 283 } 284 285 290 public void setSelectionBackground(Color color) { 291 setAttribute(SELECTOR_SELECTION, CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color)); 292 } 293 294 299 public Color getSelectionForeground() { 300 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getForeground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION)); 301 } 302 303 308 public void setSelectionForeground(Color color) { 309 setAttribute(SELECTOR_SELECTION, CSSProperty.COLOR, CSSStyleSheet.getAttribute(color)); 310 } 311 312 317 public void setSelectionFont(SFont font) { 318 setAttributes(SELECTOR_SELECTION, CSSStyleSheet.getAttributes(font)); 319 } 320 321 326 public SFont getSelectionFont() { 327 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getFont((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION)); 328 } 329 330 337 public final int getVisibleRowCount() { 338 return visibleRowCount; 339 } 340 341 351 public void setVisibleRowCount(int visibleRowCount) { 352 if (this.visibleRowCount != visibleRowCount) { 353 this.visibleRowCount = Math.max(0, visibleRowCount); 354 reload(); 355 } 357 } 358 359 360 363 364 365 371 public ListModel getModel() { 372 return dataModel; 373 } 374 375 382 public void setModel(ListModel model) { 383 if (model == null) { 384 throw new IllegalArgumentException ("model must be non null"); 385 } 386 if (isDifferent(dataModel, model)) { 387 clearSelection(); 388 dataModel = model; 389 dataModel.addListDataListener(this); 390 reload(); 391 } 392 } 393 394 395 403 public void setListData(final Object [] listData) { 404 setModel(new AbstractListModel() { 405 public int getSize() { 406 return listData.length; 407 } 408 409 public Object getElementAt(int i) { 410 return listData[i]; 411 } 412 }); 413 } 414 415 416 423 public void setListData(final List listData) { 424 setModel(new AbstractListModel() { 425 public int getSize() { 426 return listData.size(); 427 } 428 429 public Object getElementAt(int i) { 430 return listData.get(i); 431 } 432 }); 433 } 434 435 440 protected SListSelectionModel createSelectionModel() { 441 return new SDefaultListSelectionModel(); 442 } 443 444 445 455 public SListSelectionModel getSelectionModel() { 456 return selectionModel; 457 } 458 459 460 468 protected void fireSelectionValueChanged(int firstIndex, int lastIndex, 469 boolean isAdjusting) { 470 Object [] listeners = getListenerList(); 471 ListSelectionEvent e = null; 472 473 for (int i = listeners.length - 2; i >= 0; i -= 2) { 474 if (listeners[i] == ListSelectionListener .class) { 475 if (e == null) { 476 e = new ListSelectionEvent (this, firstIndex, lastIndex, 477 isAdjusting); 478 } 479 ((ListSelectionListener ) listeners[i + 1]).valueChanged(e); 480 } 481 } 482 } 483 484 485 489 private final class ListSelectionHandler 490 implements ListSelectionListener , Serializable { 491 492 public void valueChanged(ListSelectionEvent e) { 493 fireSelectionValueChanged(e.getFirstIndex(), 494 e.getLastIndex(), 495 e.getValueIsAdjusting()); 496 reload(); 497 } 498 } 499 500 501 508 public void addListSelectionListener(ListSelectionListener listener) { 509 if (selectionHandler == null) { 510 selectionHandler = new ListSelectionHandler(); 511 getSelectionModel().addListSelectionListener(selectionHandler); 512 } 513 514 addEventListener(ListSelectionListener .class, listener); 515 } 516 517 518 526 public void removeListSelectionListener(ListSelectionListener listener) { 527 removeEventListener(ListSelectionListener .class, listener); 528 } 529 530 531 538 public ListSelectionListener [] getListSelectionListeners() { 539 return (ListSelectionListener []) getListeners(ListSelectionListener .class); 540 } 541 542 543 551 public void setSelectionModel(SListSelectionModel selectionModel) { 552 if (selectionModel == null) { 553 throw new IllegalArgumentException ("selectionModel must be non null"); 554 } 555 556 if (selectionHandler != null) { 557 this.selectionModel.removeListSelectionListener(selectionHandler); 558 selectionModel.addListSelectionListener(selectionHandler); 559 } 560 561 this.selectionModel = selectionModel; 563 } 564 565 566 580 public void setSelectionMode(int selectionMode) { 581 selectionModel.setSelectionMode(selectionMode); 582 } 583 584 590 public int getSelectionMode() { 591 return getSelectionModel().getSelectionMode(); 592 } 593 594 595 602 public int getAnchorSelectionIndex() { 603 return getSelectionModel().getAnchorSelectionIndex(); 604 } 605 606 607 614 public int getLeadSelectionIndex() { 615 return getSelectionModel().getLeadSelectionIndex(); 616 } 617 618 619 624 public int getMinSelectionIndex() { 625 return getSelectionModel().getMinSelectionIndex(); 626 } 627 628 629 634 public int getMaxSelectionIndex() { 635 return getSelectionModel().getMaxSelectionIndex(); 636 } 637 638 639 645 public boolean isSelectedIndex(int index) { 646 return getSelectionModel().isSelectedIndex(index); 647 } 648 649 650 656 public boolean isSelectionEmpty() { 657 return getSelectionModel().isSelectionEmpty(); 658 } 659 660 661 666 public void clearSelection() { 667 if (!getSelectionModel().isSelectionEmpty()) { 668 getSelectionModel().clearSelection(); 669 reload(); 670 } 671 } 672 673 674 682 public void setSelectionInterval(int anchor, int lead) { 683 getSelectionModel().setSelectionInterval(anchor, lead); 684 } 685 686 687 695 public void addSelectionInterval(int anchor, int lead) { 696 getSelectionModel().addSelectionInterval(anchor, lead); 697 } 698 699 700 708 public void removeSelectionInterval(int index0, int index1) { 709 getSelectionModel().removeSelectionInterval(index0, index1); 710 } 711 712 713 717 public void setValueIsAdjusting(boolean b) { 718 getSelectionModel().setValueIsAdjusting(b); 719 } 720 721 725 public boolean getValueIsAdjusting() { 726 return getSelectionModel().getValueIsAdjusting(); 727 } 728 729 730 737 public int[] getSelectedIndices() { 738 ListSelectionModel sm = getSelectionModel(); 739 int iMin = sm.getMinSelectionIndex(); 740 int iMax = sm.getMaxSelectionIndex(); 741 742 if ((iMin < 0) || (iMax < 0)) { 743 return new int[0]; 744 } 745 746 int[] rvTmp = new int[1 + (iMax - iMin)]; 747 int n = 0; 748 for (int i = iMin; i <= iMax; i++) { 749 if (sm.isSelectedIndex(i)) { 750 rvTmp[n++] = i; 751 } 752 } 753 int[] rv = new int[n]; 754 System.arraycopy(rvTmp, 0, rv, 0, n); 755 return rv; 756 } 757 758 759 767 public void setSelectedIndex(int index) { 768 getSelectionModel().setSelectionInterval(index, index); 769 } 770 771 772 780 public void setSelectedIndices(int[] indices) { 781 ListSelectionModel sm = getSelectionModel(); 782 sm.clearSelection(); 783 for (int i = 0; i < indices.length; i++) { 784 sm.addSelectionInterval(indices[i], indices[i]); 785 } 786 } 787 788 789 799 public Object [] getSelectedValues() { 800 ListSelectionModel sm = getSelectionModel(); 801 ListModel dm = getModel(); 802 803 int iMin = sm.getMinSelectionIndex(); 804 int iMax = sm.getMaxSelectionIndex(); 805 806 if ((iMin < 0) || (iMax < 0)) { 807 return new Object [0]; 808 } 809 810 Object [] rvTmp = new Object [1 + (iMax - iMin)]; 811 int n = 0; 812 for (int i = iMin; i <= iMax; i++) { 813 if (sm.isSelectedIndex(i) && i < dm.getSize()) { 814 rvTmp[n++] = dm.getElementAt(i); 815 } 816 } 817 Object [] rv = new Object [n]; 818 System.arraycopy(rvTmp, 0, rv, 0, n); 819 return rv; 820 } 821 822 823 830 public int getSelectedIndex() { 831 return getMinSelectionIndex(); 832 } 833 834 835 844 public Object getSelectedValue() { 845 int i = getMinSelectionIndex(); 846 return (i == -1) ? null : getModel().getElementAt(i); 847 } 848 849 850 855 public void setSelectedValue(Object anObject) { 856 if (anObject == null) 857 setSelectedIndex(-1); 858 else if (!anObject.equals(getSelectedValue())) { 859 int i, c; 860 ListModel dm = getModel(); 861 for (i = 0, c = dm.getSize(); i < c; i++) 862 if (anObject.equals(dm.getElementAt(i))) { 863 setSelectedIndex(i); 864 return; 865 } 866 setSelectedIndex(-1); 867 } 868 } 869 870 871 883 public void setType(String t) { 884 if (t != null) 885 type = t; 886 else 887 type = UNORDERED_LIST; 888 } 889 890 895 public String getType() { 896 return type; 897 } 898 899 902 public void setOrderType(String t) { 903 orderType = t; 904 } 905 906 909 public String getOrderType() { 910 return orderType; 911 } 912 913 917 public void setType(String [] t) { 918 if (t == null) { 919 setType((String ) null); 920 setOrderType(null); 921 } else if (t.length == 2) { 922 setType(t[0]); 923 setOrderType(t[1]); 924 } 925 } 926 927 930 public void setStart(int s) { 931 start = s; 932 } 933 934 937 public int getStart() { 938 return start; 939 } 940 941 public void fireIntermediateEvents() { 942 getSelectionModel().fireDelayedIntermediateEvents(); 943 } 944 945 public void fireFinalEvents() { 946 super.fireFinalEvents(); 947 getSelectionModel().fireDelayedFinalEvents(); 949 } 950 951 954 public boolean isEpochCheckEnabled() { 955 return epochCheckEnabled; 956 } 957 958 961 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 962 this.epochCheckEnabled = epochCheckEnabled; 963 } 964 965 970 public void processLowLevelEvent(String action, String [] values) { 971 processKeyEvents(values); 972 973 getSelectionModel().setDelayEvents(true); 975 976 getSelectionModel().setValueIsAdjusting(true); 977 if (getShowAsFormComponent()) { 980 981 ArrayList selectedIndices = new ArrayList (); 982 for (int i = 0; i < values.length; i++) { 983 984 985 if (values[i].length() < 2) continue; 987 String indexString = values[i].substring(1); 988 try { 989 int index = Integer.parseInt(indexString); 990 991 if (values[i].charAt(0) == 'a') { 993 selectedIndices.add(new Integer (index)); 994 addSelectionInterval(index, index); 995 } 996 } catch (Exception ex) { 997 } 999 } 1000 for (int i = 0; i < getModel().getSize(); i++) { 1003 if (isSelectedIndex(i) && 1004 !selectedIndices.contains(new Integer (i))) { 1005 removeSelectionInterval(i, i); 1006 } 1007 } 1008 } else { 1009 1010 for (int i = 0; i < values.length; i++) { 1011 1012 if (values[i].length() < 2) continue; 1014 String indexString = values[i].substring(1); 1016 try { 1017 int index = Integer.parseInt(indexString); 1018 1019 if (values[i].charAt(0) == 'a') { 1020 addSelectionInterval(index, index); 1021 } else if (values[i].charAt(0) == 'r') { 1022 removeSelectionInterval(index, index); 1023 } } catch (Exception ex) { 1025 } 1026 1027 } 1028 } 1029 getSelectionModel().setValueIsAdjusting(false); 1030 1031 1032 getSelectionModel().setDelayEvents(false); 1033 1034 SForm.addArmedComponent(this); 1035 } 1036 1037 1043 public Rectangle getScrollableViewportSize() { 1044 return new Rectangle(0, 0, 1, dataModel.getSize()); 1045 } 1046 1047 1052 public void setViewportSize(Rectangle d) { 1053 if (isDifferent(viewport, d)) { 1054 viewport = d; 1055 reload(); 1056 } 1057 } 1058 1059 1064 public final Rectangle getViewportSize() { 1065 return viewport; 1066 } 1067 1068 public Dimension getPreferredExtent() { 1069 return new Dimension(1, Math.min(getVisibleRowCount(), getModel().getSize())); 1070 } 1071 1073 public void setParent(SContainer p) { 1074 super.setParent(p); 1075 if (getCellRendererPane() != null) 1076 getCellRendererPane().setParent(p); 1077 } 1078 1079 protected void setParentFrame(SFrame f) { 1080 super.setParentFrame(f); 1081 if (getCellRendererPane() != null) 1082 getCellRendererPane().setParentFrame(f); 1083 } 1084 1085 1086 private SCellRendererPane cellRendererPane = new SCellRendererPane(); 1088 1089 1090 public SCellRendererPane getCellRendererPane() { 1091 return cellRendererPane; 1092 } 1093 1094 1095 public void removeCellRendererPane() { 1096 cellRendererPane.setParent(null); 1097 cellRendererPane = null; 1098 } 1099 1100 public void setCG(ListCG cg) { 1101 super.setCG(cg); 1102 } 1103 1104 public String getToggleSelectionParameter(int index) { 1105 return isSelectedIndex(index) ? getDeselectionParameter(index) : 1106 getSelectionParameter(index); 1107 } 1108 1109 public String getSelectionParameter(int index) { 1110 return "a" + Integer.toString(index); 1111 } 1112 1113 public String getDeselectionParameter(int index) { 1114 return "r" + Integer.toString(index); 1115 } 1116 1117 1121 1122 public void contentsChanged(javax.swing.event.ListDataEvent e) { 1123 reload(); 1124 } 1125 1126 public void intervalAdded(javax.swing.event.ListDataEvent e) { 1127 reload(); 1128 } 1129 1130 public void intervalRemoved(javax.swing.event.ListDataEvent e) { 1131 reload(); 1132 } 1133 1134} 1135 1136 1137 | Popular Tags |