KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > STable


1 /*
2  * $Id: STable.java,v 1.17 2005/05/27 15:20:54 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

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 JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ListSelectionEvent JavaDoc;
27 import javax.swing.event.ListSelectionListener JavaDoc;
28 import javax.swing.event.TableModelEvent JavaDoc;
29 import javax.swing.event.TableModelListener JavaDoc;
30 import javax.swing.table.DefaultTableModel JavaDoc;
31 import javax.swing.table.TableModel JavaDoc;
32 import java.util.EventObject JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.awt.*;
35
36
37 /**
38  * @author <a HREF="mailto:engels@mercatis.de">Holger Engels</a>
39  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
40  * @version $Revision: 1.17 $
41  */

42 public class STable extends SComponent
43         implements TableModelListener JavaDoc, Scrollable, CellEditorListener JavaDoc, LowLevelEventListener {
44
45     /**
46      * Table selection model. See {@link STable#setSelectionMode(int)}
47      */

48     public static final int NO_SELECTION = SListSelectionModel.NO_SELECTION;
49     /**
50      * Table selection model. See {@link STable#setSelectionMode(int)}
51      */

52     public static final int SINGLE_SELECTION = SListSelectionModel.SINGLE_SELECTION;
53     /**
54      * Table selection model. See {@link STable#setSelectionMode(int)}
55      */

56     public static final int SINGLE_INTERVAL_SELECTION = SListSelectionModel.SINGLE_INTERVAL_SELECTION;
57     /**
58      * Table selection model. See {@link STable#setSelectionMode(int)}
59      */

60     public static final int MULTIPLE_SELECTION = SListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
61     /**
62      * Table selection model. See {@link STable#setSelectionMode(int)}
63      */

64     public static final int MULTIPLE_INTERVAL_SELECTION = SListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
65
66
67     /**
68      * <p>the table model.</p>
69      */

70     protected TableModel JavaDoc model;
71
72     /**
73      * <p>the selection model.</p>
74      */

75     protected SListSelectionModel selectionModel;
76
77     /**
78      * <p>The default renderer is used if no other renderer is set for the
79      * content of a cell.</p>
80      */

81     protected STableCellRenderer defaultRenderer;
82
83     /**
84      * <p>The <code>headerRenderer</code> is used to render the header line.</p>
85      */

86     protected STableCellRenderer headerRenderer;
87
88     /**
89      * <p>In this <code>Map</code>, the renderers for the different
90      * classes of cell content are stored.</p><p>The class is treated
91      * as key, the renderer as the value.</p>
92      */

93     protected final HashMap JavaDoc renderer = new HashMap JavaDoc();
94
95     protected boolean editable = true;
96
97     /**
98      * <p>If editing, this is the <code>SComponent</code> that is handling the editing.
99      */

100     transient protected SComponent editorComp;
101
102     /**
103      * <p>The object that overwrites the screen real estate occupied by the
104      * current cell and allows the user to change those contents.</p>
105      */

106     transient protected STableCellEditor cellEditor;
107
108     /**
109      * <p>Identifies the column of the cell being edited.</p>
110      */

111     transient protected int editingColumn = -1;
112
113     /**
114      * <p>Identifies the row of the cell being edited.</p>
115      */

116     transient protected int editingRow = -1;
117
118     /**
119      * <p>In this <code>Map</code>, the <code>STableCellEditor</code>s for the different
120      * classes of cell content are stored.</p><p>The class is treated
121      * as key, the <code>STableCellEditor</code> as the value.</p>
122      */

123     protected final HashMap JavaDoc editors = new HashMap JavaDoc();
124
125     /**
126      * <p>Determines whether the header is visible or not.</p><p>By
127      * default the header is visible.</p> <p><em>CAVEAT:</em>The
128      * header is not (yet) implemented like in Swing. But maybe
129      * someday. So you can disable it if you like. TODO.</p>
130      */

131     protected boolean headerVisible = true;
132
133     /**
134      * <p>Determines if horizontal lines in the table should be
135      * painted.</p><p>This is off by default.</p>
136      */

137     protected boolean showHorizontalLines = false;
138
139     /**
140      * <p>Determines if vertical lines in the table should be
141      * painted.</p><p>This is off by default.</p>
142      */

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     /**
152      * @see LowLevelEventListener#isEpochCheckEnabled()
153      */

154     protected boolean epochCheckEnabled = true;
155
156     /**
157      * A Pseudo CSS selector addressing the header row elements.
158      * Refer to {@link SComponent#setAttribute(org.wings.style.CSSSelector, org.wings.style.CSSProperty, String)}
159      */

160     public static final CSSSelector.Pseudo SELECTOR_HEADER = new CSSSelector.Pseudo("HEADER");
161
162     /**
163      * A Pseudo CSS selector addressing the selected row elements.
164      * Refer to {@link SComponent#setAttribute(org.wings.style.CSSSelector, org.wings.style.CSSProperty, String)}
165      */

166     public static final CSSSelector.Pseudo SELECTOR_SELECTION = new CSSSelector.Pseudo("SELECTION");
167
168     /**
169      * <p>Creates a new <code>STable</code>.</p>
170      */

171     public STable() {
172         this(null);
173     }
174
175     /**
176      * <p>Creates a new <code>STable</code>.</p>
177      *
178      * @param tm the <code>TableModel</code> for the table's contents.
179      */

180     public STable(TableModel JavaDoc tm) {
181         setSelectionModel(new SDefaultListSelectionModel());
182         createDefaultEditors();
183         setModel(tm);
184     }
185
186     /**
187      * <p>Sets the model of the table.</p>
188      *
189      * @param tm the <code>TableModel</code> to set.
190      */

191     public void setModel(TableModel JavaDoc 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 JavaDoc();
200
201         model.addTableModelListener(this);
202     }
203
204     /**
205      * <p>returns the model of the table</p>
206      */

207     public TableModel JavaDoc getModel() {
208         return model;
209     }
210
211     public int getColumnCount() {
212         return model.getColumnCount();
213     }
214
215     public String JavaDoc getColumnName(int col) {
216         return model.getColumnName(col);
217     }
218
219     public Class JavaDoc getColumnClass(int col) {
220         return model.getColumnClass(col);
221     }
222
223     public int getRowCount() {
224         return model.getRowCount();
225     }
226
227     public Object JavaDoc getValueAt(int row, int column) {
228         return model.getValueAt(row, column);
229     }
230
231     public void setValueAt(Object JavaDoc 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     /**
240      * Adds the row from <i>index0</i> to <i>index0</i> inclusive to the current selection.
241      */

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 JavaDoc action, String JavaDoc[] values) {
263         processKeyEvents(values);
264
265         // delay events...
266
getSelectionModel().setDelayEvents(true);
267         getSelectionModel().setValueIsAdjusting(true);
268
269         for (int i = 0; i < values.length; i++) {
270             String JavaDoc 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; // maybe next value fits ...
279

280                 try {
281
282                     int row = Integer.parseInt(value.substring(0, colonIndex));
283                     int col = Integer.parseInt(value.substring(colonIndex + 1));
284                     // editor event
285
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 JavaDoc ex) {
303                     // hier loggen...
304
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 JavaDoc columnClass, STableCellRenderer r) {
330         renderer.remove(columnClass);
331         if (renderer != null)
332             renderer.put(columnClass, r);
333     }
334
335     public STableCellRenderer getDefaultRenderer(Class JavaDoc columnClass) {
336         if (columnClass == null) {
337             return defaultRenderer;
338         } else {
339             Object JavaDoc 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     /**
383      * Set a default editor to be used if no editor has been set in
384      * a TableColumn. If no editing is required in a table, or a
385      * particular column in a table, use the isCellEditable()
386      * method in the TableModel interface to ensure that the
387      * STable will not start an editor in these columns.
388      * If editor is null, remove the default editor for this
389      * column class.
390      *
391      * @see TableModel#isCellEditable
392      * @see #getDefaultEditor
393      * @see #setDefaultRenderer
394      */

395     public void setDefaultEditor(Class JavaDoc columnClass, STableCellEditor r) {
396         editors.remove(columnClass);
397         if (editors != null)
398             editors.put(columnClass, r);
399     }
400
401     /*
402      * Returns the editor to be used when no editor has been set in
403      * a TableColumn. During the editing of cells the editor is fetched from
404      * a Map of entries according to the class of the cells in the column. If
405      * there is no entry for this <I>columnClass</I> the method returns
406      * the entry for the most specific superclass. The STable installs entries
407      * for <I>Object</I>, <I>Number</I> and <I>Boolean</I> all which can be modified
408      * or replaced.
409      *
410      * @see #setDefaultEditor
411      * @see #getColumnClass
412      */

413     public STableCellEditor getDefaultEditor(Class JavaDoc columnClass) {
414         if (columnClass == null) {
415             return null;
416         } else {
417             Object JavaDoc r = editors.get(columnClass);
418             if (r != null) {
419                 return (STableCellEditor) r;
420             } else {
421                 return getDefaultEditor(columnClass.getSuperclass());
422             }
423         }
424     }
425
426     //
427
// Editing Support
428
//
429

430     /**
431      * Programmatically starts editing the cell at <I>row</I> and
432      * <I>column</I>, if the cell is editable.
433      *
434      * @param row the row to be edited
435      * @param column the column to be edited
436      * @return false if for any reason the cell cannot be edited.
437      * @throws IllegalArgumentException If <I>row</I> or <I>column</I>
438      * are not in the valid range
439      */

440     public boolean editCellAt(int row, int column) {
441         return editCellAt(row, column, null);
442     }
443
444     /**
445      * Programmatically starts editing the cell at <I>row</I> and
446      * <I>column</I>, if the cell is editable.
447      * To prevent the STable from editing a particular table, column or
448      * cell value, return false from the isCellEditable() method in the
449      * TableModel interface.
450      *
451      * @param row the row to be edited
452      * @param column the column to be edited
453      * @param e event to pass into
454      * shouldSelectCell
455      * @return false if for any reason the cell cannot be edited.
456      * @throws IllegalArgumentException If <I>row</I> or <I>column</I>
457      * are not in the valid range
458      */

459     public boolean editCellAt(int row, int column, EventObject JavaDoc e) {
460         if (isEditing()) {
461             // Try to stop the current editor
462
if (cellEditor != null) {
463                 boolean stopped = cellEditor.stopCellEditing();
464                 if (!stopped)
465                     return false; // The current editor not resigning
466
}
467         }
468
469         if (!isCellEditable(row, column))
470             return false;
471
472         STableCellEditor editor = getCellEditor(row, column);
473         if (editor != null) {
474             // set up editor environment and make it possible for the editor, to
475
// stop/cancel editing on preparation
476
editor.addCellEditorListener(this);
477             setCellEditor(editor);
478             setEditingRow(row);
479             setEditingColumn(column);
480
481             // prepare editor
482
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             } // end of else
490

491         }
492         return false;
493     }
494
495     /**
496      * Returns true if the cell at <I>row</I> and <I>column</I>
497      * is editable. Otherwise, setValueAt() on the cell will not change
498      * the value of that cell.
499      *
500      * @param row the row whose value is to be looked up
501      * @param col the column whose value is to be looked up
502      * @return true if the cell is editable.
503      * @see #setValueAt
504      */

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     /**
513      * Returns true is the table is editing a cell.
514      *
515      * @return true is the table is editing a cell
516      * @see #editingColumn
517      * @see #editingRow
518      */

519     public boolean isEditing() {
520         return (cellEditor == null) ? false : true;
521     }
522
523     /**
524      * If the receiver is currently editing this will return the Component
525      * that was returned from the CellEditor.
526      *
527      * @return SComponent handling editing session
528      */

529     public SComponent getEditorComponent() {
530         return editorComp;
531     }
532
533     /**
534      * This returns the index of the editing column.
535      *
536      * @return the index of the column being edited
537      * @see #editingRow
538      */

539     public int getEditingColumn() {
540         return editingColumn;
541     }
542
543     /**
544      * Returns the index of the editing row.
545      *
546      * @return the index of the row being edited
547      * @see #editingColumn
548      */

549     public int getEditingRow() {
550         return editingRow;
551     }
552
553     /**
554      * Return the cellEditor.
555      *
556      * @return the STableCellEditor that does the editing
557      * @see #cellEditor
558      */

559     public STableCellEditor getCellEditor() {
560         return cellEditor;
561     }
562
563     /**
564      * Set the cellEditor variable.
565      *
566      * @param anEditor the STableCellEditor that does the editing
567      * @see #cellEditor
568      */

569     protected void setCellEditor(STableCellEditor anEditor) {
570         cellEditor = anEditor;
571     }
572
573     /**
574      * Set the editingColumn variable.
575      *
576      * @see #editingColumn
577      */

578     public void setEditingColumn(int aColumn) {
579         int oldEditingColumn = editingColumn;
580         editingColumn = aColumn;
581         if (editingColumn != oldEditingColumn)
582             reload();
583     }
584
585     /**
586      * Set the editingRow variable.
587      *
588      * @see #editingRow
589      */

590     public void setEditingRow(int aRow) {
591         int oldEditingRow = editingRow;
592         editingRow = aRow;
593         if (editingRow != oldEditingRow)
594             reload();
595     }
596
597     /**
598      * Return an appropriate editor for the cell specified by this row and
599      * column. If the TableColumn for this column has a non-null editor, return that.
600      * If not, find the class of the data in this column (using getColumnClass())
601      * and return the default editor for this type of data.
602      *
603      * @param row the row of the cell to edit, where 0 is the first
604      * @param column the column of the cell to edit, where 0 is the first
605      */

606     public STableCellEditor getCellEditor(int row, int column) {
607         // TableColumn tableColumn = getColumnModel().getColumn(column);
608
// STableCellEditor editor = tableColumn.getCellEditor();
609
// if (editor == null) {
610
STableCellEditor editor = getDefaultEditor(getColumnClass(column));
611         // }
612
return editor;
613     }
614
615
616     /**
617      * Prepares the specified editor using the value at the specified cell.
618      *
619      * @param editor the TableCellEditor to set up
620      * @param row the row of the cell to edit, where 0 is the first
621      * @param col the column of the cell to edit, where 0 is the first
622      */

623     protected SComponent prepareEditor(STableCellEditor editor, int row, int col) {
624         return editor.getTableCellEditorComponent(this,
625                 model.getValueAt(row, col),
626                 isRowSelected(row), // true?
627
row, col);
628     }
629
630     /**
631      * Discard the editor object and return the real estate it used to
632      * cell rendering.
633      */

634     public void removeEditor() {
635         STableCellEditor editor = getCellEditor();
636         if (editor != null) {
637             editor.removeCellEditorListener(this);
638             //remove(editorComp);
639
setCellEditor(null);
640             setEditingColumn(-1);
641             setEditingRow(-1);
642             if (editorComp != null) {
643                 editorComp.setParent(null);
644             } // end of if ()
645
editorComp = null;
646         }
647     }
648
649
650     //
651
// Implementing the CellEditorListener interface
652
//
653

654     /**
655      * Invoked when editing is finished. The changes are saved and the
656      * editor object is discarded.
657      *
658      * @see CellEditorListener
659      */

660     public void editingStopped(ChangeEvent JavaDoc e) {
661         // Take in the new value
662
STableCellEditor editor = getCellEditor();
663         if (editor != null) {
664             Object JavaDoc value = editor.getCellEditorValue();
665             setValueAt(value, editingRow, editingColumn);
666             removeEditor();
667             reload();
668         }
669     }
670
671     /**
672      * Invoked when editing is canceled. The editor object is discarded
673      * and the cell is rendered once again.
674      *
675      * @see CellEditorListener
676      */

677     public void editingCanceled(ChangeEvent JavaDoc e) {
678         removeEditor();
679         reload();
680     }
681
682     /**
683      * Creates default cell editors for Objects, numbers, and boolean values.
684      */

685     protected void createDefaultEditors() {
686         editors.clear();
687
688         // Objects
689
STextField textField = new STextField();
690         setDefaultEditor(Object JavaDoc.class, new SDefaultCellEditor(textField));
691         setDefaultEditor(Number JavaDoc.class, new SDefaultCellEditor(textField));
692
693         // Numbers
694
//STextField rightAlignedTextField = new STextField();
695
//rightAlignedTextField.setHorizontalAlignment(STextField.RIGHT);
696
//setDefaultEditor(Number.class, new SDefaultCellEditor(rightAlignedTextField));
697

698         // Booleans
699
SCheckBox centeredCheckBox = new SCheckBox();
700         //centeredCheckBox.setHorizontalAlignment(JCheckBox.CENTER);
701
setDefaultEditor(Boolean JavaDoc.class, new SDefaultCellEditor(centeredCheckBox));
702     }
703
704
705     public SListSelectionModel getSelectionModel() {
706         return selectionModel;
707     }
708
709     /**
710      * Sets the row selection model for this table to <code>model</code>.
711      *
712      * @param model the new selection model
713      * @throws IllegalArgumentException if <code>model</code>
714      * is <code>null</code>
715      * @see #getSelectionModel
716      */

717     public void setSelectionModel(SListSelectionModel model) {
718         if (getSelectionModel() != null) {
719             removeSelectionListener(reloadOnSelectionChangeListener);
720         }
721
722         if (model == null) {
723             throw new IllegalArgumentException JavaDoc("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     /**
761      * Deselects all selected columns and rows.
762      */

763     public void clearSelection() {
764         getSelectionModel().clearSelection();
765     }
766
767
768     public boolean isRowSelected(int row) {
769         return getSelectionModel().isSelectedIndex(row);
770     }
771
772     /**
773      * Sets the selection mode. Use one of the following values:
774      * <UL>
775      * <LI> {@link #NO_SELECTION}
776      * <LI> {@link javax.swing.ListSelectionModel#SINGLE_SELECTION} or
777      * {@link #SINGLE_SELECTION}
778      * <LI> {@link javax.swing.ListSelectionModel#SINGLE_INTERVAL_SELECTION} or
779      * {@link #SINGLE_INTERVAL_SELECTION}
780      * <LI> {@link javax.swing.ListSelectionModel#MULTIPLE_INTERVAL_SELECTION} or
781      * {@link #MULTIPLE_SELECTION}
782      * </UL>
783      */

784     public void setSelectionMode(int s) {
785         getSelectionModel().setSelectionMode(s);
786     }
787
788     /**
789      * @return <UL>
790      * <LI> {@link #NO_SELECTION}
791      * <LI> {@link javax.swing.ListSelectionModel#SINGLE_SELECTION} or
792      * {@link #SINGLE_SELECTION}
793      * <LI> {@link javax.swing.ListSelectionModel#SINGLE_INTERVAL_SELECTION} or
794      * {@link #SINGLE_INTERVAL_SELECTION}
795      * <LI> {@link javax.swing.ListSelectionModel#MULTIPLE_INTERVAL_SELECTION} or
796      * {@link #MULTIPLE_SELECTION}
797      * </UL>
798      */

799     public int getSelectionMode() {
800         return getSelectionModel().getSelectionMode();
801     }
802
803
804     public void addSelectionListener(ListSelectionListener JavaDoc listener) {
805         getSelectionModel().addListSelectionListener(listener);
806     }
807
808
809     public void removeSelectionListener(ListSelectionListener JavaDoc listener) {
810         getSelectionModel().removeListSelectionListener(listener);
811     }
812
813     public void fireIntermediateEvents() {
814         getSelectionModel().fireDelayedIntermediateEvents();
815     }
816
817     public void fireFinalEvents() {
818         super.fireFinalEvents();
819         // fire selection events...
820
getSelectionModel().fireDelayedFinalEvents();
821     }
822
823     /**
824      * @see LowLevelEventListener#isEpochCheckEnabled()
825      */

826     public boolean isEpochCheckEnabled() {
827         return epochCheckEnabled;
828     }
829
830     /**
831      * @see LowLevelEventListener#isEpochCheckEnabled()
832      */

833     public void setEpochCheckEnabled(boolean epochCheckEnabled) {
834         this.epochCheckEnabled = epochCheckEnabled;
835     }
836
837     public void tableChanged(TableModelEvent JavaDoc e) {
838         // kill active editors
839
editingCanceled(null);
840
841         if (e == null || e.getFirstRow() == TableModelEvent.HEADER_ROW) {
842             // The whole thing changed
843
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     /**
863      * Return the background color.
864      *
865      * @return the background color
866      */

867     public Color getSelectionBackground() {
868         return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getBackground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION));
869     }
870
871     /**
872      * Set the foreground color.
873      *
874      * @param color the new foreground color
875      */

876     public void setSelectionBackground(Color color) {
877         setAttribute(SELECTOR_SELECTION, CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color));
878     }
879
880     /**
881      * Return the foreground color.
882      *
883      * @return the foreground color
884      */

885     public Color getSelectionForeground() {
886         return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getForeground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION));
887     }
888
889     /**
890      * Set the foreground color.
891      *
892      * @param color the new foreground color
893      */

894     public void setSelectionForeground(Color color) {
895         setAttribute(SELECTOR_SELECTION, CSSProperty.COLOR, CSSStyleSheet.getAttribute(color));
896     }
897
898     /**
899      * Set the font.
900      *
901      * @param font the new font
902      */

903     public void setSelectionFont(SFont font) {
904         setAttributes(SELECTOR_SELECTION, CSSStyleSheet.getAttributes(font));
905     }
906
907     /**
908      * Return the font.
909      *
910      * @return the font
911      */

912     public SFont getSelectionFont() {
913         return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getFont((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION));
914     }
915
916     /**
917      * Return the background color.
918      *
919      * @return the background color
920      */

921     public Color getHeaderBackground() {
922         return dynamicStyles == null || dynamicStyles.get(SELECTOR_HEADER) == null ? null : CSSStyleSheet.getBackground((CSSAttributeSet) dynamicStyles.get(SELECTOR_HEADER));
923     }
924
925     /**
926      * Set the foreground color.
927      *
928      * @param color the new foreground color
929      */

930     public void setHeaderBackground(Color color) {
931         setAttribute(SELECTOR_HEADER, CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color));
932     }
933
934     /**
935      * Return the foreground color.
936      *
937      * @return the foreground color
938      */

939     public Color getHeaderForeground() {
940         return dynamicStyles == null || dynamicStyles.get(SELECTOR_HEADER) == null ? null : CSSStyleSheet.getForeground((CSSAttributeSet) dynamicStyles.get(SELECTOR_HEADER));
941     }
942
943     /**
944      * Set the foreground color.
945      *
946      * @param color the new foreground color
947      */

948     public void setHeaderForeground(Color color) {
949         setAttribute(SELECTOR_HEADER, CSSProperty.COLOR, CSSStyleSheet.getAttribute(color));
950     }
951
952     /**
953      * Set the font.
954      *
955      * @param font the new font
956      */

957     public void setHeaderFont(SFont font) {
958         setAttributes(SELECTOR_HEADER, CSSStyleSheet.getAttributes(font));
959     }
960
961     /**
962      * Return the font.
963      *
964      * @return the font
965      */

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    /*
1009     * Implementiert das cellspacing Attribut des HTML Tables. Da dieses
1010     * nur eindimensional ist, wird nur der width Wert der Dimension in
1011     * den HTML Code uebernommen.
1012     */

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    /*
1026     * Implementiert das cellpadding Attribut des HTML Tables. Da dieses
1027     * nur eindimensional ist, wird nur der width Wert der Dimension in
1028     * den HTML Code uebernommen.
1029     */

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 JavaDoc getEditParameter(int row, int col) {
1048        return "e" + row + ":" + col;
1049    }
1050
1051    public String JavaDoc getToggleSelectionParameter(int row, int col) {
1052        return "t" + row + ":" + col;
1053    }
1054
1055    public String JavaDoc getSelectionParameter(int row, int col) {
1056        return "s" + row + ":" + col;
1057    }
1058
1059    public String JavaDoc getDeselectionParameter(int row, int col) {
1060        return "d" + row + ":" + col;
1061    }
1062
1063    /**
1064     * Returns the maximum size of this table.
1065     *
1066     * @return maximum size
1067     */

1068    public Rectangle getScrollableViewportSize() {
1069        return new Rectangle(0, 0, getColumnCount(), getRowCount());
1070    }
1071
1072    /*
1073     * Setzt den anzuzeigenden Teil
1074     */

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    /**
1091     * if selection changes, we have to reload code...
1092     */

1093    protected final ListSelectionListener JavaDoc reloadOnSelectionChangeListener =
1094            new ListSelectionListener JavaDoc() {
1095                public void valueChanged(ListSelectionEvent JavaDoc e) {
1096                    reload();
1097                }
1098            };
1099
1100    public void setSelectedRow(int selectedIndex) {
1101        getSelectionModel().setSelectionInterval(selectedIndex, selectedIndex);
1102    }
1103}
1104
1105
1106
Popular Tags