KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > Table


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app;
31
32 import java.util.EventListener JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import nextapp.echo2.app.event.ActionEvent;
37 import nextapp.echo2.app.event.ActionListener;
38 import nextapp.echo2.app.event.ChangeEvent;
39 import nextapp.echo2.app.event.ChangeListener;
40 import nextapp.echo2.app.event.TableColumnModelEvent;
41 import nextapp.echo2.app.event.TableColumnModelListener;
42 import nextapp.echo2.app.event.TableModelEvent;
43 import nextapp.echo2.app.event.TableModelListener;
44 import nextapp.echo2.app.list.DefaultListSelectionModel;
45 import nextapp.echo2.app.list.ListSelectionModel;
46 import nextapp.echo2.app.table.DefaultTableCellRenderer;
47 import nextapp.echo2.app.table.DefaultTableColumnModel;
48 import nextapp.echo2.app.table.DefaultTableModel;
49 import nextapp.echo2.app.table.TableCellRenderer;
50 import nextapp.echo2.app.table.TableColumn;
51 import nextapp.echo2.app.table.TableColumnModel;
52 import nextapp.echo2.app.table.TableModel;
53
54 /**
55  * A component used to display data in a tabular format.
56  *
57  * @see nextapp.echo2.app.table
58  */

59 public class Table extends Component {
60     
61     /**
62      * The default renderer for table cells.
63      */

64     public static final TableCellRenderer DEFAULT_TABLE_CELL_RENDERER = new DefaultTableCellRenderer();
65
66     public static final String JavaDoc PROPERTY_ACTION_COMMAND = "actionCommand";
67     public static final String JavaDoc PROPERTY_BORDER = "border";
68     public static final String JavaDoc PROPERTY_INSETS = "insets";
69     public static final String JavaDoc PROPERTY_ROLLOVER_BACKGROUND = "rolloverBackground";
70     public static final String JavaDoc PROPERTY_ROLLOVER_BACKGROUND_IMAGE = "rolloverBackgroundImage";
71     public static final String JavaDoc PROPERTY_ROLLOVER_ENABLED = "rolloverEnabled";
72     public static final String JavaDoc PROPERTY_ROLLOVER_FONT = "rolloverFont";
73     public static final String JavaDoc PROPERTY_ROLLOVER_FOREGROUND = "rolloverForeground";
74     public static final String JavaDoc PROPERTY_SELECTION_BACKGROUND = "selectionBackground";
75     public static final String JavaDoc PROPERTY_SELECTION_BACKGROUND_IMAGE = "selectionBackgroundImage";
76     public static final String JavaDoc PROPERTY_SELECTION_ENABLED = "selectionEnabled";
77     public static final String JavaDoc PROPERTY_SELECTION_FONT = "selectionFont";
78     public static final String JavaDoc PROPERTY_SELECTION_FOREGROUND= "selectionForeground";
79     public static final String JavaDoc PROPERTY_WIDTH = "width";
80     
81     public static final String JavaDoc INPUT_ACTION = "action";
82
83     public static final String JavaDoc ACTION_LISTENERS_CHANGED_PROPERTY = "actionListeners";
84     public static final String JavaDoc AUTO_CREATE_COLUMNS_FROM_MODEL_CHANGED_PROPERTY = "autoCreateColumnsFromModel";
85     public static final String JavaDoc COLUMN_MODEL_CHANGED_PROPERTY = "columnModel";
86     public static final String JavaDoc DEFAULT_HEADER_RENDERER_CHANGED_PROPERTY = "defaultHeaderRenderer";
87     public static final String JavaDoc DEFAULT_RENDERER_CHANGED_PROPERTY = "defaultRenderer";
88     public static final String JavaDoc HEADER_VISIBLE_CHANGED_PROPERTY = "headerVisible";
89     public static final String JavaDoc MODEL_CHANGED_PROPERTY = "model";
90     public static final String JavaDoc SELECTION_CHANGED_PROPERTY = "selection";
91     public static final String JavaDoc SELECTION_MODEL_CHANGED_PROPERTY = "selectionModel";
92     
93     public static final int HEADER_ROW = -1;
94     
95     private boolean autoCreateColumnsFromModel;
96     private boolean headerVisible = true;
97     private TableModel model;
98     private TableColumnModel columnModel;
99     private boolean valid;
100     private Map JavaDoc defaultRendererMap = new HashMap JavaDoc();
101     private TableCellRenderer defaultHeaderRenderer;
102     private ListSelectionModel selectionModel;
103     private boolean suppressChangeNotifications;
104     
105     /**
106      * Listener to monitor changes to model.
107      */

108     private TableModelListener modelListener = new TableModelListener() {
109         
110         /**
111          * @see nextapp.echo2.app.event.TableModelListener#tableChanged(nextapp.echo2.app.event.TableModelEvent)
112          */

113         public void tableChanged(TableModelEvent e) {
114             invalidate();
115             if ((e == null || e.getType() == TableModelEvent.STRUCTURE_CHANGED) && isAutoCreateColumnsFromModel()) {
116                 createDefaultColumnsFromModel();
117             }
118         }
119     };
120     
121     /**
122      * Listener to monitor changes to column model.
123      */

124     private TableColumnModelListener columnModelListener = new TableColumnModelListener() {
125
126         /**
127          * @see nextapp.echo2.app.event.TableColumnModelListener#columnAdded(nextapp.echo2.app.event.TableColumnModelEvent)
128          */

129         public void columnAdded(TableColumnModelEvent e) {
130             invalidate();
131         }
132
133         /**
134          * @see nextapp.echo2.app.event.TableColumnModelListener#columnMoved(nextapp.echo2.app.event.TableColumnModelEvent)
135          */

136         public void columnMoved(TableColumnModelEvent e) {
137             invalidate();
138         }
139
140         /**
141          * @see nextapp.echo2.app.event.TableColumnModelListener#columnRemoved(nextapp.echo2.app.event.TableColumnModelEvent)
142          */

143         public void columnRemoved(TableColumnModelEvent e) {
144             invalidate();
145         }
146     };
147
148     /**
149      * Local handler for list selection events.
150      */

151     private ChangeListener changeHandler = new ChangeListener() {
152         
153         /**
154          * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
155          */

156         public void stateChanged(ChangeEvent e) {
157             if (!suppressChangeNotifications) {
158                 firePropertyChange(SELECTION_CHANGED_PROPERTY, null, null);
159             }
160         }
161     };
162     
163     /**
164      * Creates a new <code>Table</code> with an empty
165      * <code>DefaultTableModel</code>.
166      */

167     public Table() {
168         this(new DefaultTableModel());
169     }
170     
171     /**
172      * Creates a new <code>Table</code> with a new
173      * <code>DefaultTableModel</code> with the specified dimensions.
174      *
175      * @param columns the initial column count
176      * @param rows the initial row count
177      */

178     public Table(int columns, int rows) {
179         this(new DefaultTableModel(columns, rows));
180     }
181     
182     /**
183      * Creates a <code>Table</code> using the supplied
184      * <code>TableModel</code>.
185      *
186      * @param model the initial model
187      */

188     public Table(TableModel model) {
189         this(model, null);
190     }
191     
192     /**
193      * Creates a <code>Table</code> with the supplied
194      * <code>TableModel</code> and the specified <code>TableColumnModel</code>.
195      *
196      * @param model the initial model
197      * @param columnModel the initial column model
198      */

199     public Table(TableModel model, TableColumnModel columnModel) {
200         super();
201
202         if (columnModel == null) {
203             setColumnModel(new DefaultTableColumnModel());
204             setAutoCreateColumnsFromModel(true);
205         } else {
206             setColumnModel(columnModel);
207         }
208         setSelectionModel(new DefaultListSelectionModel());
209         setModel(model);
210     }
211     
212     /**
213      * Returns the action command which will be provided in
214      * <code>ActionEvent</code>s fired by this
215      * <code>Table</code>.
216      *
217      * @return the action command
218      */

219     public String JavaDoc getActionCommand() {
220         return (String JavaDoc) getProperty(PROPERTY_ACTION_COMMAND);
221     }
222     
223     /**
224      * Adds an <code>ActionListener</code> to the <code>Table</code>.
225      * <code>ActionListener</code>s will be invoked when the user
226      * selects a row.
227      *
228      * @param l the <code>ActionListener</code> to add
229      */

230     public void addActionListener(ActionListener l) {
231         getEventListenerList().addListener(ActionListener.class, l);
232         // Notification of action listener changes is provided due to
233
// existence of hasActionListeners() method.
234
firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, null, l);
235     }
236
237     /**
238      * Creates a <code>TableColumnModel</code> based on the
239      * <code>TableModel</code>. This method is invoked automatically when the
240      * <code>TableModel</code>'s structure changes if the
241      * <code>autoCreateColumnsFromModel</code> flag is set.
242      */

