KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > table > DefaultCellRenderer


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.table;
14
15 import org.w3c.dom.Element JavaDoc;
16
17 import com.tonbeller.wcf.controller.RequestContext;
18 import com.tonbeller.wcf.format.FormatException;
19 import com.tonbeller.wcf.format.FormatHandler;
20 import com.tonbeller.wcf.format.Formatter;
21 import com.tonbeller.wcf.utils.DomUtils;
22
23 /**
24  * a renderer that renders TableRow values using the Formatter instance for this request.
25  * @see com.tonbeller.xoplon.format.Formatter
26  */

27 public class DefaultCellRenderer implements CellRenderer {
28   Alignment alignment = Alignment.GUESS;
29   String JavaDoc format;
30   String JavaDoc type;
31   private static final String JavaDoc NBSP = "\u00a0";
32
33   public static class Alignment {
34     
35     /**
36      * alignment depends on content. Numbers are right-aligned,
37      * String left-aligned
38      */

39     public static final Alignment GUESS = new Alignment("");
40
41     /**
42      * Left alignment
43      */

44     public static final Alignment LEFT = new Alignment("left");
45     
46     /**
47      * Right alignment
48      */

49     public static final Alignment RIGHT = new Alignment("right");
50
51     private String JavaDoc attributeValue;
52     
53     private Alignment(String JavaDoc attributeValue) {
54       this.attributeValue = attributeValue;
55     }
56     
57     public String JavaDoc getAttributeValue() {
58       return attributeValue;
59     }
60   }
61   
62   public DefaultCellRenderer() {
63   }
64
65   public void render(RequestContext context, Element JavaDoc td, Object JavaDoc value) throws FormatException {
66     Element JavaDoc elem = td;
67     boolean needSpace = true;
68
69     if (value instanceof DefaultCell) {
70       DefaultCell hc = (DefaultCell)value;
71       value = hc.getValue();
72       if (hc.getURL() != null) {
73         elem = DomUtils.appendElement(elem, "a");
74         elem.setAttribute("href", hc.getURL());
75         if (hc.getTarget() != null)
76           elem.setAttribute("target", hc.getTarget());
77         if (hc.getOnClick() != null)
78           elem.setAttribute("onClick", hc.getOnClick());
79       }
80
81       if (hc.getImage() != null) {
82         Element JavaDoc img = DomUtils.appendElement(elem, "img");
83         img.setAttribute("border", "0");
84         img.setAttribute("src", hc.getImage());
85         DomUtils.appendText(elem, " ");
86         needSpace = false;
87       }
88     }
89
90     if (value != null) {
91       Formatter fmt = context.getFormatter();
92       FormatHandler fh = null;
93       if (type != null)
94         fh = fmt.getHandler(type);
95       if (fh == null)
96         fh = fmt.guessHandler(value);
97       String JavaDoc s;
98       if (fh != null)
99         s = fh.format(value, format);
100       else if (value != null)
101         s = value.toString();
102       else
103         s = null;
104       if (s == null || s.length() == 0 || s.trim().length()==0)
105         s = NBSP;
106       DomUtils.appendText(elem, s);
107       if(alignment == Alignment.GUESS) {
108         if (value instanceof Number JavaDoc)
109           td.setAttribute("align", "right");
110         else
111           td.setAttribute("align", "left");
112       }
113       else
114         td.setAttribute("align", alignment.getAttributeValue());
115       
116     }
117
118     else if (needSpace)
119       DomUtils.setText(elem, NBSP);
120   }
121
122   /**
123    * format string for formatter
124    * @see com.tonbeller.xoplon.format.FormatHandler
125    */

126   public void setFormat(String JavaDoc newFormat) {
127     format = newFormat;
128   }
129   /**
130    * format string for formatter
131    * @see com.tonbeller.xoplon.format.FormatHandler
132    */

133   public String JavaDoc getFormat() {
134     return format;
135   }
136   /**
137    * data type for formatter
138    * @see com.tonbeller.xoplon.format.FormatHandler
139    */

140   public void setType(String JavaDoc newType) {
141     type = newType;
142   }
143   /**
144    * data type for formatter
145    * @see com.tonbeller.xoplon.format.FormatHandler
146    */

147   public String JavaDoc getType() {
148     return type;
149   }
150
151   /**
152    * set column alignment
153    */

154   public void setAlignment(Alignment alignment) {
155     this.alignment = alignment;
156   }
157 }
Popular Tags