Source code: com/RuntimeCollective/webapps/tag/HtmlInputTag.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/HtmlInputTag.java,v 1.5 2003/09/30 15:13:18 joe Exp $
2 * $Revision: 1.5 $
3 * $Date: 2003/09/30 15:13:18 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.tag;
31
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import javax.servlet.jsp.JspWriter;
35 import javax.servlet.jsp.PageContext;
36 import javax.servlet.jsp.JspException;
37 import javax.servlet.jsp.JspTagException;
38 import javax.servlet.jsp.tagext.Tag;
39 import javax.servlet.jsp.tagext.TagSupport;
40 import org.apache.struts.action.ActionForm;
41 import org.apache.struts.util.RequestUtils;
42 import org.apache.struts.util.ResponseUtils;
43 import com.RuntimeCollective.webapps.RuntimeParameters;
44
45
46 /**
47 * An abstract class for custom JSP tags that display a Java applet for WSIWYG editing of HTML.
48 * <p>
49 * This class must be extended to provide methods for what applet class to use, what parameters to send in, and
50 * what script snippets (if any) are necessary.
51 * <p>
52 * <b>IMPORTANT:</b> This tag must be used inside a form, and the form creation tag must contain <code>onsubmit="return getHTMLEditorAppletValue()"</code>,
53 * for example:<blockquote><code>
54 * <html:form action="/testHtmlAction" onsubmit="return getHTMLEditorAppletValue()">
55 * </code></blockquote>
56 * <p>
57 * Only one HtmlInputTag can be used per HTML page.
58 * <p>
59 * This tag takes the following attributes:
60 * <ul>
61 * <li> <code> property </code> - the property of the form bean that contains the <code>String</code> of HTML text to edit. [Mandatory]</li>
62 * <li> <code> name </code> - the name of the form bean that contains the HTML property.
63 * <li> <code> width </code> - the width of the editing applet, in pixels [Optional]
64 * <li> <code> height </code> - the height of the editing applet, in pixels [Optional]
65 * <li> <code> rows </code> - the number of rows the WSIWYG editor will use [Optional]
66 * <li> <code> columns </code> - the number of rows the WSIWYG editor will use [Optional]
67 * </ul>
68 *
69 * @version $Id: HtmlInputTag.java,v 1.5 2003/09/30 15:13:18 joe Exp $
70 */
71 public abstract class HtmlInputTag extends TagSupport {
72
73
74
75 // == Properties ==================================
76
77 /** The property name that stores the HTML generated by the applet. Must be of type String. */
78 protected String property = null;
79 /** Get the property name. */
80 public String getProperty() { return this.property; }
81 /** Set the property name. */
82 public void setProperty( String property ) { this.property = property; }
83
84 /** The form bean name. */
85 protected String name = null;
86 /** Get the form bean name. */
87 public String getName() { return this.name; }
88 /** Set the form bean name. */
89 public void setName( String name ) { this.name = name; }
90
91 /** The width */
92 protected int width = 0;
93 /** Get the width */
94 public int getWidth() { return this.width; }
95 /** Set the width */
96 public void setWidth( int width ) { this.width = width; }
97
98 /** The height */
99 protected int height = 0;
100 /** Get the height */
101 public int getHeight() { return this.height; }
102 /** Set the height */
103 public void setHeight( int height ) { this.height = height; }
104
105 /** The rows */
106 protected int rows = 0;
107 /** Get the rows */
108 public int getRows() { return this.rows; }
109 /** Set the rows */
110 public void setRows( int rows ) { this.rows = rows; }
111
112 /** The columns */
113 protected int columns = 0;
114 /** Get the columns */
115 public int getColumns() { return this.columns; }
116 /** Set the columns */
117 public void setColumns( int columns ) { this.columns = columns; }
118
119
120 // == Tag methods ==================================
121
122 public final int doStartTag() throws JspException {
123
124 RuntimeParameters.logDebug(this, "Well? getAppletClass: "+getAppletClass()+", this.getAppletClass: "+this.getAppletClass());
125
126 // The default HTML String
127 String defHtml;
128 // InputDate defDate;
129
130 // Get the default values from the form
131 // Not *entirely* sure if this is right / does anything AT ALL ;)
132 defHtml = (String) RequestUtils.lookup( pageContext, name, property, null );
133
134 // Filter special HTML characters
135 defHtml = ResponseUtils.filter(defHtml);
136
137 // Sort out height and width; if they're null,
138 // give them some default values
139 if (width == 0) {
140 width = 600;
141 }
142 if (height == 0) {
143 height = 400;
144 }
145
146 // Generate the input tags for this property.
147 String html;
148 html = "<!-- WSIWYG applet code-->";
149
150
151 // Add any script that needs to be added
152 html = html + getScript();
153
154
155 // Display the applet
156 html = html + "<applet codebase=\"applets\"\n";
157 html = html + " name=\"HTMLEditorApplet\"\n";
158 html = html + " code=\""+getAppletClass()+"\"\n";
159 html = html + " archive=\"HtmlEditor.jar,kfc.jar\"\n";
160 html = html + " width=\""+getWidth()+"\" height=\""+getHeight()+"\">\n";
161 html = html + "<param name=\"originalText\" value=\""+defHtml+"\">\n";
162
163 // Put in any extra parameters here
164 html = html + getParsedExtraParameters(false);
165
166 // Rows and columns parameters (if necessary)
167 if (rows != 0 && columns != 0) {
168 html = html + "<param name=\"rows\" value=\""+getRows()+"\">\n";
169 html = html + "<param name=\"columns\" value=\""+getColumns()+"\">\n";
170 }
171 html = html + "</applet>\n";
172
173
174 /*
175 * The plug-in version of the above
176 * Sadly, this doesn't work with Netscape, so we're leaving it out for now.
177 *
178 * In time, this should be converted to a JavaScript browser-sniff,
179 * to give the plug-in for IE on Windows, and <APPLET> for anything else
180 *
181 * WARNING: has not been fully tested.
182
183 html = html + "<OBJECT classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\" width=\""+getWidth()+"\" height=\""+getHeight()+"\" codebase=\"http://java.sun.com/products/plugin/1.2.2/jinstall-1_2_2-win.cab#Version=1,2,2,0\">\n";
184 html = html + "<PARAM name=\"java_code\" value=\""+getAppletClass()+"\">\n";
185 html = html + "<PARAM name=\"java_codebase\" value=\"applets\">\n";
186 html = html + "<PARAM name=\"java_archive\" value=\"HtmlEditor.jar,kfc.jar\">\n";
187 html = html + "<PARAM name=\"type\" value=\"application/x-java-applet;version=1.2\">\n";
188 html = html + "<PARAM name=\"originalText\" value=\""+defHtml+"\">\n";
189 html = html + "<PARAM name=\"name\" value=\"HTMLEditorApplet\">\n";
190
191 // Put in any extra parameters here
192 html = html + getParsedExtraParameters(false);
193
194 // Rows and columns parameters (if necessary)
195 if (rows != 0 && columns != 0) {
196 html = html + "<param name=\"rows\" value=\""+getRows()+"\">\n";
197 html = html + "<param name=\"columns\" value=\""+getColumns()+"\">\n";
198 }
199 html = html + "<COMMENT>\n";
200 html = html + "<EMBED type=\"application/x-java-applet;version=1.2\" name=\"HTMLEditorApplet\" width=\""+getWidth()+"\" height=\""+getHeight()+"\" pluginspage=\"http://java.sun.com/products/plugin/\" java_code=\""+getAppletClass()+"\" java_codebase=\"applets\" java_archive=\"HtmlEditor.jar,kfc.jar\" originalText=\""+defHtml+"\"";
201
202 // Put in any extra parameters here
203 html = html + getParsedExtraParameters(true);
204
205 // Rows and columns parameters (if necessary)
206 if (rows != 0 && columns != 0) {
207 html = html + "rows=\""+getRows()+"\" columns=\""+getColumns()+"\"";
208 }
209 html = html + ">\n";
210 html = html + "<NOEMBED>\n";
211 html = html + "</COMMENT>\n";
212 html = html + "Sorry - your browser does not support Java plug-ins.\n";
213 html = html + "</NOEMBED></EMBED>\n";
214 html = html + "</OBJECT>\n";
215 */
216
217
218
219 // And write the html to the jsp page.
220 try {
221 pageContext.getOut().println( html );
222 } catch (IOException e) {
223 throw new JspTagException("I/O Exception " + e.getMessage() );
224 }
225
226 return SKIP_BODY;
227 }
228
229
230 /** Get the name of the Applet class to use */
231 public abstract String getAppletClass();
232
233 /** Get any scripting functions (eg some Javascript) that have to be defined for the applet to work.
234 * Unless overridden, returns the empty string.
235 */
236 public String getScript() {
237 return "";
238 }
239
240
241 /** Go through all the parameters set by the implementing class, and build them up
242 * in a String, depending what kind of applet tags we're using
243 *
244 * @param useEmbedStyle Return EMBED-style parameters, i.e. theName="theValue". If false, use the usual
245 * <param name="theName" value="theValue"> style.
246 */
247 protected String getParsedExtraParameters(boolean useEmbedStyle) {
248 String[][] allParams = getExtraParameters();
249
250 if (allParams==null) {
251 RuntimeParameters.logDebug(this, "In getParsedExtraParameters - allParams is NULL!");
252 return "";
253 }
254
255 RuntimeParameters.logDebug(this, "In getParsedExtraParameters: useEmbedStyle is "+useEmbedStyle+", and allParams = "+allParams+", of length "+allParams.length);
256
257 String params = "";
258 if (useEmbedStyle) {
259 for (int i=0; i < allParams.length; i++) {
260 params = params + " "+allParams[i][0]+"=\""+allParams[i][1]+"\" ";
261 }
262 } else {
263 for (int i=0; i < allParams.length; i++) {
264 params = params + "<param name=\""+allParams[i][0]+"\" value=\""+allParams[i][1]+"\">\n";
265 }
266 }
267
268 return params;
269
270 }
271
272
273
274 /*
275 * Get extra parameters for passing to the applet.
276 * They should be an array of name/value pairs, e.g.
277 * <pre></code>
278 * String[][] allParams = new String[][]{
279 * new String[]{"firstName","firstValue"},
280 * new String[]{"secondName",secondValue"}
281 * }
282 * </code></pre>
283 * If this method is not overridden, no extra parameters are set, and the method returns null.
284 */
285 public String[][] getExtraParameters() {
286 RuntimeParameters.logDebug(this, "It's HtmlInputTag's getExtraParameters! Shouldn't ever be for POST!");
287 return null;
288 }
289
290 }
291
292