243     public void createDefaultColumnsFromModel() {
244         if (model != null) {
245             while (columnModel.getColumnCount() > 0) {
246                 columnModel.removeColumn(columnModel.getColumn(0));
247             }
248             
249             int columnCount = model.getColumnCount();
250             for (int index = 0; index < columnCount; ++index) {
251                 columnModel.addColumn(new TableColumn(index));
252             }
253         }
254     }
255
256     /**
257      * Re-renders changed rows.
258      */

259     protected void doRender() {
260         removeAll();
261         int rowCount = model.getRowCount();
262         int columnCount = columnModel.getColumnCount();
263         
264         TableColumn[] tableColumns = new TableColumn[columnCount];
265         TableCellRenderer[] columnRenderers = new TableCellRenderer[columnCount];
266         
267         for (int columnIndex = 0; columnIndex < columnCount; ++columnIndex) {
268             tableColumns[columnIndex] = columnModel.getColumn(columnIndex);
269             
270             TableCellRenderer renderer = tableColumns[columnIndex].getCellRenderer();
271             if (renderer == null) {
272                 Class JavaDoc columnClass = model.getColumnClass(tableColumns[columnIndex].getModelIndex());
273                 renderer = getDefaultRenderer(columnClass);
274                 if (renderer == null) {
275                     renderer = DEFAULT_TABLE_CELL_RENDERER;
276                 }
277             }
278             columnRenderers[columnIndex] = renderer;
279
280         }
281
282         if (isHeaderVisible()) {
283             for (int columnIndex = 0; columnIndex < columnCount; ++columnIndex) {
284                 int modelColumnIndex = tableColumns[columnIndex].getModelIndex();
285                 Object JavaDoc headerValue = tableColumns[columnIndex].getHeaderValue();
286                 if (headerValue == null) {
287                     headerValue = model.getColumnName(modelColumnIndex);
288                 }
289                 TableCellRenderer headerRenderer = tableColumns[columnIndex].getHeaderRenderer();
290                 if (headerRenderer == null) {
291                     headerRenderer = defaultHeaderRenderer;
292                     if (headerRenderer == null) {
293                         headerRenderer = DEFAULT_TABLE_CELL_RENDERER;
294                     }
295                 }
296                 Component renderedComponent
297                         = headerRenderer.getTableCellRendererComponent(this, headerValue, modelColumnIndex, HEADER_ROW);
298                 if (renderedComponent == null) {
299                     renderedComponent = new Label();
300                 }
301                 add(renderedComponent);
302             }
303         }
304         
305         for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
306             for (int columnIndex = 0; columnIndex < columnCount; ++columnIndex) {
307                 int modelColumnIndex = tableColumns[columnIndex].getModelIndex();
308                 Object JavaDoc modelValue = model.getValueAt(modelColumnIndex, rowIndex);
309                 Component renderedComponent
310                         = columnRenderers[columnIndex].getTableCellRendererComponent(this, modelValue, modelColumnIndex, rowIndex);
311                 if (renderedComponent == null) {
312                     renderedComponent = new Label();
313                 }
314                 add(renderedComponent);
315             }
316         }
317     }
318     
319     /**
320      * Fires an action event to all listeners.
321      */

