1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.view;
17
18 import java.util.Iterator;
19 import java.util.Map;
20
21 import org.seasar.tuigwaa.util.TgwContext;
22 import org.seasar.tuigwaa.util.TgwResource;
23
24 import com.isenshi.util.CharUtil;
25 import com.isenshi.util.HtmlBuffer;
26
27 public abstract class AbstractFormComponent implements FormComponent {
28
29 protected static final String SUBMIT_BUTTON = "button.save";
30
31 private String serverUrl;
32
33 private String javascript;
34
35 private String actionName;
36
37 private String formName;
38
39 private String siteName;
40
41 private String forwardPageName;
42
43 private String thisPageName;
44
45 private boolean needJavaScript = true;
46
47 private boolean jspTemplate = false;
48
49 private String displayName;
50
51 public AbstractFormComponent(String siteName, String javascript) {
52 this.siteName = siteName;
53 this.javascript = javascript;
54 }
55
56
57
58 public void setServerUrl(String serverUrl) {
59 this.serverUrl = serverUrl;
60 }
61
62 public final void setActionName(String actionName) {
63 this.actionName = actionName;
64 }
65
66 public final void setFormName(String formName) {
67 this.formName = formName;
68 }
69
70 public final String getActionName() {
71 return actionName;
72 }
73
74 public final String getFormName() {
75 return formName;
76 }
77
78 public void setNeedJavaScript(boolean needJavaScript) {
79 this.needJavaScript = needJavaScript;
80 }
81
82 public boolean isNeedJavaScript() {
83 return needJavaScript;
84 }
85
86 public void setJspTemplate(boolean jspTemplate) {
87 this.jspTemplate = jspTemplate;
88 }
89
90 public boolean isJspTemplate() {
91 return jspTemplate;
92 }
93
94 public final String getForwardPageName() {
95 return forwardPageName;
96 }
97
98 public void setForwardPageName(String pageName) {
99 this.forwardPageName = pageName;
100 }
101
102 public String getThisPageName() {
103 return thisPageName;
104 }
105
106 public void setThisPageName(String thisPageName) {
107 this.thisPageName = thisPageName;
108 }
109
110 public String getOnSubmitAttribute() {
111 if (getOnSubmitFunction() != null) {
112 return "onsubmit=\"" + getOnSubmitFunction() + "\"";
113 }
114 return null;
115 }
116
117 public String getEnctypeAttribute() {
118 if (isMultiPart()) {
119 return "enctype=\"multipart/form-data\"";
120 }
121 return null;
122 }
123
124 public void setDisplayName(String displayName) {
125 this.displayName = displayName;
126 }
127
128 public String getDisplayName() {
129 return displayName;
130 }
131
132
133
134 public final String getJavascript() {
135 if (javascript == null) {
136 return "";
137 }
138 return javascript;
139 }
140
141 public final String getHiddenFields() {
142 Map hiddenProps = getHiddenProperties();
143 if (hiddenProps == null) {
144 return "";
145 }
146 HtmlBuffer buf = new HtmlBuffer();
147 for (Iterator i = hiddenProps.keySet().iterator(); i.hasNext();) {
148 String key = (String) i.next();
149 buf.appendHidden(key, (String) hiddenProps.get(key));
150 }
151 return buf.toString();
152 }
153
154 public String getSiteName() {
155 return siteName;
156 }
157
158 public final String getFormTagStart() {
159 String actionPath = getActionPath();
160 String name = getFormName();
161 String onsubmit = getOnSubmitFunction();
162
163 boolean isMultipart = isMultiPart();
164
165 HtmlBuffer buf = new HtmlBuffer();
166 buf.appendStartTag(HtmlBuffer.TAG_FORM);
167 if (name != null) {
168 buf.appendAttribute("name", name);
169 }
170 if (onsubmit != null) {
171 buf.appendAttribute("onsubmit", onsubmit);
172 }
173 buf.appendAttribute("method", "post");
174 buf.appendAttribute("action", actionPath);
175 if (isMultipart) {
176 buf.appendAttribute("enctype", "multipart/form-data");
177 }
178 buf.closeTag();
179 return buf.toString();
180 }
181
182 private String customActionPath;
183
184 public final void setActionPath(String path) {
185 this.customActionPath = path;
186 }
187
188 public final String getActionPath() {
189 if (customActionPath != null) {
190 return customActionPath;
191 } else {
192 return TgwContext.getContextPath() + "/" + siteName
193 + getActionName() + ".do";
194 }
195 }
196
197 public final String getActionFullPath() {
198 return CharUtil.chartrim(serverUrl, '/') + getActionPath();
199 }
200
201 protected String getMessage(String key) {
202 return TgwResource.getMessage(key);
203 }
204
205 protected abstract String getOnSubmitFunction();
206
207 protected abstract boolean isMultiPart();
208
209 protected abstract Map getHiddenProperties();
210 }