001    /**
002     * Copyright (c) 2000-present 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.Editor;
018    import com.liferay.portal.kernel.editor.configuration.EditorConfiguration;
019    import com.liferay.portal.kernel.editor.configuration.EditorConfigurationFactoryUtil;
020    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
021    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
022    import com.liferay.portal.kernel.servlet.DirectRequestDispatcherFactoryUtil;
023    import com.liferay.portal.kernel.servlet.PortalWebResourceConstants;
024    import com.liferay.portal.kernel.servlet.PortalWebResourcesUtil;
025    import com.liferay.portal.kernel.util.JavaConstants;
026    import com.liferay.portal.kernel.util.MapUtil;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.kernel.util.PropsUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.kernel.util.WebKeys;
031    import com.liferay.portal.model.Portlet;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.registry.Registry;
034    import com.liferay.registry.RegistryUtil;
035    import com.liferay.registry.ServiceReference;
036    import com.liferay.registry.collections.ServiceReferenceMapper;
037    import com.liferay.registry.collections.ServiceTrackerCollections;
038    import com.liferay.registry.collections.ServiceTrackerMap;
039    import com.liferay.taglib.util.IncludeTag;
040    
041    import java.util.Enumeration;
042    import java.util.HashMap;
043    import java.util.Map;
044    
045    import javax.servlet.RequestDispatcher;
046    import javax.servlet.http.HttpServletRequest;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     */
051    public class InputEditorTag extends IncludeTag {
052    
053            public void setAllowBrowseDocuments(boolean allowBrowseDocuments) {
054                    _allowBrowseDocuments = allowBrowseDocuments;
055            }
056    
057            public void setAutoCreate(boolean autoCreate) {
058                    _autoCreate = autoCreate;
059            }
060    
061            public void setConfigKey(String configKey) {
062                    _configKey = configKey;
063            }
064    
065            public void setConfigParams(Map<String, String> configParams) {
066                    _configParams = configParams;
067            }
068    
069            public void setContents(String contents) {
070                    _contents = contents;
071            }
072    
073            public void setContentsLanguageId(String contentsLanguageId) {
074                    _contentsLanguageId = contentsLanguageId;
075            }
076    
077            public void setCssClass(String cssClass) {
078                    _cssClass = cssClass;
079            }
080    
081            public void setData(Map<String, Object> data) {
082                    _data = data;
083            }
084    
085            /**
086             * @deprecated As of 7.0.0, replaced by {@link #setEditorName(String)}
087             */
088            @Deprecated
089            public void setEditorImpl(String editorImpl) {
090                    _editorName = PropsUtil.get(editorImpl);
091            }
092    
093            public void setEditorName(String editorName) {
094                    _editorName = editorName;
095            }
096    
097            public void setFileBrowserParams(Map<String, String> fileBrowserParams) {
098                    _fileBrowserParams = fileBrowserParams;
099            }
100    
101            public void setHeight(String height) {
102                    _height = height;
103            }
104    
105            /**
106             * @deprecated As of 7.0.0, replaced by {@link #setContents(String)}
107             */
108            @Deprecated
109            public void setInitMethod(String initMethod) {
110                    _initMethod = initMethod;
111            }
112    
113            public void setInlineEdit(boolean inlineEdit) {
114                    _inlineEdit = inlineEdit;
115            }
116    
117            public void setInlineEditSaveURL(String inlineEditSaveURL) {
118                    _inlineEditSaveURL = inlineEditSaveURL;
119            }
120    
121            public void setName(String name) {
122                    _name = name;
123            }
124    
125            public void setOnBlurMethod(String onBlurMethod) {
126                    _onBlurMethod = onBlurMethod;
127            }
128    
129            public void setOnChangeMethod(String onChangeMethod) {
130                    _onChangeMethod = onChangeMethod;
131            }
132    
133            public void setOnFocusMethod(String onFocusMethod) {
134                    _onFocusMethod = onFocusMethod;
135            }
136    
137            public void setOnInitMethod(String onInitMethod) {
138                    _onInitMethod = onInitMethod;
139            }
140    
141            public void setPlaceholder(String placeholder) {
142                    _placeholder = placeholder;
143            }
144    
145            public void setResizable(boolean resizable) {
146                    _resizable = resizable;
147            }
148    
149            public void setShowSource(boolean showSource) {
150                    _showSource = showSource;
151            }
152    
153            public void setSkipEditorLoading(boolean skipEditorLoading) {
154                    _skipEditorLoading = skipEditorLoading;
155            }
156    
157            public void setToolbarSet(String toolbarSet) {
158                    _toolbarSet = toolbarSet;
159            }
160    
161            public void setWidth(String width) {
162                    _width = width;
163            }
164    
165            @Override
166            protected void cleanUp() {
167                    _allowBrowseDocuments = true;
168                    _autoCreate = true;
169                    _configKey = null;
170                    _configParams = null;
171                    _contents = null;
172                    _contentsLanguageId = null;
173                    _cssClass = null;
174                    _data = null;
175                    _editorName = null;
176                    _fileBrowserParams = null;
177                    _height = null;
178                    _initMethod = "initEditor";
179                    _inlineEdit = false;
180                    _inlineEditSaveURL = null;
181                    _name = "editor";
182                    _onChangeMethod = null;
183                    _onBlurMethod = null;
184                    _onFocusMethod = null;
185                    _onInitMethod = null;
186                    _placeholder = null;
187                    _resizable = true;
188                    _showSource = true;
189                    _skipEditorLoading = false;
190                    _toolbarSet = "liferay";
191                    _width = null;
192            }
193    
194            protected String getConfigKey() {
195                    String configKey = _configKey;
196    
197                    if (Validator.isNull(configKey)) {
198                            configKey = _name;
199                    }
200    
201                    return configKey;
202            }
203    
204            protected String getContentsLanguageId() {
205                    if (_contentsLanguageId == null) {
206                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
207                                    WebKeys.THEME_DISPLAY);
208    
209                            _contentsLanguageId = themeDisplay.getLanguageId();
210                    }
211    
212                    return _contentsLanguageId;
213            }
214    
215            protected String getCssClasses() {
216                    Portlet portlet = (Portlet)request.getAttribute(WebKeys.RENDER_PORTLET);
217    
218                    String cssClasses = "portlet ";
219    
220                    if (portlet != null) {
221                            cssClasses += portlet.getCssClassWrapper();
222                    }
223    
224                    return cssClasses;
225            }
226    
227            protected Map<String, Object> getData() {
228                    Portlet portlet = (Portlet)request.getAttribute(WebKeys.RENDER_PORTLET);
229    
230                    if (portlet == null) {
231                            return _data;
232                    }
233    
234                    Map<String, Object> attributes = new HashMap<>();
235    
236                    Enumeration<String> enumeration = request.getAttributeNames();
237    
238                    while (enumeration.hasMoreElements()) {
239                            String attributeName = enumeration.nextElement();
240    
241                            if (attributeName.startsWith("liferay-ui:input-editor")) {
242                                    attributes.put(
243                                            attributeName, request.getAttribute(attributeName));
244                            }
245                    }
246    
247                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
248                            WebKeys.THEME_DISPLAY);
249    
250                    LiferayPortletResponse portletResponse =
251                            (LiferayPortletResponse)request.getAttribute(
252                                    JavaConstants.JAVAX_PORTLET_RESPONSE);
253    
254                    EditorConfiguration editorConfiguration =
255                            EditorConfigurationFactoryUtil.getEditorConfiguration(
256                                    portlet.getPortletId(), getConfigKey(), getEditorName(request),
257                                    attributes, themeDisplay, portletResponse);
258    
259                    Map<String, Object> data = editorConfiguration.getData();
260    
261                    if (MapUtil.isNotEmpty(_data)) {
262                            MapUtil.merge(_data, data);
263                    }
264    
265                    return data;
266            }
267    
268            protected Editor getEditor(HttpServletRequest request) {
269                    String editorName = _editorName;
270    
271                    if (!BrowserSnifferUtil.isRtf(request)) {
272                            return _serviceTrackerMap.getService("simple");
273                    }
274    
275                    if (Validator.isNull(editorName)) {
276                            return _serviceTrackerMap.getService(_EDITOR_WYSIWYG_DEFAULT);
277                    }
278    
279                    if (!_serviceTrackerMap.containsKey(editorName)) {
280                            return _serviceTrackerMap.getService(_EDITOR_WYSIWYG_DEFAULT);
281                    }
282    
283                    return _serviceTrackerMap.getService(editorName);
284            }
285    
286            protected String getEditorName(HttpServletRequest request) {
287                    Editor editor = getEditor(request);
288    
289                    return editor.getName();
290            }
291    
292            @Override
293            protected String getPage() {
294                    Editor editor = getEditor(request);
295    
296                    return editor.getJspPath(request);
297            }
298    
299            @Override
300            protected RequestDispatcher getRequestDispatcher(String page) {
301                    return DirectRequestDispatcherFactoryUtil.getRequestDispatcher(
302                            PortalWebResourcesUtil.getServletContext(
303                                    PortalWebResourceConstants.RESOURCE_TYPE_EDITORS),
304                            page);
305            }
306    
307            @Override
308            protected void setAttributes(HttpServletRequest request) {
309                    request.setAttribute(
310                            "liferay-ui:input-editor:allowBrowseDocuments",
311                            String.valueOf(_allowBrowseDocuments));
312                    request.setAttribute(
313                            "liferay-ui:input-editor:autoCreate", String.valueOf(_autoCreate));
314                    request.setAttribute(
315                            "liferay-ui:input-editor:configParams", _configParams);
316                    request.setAttribute("liferay-ui:input-editor:contents", _contents);
317                    request.setAttribute(
318                            "liferay-ui:input-editor:contentsLanguageId",
319                            getContentsLanguageId());
320                    request.setAttribute("liferay-ui:input-editor:cssClass", _cssClass);
321                    request.setAttribute(
322                            "liferay-ui:input-editor:cssClasses", getCssClasses());
323                    request.setAttribute(
324                            "liferay-ui:input-editor:editorName", getEditorName(request));
325                    request.setAttribute(
326                            "liferay-ui:input-editor:fileBrowserParams", _fileBrowserParams);
327                    request.setAttribute("liferay-ui:input-editor:height", _height);
328                    request.setAttribute("liferay-ui:input-editor:initMethod", _initMethod);
329                    request.setAttribute(
330                            "liferay-ui:input-editor:inlineEdit", String.valueOf(_inlineEdit));
331                    request.setAttribute(
332                            "liferay-ui:input-editor:inlineEditSaveURL", _inlineEditSaveURL);
333                    request.setAttribute("liferay-ui:input-editor:name", _name);
334                    request.setAttribute(
335                            "liferay-ui:input-editor:onBlurMethod", _onBlurMethod);
336                    request.setAttribute(
337                            "liferay-ui:input-editor:onChangeMethod", _onChangeMethod);
338                    request.setAttribute(
339                            "liferay-ui:input-editor:onFocusMethod", _onFocusMethod);
340                    request.setAttribute(
341                            "liferay-ui:input-editor:onInitMethod", _onInitMethod);
342                    request.setAttribute(
343                            "liferay-ui:input-editor:placeholder", _placeholder);
344                    request.setAttribute(
345                            "liferay-ui:input-editor:resizable", String.valueOf(_resizable));
346                    request.setAttribute(
347                            "liferay-ui:input-editor:showSource", String.valueOf(_showSource));
348                    request.setAttribute(
349                            "liferay-ui:input-editor:skipEditorLoading",
350                            String.valueOf(_skipEditorLoading));
351                    request.setAttribute("liferay-ui:input-editor:toolbarSet", _toolbarSet);
352                    request.setAttribute("liferay-ui:input-editor:width", _width);
353    
354                    request.setAttribute("liferay-ui:input-editor:data", getData());
355    
356                    Editor editor = getEditor(request);
357    
358                    editor.setItemSelectorAttribute(request);
359            }
360    
361            private static final String _EDITOR_WYSIWYG_DEFAULT = PropsUtil.get(
362                    PropsKeys.EDITOR_WYSIWYG_DEFAULT);
363    
364            private static final ServiceTrackerMap<String, Editor> _serviceTrackerMap =
365                    ServiceTrackerCollections.singleValueMap(
366                            Editor.class, null,
367                            new ServiceReferenceMapper<String, Editor>() {
368    
369                                    @Override
370                                    public void map(
371                                            ServiceReference<Editor> serviceReference,
372                                            Emitter<String> emitter) {
373    
374                                            Registry registry = RegistryUtil.getRegistry();
375    
376                                            Editor editor = registry.getService(serviceReference);
377    
378                                            emitter.emit(editor.getName());
379                                    }
380    
381                            });
382    
383            static {
384                    _serviceTrackerMap.open();
385            }
386    
387            private boolean _allowBrowseDocuments = true;
388            private boolean _autoCreate = true;
389            private String _configKey;
390            private Map<String, String> _configParams;
391            private String _contents;
392            private String _contentsLanguageId;
393            private String _cssClass;
394            private Map<String, Object> _data = null;
395            private String _editorName;
396            private Map<String, String> _fileBrowserParams;
397            private String _height;
398            private String _initMethod = "initEditor";
399            private boolean _inlineEdit;
400            private String _inlineEditSaveURL;
401            private String _name = "editor";
402            private String _onBlurMethod;
403            private String _onChangeMethod;
404            private String _onFocusMethod;
405            private String _onInitMethod;
406            private String _placeholder;
407            private boolean _resizable = true;
408            private boolean _showSource = true;
409            private boolean _skipEditorLoading;
410            private String _toolbarSet = "liferay";
411            private String _width;
412    
413    }