322     private void fireActionEvent() {
323         if (!hasEventListenerList()) {
324             return;
325         }
326         EventListener JavaDoc[] listeners = getEventListenerList().getListeners(ActionListener.class);
327         ActionEvent e = null;
328         for (int i = 0; i < listeners.length; ++i) {
329             if (e == null) {
330                 e = new ActionEvent(this, (String JavaDoc) getRenderProperty(PROPERTY_ACTION_COMMAND));
331             }
332             ((ActionListener) listeners[i]).actionPerformed(e);
333         }
334     }
335     
336     /**
337      * Returns the <code>Border</code>.
338      *
339      * @return the border
340      */

341     public Border getBorder() {
342         return (Border) getProperty(PROPERTY_BORDER);
343     }
344
345     /**
346      * Returns the component rendered at the specified cell position.
347      * Invocation will automatically perform validation if required.
348      *
349      * @param column the column
350      * @param row the row
351      * @return the component
352      */

353     public Component getCellComponent(int column, int row) {
354         if (!valid) {
355             validate();
356         }
357         if (isHeaderVisible()) {
358             return getComponent((row + 1) * columnModel.getColumnCount() + column);
359         } else {
360             if (row == HEADER_ROW) {
361                 return null;
362             } else {
363                 return getComponent(row * columnModel.getColumnCount() + column);
364             }
365         }
366     }
367     
368     /**
369      * Returns the <code>TableColumnModel</code> describing this table's
370      * columns.
371      *
372      * @return the column model
373      */

