KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > edit > FormTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.edit;
11
12 import org.mmbase.bridge.jsp.taglib.util.Attribute;
13 import org.mmbase.bridge.jsp.taglib.*;
14 import org.mmbase.bridge.*;
15
16 import javax.servlet.jsp.JspTagException JavaDoc;
17
18
19 /**
20  * FormTag can be used to generate the 'action' attribute of an HTML Form. But more importantly, it
21  * collects 'validation' from mm:fieldinfo type="check" or type="errors".
22  *
23  * The result can be reported with mm:valid.
24  *
25  * @author Michiel Meeuwissen
26  * @version $Id: FormTag.java,v 1.6 2006/07/09 14:15:23 michiel Exp $
27  * @since MMBase-1.8
28  */

29
30 public class FormTag extends TransactionTag implements Writer {
31
32     private Attribute mode = Attribute.NULL;
33     private int m;
34
35     private Attribute page = Attribute.NULL;
36     private Attribute clazz = Attribute.NULL;
37
38     public static final int MODE_HTML_FORM = 0;
39     public static final int MODE_URL = 1;
40     public static final int MODE_VALIDATE = 2;
41
42
43     protected boolean valid = true;
44     public void setValid(boolean valid) {
45         this.valid = valid;
46     }
47     public boolean isValid() {
48         return valid;
49     }
50
51     public void setMode(String JavaDoc m) throws JspTagException JavaDoc {
52         mode = getAttribute(m);
53     }
54
55     public void setStyleClass(String JavaDoc c) throws JspTagException JavaDoc {
56         clazz = getAttribute(c);
57     }
58
59     private int getMode() throws JspTagException JavaDoc {
60         String JavaDoc m = mode.getString(this).toLowerCase();
61         if (m.equals("") || m.equals("form")) {
62             return MODE_HTML_FORM;
63         } else if (m.equals("url")) {
64             return MODE_URL;
65         } else if (m.equals("validate")) {
66             return MODE_VALIDATE;
67         } else {
68             throw new JspTagException JavaDoc("Value '" + m + "' not known for 'mode' attribute");
69         }
70     }
71
72
73     public void setPage(String JavaDoc p) throws JspTagException JavaDoc {
74         page = getAttribute(p);
75     }
76
77
78     public int doStartTag() throws JspTagException JavaDoc {
79         m = getMode();
80         switch(m) {
81         case MODE_URL:
82             helper.setValue(page.getString(this));
83             break;
84         case MODE_HTML_FORM:
85             String JavaDoc url = page.getString(this);
86             String JavaDoc id = getId();
87             String JavaDoc c = clazz.getString(this);
88             try {
89                 pageContext.getOut().write("<form " + (id != null ? "id=\"" + id + "\" " : "") +
90                                            "action=\"" + url + "\" method=\"post\" enctype=\"multipart/form-data\" class=\"mm_form" +
91                                            ("".equals(c) ? "" : " " + c) +
92                                            "\" >");
93             } catch (java.io.IOException JavaDoc ioe) {
94                 throw new TaglibException(ioe);
95             }
96             break;
97         }
98         valid = true;
99         return super.doStartTag();
100     }
101
102     public int doEndTag() throws JspTagException JavaDoc {
103         try {
104             if (! transaction.isCanceled() && ! transaction.isCommitted()) {
105                 if (commit.getBoolean(this, getDefaultCommit())) {
106                     transaction.commit();
107                 } else {
108                     transaction.cancel();
109                 }
110             }
111         } catch (Throwable JavaDoc t) {
112             try {
113                 // should not happen, but if it happens, don't fail the complete page, this probably is
114
// an editor!
115
pageContext.getOut().write(t.getMessage());
116             } catch (java.io.IOException JavaDoc ioe) {
117                 throw new TaglibException(ioe);
118             }
119         }
120         switch(m) {
121         case MODE_HTML_FORM:
122             try {
123                 pageContext.getOut().write("</form>");
124             } catch (java.io.IOException JavaDoc ioe) {
125                 throw new TaglibException(ioe);
126             }
127         }
128         if (getId() != null) {
129             getContextProvider().getContextContainer().unRegister(getId());
130         }
131         transaction = null;
132         return super.doEndTag();
133     }
134
135     // never commit on close, unless, explicitely requested, of course.
136
protected boolean getDefaultCommit() {
137         return false;
138     }
139
140     protected String JavaDoc getName() throws JspTagException JavaDoc {
141         if (name == Attribute.NULL) return "org.mmbase.taglib.form";
142         return (String JavaDoc) name.getValue(this);
143     }
144
145
146 }
147
Popular Tags