KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > table > renderer > LinkCellRenderer


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.components.table.renderer;
6
7 import com.opensymphony.webwork.components.table.WebTable;
8
9
10 /**
11  * @author $author$
12  * @version $Revision: 1.1 $
13  */

14 public class LinkCellRenderer extends AbstractCellRenderer {
15     //~ Instance fields ////////////////////////////////////////////////////////
16

17     /**
18      * this is the actual renderer tha will be used to display the text
19      */

20     protected CellRenderer _delegateRenderer = new DefaultCellRenderer();
21
22     /**
23      * the CSS class this link belongs to. Optional
24      */

25     protected String JavaDoc _cssClass = null;
26
27     /**
28      * the id attribute this link belongs to. Optional
29      */

30     protected String JavaDoc _cssId = null;
31
32     /**
33      * this is the link we are setting (required)
34      */

35     protected String JavaDoc _link = null;
36
37     /**
38      * the (Java)script/ function to execute when the link is clicked. Optional
39      */

40     protected String JavaDoc _onclick = null;
41
42     /**
43      * the (Java)script/ function to execute when the link is clicked twice. Optional
44      */

45     protected String JavaDoc _ondblclick = null;
46
47     /**
48      * the (Java)script/ function to execute when cursor is away from the link. Optional
49      */

50     protected String JavaDoc _onmouseout = null;
51
52     /**
53      * the (Java)script/ function to execute when cursor is over the link. Optional
54      */

55     protected String JavaDoc _onmouseover = null;
56
57     /**
58      * if set there will be a parameter attached to link. (optional)
59      * This should be extended to allow multiple parameters
60      */

61     protected String JavaDoc _param = null;
62
63     /**
64      * directly set the value for the param. Will overide paramColumn if set.
65      * optional. Either this or paramColumn must be set if param is used.
66      * Will be ignored if param not used
67      */

68     protected String JavaDoc _paramValue = null;
69
70     /**
71      * the target frame to open in. Optional
72      */

73     protected String JavaDoc _target = null;
74
75     /**
76      * the title attribute this link belongs to. Optional
77      */

78     protected String JavaDoc _title = null;
79
80     /**
81      * additional parameters after the above parameter is generated. Optional
82      */

83     protected String JavaDoc _trailParams = null;
84
85     /**
86      * if used the param value will be taken from another column in the table. Useful if each row
87      * needs a different paramter. The paramter can be taken from a hidden cell.
88      * if paramValue is also set it will overrid this. (option either this or paramValue must be set
89      * if param is used. Will be ignored if param not used
90      */

91     protected int _paramColumn = -1;
92
93     //~ Constructors ///////////////////////////////////////////////////////////
94

95     public LinkCellRenderer() {
96     }
97
98     //~ Methods ////////////////////////////////////////////////////////////////
99

100     /**
101      * should the link data be encodeed?
102      */

103     public String JavaDoc getCellValue(WebTable table, Object JavaDoc data, int row, int col) {
104         String JavaDoc value = _delegateRenderer.renderCell(table, data, row, col);
105
106         StringBuffer JavaDoc cell = new StringBuffer JavaDoc(256);
107         cell.append("<a HREF='").append(_link);
108
109         if (_param != null) {
110             cell.append("?").append(_param).append("=");
111
112             if (_paramValue != null) {
113                 cell.append(_paramValue);
114             } else if (_paramColumn >= 0) {
115                 cell.append(table.getModel().getValueAt(row, _paramColumn).toString());
116             }
117         }
118
119         if ((_trailParams != null) && !"".equals(_trailParams)) {
120             if (_param == null) {
121                 cell.append("?");
122             } else {
123                 cell.append("&");
124             }
125
126             cell.append(_trailParams);
127         }
128
129         cell.append("'");
130
131         if ((_target != null) && (!"".equals(_target))) {
132             cell.append(" target='").append(_target).append("'");
133         }
134
135         if ((_cssClass != null) && (!"".equals(_cssClass))) {
136             cell.append(" class='").append(_cssClass).append("'");
137         }
138
139         if ((_cssId != null) && (!"".equals(_cssId))) {
140             cell.append(" id='").append(_cssId).append("'");
141         }
142
143         if ((_title != null) && (!"".equals(_title))) {
144             cell.append(" title='").append(_title).append("'");
145         }
146
147         if ((_onclick != null) && (!"".equals(_onclick))) {
148             cell.append(" onclick='").append(_onclick).append("'");
149         }
150
151         if ((_ondblclick != null) && (!"".equals(_ondblclick))) {
152             cell.append(" ondblclick='").append(_ondblclick).append("'");
153         }
154
155         if ((_onmouseover != null) && (!"".equals(_onmouseover))) {
156             cell.append(" onmouseover='").append(_onmouseover).append("'");
157         }
158
159         if ((_onmouseout != null) && (!"".equals(_onmouseout))) {
160             cell.append(" onmouseout='").append(_onmouseout).append("'");
161         }
162
163         cell.append(">").append(value).append("</a>");
164
165         return cell.toString();
166     }
167
168     public void setCssClass(String JavaDoc cssClass) {
169         _cssClass = cssClass;
170     }
171
172     public void setCssId(String JavaDoc cssId) {
173         _cssId = cssId;
174     }
175
176     public void setLink(String JavaDoc link) {
177         _link = link;
178     }
179
180     public void setOnclick(String JavaDoc onclick) {
181         _onclick = onclick;
182     }
183
184     public void setOndblclick(String JavaDoc ondblclick) {
185         _ondblclick = ondblclick;
186     }
187
188     public void setOnmouseout(String JavaDoc onmouseout) {
189         _onmouseout = onmouseout;
190     }
191
192     public void setOnmouseover(String JavaDoc onmouseover) {
193         _onmouseover = onmouseover;
194     }
195
196     public void setParam(String JavaDoc param) {
197         _param = param;
198     }
199
200     public void setParamColumn(int paramColumn) {
201         _paramColumn = paramColumn;
202     }
203
204     public void setParamValue(String JavaDoc paramValue) {
205         _paramValue = paramValue;
206     }
207
208     /**
209      * used to set the renderer to delgate to.
210      * if the render is an AbstractCellRenderer then it will take the alignment from
211      * the delegate renderer and set it that way.
212      */

213     public void setRenderer(CellRenderer delegateRenderer) {
214         _delegateRenderer = delegateRenderer;
215
216         if (_delegateRenderer instanceof AbstractCellRenderer) {
217             setAlignment(((AbstractCellRenderer) _delegateRenderer).getAlignment());
218         }
219     }
220
221     public void setTarget(String JavaDoc target) {
222         _target = target;
223     }
224
225     public void setTitle(String JavaDoc title) {
226         _title = title;
227     }
228
229     public void setTrailParams(String JavaDoc trailParams) {
230         _trailParams = trailParams;
231     }
232 }
233
Popular Tags