001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.jsp;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.PrintWriter;
024: import java.util.ArrayList;
025: import java.util.Enumeration;
026:
027: import javax.servlet.http.HttpServletRequest;
028:
029: import com.salmonllc.html.*;
030: import com.salmonllc.html.events.SubmitListener;
031: import com.salmonllc.localizer.LanguagePreferences;
032: import com.salmonllc.localizer.LanguageResourceFinder;
033: import com.salmonllc.properties.Props;
034: import com.salmonllc.sql.DataStore;
035: import com.salmonllc.sql.DataStoreException;
036:
037: /**
038: * An extended version of the display box with addional functionality for building search, list and detail forms. This is the base class for the specific form components
039: *
040: */
041: public abstract class JspFormDisplayBox extends JspDisplayBox {
042: public static final int BUTTON_DISPLAY_IN_HEADER = 0;
043: public static final int BUTTON_DISPLAY_BELOW_TABLE = 1;
044: public static final int BUTTON_DISPLAY_IN_HEADER_AND_BELOW_TABLE = 2;
045: public static final String ALIGN_LEFT = HtmlTable.ALIGN_LEFT;
046: public static final String ALIGN_CENTER = HtmlTable.ALIGN_CENTER;
047: public static final String ALIGN_RIGHT = HtmlTable.ALIGN_RIGHT;
048: protected int _buttonDisplayLocation;
049: protected ArrayList _buttons = new ArrayList();
050: protected ArrayList _messageButtons = new ArrayList();
051: protected HtmlValidatorText _validator;
052: protected boolean _validatorBuiltInternally = false;
053: protected boolean _updateLocale = true;
054: protected String _buttonBgColor;
055: protected String _buttonFontStyle;
056: protected String _okButtonCap = "OK", _cancelButtonCap = "Cancel",
057: _undoButtonCap = "Undo";
058: protected String _bottomButtonAlign;
059: protected ArrayList _hidden = new ArrayList();
060:
061: public JspFormDisplayBox(String name, String theme, HtmlPage p) {
062: super (name, theme, p);
063: }
064:
065: protected class MessageButton extends HtmlSubmitButton {
066: public MessageButton(HtmlPage p, String name, String cap,
067: SubmitListener listener) {
068: super (name, cap, p);
069: this .setButtonBgColor(_buttonBgColor);
070: this .setButtonFontStyle(_buttonFontStyle);
071: if (listener != null)
072: addSubmitListener(listener);
073: }
074: }
075:
076: /**
077: *Generates the Html for the component. This method is called by the framework and should not be called directly
078: */
079: public void generateHTML(TagWriter t, String boxBody)
080: throws Exception {
081: processLocaleInfo();
082: StringBuffer sb = new StringBuffer();
083:
084: int cellPadding = getCellPadding() > -1 ? getCellPadding() : 0;
085: int cellSpacing = getCellSpacing() > -1 ? getCellSpacing() : 0;
086: //do the heading
087: generateValidatorHtml(sb);
088: generateConfirmHtml(t, sb);
089:
090: sb.append("<TABLE BORDER=\"" + getBorder()
091: + "\" CELLSPACING=\"" + cellSpacing
092: + "\" CELLPADDING=\"" + cellPadding + "\"");
093: if (getWidth() != null)
094: sb.append(" WIDTH=\"" + getWidth() + "\"");
095: sb.append(">");
096:
097: if (isHeadingSuppressed() == false) {
098: String headingColor = "";
099: if (getHeadingBackgroundColor() != null)
100: headingColor = " BGCOLOR=\""
101: + getHeadingBackgroundColor() + "\"";
102: String headingStyle = "";
103: if (getHeadingClassname() != null)
104: headingStyle = " class=\"" + getHeadingClassname()
105: + "\"";
106:
107: sb.append("<TR" + headingStyle + "><TD" + headingColor
108: + "><TABLE WIDTH=\"100%\" CELLSPACING=\""
109: + getInnerCellSpacing() + "\" CELLPADDING=\""
110: + getInnerCellPadding() + "\">");
111: sb.append("<TR" + headingStyle + "><TD" + headingColor
112: + " ALIGN=\"LEFT\">" + _fontStartTag
113: + getHeadingCaption() + _fontEndTag + "</TD><TD"
114: + headingColor + " ALIGN=\"RIGHT\" NOWRAP>");
115: if (_buttonDisplayLocation == BUTTON_DISPLAY_IN_HEADER
116: || _buttonDisplayLocation == BUTTON_DISPLAY_IN_HEADER_AND_BELOW_TABLE)
117: generateButtonHtml(sb);
118: sb.append("</TD></TR></TABLE></TD></TR>");
119: }
120:
121: String boxColor = "";
122: if (getBackgroundColor() != null)
123: boxColor = " BGCOLOR=\"" + getBackgroundColor() + "\"";
124: String bodyStyle = "";
125: if (getBodyClassname() != null)
126: bodyStyle = " class=\"" + getBodyClassname() + "\"";
127: sb.append("<TR " + bodyStyle + "><TD" + boxColor + ">");
128:
129: t.print(sb.toString(), TagWriter.TYPE_BEGIN_TAG);
130: sb.setLength(0);
131:
132: if (boxBody != null)
133: t.print(boxBody, TagWriter.TYPE_CONTENT);
134:
135: sb.append("</TD></TR>");
136: if (_buttonDisplayLocation == BUTTON_DISPLAY_BELOW_TABLE
137: || _buttonDisplayLocation == BUTTON_DISPLAY_IN_HEADER_AND_BELOW_TABLE
138: || isHeadingSuppressed()) {
139: sb.append("<TR><TD" + boxColor);
140: if (_bottomButtonAlign != null) {
141: sb.append(" ALIGN=\"");
142: sb.append(_bottomButtonAlign);
143: }
144: sb.append("\">");
145: generateButtonHtml(sb);
146: sb.append("</TD></TR>");
147: }
148: sb.append("</TABLE>");
149: sb.append("<a name=\"");
150: sb.append(getFullName());
151: sb.append("scrollToMe\">");
152: t.print(sb.toString(), TagWriter.TYPE_END_TAG);
153: }
154:
155: private void generateValidatorHtml(StringBuffer sb)
156: throws Exception {
157: if (!_validatorBuiltInternally)
158: return;
159: if (_validator == null)
160: return;
161: ByteArrayOutputStream out = new ByteArrayOutputStream();
162: PrintWriter pw = new PrintWriter(out);
163: _validator.generateHTML(pw, -1);
164: pw.flush();
165: sb.append(out.toString());
166: pw.close();
167: }
168:
169: private void generateButtonHtml(StringBuffer sb) throws Exception {
170: ByteArrayOutputStream out = new ByteArrayOutputStream();
171: PrintWriter pw = new PrintWriter(out);
172: for (int i = 0; i < _buttons.size(); i++) {
173: HtmlSubmitButton b = (HtmlSubmitButton) _buttons.get(i);
174: b.generateHTML(pw, -1);
175: }
176: pw.flush();
177: sb.append(out.toString());
178: pw.close();
179: }
180:
181: /* Nicolás Genta - 9/27/04 Added for javascript confirm messages */
182: private void generateConfirmHtml(TagWriter tw, StringBuffer sb)
183: throws Exception {
184: if (tw.getDreamWeaverConv())
185: return;
186: ByteArrayOutputStream out = new ByteArrayOutputStream();
187: PrintWriter pw = new PrintWriter(out);
188: for (int i = 0; i < _hidden.size(); i++) {
189: HtmlHiddenField h = (HtmlHiddenField) _hidden.get(i);
190: h.setValue("0");
191: h.generateHTML(pw, -1);
192: }
193: pw.flush();
194: sb.append(out.toString());
195: pw.close();
196: }
197:
198: /**
199: * Binds various components to the component based on their names passed to the constructor. CriteriaBuilder, CriteriaValidator and ListForm. This method is called by the framework and should not be called directly.
200: */
201: public abstract void autoBindComponents() throws Exception;
202:
203: protected HtmlFormComponent findFirstFormComponent(JspContainer cont) {
204: Enumeration en = cont.getComponents();
205: while (en.hasMoreElements()) {
206: Object o = en.nextElement();
207: if (o instanceof JspContainer) {
208: HtmlFormComponent comp = findFirstFormComponent((JspContainer) o);
209: if (comp != null)
210: return comp;
211: } else if (o instanceof HtmlFormComponent)
212: return (HtmlFormComponent) o;
213: }
214: return null;
215: }
216:
217: protected HtmlCheckBox findFirstUnboundCheckBox(JspContainer cont) {
218: Enumeration en = cont.getComponents();
219: while (en.hasMoreElements()) {
220: Object o = en.nextElement();
221: if (o instanceof JspContainer) {
222: HtmlCheckBox comp = findFirstUnboundCheckBox((JspContainer) o);
223: if (comp != null)
224: return comp;
225: } else if (o instanceof HtmlCheckBox) {
226: HtmlCheckBox cbx = (HtmlCheckBox) o;
227: if (cbx.getBoundDataStore() == null)
228: return cbx;
229: }
230: }
231: return null;
232: }
233:
234: protected HtmlText findFirstTextComponent(JspContainer cont) {
235: int count = cont.getComponentCount();
236: for (int i = 0; i < count; i++) {
237: if (cont.getComponentType(i) == JspContainer.TYPE_ROW) {
238: Object o = cont.getComponent(i);
239: if (o instanceof JspContainer) {
240: HtmlText comp = findFirstTextComponent((JspContainer) o);
241: if (comp != null)
242: return comp;
243: } else if (o instanceof HtmlText)
244: return (HtmlText) o;
245: }
246: }
247: return null;
248: }
249:
250: /**
251: * Updates the display box button labels for the current language
252: */
253: public void updateLocale() {
254: _updateLocale = true;
255: }
256:
257: /**
258: * Sets the theme for the component
259: */
260: public void setTheme(String theme) {
261: super .setTheme(theme);
262: Props props = getPage().getPageProperties();
263: _buttonBgColor = props.getThemeProperty(theme,
264: Props.DETAIL_FORM_DISPLAY_BOX_BUTTON_BG_COLOR);
265: _buttonFontStyle = props.getThemeProperty(theme,
266: Props.DETAIL_FORM_DISPLAY_BOX_BUTTON_FONT_STYLE);
267: _okButtonCap = props.getThemeProperty(theme,
268: Props.FORM_DISPLAY_BOX_OK_BUTTON_CAPTION);
269: _cancelButtonCap = props.getThemeProperty(theme,
270: Props.FORM_DISPLAY_BOX_CANCEL_BUTTON_CAPTION);
271: _undoButtonCap = props.getThemeProperty(theme,
272: Props.FORM_DISPLAY_BOX_UNDO_BUTTON_CAPTION);
273: }
274:
275: protected void setUpButtons() {
276: if (_buttons != null) {
277: for (int i = 0; i < _buttons.size(); i++) {
278: HtmlSubmitButton b = (HtmlSubmitButton) _buttons.get(i);
279: if (_buttonBgColor != null)
280: b.setButtonBgColor(_buttonBgColor);
281: if (_buttonFontStyle != null)
282: ;
283: b.setButtonFontStyle(_buttonFontStyle);
284: }
285: }
286:
287: if (_messageButtons != null) {
288: for (int i = 0; i < _messageButtons.size(); i++) {
289: HtmlSubmitButton b = (HtmlSubmitButton) _messageButtons
290: .get(i);
291: if (_buttonBgColor != null)
292: b.setButtonBgColor(_buttonBgColor);
293: if (_buttonFontStyle != null)
294: ;
295: b.setButtonFontStyle(_buttonFontStyle);
296: }
297: }
298: }
299:
300: /**
301: * Sets the font style for the search and add buttons
302: */
303: public void setButtonFontStyle(String style) {
304: for (int i = 0; i < _buttons.size(); i++) {
305: HtmlSubmitButton but = (HtmlSubmitButton) _buttons.get(i);
306: but.setButtonFontStyle(style);
307: }
308: for (int i = 0; i < _messageButtons.size(); i++) {
309: HtmlSubmitButton but = (HtmlSubmitButton) _messageButtons
310: .get(i);
311: but.setButtonFontStyle(style);
312: }
313: }
314:
315: /**
316: * Sets the background color for the search and add buttons
317: */
318: public void setButtonBgColor(String bgColor) {
319: for (int i = 0; i < _buttons.size(); i++) {
320: HtmlSubmitButton but = (HtmlSubmitButton) _buttons.get(i);
321: but.setButtonBgColor(bgColor);
322: }
323: for (int i = 0; i < _messageButtons.size(); i++) {
324: HtmlSubmitButton but = (HtmlSubmitButton) _messageButtons
325: .get(i);
326: but.setButtonBgColor(bgColor);
327: }
328: }
329:
330: /**
331: * Sets the component and all subcomponents visible or not
332: */
333: public void setVisible(boolean vis) {
334: boolean butVal[] = new boolean[_buttons.size()];
335: for (int i = 0; i < _buttons.size(); i++)
336: butVal[i] = ((HtmlSubmitButton) _buttons.get(i))
337: .getVisible();
338: super .setVisible(vis);
339: for (int i = 0; i < _buttons.size(); i++)
340: ((HtmlSubmitButton) _buttons.get(i)).setVisible(butVal[i]);
341: }
342:
343: protected void processLocaleInfo() {
344: LanguagePreferences p = getPage().getLanguagePreferences();
345:
346: String descr = null;
347: String key = "FormDisplayBox.ok";
348: String appName = getPage().getApplicationName();
349: descr = LanguageResourceFinder.getResource(appName, key, p);
350: if (descr != null)
351: _okButtonCap = descr;
352:
353: descr = null;
354: key = "FormDisplayBox.cancel";
355: descr = LanguageResourceFinder.getResource(appName, key, p);
356: if (descr != null)
357: _cancelButtonCap = descr;
358:
359: descr = null;
360: key = "FormDisplayBox.undo";
361: descr = LanguageResourceFinder.getResource(appName, key, p);
362: if (descr != null)
363: _undoButtonCap = descr;
364:
365: setUpButtons();
366: }
367:
368: /**
369: * @return the caption for the cancel button
370: */
371: public String getCancelButtonCap() {
372: return _cancelButtonCap;
373: }
374:
375: /**
376: * @return the caption for the ok button
377: */
378: public String getOkButtonCap() {
379: return _okButtonCap;
380: }
381:
382: /**
383: * @return the caption for the undo button
384: */
385: public String getUndoButtonCap() {
386: return _undoButtonCap;
387: }
388:
389: /**
390: * sets the caption for the cancel button
391: */
392: public void setCancelButtonCap(String string) {
393: _cancelButtonCap = string;
394: }
395:
396: /**
397: * sets the caption for the ok button
398: */
399: public void setOkButtonCap(String string) {
400: _okButtonCap = string;
401: }
402:
403: /**
404: * sets the caption for the undo button
405: */
406: public void setUndoButtonCap(String string) {
407: _undoButtonCap = string;
408: }
409:
410: public static String buildWhereFromDsRow(DataStore dsb, int row)
411: throws DataStoreException {
412: StringBuffer ret = new StringBuffer(255);
413: int colCount = dsb.getColumnCount();
414: for (int i = 0; i < colCount; i++) {
415: if (dsb.isColumnPrimaryKey(i)) {
416: if (ret.length() != 0)
417: ret.append(" and ");
418: ret.append(dsb.getColumnDatabaseName(i));
419: ret.append(" = ");
420: }
421: }
422: return ret.toString();
423: }
424:
425: protected boolean[] getButtonsVisible() {
426: boolean ret[] = new boolean[_buttons.size()];
427: for (int i = 0; i < _buttons.size(); i++) {
428: HtmlSubmitButton b = (HtmlSubmitButton) _buttons.get(i);
429: ret[i] = b.getVisible();
430: }
431: return ret;
432: }
433:
434: protected void setButtonVisible(boolean vis[]) {
435: for (int i = 0; i < _buttons.size(); i++) {
436: HtmlSubmitButton b = (HtmlSubmitButton) _buttons.get(i);
437: b.setVisible(vis[i]);
438: }
439: }
440:
441: protected boolean isFromLookup() {
442: return (getLookupReturnToURL(null, null) != null);
443: }
444:
445: protected boolean isFromPopupLookup() {
446: return (getPage()
447: .getParameter(HtmlLookUpComponent.PARAM_LOOKUP_CONTROLLER)) != null;
448: }
449:
450: protected String getLookupReturnToURL(String keyVal, String descVal) {
451: HttpServletRequest req = getPage().getCurrentRequest();
452: String retTo = req.getParameter("returnTo");
453: if (retTo == null)
454: return null;
455: String comp = req.getParameter("comp");
456: if (comp == null)
457: return null;
458:
459: String ret = retTo;
460: if (keyVal != null) {
461: ret += "?" + comp + "=" + keyVal;
462: if (descVal != null)
463: ret += "&descReturn=" + descVal;
464: }
465: return ret;
466: }
467:
468: /* Nicolás Genta - 9/27/04 Added for javascript confirm messages */
469: protected void addConfirmScript(String message,
470: HtmlHiddenField hidden) {
471: String script = "if (confirm('" + message + "')) {\n"
472: + "\tdocument.forms['pageForm']."
473: + hidden.getFullName() + ".value = 1;\n" + "}\n"
474: + "document.forms['pageForm'].submit();";
475: getPage().writeScript(script);
476: }
477:
478: /**
479: * @return the alignment for buttons that appear at the bottom of the component under the table
480: */
481: public String getBottomButtonAlign() {
482: return _bottomButtonAlign;
483: }
484:
485: /**
486: * rets the alignment for buttons that appear at the bottom of the component under the table. Valid values are ALIGN_LEFT, ALIGN_CENTER and ALIGN_RIGHT
487: */
488: public void setBottomButtonAlign(String string) {
489: _bottomButtonAlign = string;
490: }
491:
492: }
|