001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.databinding.datagrid.api.rendering;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import javax.servlet.jsp.JspException;
024:
025: import org.apache.beehive.netui.tags.html.FormatTag.Formatter;
026: import org.apache.beehive.netui.util.logging.Logger;
027: import org.apache.beehive.netui.util.Bundle;
028:
029: /**
030: * <p>
031: * The CellModel is the base class for JavaBean objects that are used to configure the rendering of a CellDecorator.
032: * A CellModel exposes primitive services and state that can be used by {@link CellDecorator}s during rendering.
033: * </p>
034: */
035: public class CellModel {
036:
037: private static final Logger LOGGER = Logger
038: .getInstance(CellModel.class);
039:
040: private DataGridTagModel _dataGridTagModel;
041: private ArrayList/*<Formatter>*/_formatters;
042:
043: /**
044: * Get the {@link DataGridTagModel} which is associated with the data grid tag that contains this
045: * cell.
046: *
047: * @return the {@link DataGridTagModel} for this cell. Inside a valid data grid, this method should
048: * not return <code>null</code>.
049: */
050: public DataGridTagModel getDataGridTagModel() {
051: return _dataGridTagModel;
052: }
053:
054: /**
055: * Set the {@link DataGridTagModel} for this cell.
056: *
057: * @param dataGridTagModel the new {@link DataGridTagModel} value.
058: */
059: public void setDataGridTagModel(DataGridTagModel dataGridTagModel) {
060: _dataGridTagModel = dataGridTagModel;
061: }
062:
063: /**
064: * Add a {@link Formatter} which can be used to format an Object for rendering. Many
065: * {@link Formatter} instances can be registered and will be executed in the order in
066: * which they were added. This method is provided as a service to CellModel subclasses;
067: * the use of formatters can vary based on the implementation of a {@link CellDecorator}.
068: *
069: * @param formatter the {@link Formatter} to add
070: */
071: public void addFormatter(Formatter formatter) {
072: if (_formatters == null)
073: _formatters = new ArrayList/*<Formatter>*/();
074:
075: _formatters.add(formatter);
076: }
077:
078: /**
079: * Format an {@link Object} value. This method is used to apply a chain of formatters to some
080: * value. It will return <code>null</code> if the provided value is null; in this case, it
081: * is up to the caller to provide an appropriate default value.
082: *
083: * @param value the {@link Object} to format
084: * @return If the <code>value</code> is null, return <code>null</code>. If there are no registered
085: * {@link Formatter} instances, return {@link Object#toString()} for the <code>value</code> parameter.
086: * Otherwisee, return the formatted value.
087: */
088: public String formatText(Object value) {
089: if (value == null)
090: return null;
091:
092: if (_formatters == null)
093: return value.toString();
094:
095: Object formatted = value;
096: for (int i = 0; i < _formatters.size(); i++) {
097: assert _formatters.get(i) instanceof Formatter : "Found invalid formatter type \""
098: + (_formatters.get(i) != null ? _formatters.get(i)
099: .getClass().getName() : "null") + "\"";
100:
101: Formatter formatter = (Formatter) _formatters.get(i);
102: assert formatter != null;
103: try {
104: formatted = formatter.format(formatted);
105: } catch (JspException e) {
106: /* todo: error reporting */
107: LOGGER.error(Bundle.getErrorString(
108: "CellModel_FormatterThrewException",
109: new Object[] { formatter.getClass().getName(),
110: e }), e);
111: }
112: }
113:
114: assert formatted != null;
115: return formatted.toString();
116: }
117: }
|