001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.editor.EditorUtil;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.PropsUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.taglib.util.IncludeTag;
027    
028    import java.util.Map;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class InputEditorTag extends IncludeTag {
036    
037            public void setConfigParams(Map<String, String> configParams) {
038                    _configParams = configParams;
039            }
040    
041            public void setContentsLanguageId(String contentsLanguageId) {
042                    _contentsLanguageId = contentsLanguageId;
043            }
044    
045            public void setCssClass(String cssClass) {
046                    _cssClass = cssClass;
047            }
048    
049            public void setEditorImpl(String editorImpl) {
050                    _editorImpl = editorImpl;
051            }
052    
053            public void setFileBrowserParams(Map<String, String> fileBrowserParams) {
054                    _fileBrowserParams = fileBrowserParams;
055            }
056    
057            public void setHeight(String height) {
058                    _height = height;
059            }
060    
061            public void setInitMethod(String initMethod) {
062                    _initMethod = initMethod;
063            }
064    
065            public void setInlineEdit(boolean inlineEdit) {
066                    _inlineEdit = inlineEdit;
067            }
068    
069            public void setInlineEditSaveURL(String inlineEditSaveURL) {
070                    _inlineEditSaveURL = inlineEditSaveURL;
071            }
072    
073            public void setName(String name) {
074                    _name = name;
075            }
076    
077            public void setOnBlurMethod(String onBlurMethod) {
078                    _onBlurMethod = onBlurMethod;
079            }
080    
081            public void setOnChangeMethod(String onChangeMethod) {
082                    _onChangeMethod = onChangeMethod;
083            }
084    
085            public void setOnFocusMethod(String onFocusMethod) {
086                    _onFocusMethod = onFocusMethod;
087            }
088    
089            public void setResizable(boolean resizable) {
090                    _resizable = resizable;
091            }
092    
093            public void setSkipEditorLoading(boolean skipEditorLoading) {
094                    _skipEditorLoading = skipEditorLoading;
095            }
096    
097            public void setToolbarSet(String toolbarSet) {
098                    _toolbarSet = toolbarSet;
099            }
100    
101            public void setWidth(String width) {
102                    _width = width;
103            }
104    
105            @Override
106            protected void cleanUp() {
107                    _configParams = null;
108                    _contentsLanguageId = null;
109                    _cssClass = null;
110                    _editorImpl = null;
111                    _fileBrowserParams = null;
112                    _height = null;
113                    _initMethod = "initEditor";
114                    _inlineEdit = false;
115                    _inlineEditSaveURL = null;
116                    _name = "editor";
117                    _onChangeMethod = null;
118                    _onBlurMethod = null;
119                    _onFocusMethod = null;
120                    _page = null;
121                    _resizable = true;
122                    _skipEditorLoading = false;
123                    _toolbarSet = "liferay";
124                    _width = null;
125            }
126    
127            @Override
128            protected String getPage() {
129                    return _page;
130            }
131    
132            @Override
133            protected void setAttributes(HttpServletRequest request) {
134                    if (_contentsLanguageId == null) {
135                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
136                                    WebKeys.THEME_DISPLAY);
137    
138                            _contentsLanguageId = themeDisplay.getLanguageId();
139                    }
140    
141                    String cssClasses = "portlet ";
142    
143                    Portlet portlet = (Portlet)request.getAttribute(WebKeys.RENDER_PORTLET);
144    
145                    if (portlet != null) {
146                            cssClasses += portlet.getCssClassWrapper();
147                    }
148    
149                    String editorImpl = EditorUtil.getEditorValue(request, _editorImpl);
150    
151                    String ckEditorVersion = PropsUtil.get(
152                            PropsKeys.EDITOR_CKEDITOR_VERSION);
153    
154                    if (Validator.equals(editorImpl, "ckeditor") &&
155                            Validator.equals(ckEditorVersion, "latest")) {
156    
157                            StringBundler sb = new StringBundler(5);
158    
159                            sb.append("/html/js/editor/");
160                            sb.append(editorImpl);
161                            sb.append(StringPool.UNDERLINE);
162                            sb.append(ckEditorVersion);
163                            sb.append(".jsp");
164    
165                            _page = sb.toString();
166                    }
167                    else {
168                            _page = "/html/js/editor/" + editorImpl + ".jsp";
169                    }
170    
171                    request.setAttribute(
172                            "liferay-ui:input-editor:configParams", _configParams);
173                    request.setAttribute(
174                            "liferay-ui:input-editor:contentsLanguageId", _contentsLanguageId);
175                    request.setAttribute("liferay-ui:input-editor:cssClass", _cssClass);
176                    request.setAttribute("liferay-ui:input-editor:cssClasses", cssClasses);
177                    request.setAttribute("liferay-ui:input-editor:editorImpl", editorImpl);
178                    request.setAttribute(
179                            "liferay-ui:input-editor:fileBrowserParams", _fileBrowserParams);
180    
181                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
182                            WebKeys.THEME_DISPLAY);
183    
184                    long groupId = themeDisplay.getDoAsGroupId();
185    
186                    if (groupId == 0) {
187                            groupId = themeDisplay.getScopeGroupId();
188                    }
189    
190                    request.setAttribute("liferay-ui:input-editor:groupId", groupId);
191                    request.setAttribute("liferay-ui:input-editor:height", _height);
192                    request.setAttribute("liferay-ui:input-editor:initMethod", _initMethod);
193                    request.setAttribute(
194                            "liferay-ui:input-editor:inlineEdit", String.valueOf(_inlineEdit));
195                    request.setAttribute(
196                            "liferay-ui:input-editor:inlineEditSaveURL", _inlineEditSaveURL);
197                    request.setAttribute("liferay-ui:input-editor:name", _name);
198                    request.setAttribute(
199                            "liferay-ui:input-editor:onBlurMethod", _onBlurMethod);
200                    request.setAttribute(
201                            "liferay-ui:input-editor:onChangeMethod", _onChangeMethod);
202                    request.setAttribute(
203                            "liferay-ui:input-editor:onFocusMethod", _onFocusMethod);
204                    request.setAttribute(
205                            "liferay-ui:input-editor:resizable", String.valueOf(_resizable));
206                    request.setAttribute(
207                            "liferay-ui:input-editor:skipEditorLoading",
208                            String.valueOf(_skipEditorLoading));
209                    request.setAttribute("liferay-ui:input-editor:toolbarSet", _toolbarSet);
210                    request.setAttribute("liferay-ui:input-editor:width", _width);
211            }
212    
213            private Map<String, String> _configParams;
214            private String _contentsLanguageId;
215            private String _cssClass;
216            private String _editorImpl;
217            private Map<String, String> _fileBrowserParams;
218            private String _height;
219            private String _initMethod = "initEditor";
220            private boolean _inlineEdit;
221            private String _inlineEditSaveURL;
222            private String _name = "editor";
223            private String _onBlurMethod;
224            private String _onChangeMethod;
225            private String _onFocusMethod;
226            private String _page;
227            private boolean _resizable = true;
228            private boolean _skipEditorLoading;
229            private String _toolbarSet = "liferay";
230            private String _width;
231    
232    }