View Javadoc

1   /*
2    * $Id: AbstractCellRenderer.java 418521 2006-07-01 23:36:50Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components.table.renderer;
19  
20  import org.apache.struts2.components.table.WebTable;
21  
22  
23  /***
24   * this is the base class that most renderers will be derived from.
25   * It allows setting the alignment.  Subclasses should set there actuall
26   * content by implementing getCellValue
27   */
28  abstract public class AbstractCellRenderer implements CellRenderer {
29  
30      /***
31       * used for horizontal cell alignmnet
32       */
33      protected String _alignment = null;
34  
35  
36      public void setAlignment(String alignment) {
37          _alignment = alignment;
38      }
39  
40      public String getAlignment() {
41          return _alignment;
42      }
43  
44      /***
45       * implememnts CellRenderer renderCell.  It sets the alignment.  gets the actual
46       * data from getCellValue
47       */
48      public String renderCell(WebTable table, Object data, int row, int col) {
49          if (isAligned()) {
50              StringBuffer buf = new StringBuffer(256);
51              buf.append("<div align='").append(_alignment).append("'>");
52              buf.append(getCellValue(table, data, row, col));
53              buf.append("</div>");
54  
55              return buf.toString();
56          }
57  
58          return getCellValue(table, data, row, col);
59      }
60  
61      protected boolean isAligned() {
62          return _alignment != null;
63      }
64  
65      /***
66       * this is the method that subclasses need to implement to set their value.
67       * they should not override renderCell unless they want to change the alignmnent
68       * renderering
69       */
70      abstract protected String getCellValue(WebTable table, Object data, int row, int col);
71  }