1 14 package org.wings; 15 16 import org.wings.plaf.TableCG; 17 import org.wings.style.CSSAttributeSet; 18 import org.wings.style.CSSProperty; 19 import org.wings.style.CSSSelector; 20 import org.wings.style.CSSStyleSheet; 21 import org.wings.table.STableCellEditor; 22 import org.wings.table.STableCellRenderer; 23 24 import javax.swing.event.CellEditorListener ; 25 import javax.swing.event.ChangeEvent ; 26 import javax.swing.event.ListSelectionEvent ; 27 import javax.swing.event.ListSelectionListener ; 28 import javax.swing.event.TableModelEvent ; 29 import javax.swing.event.TableModelListener ; 30 import javax.swing.table.DefaultTableModel ; 31 import javax.swing.table.TableModel ; 32 import java.util.EventObject ; 33 import java.util.HashMap ; 34 import java.awt.*; 35 36 37 42 public class STable extends SComponent 43 implements TableModelListener , Scrollable, CellEditorListener , LowLevelEventListener { 44 45 48 public static final int NO_SELECTION = SListSelectionModel.NO_SELECTION; 49 52 public static final int SINGLE_SELECTION = SListSelectionModel.SINGLE_SELECTION; 53 56 public static final int SINGLE_INTERVAL_SELECTION = SListSelectionModel.SINGLE_INTERVAL_SELECTION; 57 60 public static final int MULTIPLE_SELECTION = SListSelectionModel.MULTIPLE_INTERVAL_SELECTION; 61 64 public static final int MULTIPLE_INTERVAL_SELECTION = SListSelectionModel.MULTIPLE_INTERVAL_SELECTION; 65 66 67 70 protected TableModel model; 71 72 75 protected SListSelectionModel selectionModel; 76 77 81 protected STableCellRenderer defaultRenderer; 82 83 86 protected STableCellRenderer headerRenderer; 87 88 93 protected final HashMap renderer = new HashMap (); 94 95 protected boolean editable = true; 96 97 100 transient protected SComponent editorComp; 101 102 106 transient protected STableCellEditor cellEditor; 107 108 111 transient protected int editingColumn = -1; 112 113 116 transient protected int editingRow = -1; 117 118 123 protected final HashMap editors = new HashMap (); 124 125 131 protected boolean headerVisible = true; 132 133 137 protected boolean showHorizontalLines = false; 138 139 143 protected boolean showVerticalLines = false; 144 145 protected SDimension intercellSpacing; 146 147 protected SDimension intercellPadding = new SDimension("1", "1"); 148 149 protected Rectangle viewport; 150 151 154 protected boolean epochCheckEnabled = true; 155 156 160 public static final CSSSelector.Pseudo SELECTOR_HEADER = new CSSSelector.Pseudo("HEADER"); 161 162 166 public static final CSSSelector.Pseudo SELECTOR_SELECTION = new CSSSelector.Pseudo("SELECTION"); 167 168 171 public STable() { 172 this(null); 173 } 174 175 180 public STable(TableModel tm) { 181 setSelectionModel(new SDefaultListSelectionModel()); 182 createDefaultEditors(); 183 setModel(tm); 184 } 185 186 191 public void setModel(TableModel tm) { 192 if (model != null) 193 model.removeTableModelListener(this); 194 195 reloadIfChange(model, tm); 196 197 model = tm; 198 if (model == null) 199 model = new DefaultTableModel (); 200 201 model.addTableModelListener(this); 202 } 203 204 207 public TableModel getModel() { 208 return model; 209 } 210 211 public int getColumnCount() { 212 return model.getColumnCount(); 213 } 214 215 public String getColumnName(int col) { 216 return model.getColumnName(col); 217 } 218 219 public Class getColumnClass(int col) { 220 return model.getColumnClass(col); 221 } 222 223 public int getRowCount() { 224 return model.getRowCount(); 225 } 226 227 public Object getValueAt(int row, int column) { 228 return model.getValueAt(row, column); 229 } 230 231 public void setValueAt(Object v, int row, int column) { 232 model.setValueAt(v, row, column); 233 } 234 235 public int convertColumnIndexToModel(int viewColumnIndex) { 236 return viewColumnIndex; 237 } 238 239 242 public void addRowSelectionInterval(int index0, int index1) { 243 selectionModel.addSelectionInterval(index0, index1); 244 } 245 246 public void setParent(SContainer p) { 247 super.setParent(p); 248 249 if (getCellRendererPane() != null) 250 getCellRendererPane().setParent(p); 251 252 if (editorComp != null) 253 editorComp.setParent(p); 254 } 255 256 protected void setParentFrame(SFrame f) { 257 super.setParentFrame(f); 258 if (getCellRendererPane() != null) 259 getCellRendererPane().setParentFrame(f); 260 } 261 262 public void processLowLevelEvent(String action, String [] values) { 263 processKeyEvents(values); 264 265 getSelectionModel().setDelayEvents(true); 267 getSelectionModel().setValueIsAdjusting(true); 268 269 for (int i = 0; i < values.length; i++) { 270 String value = values[i]; 271 if (value.length() > 1) { 272 273 char modus = value.charAt(0); 274 value = value.substring(1); 275 276 int colonIndex = value.indexOf(':'); 277 if (colonIndex < 0) 278 continue; 280 try { 281 282 int row = Integer.parseInt(value.substring(0, colonIndex)); 283 int col = Integer.parseInt(value.substring(colonIndex + 1)); 284 switch (modus) { 286 case 'e': 287 editCellAt(row, col, null); 288 break; 289 case 't': 290 if (getSelectionModel().isSelectedIndex(row)) 291 getSelectionModel().removeSelectionInterval(row, row); 292 else 293 getSelectionModel().addSelectionInterval(row, row); 294 break; 295 case 's': 296 getSelectionModel().addSelectionInterval(row, row); 297 break; 298 case 'd': 299 getSelectionModel().removeSelectionInterval(row, row); 300 break; 301 } 302 } catch (NumberFormatException ex) { 303 ex.printStackTrace(); 305 } 306 } 307 } 308 309 getSelectionModel().setValueIsAdjusting(false); 310 getSelectionModel().setDelayEvents(false); 311 SForm.addArmedComponent(this); 312 313 } 314 315 private SCellRendererPane cellRendererPane = new SCellRendererPane(); 316 317 public SCellRendererPane getCellRendererPane() { 318 return cellRendererPane; 319 } 320 321 public void setDefaultRenderer(STableCellRenderer r) { 322 defaultRenderer = r; 323 } 324 325 public STableCellRenderer getDefaultRenderer() { 326 return defaultRenderer; 327 } 328 329 public void setDefaultRenderer(Class columnClass, STableCellRenderer r) { 330 renderer.remove(columnClass); 331 if (renderer != null) 332 renderer.put(columnClass, r); 333 } 334 335 public STableCellRenderer getDefaultRenderer(Class columnClass) { 336 if (columnClass == null) { 337 return defaultRenderer; 338 } else { 339 Object r = renderer.get(columnClass); 340 if (r != null) { 341 return (STableCellRenderer) r; 342 } else { 343 return getDefaultRenderer(columnClass.getSuperclass()); 344 } 345 } 346 } 347 348 public void setHeaderRenderer(STableCellRenderer r) { 349 headerRenderer = r; 350 } 351 352 public STableCellRenderer getHeaderRenderer() { 353 return headerRenderer; 354 } 355 356 public STableCellRenderer getCellRenderer(int row, int column) { 357 return getDefaultRenderer(getColumnClass(column)); 358 } 359 360 public SComponent prepareRenderer(STableCellRenderer r, int row, int col) { 361 return r.getTableCellRendererComponent(this, 362 model.getValueAt(row, col), 363 isRowSelected(row), 364 row, col); 365 } 366 367 public SComponent prepareHeaderRenderer(int col) { 368 return headerRenderer.getTableCellRendererComponent(this, 369 col >= 0 ? model.getColumnName(col) : null, 370 false, 371 -1, col); 372 } 373 374 public boolean isEditable() { 375 return editable; 376 } 377 378 public void setEditable(boolean editable) { 379 this.editable = editable; 380 } 381 382 395 public void setDefaultEditor(Class columnClass, STableCellEditor r) { 396 editors.remove(columnClass); 397 if (editors != null) 398 editors.put(columnClass, r); 399 } 400 401 413 public STableCellEditor getDefaultEditor(Class columnClass) { 414 if (columnClass == null) { 415 return null; 416 } else { 417 Object r = editors.get(columnClass); 418 if (r != null) { 419 return (STableCellEditor) r; 420 } else { 421 return getDefaultEditor(columnClass.getSuperclass()); 422 } 423 } 424 } 425 426 430 440 public boolean editCellAt(int row, int column) { 441 return editCellAt(row, column, null); 442 } 443 444 459 public boolean editCellAt(int row, int column, EventObject e) { 460 if (isEditing()) { 461 if (cellEditor != null) { 463 boolean stopped = cellEditor.stopCellEditing(); 464 if (!stopped) 465 return false; } 467 } 468 469 if (!isCellEditable(row, column)) 470 return false; 471 472 STableCellEditor editor = getCellEditor(row, column); 473 if (editor != null) { 474 editor.addCellEditorListener(this); 477 setCellEditor(editor); 478 setEditingRow(row); 479 setEditingColumn(column); 480 481 editorComp = prepareEditor(editor, row, column); 483 484 if (editor.isCellEditable(e) && editor.shouldSelectCell(e)) { 485 return true; 486 } else { 487 setValueAt(editor.getCellEditorValue(), row, column); 488 removeEditor(); 489 } 491 } 492 return false; 493 } 494 495 505 public boolean isCellEditable(int row, int col) { 506 if (col >= getColumnCount() || row == -1) 507 return false; 508 else 509 return getModel().isCellEditable(row, col); 510 } 511 512 519 public boolean isEditing() { 520 return (cellEditor == null) ? false : true; 521 } 522 523 529 public SComponent getEditorComponent() { 530 return editorComp; 531 } 532 533 539 public int getEditingColumn() { 540 return editingColumn; 541 } 542 543 549 public int getEditingRow() { 550 return editingRow; 551 } 552 553 559 public STableCellEditor getCellEditor() { 560 return cellEditor; 561 } 562 563 569 protected void setCellEditor(STableCellEditor anEditor) { 570 cellEditor = anEditor; 571 } 572 573 578 public void setEditingColumn(int aColumn) { 579 int oldEditingColumn = editingColumn; 580 editingColumn = aColumn; 581 if (editingColumn != oldEditingColumn) 582 reload(); 583 } 584 585 590 public void setEditingRow(int aRow) { 591 int oldEditingRow = editingRow; 592 editingRow = aRow; 593 if (editingRow != oldEditingRow) 594 reload(); 595 } 596 597 606 public STableCellEditor getCellEditor(int row, int column) { 607 STableCellEditor editor = getDefaultEditor(getColumnClass(column)); 611 return editor; 613 } 614 615 616 623 protected SComponent prepareEditor(STableCellEditor editor, int row, int col) { 624 return editor.getTableCellEditorComponent(this, 625 model.getValueAt(row, col), 626 isRowSelected(row), row, col); 628 } 629 630 634 public void removeEditor() { 635 STableCellEditor editor = getCellEditor(); 636 if (editor != null) { 637 editor.removeCellEditorListener(this); 638 setCellEditor(null); 640 setEditingColumn(-1); 641 setEditingRow(-1); 642 if (editorComp != null) { 643 editorComp.setParent(null); 644 } editorComp = null; 646 } 647 } 648 649 650 654 660 public void editingStopped(ChangeEvent e) { 661 STableCellEditor editor = getCellEditor(); 663 if (editor != null) { 664 Object value = editor.getCellEditorValue(); 665 setValueAt(value, editingRow, editingColumn); 666 removeEditor(); 667 reload(); 668 } 669 } 670 671 677 public void editingCanceled(ChangeEvent e) { 678 removeEditor(); 679 reload(); 680 } 681 682 685 protected void createDefaultEditors() { 686 editors.clear(); 687 688 STextField textField = new STextField(); 690 setDefaultEditor(Object .class, new SDefaultCellEditor(textField)); 691 setDefaultEditor(Number .class, new SDefaultCellEditor(textField)); 692 693 698 SCheckBox centeredCheckBox = new SCheckBox(); 700 setDefaultEditor(Boolean .class, new SDefaultCellEditor(centeredCheckBox)); 702 } 703 704 705 public SListSelectionModel getSelectionModel() { 706 return selectionModel; 707 } 708 709 717 public void setSelectionModel(SListSelectionModel model) { 718 if (getSelectionModel() != null) { 719 removeSelectionListener(reloadOnSelectionChangeListener); 720 } 721 722 if (model == null) { 723 throw new IllegalArgumentException ("cannot set a null SListSelectionModel"); 724 } 725 selectionModel = model; 726 727 addSelectionListener(reloadOnSelectionChangeListener); 728 } 729 730 731 public int getSelectedRowCount() { 732 int result = 0; 733 for (int i = getSelectionModel().getMinSelectionIndex(); 734 i <= getSelectionModel().getMaxSelectionIndex(); i++) { 735 if (getSelectionModel().isSelectedIndex(i)) 736 result++; 737 } 738 739 return result; 740 } 741 742 743 public int getSelectedRow() { 744 return getSelectionModel().getMinSelectionIndex(); 745 } 746 747 public int[] getSelectedRows() { 748 int[] result = new int[getSelectedRowCount()]; 749 750 int index = 0; 751 for (int i = getSelectionModel().getMinSelectionIndex(); 752 i <= getSelectionModel().getMaxSelectionIndex(); i++) { 753 if (getSelectionModel().isSelectedIndex(i)) 754 result[index++] = i; 755 } 756 757 return result; 758 } 759 760 763 public void clearSelection() { 764 getSelectionModel().clearSelection(); 765 } 766 767 768 public boolean isRowSelected(int row) { 769 return getSelectionModel().isSelectedIndex(row); 770 } 771 772 784 public void setSelectionMode(int s) { 785 getSelectionModel().setSelectionMode(s); 786 } 787 788 799 public int getSelectionMode() { 800 return getSelectionModel().getSelectionMode(); 801 } 802 803 804 public void addSelectionListener(ListSelectionListener listener) { 805 getSelectionModel().addListSelectionListener(listener); 806 } 807 808 809 public void removeSelectionListener(ListSelectionListener listener) { 810 getSelectionModel().removeListSelectionListener(listener); 811 } 812 813 public void fireIntermediateEvents() { 814 getSelectionModel().fireDelayedIntermediateEvents(); 815 } 816 817 public void fireFinalEvents() { 818 super.fireFinalEvents(); 819 getSelectionModel().fireDelayedFinalEvents(); 821 } 822 823 826 public boolean isEpochCheckEnabled() { 827 return epochCheckEnabled; 828 } 829 830 833 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 834 this.epochCheckEnabled = epochCheckEnabled; 835 } 836 837 public void tableChanged(TableModelEvent e) { 838 editingCanceled(null); 840 841 if (e == null || e.getFirstRow() == TableModelEvent.HEADER_ROW) { 842 clearSelection(); 844 } else { 845 switch (e.getType()) { 846 case TableModelEvent.INSERT: 847 if (e.getFirstRow() >= 0) 848 getSelectionModel().insertIndexInterval(e.getFirstRow(), 849 e.getLastRow(), true); 850 break; 851 852 case TableModelEvent.DELETE: 853 if (e.getFirstRow() >= 0) 854 getSelectionModel().removeIndexInterval(e.getFirstRow(), 855 e.getLastRow()); 856 break; 857 } 858 } 859 reload(); 860 } 861 862 867 public Color getSelectionBackground() { 868 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getBackground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION)); 869 } 870 871 876 public void setSelectionBackground(Color color) { 877 setAttribute(SELECTOR_SELECTION, CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color)); 878 } 879 880 885 public Color getSelectionForeground() { 886 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getForeground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION)); 887 } 888 889 894 public void setSelectionForeground(Color color) { 895 setAttribute(SELECTOR_SELECTION, CSSProperty.COLOR, CSSStyleSheet.getAttribute(color)); 896 } 897 898 903 public void setSelectionFont(SFont font) { 904 setAttributes(SELECTOR_SELECTION, CSSStyleSheet.getAttributes(font)); 905 } 906 907 912 public SFont getSelectionFont() { 913 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getFont((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION)); 914 } 915 916 921 public Color getHeaderBackground() { 922 return dynamicStyles == null || dynamicStyles.get(SELECTOR_HEADER) == null ? null : CSSStyleSheet.getBackground((CSSAttributeSet) dynamicStyles.get(SELECTOR_HEADER)); 923 } 924 925 930 public void setHeaderBackground(Color color) { 931 setAttribute(SELECTOR_HEADER, CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color)); 932 } 933 934 939 public Color getHeaderForeground() { 940 return dynamicStyles == null || dynamicStyles.get(SELECTOR_HEADER) == null ? null : CSSStyleSheet.getForeground((CSSAttributeSet) dynamicStyles.get(SELECTOR_HEADER)); 941 } 942 943 948 public void setHeaderForeground(Color color) { 949 setAttribute(SELECTOR_HEADER, CSSProperty.COLOR, CSSStyleSheet.getAttribute(color)); 950 } 951 952 957 public void setHeaderFont(SFont font) { 958 setAttributes(SELECTOR_HEADER, CSSStyleSheet.getAttributes(font)); 959 } 960 961 966 public SFont getHeaderFont() { 967 return dynamicStyles == null || dynamicStyles.get(SELECTOR_HEADER) == null ? null : CSSStyleSheet.getFont((CSSAttributeSet) dynamicStyles.get(SELECTOR_HEADER)); 968 } 969 970 public void setHeaderVisible(boolean hv) { 971 boolean oldHeaderVisible = headerVisible; 972 headerVisible = hv; 973 if (oldHeaderVisible != headerVisible) 974 reload(); 975 } 976 977 public boolean isHeaderVisible() { 978 return headerVisible; 979 } 980 981 public void setShowGrid(boolean b) { 982 setShowHorizontalLines(b); 983 setShowVerticalLines(b); 984 } 985 986 public void setShowHorizontalLines(boolean b) { 987 boolean oldShowHorizontalLines = showHorizontalLines; 988 showHorizontalLines = b; 989 if (showHorizontalLines != oldShowHorizontalLines) 990 reload(); 991 } 992 993 public boolean getShowHorizontalLines() { 994 return showHorizontalLines; 995 } 996 997 public void setShowVerticalLines(boolean b) { 998 boolean oldShowVerticalLines = showVerticalLines; 999 showVerticalLines = b; 1000 if (showVerticalLines != oldShowVerticalLines) 1001 reload(); 1002 } 1003 1004 public boolean getShowVerticalLines() { 1005 return showVerticalLines; 1006 } 1007 1008 1013 public void setIntercellSpacing(SDimension d) { 1014 SDimension oldIntercellSpacing = intercellSpacing; 1015 intercellSpacing = d; 1016 if ((intercellSpacing == null && oldIntercellSpacing != null) || 1017 intercellSpacing != null && !intercellSpacing.equals(oldIntercellSpacing)) 1018 reload(); 1019 } 1020 1021 public SDimension getIntercellSpacing() { 1022 return intercellSpacing; 1023 } 1024 1025 1030 1031 public void setIntercellPadding(SDimension d) { 1032 SDimension oldIntercellPadding = intercellPadding; 1033 intercellPadding = d; 1034 if ((intercellPadding == null && oldIntercellPadding != null) || 1035 intercellPadding != null && !intercellPadding.equals(oldIntercellPadding)) 1036 reload(); 1037 } 1038 1039 public SDimension getIntercellPadding() { 1040 return intercellPadding; 1041 } 1042 1043 public void setCG(TableCG cg) { 1044 super.setCG(cg); 1045 } 1046 1047 public String getEditParameter(int row, int col) { 1048 return "e" + row + ":" + col; 1049 } 1050 1051 public String getToggleSelectionParameter(int row, int col) { 1052 return "t" + row + ":" + col; 1053 } 1054 1055 public String getSelectionParameter(int row, int col) { 1056 return "s" + row + ":" + col; 1057 } 1058 1059 public String getDeselectionParameter(int row, int col) { 1060 return "d" + row + ":" + col; 1061 } 1062 1063 1068 public Rectangle getScrollableViewportSize() { 1069 return new Rectangle(0, 0, getColumnCount(), getRowCount()); 1070 } 1071 1072 1075 public void setViewportSize(Rectangle d) { 1076 if (isDifferent(viewport, d)) { 1077 viewport = d; 1078 reload(); 1079 } 1080 } 1081 1082 public Rectangle getViewportSize() { 1083 return viewport; 1084 } 1085 1086 public Dimension getPreferredExtent() { 1087 return null; 1088 } 1089 1090 1093 protected final ListSelectionListener reloadOnSelectionChangeListener = 1094 new ListSelectionListener () { 1095 public void valueChanged(ListSelectionEvent e) { 1096 reload(); 1097 } 1098 }; 1099 1100 public void setSelectedRow(int selectedIndex) { 1101 getSelectionModel().setSelectionInterval(selectedIndex, selectedIndex); 1102 } 1103} 1104 1105 1106 | Popular Tags |