001: /*
002: * Created on Oct 21, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package com.salmonllc.html;
008:
009: import java.io.PrintWriter;
010: import java.util.Hashtable;
011:
012: import com.salmonllc.html.events.ValueChangedEvent;
013: import com.salmonllc.jsp.JspContainer;
014: import com.salmonllc.jsp.JspDataTable;
015: import com.salmonllc.properties.Props;
016: import com.salmonllc.sql.DataStoreBuffer;
017:
018: /**
019: * @author Dan
020: *
021: * TODO To change the template for this generated type comment go to Window -
022: * Preferences - Java - Code Style - Code Templates
023: */
024: public class HtmlRowHighlighter extends HtmlFormComponent {
025:
026: private String _highlightColor = "yellow";
027: private boolean _multiple = true;
028: private String _trueValue = "1";
029: private String _falseValue = "0";
030: private int _lastPrintedRow = -1;
031: private int _lastPrintedTR = -1;
032:
033: /**
034: * @param name
035: * @param p
036: */
037: public HtmlRowHighlighter(String name, HtmlPage p) {
038: super (name, p);
039: setTheme(null);
040: }
041:
042: public void resetForGenerate() {
043: _lastPrintedRow = -1;
044: _lastPrintedTR = -1;
045: }
046:
047: public HtmlComponent getParentDataTable() {
048: HtmlComponent parent = getParent();
049: while (parent != null) {
050: if (parent instanceof JspDataTable)
051: return parent;
052: if (parent instanceof HtmlDataTable)
053: return parent;
054: parent = parent.getParent();
055: }
056: return null;
057: }
058:
059: public String generateJavaScriptForTrOnClick() {
060: if (!getVisible())
061: return "";
062: else
063: return "doSelect=true;";
064: }
065:
066: public String generateJavaScriptForTrOnMouseDown(int row,
067: HtmlComponent tr) {
068: if (!getVisible())
069: return "";
070: else {
071: HtmlComponent comp = getParentDataTable();
072: String formSt = getFormString();
073: formSt = formSt.substring(0, formSt.length() - 1);
074: int rowSt = -1;
075: ;
076: int rowEnd = -1;
077: String name = comp.getFullName();
078: if (comp != null && comp instanceof JspDataTable) {
079: JspDataTable tab = (JspDataTable) comp;
080: rowSt = tab.getFirstRowOnPage();
081: rowEnd = tab.getRowsPerPage() + rowSt - 1;
082:
083: if (tab != null) {
084: DataStoreBuffer dsb = tab.getDataStoreBuffer();
085: if (dsb != null && rowEnd >= dsb.getRowCount())
086: rowEnd = tab.getDataStoreBuffer().getRowCount() - 1;
087: }
088: } else if (comp != null && comp instanceof HtmlDataTable) {
089: HtmlDataTable tab = (HtmlDataTable) comp;
090: rowSt = tab.getFirstRowOnPage();
091: rowEnd = tab.getRowsPerPage() + rowSt - 1;
092: if (tab != null) {
093: DataStoreBuffer dsb = tab.getDataStoreBuffer();
094: if (dsb != null && rowEnd >= dsb.getRowCount())
095: rowEnd = tab.getDataStoreBuffer().getRowCount() - 1;
096: }
097: }
098:
099: String st = "doSelect=false;";
100: boolean addToScript = true;
101: if (tr != null) {
102: addToScript = false;
103: Object parent = tr.getParent();
104: if (parent instanceof JspContainer)
105: if (((JspContainer) parent).getComponentType(tr) == JspContainer.TYPE_ROW)
106: addToScript = true;
107: }
108: if (addToScript)
109: st += "return selectRows(" + formSt + ",'" + name
110: + "'," + row + "," + rowSt + "," + rowEnd
111: + ",event,'" + _trueValue + "','" + _falseValue
112: + "','" + _highlightColor + "'," + !_multiple
113: + ");";
114: return st;
115: }
116: }
117:
118: public String generateHtmlForTd(int rowNo, int trNo, String bgColor) {
119: if (!getVisible())
120: return "";
121:
122: if (rowNo <= _lastPrintedRow && trNo <= _lastPrintedTR)
123: return "";
124: else {
125: HtmlComponent comp = getParentDataTable();
126: String name = getFullName() + "r" + trNo + "_color";
127: if (comp != null)
128: name = comp.getFullName() + "r" + trNo + "_color";
129: if (rowNo > -1)
130: name += "_" + rowNo;
131: _lastPrintedRow = rowNo;
132: _lastPrintedTR = trNo;
133: return ("<INPUT TYPE=\"HIDDEN\" name=\"" + name
134: + "\" value=\"" + bgColor + "\">");
135: }
136: }
137:
138: public String getBgColorForRow(int rowNo, String defColor,
139: HtmlComponent comp) {
140: if (comp != null) {
141: Object parent = comp.getParent();
142: if (parent instanceof JspContainer) {
143: if (((JspContainer) parent).getComponentType(comp) != JspContainer.TYPE_ROW)
144: return defColor;
145: }
146: }
147:
148: String value = getValue(rowNo);
149: if (valuesEqual(value, _trueValue))
150: return _highlightColor;
151: else
152: return defColor;
153: }
154:
155: /*
156: * (non-Javadoc)
157: *
158: * @see com.salmonllc.html.HtmlComponent#generateHTML(java.io.PrintWriter,
159: * int)
160: */
161: public void generateHTML(PrintWriter p, int rowNo) throws Exception {
162: HtmlScriptGenerator gen = new HtmlScriptGenerator(getPage());
163: gen.generateSelectRowScript();
164: HtmlComponent comp = getParentDataTable();
165: String name = getFullName();
166: if (comp != null)
167: name = comp.getFullName() + "_select";
168:
169: if (rowNo > -1)
170: name += "_" + rowNo;
171:
172: if (!getVisible())
173: return;
174:
175: _value = getValue(rowNo);
176:
177: String val = _value == null ? _falseValue : _value;
178:
179: String out = "<INPUT TYPE=\"HIDDEN\" name=\"" + name
180: + "\" value=\"" + val + "\">";
181:
182: p.println(out);
183: }
184:
185: /**
186: * @return Returns the falseValue.
187: */
188: public String getFalseValue() {
189: return _falseValue;
190: }
191:
192: /**
193: * @param falseValue
194: * The falseValue to set.
195: */
196: public void setFalseValue(String falseValue) {
197: _falseValue = falseValue;
198: }
199:
200: /**
201: * @return Returns the highlightColor.
202: */
203: public String getHighlightColor() {
204: return _highlightColor;
205: }
206:
207: /**
208: * @param highlightColor
209: * The highlightColor to set.
210: */
211: public void setHighlightColor(String highlightColor) {
212: _highlightColor = highlightColor;
213: }
214:
215: /**
216: * @return Returns the multiple.
217: */
218: public boolean isMultiple() {
219: return _multiple;
220: }
221:
222: /**
223: * @param multiple
224: * The multiple to set.
225: */
226: public void setMultiple(boolean multiple) {
227: _multiple = multiple;
228: }
229:
230: /**
231: * @return Returns the trueValue.
232: */
233: public String getTrueValue() {
234: return _trueValue;
235: }
236:
237: /**
238: * @param trueValue
239: * The trueValue to set.
240: */
241: public void setTrueValue(String trueValue) {
242: _trueValue = trueValue;
243: }
244:
245: /*
246: * (non-Javadoc)
247: *
248: * @see com.salmonllc.html.HtmlComponent#setTheme(java.lang.String)
249: */
250: public void setTheme(String theme) {
251: _highlightColor = getPage()
252: .getPageProperties()
253: .getThemeProperty(
254: theme,
255: Props.LIST_FORM_DISPLAY_BOX_ROW_HIGHLIGHT_COLOR,
256: "yellow");
257: super .setTheme(theme);
258: }
259:
260: public boolean processParms(Hashtable parms, int rowNo)
261: throws Exception {
262: Object oldValue = _value;
263:
264: HtmlComponent comp = getParentDataTable();
265: String name = getFullName();
266: if (comp != null)
267: name = comp.getFullName() + "_select";
268:
269: if (rowNo > -1) {
270: name += "_" + rowNo;
271: if (_dsBuff != null) {
272: try {
273: oldValue = _dsBuff.getAny(rowNo, _dsColNo);
274: } catch (Exception e) {
275: oldValue = null;
276: }
277: }
278: } else {
279: if (_dsBuff != null) {
280: try {
281: oldValue = _dsBuff.getAny(_dsColNo);
282: } catch (Exception e) {
283: oldValue = null;
284: }
285: }
286: }
287:
288: String val[] = (String[]) parms.get(name);
289:
290: if (val != null)
291: _value = val[0];
292: else
293: _value = null;
294:
295: if (_value != null && _value.trim().length() == 0)
296: _value = null;
297:
298: if (!valuesEqual(oldValue, _value)) {
299: String s = null;
300: if (oldValue != null)
301: s = oldValue.toString();
302: ValueChangedEvent e = new ValueChangedEvent(getPage(),
303: this , getName(), getFullName(), s, _value, rowNo,
304: _dsColNo, _dsBuff);
305: addEvent(e);
306: }
307: return false;
308: }
309: }
|