001: /*
002: * $Id: LinkCellRenderer.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components.table.renderer;
022:
023: import org.apache.struts2.components.table.WebTable;
024:
025: /**
026: */
027: public class LinkCellRenderer extends AbstractCellRenderer {
028:
029: /**
030: * this is the actual renderer tha will be used to display the text
031: */
032: protected CellRenderer _delegateRenderer = new DefaultCellRenderer();
033:
034: /**
035: * the CSS class this link belongs to. Optional
036: */
037: protected String _cssClass = null;
038:
039: /**
040: * the id attribute this link belongs to. Optional
041: */
042: protected String _cssId = null;
043:
044: /**
045: * this is the link we are setting (required)
046: */
047: protected String _link = null;
048:
049: /**
050: * the (Java)script/ function to execute when the link is clicked. Optional
051: */
052: protected String _onclick = null;
053:
054: /**
055: * the (Java)script/ function to execute when the link is clicked twice. Optional
056: */
057: protected String _ondblclick = null;
058:
059: /**
060: * the (Java)script/ function to execute when cursor is away from the link. Optional
061: */
062: protected String _onmouseout = null;
063:
064: /**
065: * the (Java)script/ function to execute when cursor is over the link. Optional
066: */
067: protected String _onmouseover = null;
068:
069: /**
070: * if set there will be a parameter attached to link. (optional)
071: * This should be extended to allow multiple parameters
072: */
073: protected String _param = null;
074:
075: /**
076: * directly set the value for the param. Will overide paramColumn if set.
077: * optional. Either this or paramColumn must be set if param is used.
078: * Will be ignored if param not used
079: */
080: protected String _paramValue = null;
081:
082: /**
083: * the target frame to open in. Optional
084: */
085: protected String _target = null;
086:
087: /**
088: * the title attribute this link belongs to. Optional
089: */
090: protected String _title = null;
091:
092: /**
093: * additional parameters after the above parameter is generated. Optional
094: */
095: protected String _trailParams = null;
096:
097: /**
098: * if used the param value will be taken from another column in the table. Useful if each row
099: * needs a different paramter. The paramter can be taken from a hidden cell.
100: * if paramValue is also set it will overrid this. (option either this or paramValue must be set
101: * if param is used. Will be ignored if param not used
102: */
103: protected int _paramColumn = -1;
104:
105: public LinkCellRenderer() {
106: }
107:
108: /**
109: * should the link data be encodeed?
110: */
111: public String getCellValue(WebTable table, Object data, int row,
112: int col) {
113: String value = _delegateRenderer.renderCell(table, data, row,
114: col);
115:
116: StringBuffer cell = new StringBuffer(256);
117: cell.append("<a href='").append(_link);
118:
119: if (_param != null) {
120: cell.append("?").append(_param).append("=");
121:
122: if (_paramValue != null) {
123: cell.append(_paramValue);
124: } else if (_paramColumn >= 0) {
125: cell.append(table.getModel().getValueAt(row,
126: _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)
164: .append("'");
165: }
166:
167: if ((_onmouseover != null) && (!"".equals(_onmouseover))) {
168: cell.append(" onmouseover='").append(_onmouseover).append(
169: "'");
170: }
171:
172: if ((_onmouseout != null) && (!"".equals(_onmouseout))) {
173: cell.append(" onmouseout='").append(_onmouseout)
174: .append("'");
175: }
176:
177: cell.append(">").append(value).append("</a>");
178:
179: return cell.toString();
180: }
181:
182: public void setCssClass(String cssClass) {
183: _cssClass = cssClass;
184: }
185:
186: public void setCssId(String cssId) {
187: _cssId = cssId;
188: }
189:
190: public void setLink(String link) {
191: _link = link;
192: }
193:
194: public void setOnclick(String onclick) {
195: _onclick = onclick;
196: }
197:
198: public void setOndblclick(String ondblclick) {
199: _ondblclick = ondblclick;
200: }
201:
202: public void setOnmouseout(String onmouseout) {
203: _onmouseout = onmouseout;
204: }
205:
206: public void setOnmouseover(String onmouseover) {
207: _onmouseover = onmouseover;
208: }
209:
210: public void setParam(String param) {
211: _param = param;
212: }
213:
214: public void setParamColumn(int paramColumn) {
215: _paramColumn = paramColumn;
216: }
217:
218: public void setParamValue(String paramValue) {
219: _paramValue = paramValue;
220: }
221:
222: /**
223: * used to set the renderer to delgate to.
224: * if the render is an AbstractCellRenderer then it will take the alignment from
225: * the delegate renderer and set it that way.
226: */
227: public void setRenderer(CellRenderer delegateRenderer) {
228: _delegateRenderer = delegateRenderer;
229:
230: if (_delegateRenderer instanceof AbstractCellRenderer) {
231: setAlignment(((AbstractCellRenderer) _delegateRenderer)
232: .getAlignment());
233: }
234: }
235:
236: public void setTarget(String target) {
237: _target = target;
238: }
239:
240: public void setTitle(String title) {
241: _title = title;
242: }
243:
244: public void setTrailParams(String trailParams) {
245: _trailParams = trailParams;
246: }
247: }
|