View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2003-2008 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  
22  package net.fckeditor.tags;
23  
24  import java.io.IOException;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.jsp.JspException;
29  import javax.servlet.jsp.JspWriter;
30  import javax.servlet.jsp.tagext.TagSupport;
31  
32  import net.fckeditor.FCKeditor;
33  
34  /**
35   * TODO check, if usage sample in comment is correct !!! (Advanced usage) <br><br>
36   * 
37   * FCKeditor Tag class to access the
38   * {@linkplain net.fckeditor.FCKeditor container}.<br>
39   * <p>
40   * <b>Simple usage</b>:
41   * 
42   * <pre>
43   * &lt;FCK:editor
44   * 	instanceName=&quot;EditorAccessibility&quot;
45   * 	width=&quot;80%&quot;
46   * 	height=&quot;120&quot;
47   * 	toolbarSet=&quot;Accessibility&quot;
48   * &quot;&gt;This is another test. &lt;BR&gt;&lt;BR&gt;The &quot;Second&quot; row.&lt;/BR&gt;&lt;/FCK:editor&quot;&gt;
49   * </pre>
50   * 
51   * <p>
52   * In this example we set all the attribute for the fckedit tag.
53   * 
54   * <p>
55   * <b>Advanced usage of the tag</b>:
56   * 
57   * <pre>
58   * &lt;FCK:editor instanceName=&quot;EditorDefault&quot; basePath=&quot;/fckeditor/&quot;
59   * 	styleNames=&quot;;Style 1;Style 2; Style 3&quot; 
60   * 	fontNames=&quot;;Arial;Courier New;Times New Roman;Verdana&quot; &gt;
61   * 	This is some &lt;B&gt;sample text&lt;/B&gt;.
62   * &lt;/FCK:editor&gt;
63   * </pre>
64   * 
65   * In this example we set the id and the basePath of the editor (since it is
66   * /fckeditor/ we could have omitted it because it's already the default value).<br>
67   * Then we used the the optional attributes to set some advanced configuration
68   * settings.
69   * 
70   * @version $Id: EditorTag.java 2007 2008-05-19 17:53:34Z th-schwarz $
71   */
72  public class EditorTag extends TagSupport {
73  
74  	private static final long serialVersionUID = -173091731589866140L;
75  
76  	private String instanceName;
77  	private String width;
78  	private String height;
79  	private String toolbarSet;
80  	private String value;
81  	private String basePath;
82  
83  	/** The underlying FCKeditor object */
84  	private FCKeditor fckEditor;
85  
86  	/**
87  	 * Sets the name for the given editor instance
88  	 * 
89  	 * @param instanceName
90  	 *            some name without whitespaces
91  	 */
92  	public void setInstanceName(String instanceName) {
93  		this.instanceName = instanceName;
94  	}
95  
96  	/**
97  	 * Sets the width of the textarea
98  	 * 
99  	 * @param width
100 	 *            width of the editor instance
101 	 * 
102 	 */
103 	public void setWidth(String width) {
104 		this.width = width;
105 	}
106 
107 	/**
108 	 * Sets the height of the textarea
109 	 * 
110 	 * @param height
111 	 *            height of the editor instance
112 	 */
113 	public void setHeight(String height) {
114 		this.height = height;
115 	}
116 
117 	/**
118 	 * Sets the name of the toolbar to display
119 	 * 
120 	 * @param toolbarSet
121 	 *            toolbar set of the editor instance
122 	 */
123 	public void setToolbarSet(String toolbarSet) {
124 		this.toolbarSet = toolbarSet;
125 	}
126 
127 	/**
128 	 * Sets the editor document content
129 	 * 
130 	 * @param value
131 	 *            any HTML string
132 	 */
133 	public void setValue(String value) {
134 		this.value = value;
135 	}
136 
137 	/**
138 	 * Sets the dir where the FCKeditor files reside on the server
139 	 * 
140 	 * @param basePath
141 	 *            basePath of the editor instance (e.g. /fckeditor)
142 	 */
143 	public void setBasePath(String basePath) {
144 		this.basePath = basePath;
145 	}
146 
147 	void setConfigParamAll(Map<String, String> map) {
148 		fckEditor.getConfig().putAll(map);
149 	}
150 
151 	/**
152 	 * Initializes the FCKeditor container and Sets attributes
153 	 * 
154 	 * @return EVAL_BODY_INCLUDE
155 	 */
156 	public int doStartTag() throws JspException {
157 		fckEditor = new FCKeditor(
158 				(HttpServletRequest) pageContext.getRequest(), instanceName,
159 				width, height, toolbarSet, value, basePath);
160 		fckEditor.setValue(value);
161 
162 		return EVAL_BODY_INCLUDE;
163 	}
164 
165 	@Override
166 	public int doEndTag() throws JspException {
167 
168 		JspWriter out = pageContext.getOut();
169 
170 		try {
171 			out.println(fckEditor);
172 		} catch (IOException ioe) {
173 			throw new JspException(
174 					"Error: IOException while writing to the user");
175 		}
176 
177 		return EVAL_PAGE;
178 	}
179 
180 }