374     public TableColumnModel getColumnModel() {
375         return columnModel;
376     }
377     
378     /**
379      * Returns the default <code>TableCellRenderer</code> used to render
380      * header cells. The default header renderer will be used in the event
381      * that a <code>TableColumn</code> does not provide a specific header
382      * renderer.
383      *
384      * @return the <code>TableCellRenderer</code>
385      */

386     public TableCellRenderer getDefaultHeaderRenderer() {
387         return defaultHeaderRenderer;
388     }
389     
390     /**
391      * Returns the default <code>TableCellRenderer</code> for the specified
392      * column class. The default renderer will be used in the event that
393      * a <code>TableColumn</code> does not provide a specific renderer.
394      *
395      * @param columnClass the column <code>Class</code>
396      * @return the <code>TableCellRenderer</code>
397      */

398     public TableCellRenderer getDefaultRenderer(Class JavaDoc columnClass) {
399         return (TableCellRenderer) defaultRendererMap.get(columnClass);
400     }
401     
402     /**
403      * Returns the default cell insets.
404      *
405      * @return the default cell insets
406      */

407     public Insets getInsets() {
408         return (Insets) getProperty(PROPERTY_INSETS);
409     }
410     
411     /**
412      * Returns the <code>TableModel</code> being visualized by this
413      * <code>Table</code>.
414      *
415      * @return the model
416      */

417     public TableModel getModel() {
418         return model;
419     }
420     
421     /**
422      * Return the rollover background color displayed when the mouse is within
423      * the bounds of a row.
424      *
425      * @return the color
426      */

427     public Color getRolloverBackground() {
428         return (Color) getProperty(PROPERTY_ROLLOVER_BACKGROUND);
429     }
430
431     /**
432      * Return the rollover background image displayed when the mouse is within
433      * the bounds of a row.
434      *
435      * @return the background image
436      */

437     public FillImage getRolloverBackgroundImage() {
438         return (FillImage) getProperty(PROPERTY_ROLLOVER_BACKGROUND_IMAGE);
439     }
440
441     /**
442      * Return the rollover font displayed when the mouse is within
443      * the bounds of a row.
444      *
445      * @return the font
446      */

447     public Font getRolloverFont() {
448         return (Font) getProperty(PROPERTY_ROLLOVER_FONT);
449     }
450
451     /**
452      * Return the rollover foreground color displayed when the mouse is within
453      * the bounds of a row.
454      *
455      * @return the color
456      */

457     public Color getRolloverForeground() {
458         return (Color) getProperty(PROPERTY_ROLLOVER_FOREGROUND);
459     }
460
461     /**
462      * Returns the row selection background color.
463      *
464      * @return the background color
465      */

466     public Color getSelectionBackground() {
467         return (Color) getProperty(PROPERTY_SELECTION_BACKGROUND);
468     }
469
470     /**
471      * Returns the row selection background image.
472      *
473      * @return the background image
474      */

475     public FillImage getSelectionBackgroundImage() {
476         return (FillImage) getProperty(PROPERTY_SELECTION_BACKGROUND_IMAGE);
477     }
478     
479     /**
480      * Returns the row selection font.
481      *
482      * @return the font
483      */

484     public Font getSelectionFont() {
485         return (Font) getProperty(PROPERTY_SELECTION_FONT);
486     }
487     
488     /**
489      * Returns the row selection foreground color.
490      *
491      * @return the foreground color
492      */

493     public Color getSelectionForeground() {
494         return (Color) getProperty(PROPERTY_SELECTION_FOREGROUND);
495     }
496     
497     /**
498      * Returns the row selection model.
499      *
500      * @return the selection model
501      */

502     public ListSelectionModel getSelectionModel() {
503         return selectionModel;
504     }
505     
506     /**
507      * Returns the overall width of the grid.
508      * This property supports <code>Extent</code>s with
509      * fixed or percentile units.
510      *
511      * @return the width
512      */

