View Javadoc

1   package org.fckfaces.component.html;
2   
3   import java.io.IOException;
4   
5   import javax.faces.component.html.HtmlInputTextarea;
6   import javax.faces.context.ExternalContext;
7   import javax.faces.context.FacesContext;
8   import javax.faces.context.ResponseWriter;
9   
10  import org.apache.commons.lang.StringUtils;
11  import org.fckfaces.taglib.html.FCKFaceEditorTag;
12  import org.fckfaces.util.Util;
13  
14  /**
15   * 
16   * @author srecinto
17   *
18   */
19  public class FCKFaceEditor extends HtmlInputTextarea {
20  	public static final String CUSTOM_CONFIGURATION_PATH = "org.fckfaces.CUSTOM_CONFIGURATIONS_PATH";
21  	public static final String COMPONENT_FAMILY = "org.fckfaces.FCKFacesFamily";
22  	private String toolbarSet;
23  	private String height;
24  	private String width;
25  
26  	/**
27  	 * 
28  	 */
29  	public String getComponentType() { 
30  		return FCKFaceEditorTag.COMPONENT_TYPE; 
31  	}
32  	
33  	/**
34  	 * 
35  	 */
36  	public String getRendererType() { 
37  		return FCKFaceEditorTag.RENDERER_TYPE;
38  	} 
39  	
40  	/**
41  	 * Moved to encode end so that the inline java script will run after the textArea was rendered before this script is run
42  	 * @param context
43  	 * @throws IOException
44  	 */
45  	public void encodeEnd(FacesContext context) throws IOException {
46  		super.encodeEnd(context);
47  		
48  		ResponseWriter writer = context.getResponseWriter();
49  		
50  		//Initial Configuration
51  		final ExternalContext external = context.getExternalContext();
52  		String cstConfigPathParam = external.getInitParameter(CUSTOM_CONFIGURATION_PATH);
53  		
54  		//Initial JS link
55  		writer.startElement("script", this.getParent());
56  		writer.writeAttribute("type", "text/javascript", null);
57  		writer.writeAttribute("src", Util.internalPath("/FCKeditor/fckeditor.js"), null);
58  		writer.endElement("script");
59  		
60  		writer.startElement("script", this.getParent());
61  		
62  		String toolBar = "Default";
63  		if(StringUtils.isNotBlank(toolbarSet)) {
64  			toolBar = toolbarSet;
65  		}
66  		
67  		String heightJS = "";
68  		String widthJS = "";
69  		String configPathJS = "";
70  		
71  		if(StringUtils.isNotBlank(height)) {
72  			heightJS = "oFCKeditor.Height = '" + height + "';\r\n";
73  		}
74  		
75  		if(StringUtils.isNotBlank(width)) {
76  			widthJS = "oFCKeditor.Width = '" + width + "';\r\n";
77  		}
78  		
79  		if (StringUtils.isNotBlank(cstConfigPathParam) ) {
80  			cstConfigPathParam = Util.externalPath(cstConfigPathParam);
81  			configPathJS = "   oFCKeditor.Config['CustomConfigurationsPath']='"+cstConfigPathParam+"';\r\n";
82  		}
83  
84  		String js = 
85  		"function applyEditor" + this.getId() +"() {" +
86  		"	var sBasePath = '" + Util.internalPath("/FCKeditor/") + "';\r\n" +
87  		"	var sTextAreaName = '" + this.getClientId(context) + "';\r\n" +
88  		"	var oFCKeditor = new FCKeditor( sTextAreaName ) ;\r\n" + 
89  		configPathJS +
90  		"	oFCKeditor.BasePath	= sBasePath ;\r\n" +
91  		"	oFCKeditor.ToolbarSet='" + toolBar + "';\r\n" +
92  		heightJS +
93  		widthJS + 
94  		"	oFCKeditor.ReplaceTextarea(); \r\n" +
95  		"	var oTextbox = document.getElementById(sTextAreaName);\r\n" +
96  		"	if(oTextbox.hasChildNodes()) {\r\n" +
97  		"		var oTextNode;\r\n" +
98  		"		var oParentNode = oTextbox.parentNode;\r\n" +
99  		"		if(oTextbox.childNodes.length > 1) {\r\n" +
100 		"			for(var i = 0; i < oTextbox.childNodes.length; i++) {\r\n" +
101 		"				if(oTextbox.childNodes.item(i).nodeType != 3 ) { //Not a Text node\r\n" +
102 		"					oParentNode.appendChild(oTextbox.removeChild(oTextbox.childNodes.item(i)));\r\n" +
103 		"					i = i - 1;\r\n" +
104 		"				}\r\n" +
105 		"			}\r\n" +
106 		"		}\r\n" +
107 		"	}\r\n" +
108 		"}" +
109 		"applyEditor" + this.getId() +"();";
110 		
111 		writer.writeText(js, null);
112 		writer.endElement("script");
113 	}
114 	
115 	/**
116 	 * 
117 	 * @return
118 	 */
119 	public String getFamily() {
120 		return COMPONENT_FAMILY;
121 	}
122 	
123 	public Object saveState(FacesContext context) {
124 		Object values[] = new Object[2];
125 		values[0] = super.saveState(context);
126 		values[1] = toolbarSet;
127 		
128 		return values;
129 	}
130 	
131 	public void restoreState(FacesContext context, Object state) {
132 		Object values[] = (Object[]) state;
133 		super.restoreState(context, values[0]);
134 		this.toolbarSet = (String)values[1];
135 	}
136 
137 	public String getToolbarSet() {
138 		return toolbarSet;
139 	}
140 
141 	public void setToolbarSet(String toolbarSet) {
142 		this.toolbarSet = toolbarSet;
143 	}
144 	
145 	public String getHeight() {
146 		return height;
147 	}
148 
149 	public void setHeight(String height) {
150 		this.height = height;
151 	}
152 
153 	public String getWidth() {
154 		return width;
155 	}
156 
157 	public void setWidth(String width) {
158 		this.width = width;
159 	}
160 }