KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > api > rendering > CellModel


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.databinding.datagrid.api.rendering;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23
24 import org.apache.beehive.netui.tags.html.FormatTag.Formatter;
25 import org.apache.beehive.netui.util.logging.Logger;
26 import org.apache.beehive.netui.util.Bundle;
27
28 /**
29  * <p>
30  * The CellModel is the base class for JavaBean objects that are used to configure the rendering of a CellDecorator.
31  * A CellModel exposes primitive services and state that can be used by {@link CellDecorator}s during rendering.
32  * </p>
33  */

34 public class CellModel {
35
36     private static final Logger LOGGER = Logger.getInstance(CellModel.class);
37
38     private DataGridTagModel _dataGridTagModel;
39     private ArrayList JavaDoc/*<Formatter>*/ _formatters;
40
41     /**
42      * Get the {@link DataGridTagModel} which is associated with the data grid tag that contains this
43      * cell.
44      *
45      * @return the {@link DataGridTagModel} for this cell. Inside a valid data grid, this method should
46      * not return <code>null</code>.
47      */

48     public DataGridTagModel getDataGridTagModel() {
49         return _dataGridTagModel;
50     }
51
52     /**
53      * Set the {@link DataGridTagModel} for this cell.
54      *
55      * @param dataGridTagModel the new {@link DataGridTagModel} value.
56      */

57     public void setDataGridTagModel(DataGridTagModel dataGridTagModel) {
58         _dataGridTagModel = dataGridTagModel;
59     }
60
61     /**
62      * Add a {@link Formatter} which can be used to format an Object for rendering. Many
63      * {@link Formatter} instances can be registered and will be executed in the order in
64      * which they were added. This method is provided as a service to CellModel subclasses;
65      * the use of formatters can vary based on the implementation of a {@link CellDecorator}.
66      *
67      * @param formatter the {@link Formatter} to add
68      */

69     public void addFormatter(Formatter formatter) {
70         if(_formatters == null)
71             _formatters = new ArrayList JavaDoc/*<Formatter>*/();
72
73         _formatters.add(formatter);
74     }
75
76     /**
77      * Format an {@link Object} value. This method can be called by subclasses or by {@link CellDecorator} instances
78      * which need to format data before rendering.
79      *
80      * @param value the {@link Object} to format
81      * @return If the <code>value</code> is null, return <code>null</code>. If there are no registered
82      * {@link Formatter} instances, return {@link Object#toString()} for the <code>value</code> parameter.
83      * Otherwisee, return the formatted value.
84      */

85     public String JavaDoc formatText(Object JavaDoc value) {
86         if(value == null)
87             return null;
88
89         if(_formatters == null)
90             return value.toString();
91
92         Object JavaDoc formatted = value;
93         for(int i = 0; i < _formatters.size(); i++) {
94             assert _formatters.get(i) instanceof Formatter :
95                     "Found invalid formatter type \"" +
96                     (_formatters.get(i) != null ? _formatters.get(i).getClass().getName() : "null") + "\"";
97
98             Formatter formatter = (Formatter)_formatters.get(i);
99             assert formatter != null;
100             try {
101                 formatted = formatter.format(formatted);
102             }
103             catch(JspException JavaDoc e) {
104                 /* todo: v1 -- error reporting */
105                 LOGGER.error(Bundle.getErrorString("CellModel_FormatterThrewException", new Object JavaDoc[]{formatter.getClass().getName(), e}), e);
106             }
107         }
108
109         assert formatted != null;
110         return formatted.toString();
111     }
112 }
113
Popular Tags