513     public Extent getWidth() {
514         return (Extent) getProperty(PROPERTY_WIDTH);
515     }
516     
517     /**
518      * Determines the any <code>ActionListener</code>s are registered.
519      *
520      * @return true if any action listeners are registered
521      */

522     public boolean hasActionListeners() {
523         return getEventListenerList().getListenerCount(ActionListener.class) != 0;
524     }
525
526     /**
527      * Marks the table as needing to be re-rendered.
528      */

529     protected void invalidate() {
530         valid = false;
531     }
532     
533     /**
534      * Determines whether the <code>TableColumnModel</code> will be created
535      * automatically from the <code>TableModel</code>. If this flag is set,
536      * changes to the <code>TableModel</code> will automatically cause the
537      * <code>TableColumnModel</code> to be re-created. This flag is true
538      * by default unless a <code>TableColumnModel</code> is specified in the
539      * constructor.
540      *
541      * @return true if the <code>TableColumnModel</code> will be created
542      * automatically from the <code>TableModel</code>
543      */

544     public boolean isAutoCreateColumnsFromModel() {
545         return autoCreateColumnsFromModel;
546     }
547
548     /**
549      * Determines if the table header is visible.
550      *
551      * @return the header visibility state
552      */

553     public boolean isHeaderVisible() {
554         return headerVisible;
555     }
556     
557     /**
558      * Determines if rollover effects are enabled.
559      *
560      * @return true if rollover effects are enabled
561      * @see #setRolloverEnabled(boolean)
562      */

563     public boolean isRolloverEnabled() {
564         Boolean JavaDoc value = (Boolean JavaDoc) getProperty(PROPERTY_ROLLOVER_ENABLED);
565         return value == null ? false : value.booleanValue();
566     }
567
568     /**
569      * Determines if selection is enabled.
570      *
571      * @return true if selection is enabled
572      */

573     public boolean isSelectionEnabled() {
574         Boolean JavaDoc value = (Boolean JavaDoc) getProperty(PROPERTY_SELECTION_ENABLED);
575         return value == null ? false : value.booleanValue();
576     }
577     
578     /**
579      * @see nextapp.echo2.app.Component#processInput(java.lang.String, java.lang.Object)
580      */

581     public void processInput(String JavaDoc inputName, Object JavaDoc inputValue) {
582         super.processInput(inputName, inputValue);
583         if (inputName.equals(SELECTION_CHANGED_PROPERTY)) {
584             setSelectedIndices((int[]) inputValue);
585         } else if (INPUT_ACTION.equals(inputName)) {
586             fireActionEvent();
587         }
588     }
589     
590     /**
591      * Removes an <code>ActionListener</code> from the <code>Table</code>.
592      * <code>ActionListener</code>s will be invoked when the user
593      * selects a row.
594      *
595      * @param l the <code>ActionListener</code> to remove
596      */

597     public void removeActionListener(ActionListener l) {
598         if (!hasEventListenerList()) {
599             return;
600         }
601         getEventListenerList().removeListener(ActionListener.class, l);
602         // Notification of action listener changes is provided due to
603
// existence of hasActionListeners() method.
604
firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, l, null);
605     }
606     
607     /**
608      * Sets the action command which will be provided in
609      * <code>ActionEvent</code>s fired by this
610      * <code>Table</code>.
611      *
612      * @param newValue the new action command
613      */

614     public void setActionCommand(String JavaDoc newValue) {
615         setProperty(PROPERTY_ACTION_COMMAND, newValue);
616     }
617     
618     /**
619      * Sets whether the <code>TableColumnModel</code> will be created
620      * automatically from the <code>TableModel</code>.
621      *
622      * @param newValue true if the <code>TableColumnModel</code> should be
623      * created automatically from the <code>TableModel</code>
624      * @see #isAutoCreateColumnsFromModel()
625      */

