001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.taglib;
022:
023: import java.io.IOException;
024:
025: import javax.portlet.PortletMode;
026: import javax.portlet.PortletURL;
027: import javax.portlet.RenderRequest;
028: import javax.portlet.RenderResponse;
029: import javax.portlet.WindowState;
030: import javax.servlet.jsp.JspException;
031: import javax.servlet.jsp.JspWriter;
032: import javax.servlet.jsp.tagext.TagSupport;
033:
034: import com.nabhinc.portlet.mvcportlet.core.Constants;
035: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
036: import com.nabhinc.portlet.mvcportlet.core.Form;
037:
038: /**
039: * HTML Form tag
040: *
041: * @author Padmanabh Dabke
042: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
043: */
044: public class FormTag extends TagSupport {
045: private String ftName = null;
046: private String ftMethod = "POST";
047: private String ftEncType = null;
048: private String ftValidationMethod = null;
049: private String ftId = null;
050: private String ftOnreset = null;
051: private String ftOnsubmit = null;
052: private String ftTarget = null;
053: private String ftStyle = null;
054: private String ftStyleClass = null;
055: private String ftType = "action";
056: private String ftRequestType = null;
057: private String ftAccept = null;
058: private String ftAction = null;
059: private String ftWindowState = null;
060: private String ftPortletMode = null;
061:
062: public void release() {
063: super .release();
064: ftName = null;
065: ftMethod = "POST";
066: ftEncType = null;
067: ftValidationMethod = null;
068: ftId = null;
069: ftOnreset = null;
070: ftOnsubmit = null;
071: ftTarget = null;
072: ftStyle = null;
073: ftStyleClass = null;
074: ftType = "action";
075: ftRequestType = null;
076: ftAccept = null;
077: ftAction = null;
078: ftWindowState = null;
079: ftPortletMode = null;
080:
081: }
082:
083: /**
084: * Returns form's name
085: * @return
086: */
087: public String getName() {
088: return ftName;
089: }
090:
091: /**
092: * Sets the form name
093: * @param name
094: */
095: public void setName(String name) {
096: ftName = name;
097: }
098:
099: /**
100: * @return Returns the http method for sending data to the url defined in action attribute.
101: */
102: public String getMethod() {
103: return ftMethod;
104: }
105:
106: /**
107: * Sets method to send form data, e.g. <code>POST</code>, <code>GET</code>. Default is <code>POST</code>
108: * @param method The method to be set
109: */
110: public void setMethod(String method) {
111: ftMethod = method;
112: }
113:
114: /**
115: * Returns the form encoding type.
116: * @return Returns the string representing encode type attribute of the form.
117: */
118: public String getEnctype() {
119: return ftEncType;
120: }
121:
122: /**
123: * Sets form encoding type, e.g. <code>application/x-www-form-urlencoded</code>,
124: * <code>text/plain</code>.
125: * @param encType Form's encode type to set.
126: */
127: public void setEnctype(String encType) {
128: ftEncType = encType;
129: }
130:
131: /**
132: * @return Returns form's validation method.
133: */
134: public String getValidationMethod() {
135: return ftValidationMethod;
136: }
137:
138: /**
139: * Sets client-side validation method name. The validation will be performed
140: * if a name is set.
141: *
142: * @param m Validation method to set.
143: */
144: public void setValidationMethod(String m) {
145: ftValidationMethod = m;
146: }
147:
148: /**
149: * Returns the action url's value type. Possible values are <code>action</code> and <code>render</code>
150: * @return
151: */
152: public String getType() {
153: return ftType;
154: }
155:
156: /**
157: * Sets the action url's value type. Possible values are <code>action</code> and <code>render</code>.
158: * Default is action.
159: * @param t The action url's value type.
160: */
161: public void setType(String t) {
162: ftType = t;
163: }
164:
165: public String getRequestType() {
166: return ftRequestType;
167: }
168:
169: /**
170: *
171: * @param rType
172: */
173: public void setRequestType(String rType) {
174: ftRequestType = rType;
175: }
176:
177: /**
178: * Returns a comma separated list of content types
179: * @return Accepted content types.
180: */
181: public String getAccept() {
182: return ftAccept;
183: }
184:
185: /**
186: * A comma separated list of content types that this form accepts, so that
187: * it will be handled correctly.
188: * @param accept A comma separated list of content types.
189: */
190: public void setAccept(String accept) {
191: ftAccept = accept;
192: }
193:
194: public int doStartTag() throws JspException {
195:
196: try {
197: RenderRequest request = (RenderRequest) pageContext
198: .getRequest().getAttribute("javax.portlet.request");
199: RenderResponse response = (RenderResponse) pageContext
200: .getRequest()
201: .getAttribute("javax.portlet.response");
202: ControllerPortletConfig cPortlet = (ControllerPortletConfig) request
203: .getAttribute(Constants.PORTLET_CONFIG_ATTRIB);
204:
205: if (cPortlet != null) {
206: String name = getName();
207: if (name != null) {
208: Form form = cPortlet.getForm(getName());
209: if (form == null)
210: throw new JspException("Invalid form name: "
211: + name);
212: pageContext.setAttribute(Constants.FORM_ATTRIB,
213: form);
214: }
215:
216: }
217: JspWriter out = pageContext.getOut();
218:
219: String actionURL = ftAction;
220:
221: if (actionURL == null) {
222: PortletURL portletURL = null;
223:
224: if ("render".equals(ftType)) {
225: portletURL = response.createRenderURL();
226: } else {// default is action
227: portletURL = response.createActionURL();
228: }
229:
230: if (ftWindowState != null) {
231: portletURL.setWindowState(new WindowState(
232: ftWindowState));
233: }
234:
235: if (ftPortletMode != null) {
236: portletURL.setPortletMode(new PortletMode(
237: ftPortletMode));
238: }
239:
240: if (ftRequestType != null
241: && (!ftMethod.toLowerCase().equals("get"))) {
242: portletURL.setParameter("request_type",
243: ftRequestType);
244: }
245:
246: actionURL = portletURL.toString();
247: }
248:
249: out.print("<form action=\"");
250: out.print(actionURL);
251: out.print("\" method=\"");
252: out.print(ftMethod);
253: out.print("\" ");
254:
255: if (ftEncType != null) {
256: out.print("enctype=\"");
257: out.print(ftEncType);
258: out.print("\" ");
259: }
260:
261: if (ftAccept != null) {
262: out.print("accept=\"");
263: out.print(ftAccept);
264: out.print("\" ");
265: }
266:
267: if (ftValidationMethod != null) {
268: out.print("onsubmit=\"return ");
269: out.print(ftValidationMethod);
270: out.print("(this);\" ");
271: }
272:
273: if (ftStyleClass != null) {
274: out.print(" class=\"");
275: out.print(ftStyleClass);
276: out.print("\"");
277: }
278: if (ftOnreset != null) {
279: out.print(" onreset=\"");
280: out.print(ftOnreset);
281: out.print("\"");
282: }
283: if (ftOnsubmit != null) {
284: out.print(" onsubmit=\"");
285: out.print(ftOnsubmit);
286: out.print("\"");
287: }
288: if (ftStyle != null) {
289: out.print(" style=\"");
290: out.print(ftStyle);
291: out.print("\"");
292: }
293: if (ftName != null) {
294: out.print(" name=\"");
295: out.print(ftName);
296: out.print("\"");
297: }
298: if (ftId != null) {
299: out.print(" id=\"");
300: out.print(ftId);
301: out.print("\"");
302: }
303: if (ftTarget != null) {
304: out.print(" target=\"");
305: out.print(ftTarget);
306: out.print("\"");
307: }
308: out.print(">");
309: if (ftRequestType != null
310: && ftMethod.toLowerCase().equals("get")) {
311: out
312: .print("<input type=\"hidden\" name=\"request_type\" value=\"");
313: out.print(ftRequestType);
314: out.print("\" />");
315: }
316: } catch (Exception ex) {
317: throw new JspException(ex);
318: }
319:
320: return EVAL_BODY_INCLUDE;
321: }
322:
323: public int doEndTag() throws JspException {
324:
325: try {
326: RenderRequest request = (RenderRequest) pageContext
327: .getRequest().getAttribute("javax.portlet.request");
328: JspWriter out = pageContext.getOut();
329: out.print("</form>");
330:
331: if (ftValidationMethod != null) {
332: writeJavascript(request, out);
333: }
334:
335: return EVAL_PAGE;
336: } catch (IOException ioex) {
337: throw new JspException(ioex);
338: }
339: }
340:
341: // Empty method. This is overridden by JavascriptFormTag
342: // I am splitting the javacript functionality in the subclass
343: // since I am not sure it belong here or should be moved to
344: // a separate tag.
345: /**
346: * Not implemented
347: */
348: public void writeJavascript(RenderRequest request, JspWriter out)
349: throws IOException {
350:
351: }
352:
353: /**
354: * @return Returns the form id.
355: */
356: public String getId() {
357: return ftId;
358: }
359:
360: /**
361: * @param id The form id to set.
362: */
363: public void setId(String id) {
364: this .ftId = id;
365: }
366:
367: /**
368: * @return Returns the OnReset script.
369: */
370: public String getOnreset() {
371: return ftOnreset;
372: }
373:
374: /**
375: * @param onreset The OnReset script to set.
376: */
377: public void setOnreset(String onreset) {
378: this .ftOnreset = onreset;
379: }
380:
381: /**
382: * @return Returns the OnSubmit script.
383: */
384: public String getOnsubmit() {
385: return ftOnsubmit;
386: }
387:
388: /**
389: * @param onsubmit The onsubmit script to set.
390: */
391: public void setOnsubmit(String onsubmit) {
392: this .ftOnsubmit = onsubmit;
393: }
394:
395: /**
396: * @return Returns the inline style.
397: */
398: public String getStyle() {
399: return ftStyle;
400: }
401:
402: /**
403: * @param style The inline style to set.
404: */
405: public void setStyle(String style) {
406: this .ftStyle = style;
407: }
408:
409: /**
410: * @return Returns the stylesheet class.
411: */
412: public String getStyleClass() {
413: return ftStyleClass;
414: }
415:
416: /**
417: * @param styleClass The stylesheet class to set.
418: */
419: public void setStyleClass(String styleClass) {
420: this .ftStyleClass = styleClass;
421: }
422:
423: /**
424: * @return URI to form processing agent.
425: */
426: public String getAction() {
427: return ftAction;
428: }
429:
430: /**
431: * Sets the URI of form processing agent. By default, the action uri is url defined by
432: * <code>type</code> attribute (default is action url). This attribute will overwrite
433: * values defined in <code>type</code>, <code>requestType</code>, <code>windowState</code>
434: * and <code>portletMode</code>.
435: * @param uri The URI of the processing agent.
436: */
437: public void setAction(String uri) {
438: this .ftAction = uri;
439: }
440:
441: /**
442: * @return Window State
443: */
444: public String getWindowState() {
445: return ftWindowState;
446: }
447:
448: /**
449: * Sets window state. This attribute is
450: * used together with type attribute to set window state with action or render url.
451: * @param windowState The window state, e.g. maximized, normal, minimized
452: */
453: public void setWindowState(String windowState) {
454: this .ftWindowState = windowState;
455: }
456:
457: /**
458: * @return Portlet mode
459: */
460: public String getPortletMode() {
461: return ftPortletMode;
462: }
463:
464: /**
465: * Sets Portlet mode. The mode specified must be supported by the portlet.
466: * @param The portlet mode, e.g. view, edit, help
467: */
468: public void setPortletMode(String portletMode) {
469: this .ftPortletMode = portletMode;
470: }
471:
472: /**
473: * @return Returns the target URL.
474: */
475: public String getTarget() {
476: return ftTarget;
477: }
478:
479: /**
480: * The location to open the target URL, e.g. <code>_blank</code>, <code>_self</code>,
481: * <code>_parent</code>, <code>_top</code>.
482: * @param target The target to set.
483: */
484: public void setTarget(String target) {
485: this.ftTarget = target;
486: }
487: }
|