View Javadoc

1   /*
2    * $Id: LinkCellRenderer.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   */
25  public class LinkCellRenderer extends AbstractCellRenderer {
26  
27      /***
28       * this is the actual renderer tha will be used to display the text
29       */
30      protected CellRenderer _delegateRenderer = new DefaultCellRenderer();
31  
32      /***
33       * the CSS class this link belongs to. Optional
34       */
35      protected String _cssClass = null;
36  
37      /***
38       * the id attribute this link belongs to. Optional
39       */
40      protected String _cssId = null;
41  
42      /***
43       * this is the link we are setting (required)
44       */
45      protected String _link = null;
46  
47      /***
48       * the (Java)script/ function to execute when the link is clicked. Optional
49       */
50      protected String _onclick = null;
51  
52      /***
53       * the (Java)script/ function to execute when the link is clicked twice. Optional
54       */
55      protected String _ondblclick = null;
56  
57      /***
58       * the (Java)script/ function to execute when cursor is away from the link. Optional
59       */
60      protected String _onmouseout = null;
61  
62      /***
63       * the (Java)script/ function to execute when cursor is over the link. Optional
64       */
65      protected String _onmouseover = null;
66  
67      /***
68       * if set there will be a parameter attached to link.  (optional)
69       * This should be extended to allow multiple parameters
70       */
71      protected String _param = null;
72  
73      /***
74       * directly set the value for the param.  Will overide paramColumn if set.
75       * optional.  Either this or paramColumn must be set if param is used.
76       * Will be ignored if param not used
77       */
78      protected String _paramValue = null;
79  
80      /***
81       * the target frame to open in. Optional
82       */
83      protected String _target = null;
84  
85      /***
86       * the title attribute this link belongs to. Optional
87       */
88      protected String _title = null;
89  
90      /***
91       * additional parameters after the above parameter is generated. Optional
92       */
93      protected String _trailParams = null;
94  
95      /***
96       * if used the param value will be taken from another column in the table.  Useful if each row
97       * needs a different paramter.  The paramter can be taken from a hidden cell.
98       * if paramValue is also set it will overrid this.  (option either this or paramValue must be set
99       * if param is used. Will be ignored if param not used
100      */
101     protected int _paramColumn = -1;
102 
103 
104     public LinkCellRenderer() {
105     }
106 
107 
108     /***
109      * should the link data be encodeed?
110      */
111     public String getCellValue(WebTable table, Object data, int row, int col) {
112         String value = _delegateRenderer.renderCell(table, data, row, col);
113 
114         StringBuffer cell = new StringBuffer(256);
115         cell.append("<a href='").append(_link);
116 
117         if (_param != null) {
118             cell.append("?").append(_param).append("=");
119 
120             if (_paramValue != null) {
121                 cell.append(_paramValue);
122             } else if (_paramColumn >= 0) {
123                 cell.append(table.getModel().getValueAt(row, _paramColumn).toString());
124             }
125         }
126 
127         if ((_trailParams != null) && !"".equals(_trailParams)) {
128             if (_param == null) {
129                 cell.append("?");
130             } else {
131                 cell.append("&");
132             }
133 
134             cell.append(_trailParams);
135         }
136 
137         cell.append("'");
138 
139         if ((_target != null) && (!"".equals(_target))) {
140             cell.append(" target='").append(_target).append("'");
141         }
142 
143         if ((_cssClass != null) && (!"".equals(_cssClass))) {
144             cell.append(" class='").append(_cssClass).append("'");
145         }
146 
147         if ((_cssId != null) && (!"".equals(_cssId))) {
148             cell.append(" id='").append(_cssId).append("'");
149         }
150 
151         if ((_title != null) && (!"".equals(_title))) {
152             cell.append(" title='").append(_title).append("'");
153         }
154 
155         if ((_onclick != null) && (!"".equals(_onclick))) {
156             cell.append(" onclick='").append(_onclick).append("'");
157         }
158 
159         if ((_ondblclick != null) && (!"".equals(_ondblclick))) {
160             cell.append(" ondblclick='").append(_ondblclick).append("'");
161         }
162 
163         if ((_onmouseover != null) && (!"".equals(_onmouseover))) {
164             cell.append(" onmouseover='").append(_onmouseover).append("'");
165         }
166 
167         if ((_onmouseout != null) && (!"".equals(_onmouseout))) {
168             cell.append(" onmouseout='").append(_onmouseout).append("'");
169         }
170 
171         cell.append(">").append(value).append("</a>");
172 
173         return cell.toString();
174     }
175 
176     public void setCssClass(String cssClass) {
177         _cssClass = cssClass;
178     }
179 
180     public void setCssId(String cssId) {
181         _cssId = cssId;
182     }
183 
184     public void setLink(String link) {
185         _link = link;
186     }
187 
188     public void setOnclick(String onclick) {
189         _onclick = onclick;
190     }
191 
192     public void setOndblclick(String ondblclick) {
193         _ondblclick = ondblclick;
194     }
195 
196     public void setOnmouseout(String onmouseout) {
197         _onmouseout = onmouseout;
198     }
199 
200     public void setOnmouseover(String onmouseover) {
201         _onmouseover = onmouseover;
202     }
203 
204     public void setParam(String param) {
205         _param = param;
206     }
207 
208     public void setParamColumn(int paramColumn) {
209         _paramColumn = paramColumn;
210     }
211 
212     public void setParamValue(String paramValue) {
213         _paramValue = paramValue;
214     }
215 
216     /***
217      * used to set the renderer to delgate to.
218      * if the render is an AbstractCellRenderer then it will take the alignment from
219      * the delegate renderer and set it that way.
220      */
221     public void setRenderer(CellRenderer delegateRenderer) {
222         _delegateRenderer = delegateRenderer;
223 
224         if (_delegateRenderer instanceof AbstractCellRenderer) {
225             setAlignment(((AbstractCellRenderer) _delegateRenderer).getAlignment());
226         }
227     }
228 
229     public void setTarget(String target) {
230         _target = target;
231     }
232 
233     public void setTitle(String title) {
234         _title = title;
235     }
236 
237     public void setTrailParams(String trailParams) {
238         _trailParams = trailParams;
239     }
240 }