626     public void setAutoCreateColumnsFromModel(boolean newValue) {
627         boolean oldValue = autoCreateColumnsFromModel;
628         autoCreateColumnsFromModel = newValue;
629         
630         if (!oldValue && newValue) {
631             createDefaultColumnsFromModel();
632         }
633         
634         firePropertyChange(AUTO_CREATE_COLUMNS_FROM_MODEL_CHANGED_PROPERTY,
635                 Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
636     }
637
638     /**
639      * Sets the <code>Border</code>.
640      *
641      * @param newValue the new border
642      */

643     public void setBorder(Border newValue) {
644         setProperty(PROPERTY_BORDER, newValue);
645     }
646     
647     /**
648      * Sets the <code>TableColumnModel</code> describing this table's
649      * columns.
650      *
651      * @param newValue the new column model
652      */

653     public void setColumnModel(TableColumnModel newValue) {
654         invalidate();
655         
656         if (newValue == null) {
657             throw new IllegalArgumentException JavaDoc("The model may not be null.");
658         }
659         
660         TableColumnModel oldValue = columnModel;
661         if (oldValue != null) {
662             oldValue.removeColumnModelListener(columnModelListener);
663         }
664         columnModel = newValue;
665         newValue.addColumnModelListener(columnModelListener);
666         firePropertyChange(COLUMN_MODEL_CHANGED_PROPERTY, oldValue, newValue);
667     }
668     
669     /**
670      * Sets the default <code>TableCellRenderer</code> used to render
671      * header cells. The default header renderer will be used in the event
672      * that a <code>TableColumn</code> does not provide a specific header
673      * renderer.
674      *
675      * @param newValue the <code>TableCellRenderer</code>
676      */

677     public void setDefaultHeaderRenderer(TableCellRenderer newValue) {
678         invalidate();
679         TableCellRenderer oldValue = defaultHeaderRenderer;
680         defaultHeaderRenderer = newValue;
681         firePropertyChange(DEFAULT_HEADER_RENDERER_CHANGED_PROPERTY, oldValue, newValue);
682     }
683
684     /**
685      * Sets the default <code>TableCellRenderer</code> for the specified
686      * column class. The default renderer will be used in the event that
687      * a <code>TableColumn</code> does not provide a specific renderer.
688      *
689      * @param columnClass the column <code>Class</code>
690      * @param newValue the <code>TableCellRenderer</code>
691      */

692     public void setDefaultRenderer(Class JavaDoc columnClass, TableCellRenderer newValue) {
693         invalidate();
694         if (newValue == null) {
695             defaultRendererMap.remove(columnClass);
696         } else {
697             defaultRendererMap.put(columnClass, newValue);
698         }
699         firePropertyChange(DEFAULT_RENDERER_CHANGED_PROPERTY, null, null);
700     }
701     
702     /**
703      * Sets the visibility state of the table header.
704      *
705      * @param newValue true if the header should be displayed
706      */

707     public void setHeaderVisible(boolean newValue) {
708         invalidate();
709         boolean oldValue = headerVisible;
710         headerVisible = newValue;
711         firePropertyChange(HEADER_VISIBLE_CHANGED_PROPERTY, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
712     }
713     
714     /**
715      * Sets the default cell insets.
716      *
717      * @param newValue the new default cell insets
718      */

719     public void setInsets(Insets newValue) {
720         setProperty(PROPERTY_INSETS, newValue);
721     }
722     
723     /**
724      * Sets the <code>TableModel</code> being visualized.
725      *
726      * @param newValue the new model (may not be null)
727      */

728     public void setModel(TableModel newValue) {
729         invalidate();
730         
731         if (newValue == null) {
732             throw new IllegalArgumentException JavaDoc("The model may not be null.");
733         }
734         
735         TableModel oldValue = model;
736         if (oldValue != null) {
737             oldValue.removeTableModelListener(modelListener);
738         }
739         model = newValue;
740         newValue.addTableModelListener(modelListener);
741         
742         if (isAutoCreateColumnsFromModel()) {
743             createDefaultColumnsFromModel();
744         }
745         
746         firePropertyChange(MODEL_CHANGED_PROPERTY, oldValue, newValue);
747     }
748     
749     /**
750      * Sets the rollover background color displayed when the mouse is within
751      * the bounds of a row.
752      *
753      * @param newValue the new <code>Color</code>
754      */

755     public void setRolloverBackground(Color newValue) {
756         setProperty(PROPERTY_ROLLOVER_BACKGROUND, newValue);
757     }
758
759     /**
760      * Sets the rollover background image displayed when the mouse is within
761      * the bounds of a row.
762      *
763      * @param newValue the new background image
764      */

765     public void setRolloverBackgroundImage(FillImage newValue) {
766         setProperty(PROPERTY_ROLLOVER_BACKGROUND_IMAGE, newValue);
767     }
768
769     /**
770      * Sets whether rollover effects are enabled when the mouse cursor is
771      * within the bounds of a row. Rollover properties have no effect unless
772      * this property is set to true. The default value is false.
773      *
774      * @param newValue true if rollover effects should be enabled
775      */

776     public void setRolloverEnabled(boolean newValue) {
777         setProperty(PROPERTY_ROLLOVER_ENABLED, new Boolean JavaDoc(newValue));
778     }
779
780     /**
781      * Sets the rollover font displayed when the mouse is within
782      * the bounds of a row.
783      *
784      * @param newValue the new <code>Font</code>
785      */

786     public void setRolloverFont(Font newValue) {
787         setProperty(PROPERTY_ROLLOVER_FONT, newValue);
788     }
789
790     /**
791      * Sets the rollover foreground color displayed when the mouse is within
792      * the bounds of a row.
793      *
794      * @param newValue the new <code>Color</code>
795      */

796     public void setRolloverForeground(Color newValue) {
797         setProperty(PROPERTY_ROLLOVER_FOREGROUND, newValue);
798     }
799
800     /**
801      * Selects only the specified row indices.
802      *
803      * @param selectedIndices the indices to select
804      */

805     private void setSelectedIndices(int[] selectedIndices) {
806         // Temporarily suppress the Tables selection event notifier.
807
suppressChangeNotifications = true;
808         ListSelectionModel selectionModel = getSelectionModel();
809         selectionModel.clearSelection();
810         for (int i = 0; i < selectedIndices.length; ++i) {
811             selectionModel.setSelectedIndex(selectedIndices[i], true);
812         }
813         // End temporary suppression.
814
suppressChangeNotifications = false;
815         firePropertyChange(SELECTION_CHANGED_PROPERTY, null, selectedIndices);
816     }
817
818     /**
819      * Sets the row selection background color.
820      *
821      * @param newValue the new background color
822      */

823     public void setSelectionBackground(Color newValue) {
824         setProperty(PROPERTY_SELECTION_BACKGROUND, newValue);
825     }
826     
827     /**
828      * Sets the row selection background image.
829      *
830      * @param newValue the new background image
831      */

832     public void setSelectionBackgroundImage(FillImage newValue) {
833         setProperty(PROPERTY_SELECTION_BACKGROUND_IMAGE, newValue);
834     }
835     
836     /**
837      * Sets whether selection is enabled.
838      *
839      * @param newValue true to enable selection
840      */

841     public void setSelectionEnabled(boolean newValue) {
842         setProperty(PROPERTY_SELECTION_ENABLED, Boolean.valueOf(newValue));
843     }
844
845     /**
846      * Sets the row selection foreground color.
847      *
848      * @param newValue the new foreground color
849      */

850     public void setSelectionForeground(Color newValue) {
851         setProperty(PROPERTY_SELECTION_FOREGROUND, newValue);
852     }
853     
854     /**
855      * Sets the row selection font.
856      *
857      * @param newValue the new font
858      */

859     public void setSelectionFont(Font newValue) {
860         setProperty(PROPERTY_SELECTION_FONT, newValue);
861     }
862     
863     /**
864      * Sets the row selection model.
865      * The selection model may not be null.
866      *
867      * @param newValue the new selection model
868      */

869     public void setSelectionModel(ListSelectionModel newValue) {
870         if (newValue == null) {
871             throw new IllegalArgumentException JavaDoc("Selection model may not be null.");
872         }
873         ListSelectionModel oldValue = selectionModel;
874         if (oldValue != null) {
875             oldValue.removeChangeListener(changeHandler);
876         }
877         newValue.addChangeListener(changeHandler);
878         selectionModel = newValue;
879         firePropertyChange(SELECTION_MODEL_CHANGED_PROPERTY, oldValue, newValue);
880     }
881     
882     /**
883      * Sets the overall width of the grid.
884      * This property supports <code>Extent</code>s with
885      * fixed or percentile units.
886      *
887      * @param newValue the new width
888      */

889     public void setWidth(Extent newValue) {
890         setProperty(PROPERTY_WIDTH, newValue);
891     }
892     
893     /**
894      * @see nextapp.echo2.app.Component#validate()
895      */

896     public void validate() {
897         super.validate();
898         while (!valid) {
899             valid = true;
900             doRender();
901         }
902     }
903 }
904
Popular Tags