001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.components.table.renderer;
006:
007: import com.opensymphony.webwork.components.table.WebTable;
008:
009: /**
010: * @author $author$
011: * @version $Revision: 1282 $
012: */
013: public class LinkCellRenderer extends AbstractCellRenderer {
014:
015: /**
016: * this is the actual renderer tha will be used to display the text
017: */
018: protected CellRenderer _delegateRenderer = new DefaultCellRenderer();
019:
020: /**
021: * the CSS class this link belongs to. Optional
022: */
023: protected String _cssClass = null;
024:
025: /**
026: * the id attribute this link belongs to. Optional
027: */
028: protected String _cssId = null;
029:
030: /**
031: * this is the link we are setting (required)
032: */
033: protected String _link = null;
034:
035: /**
036: * the (Java)script/ function to execute when the link is clicked. Optional
037: */
038: protected String _onclick = null;
039:
040: /**
041: * the (Java)script/ function to execute when the link is clicked twice. Optional
042: */
043: protected String _ondblclick = null;
044:
045: /**
046: * the (Java)script/ function to execute when cursor is away from the link. Optional
047: */
048: protected String _onmouseout = null;
049:
050: /**
051: * the (Java)script/ function to execute when cursor is over the link. Optional
052: */
053: protected String _onmouseover = null;
054:
055: /**
056: * if set there will be a parameter attached to link. (optional)
057: * This should be extended to allow multiple parameters
058: */
059: protected String _param = null;
060:
061: /**
062: * directly set the value for the param. Will overide paramColumn if set.
063: * optional. Either this or paramColumn must be set if param is used.
064: * Will be ignored if param not used
065: */
066: protected String _paramValue = null;
067:
068: /**
069: * the target frame to open in. Optional
070: */
071: protected String _target = null;
072:
073: /**
074: * the title attribute this link belongs to. Optional
075: */
076: protected String _title = null;
077:
078: /**
079: * additional parameters after the above parameter is generated. Optional
080: */
081: protected String _trailParams = null;
082:
083: /**
084: * if used the param value will be taken from another column in the table. Useful if each row
085: * needs a different paramter. The paramter can be taken from a hidden cell.
086: * if paramValue is also set it will overrid this. (option either this or paramValue must be set
087: * if param is used. Will be ignored if param not used
088: */
089: protected int _paramColumn = -1;
090:
091: public LinkCellRenderer() {
092: }
093:
094: /**
095: * should the link data be encodeed?
096: */
097: public String getCellValue(WebTable table, Object data, int row,
098: int col) {
099: String value = _delegateRenderer.renderCell(table, data, row,
100: col);
101:
102: StringBuffer cell = new StringBuffer(256);
103: cell.append("<a href='").append(_link);
104:
105: if (_param != null) {
106: cell.append("?").append(_param).append("=");
107:
108: if (_paramValue != null) {
109: cell.append(_paramValue);
110: } else if (_paramColumn >= 0) {
111: cell.append(table.getModel().getValueAt(row,
112: _paramColumn).toString());
113: }
114: }
115:
116: if ((_trailParams != null) && !"".equals(_trailParams)) {
117: if (_param == null) {
118: cell.append("?");
119: } else {
120: cell.append("&");
121: }
122:
123: cell.append(_trailParams);
124: }
125:
126: cell.append("'");
127:
128: if ((_target != null) && (!"".equals(_target))) {
129: cell.append(" target='").append(_target).append("'");
130: }
131:
132: if ((_cssClass != null) && (!"".equals(_cssClass))) {
133: cell.append(" class='").append(_cssClass).append("'");
134: }
135:
136: if ((_cssId != null) && (!"".equals(_cssId))) {
137: cell.append(" id='").append(_cssId).append("'");
138: }
139:
140: if ((_title != null) && (!"".equals(_title))) {
141: cell.append(" title='").append(_title).append("'");
142: }
143:
144: if ((_onclick != null) && (!"".equals(_onclick))) {
145: cell.append(" onclick='").append(_onclick).append("'");
146: }
147:
148: if ((_ondblclick != null) && (!"".equals(_ondblclick))) {
149: cell.append(" ondblclick='").append(_ondblclick)
150: .append("'");
151: }
152:
153: if ((_onmouseover != null) && (!"".equals(_onmouseover))) {
154: cell.append(" onmouseover='").append(_onmouseover).append(
155: "'");
156: }
157:
158: if ((_onmouseout != null) && (!"".equals(_onmouseout))) {
159: cell.append(" onmouseout='").append(_onmouseout)
160: .append("'");
161: }
162:
163: cell.append(">").append(value).append("</a>");
164:
165: return cell.toString();
166: }
167:
168: public void setCssClass(String cssClass) {
169: _cssClass = cssClass;
170: }
171:
172: public void setCssId(String cssId) {
173: _cssId = cssId;
174: }
175:
176: public void setLink(String link) {
177: _link = link;
178: }
179:
180: public void setOnclick(String onclick) {
181: _onclick = onclick;
182: }
183:
184: public void setOndblclick(String ondblclick) {
185: _ondblclick = ondblclick;
186: }
187:
188: public void setOnmouseout(String onmouseout) {
189: _onmouseout = onmouseout;
190: }
191:
192: public void setOnmouseover(String onmouseover) {
193: _onmouseover = onmouseover;
194: }
195:
196: public void setParam(String param) {
197: _param = param;
198: }
199:
200: public void setParamColumn(int paramColumn) {
201: _paramColumn = paramColumn;
202: }
203:
204: public void setParamValue(String 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)
218: .getAlignment());
219: }
220: }
221:
222: public void setTarget(String target) {
223: _target = target;
224: }
225:
226: public void setTitle(String title) {
227: _title = title;
228: }
229:
230: public void setTrailParams(String trailParams) {
231: _trailParams = trailParams;
232: }
233: }
|