View Javadoc

1   /*
2    * $Id: LinkCellRenderer.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.components.table.renderer;
22  
23  import org.apache.struts2.components.table.WebTable;
24  
25  
26  /***
27   */
28  public class LinkCellRenderer extends AbstractCellRenderer {
29  
30      /***
31       * this is the actual renderer tha will be used to display the text
32       */
33      protected CellRenderer _delegateRenderer = new DefaultCellRenderer();
34  
35      /***
36       * the CSS class this link belongs to. Optional
37       */
38      protected String _cssClass = null;
39  
40      /***
41       * the id attribute this link belongs to. Optional
42       */
43      protected String _cssId = null;
44  
45      /***
46       * this is the link we are setting (required)
47       */
48      protected String _link = null;
49  
50      /***
51       * the (Java)script/ function to execute when the link is clicked. Optional
52       */
53      protected String _onclick = null;
54  
55      /***
56       * the (Java)script/ function to execute when the link is clicked twice. Optional
57       */
58      protected String _ondblclick = null;
59  
60      /***
61       * the (Java)script/ function to execute when cursor is away from the link. Optional
62       */
63      protected String _onmouseout = null;
64  
65      /***
66       * the (Java)script/ function to execute when cursor is over the link. Optional
67       */
68      protected String _onmouseover = null;
69  
70      /***
71       * if set there will be a parameter attached to link.  (optional)
72       * This should be extended to allow multiple parameters
73       */
74      protected String _param = null;
75  
76      /***
77       * directly set the value for the param.  Will overide paramColumn if set.
78       * optional.  Either this or paramColumn must be set if param is used.
79       * Will be ignored if param not used
80       */
81      protected String _paramValue = null;
82  
83      /***
84       * the target frame to open in. Optional
85       */
86      protected String _target = null;
87  
88      /***
89       * the title attribute this link belongs to. Optional
90       */
91      protected String _title = null;
92  
93      /***
94       * additional parameters after the above parameter is generated. Optional
95       */
96      protected String _trailParams = null;
97  
98      /***
99       * if used the param value will be taken from another column in the table.  Useful if each row
100      * needs a different paramter.  The paramter can be taken from a hidden cell.
101      * if paramValue is also set it will overrid this.  (option either this or paramValue must be set
102      * if param is used. Will be ignored if param not used
103      */
104     protected int _paramColumn = -1;
105 
106 
107     public LinkCellRenderer() {
108     }
109 
110 
111     /***
112      * should the link data be encodeed?
113      */
114     public String getCellValue(WebTable table, Object data, int row, int col) {
115         String value = _delegateRenderer.renderCell(table, data, row, col);
116 
117         StringBuffer cell = new StringBuffer(256);
118         cell.append("<a href='").append(_link);
119 
120         if (_param != null) {
121             cell.append("?").append(_param).append("=");
122 
123             if (_paramValue != null) {
124                 cell.append(_paramValue);
125             } else if (_paramColumn >= 0) {
126                 cell.append(table.getModel().getValueAt(row, _paramColumn).toString());
127             }
128         }
129 
130         if ((_trailParams != null) && !"".equals(_trailParams)) {
131             if (_param == null) {
132                 cell.append("?");
133             } else {
134                 cell.append("&");
135             }
136 
137             cell.append(_trailParams);
138         }
139 
140         cell.append("'");
141 
142         if ((_target != null) && (!"".equals(_target))) {
143             cell.append(" target='").append(_target).append("'");
144         }
145 
146         if ((_cssClass != null) && (!"".equals(_cssClass))) {
147             cell.append(" class='").append(_cssClass).append("'");
148         }
149 
150         if ((_cssId != null) && (!"".equals(_cssId))) {
151             cell.append(" id='").append(_cssId).append("'");
152         }
153 
154         if ((_title != null) && (!"".equals(_title))) {
155             cell.append(" title='").append(_title).append("'");
156         }
157 
158         if ((_onclick != null) && (!"".equals(_onclick))) {
159             cell.append(" onclick='").append(_onclick).append("'");
160         }
161 
162         if ((_ondblclick != null) && (!"".equals(_ondblclick))) {
163             cell.append(" ondblclick='").append(_ondblclick).append("'");
164         }
165 
166         if ((_onmouseover != null) && (!"".equals(_onmouseover))) {
167             cell.append(" onmouseover='").append(_onmouseover).append("'");
168         }
169 
170         if ((_onmouseout != null) && (!"".equals(_onmouseout))) {
171             cell.append(" onmouseout='").append(_onmouseout).append("'");
172         }
173 
174         cell.append(">").append(value).append("</a>");
175 
176         return cell.toString();
177     }
178 
179     public void setCssClass(String cssClass) {
180         _cssClass = cssClass;
181     }
182 
183     public void setCssId(String cssId) {
184         _cssId = cssId;
185     }
186 
187     public void setLink(String link) {
188         _link = link;
189     }
190 
191     public void setOnclick(String onclick) {
192         _onclick = onclick;
193     }
194 
195     public void setOndblclick(String ondblclick) {
196         _ondblclick = ondblclick;
197     }
198 
199     public void setOnmouseout(String onmouseout) {
200         _onmouseout = onmouseout;
201     }
202 
203     public void setOnmouseover(String onmouseover) {
204         _onmouseover = onmouseover;
205     }
206 
207     public void setParam(String param) {
208         _param = param;
209     }
210 
211     public void setParamColumn(int paramColumn) {
212         _paramColumn = paramColumn;
213     }
214 
215     public void setParamValue(String paramValue) {
216         _paramValue = paramValue;
217     }
218 
219     /***
220      * used to set the renderer to delgate to.
221      * if the render is an AbstractCellRenderer then it will take the alignment from
222      * the delegate renderer and set it that way.
223      */
224     public void setRenderer(CellRenderer delegateRenderer) {
225         _delegateRenderer = delegateRenderer;
226 
227         if (_delegateRenderer instanceof AbstractCellRenderer) {
228             setAlignment(((AbstractCellRenderer) _delegateRenderer).getAlignment());
229         }
230     }
231 
232     public void setTarget(String target) {
233         _target = target;
234     }
235 
236     public void setTitle(String title) {
237         _title = title;
238     }
239 
240     public void setTrailParams(String trailParams) {
241         _trailParams = trailParams;
242     }
243 }