com.liferay.blogs.editor.configuration.internal.BlogsContentEditorConfigContributor.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.blogs.editor.configuration.internal.BlogsContentEditorConfigContributor.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.blogs.editor.configuration.internal;

import com.liferay.blogs.item.selector.criterion.BlogsItemSelectorCriterion;
import com.liferay.blogs.web.constants.BlogsPortletKeys;
import com.liferay.item.selector.ItemSelector;
import com.liferay.item.selector.ItemSelectorCriterion;
import com.liferay.item.selector.ItemSelectorReturnType;
import com.liferay.item.selector.criteria.FileEntryItemSelectorReturnType;
import com.liferay.item.selector.criteria.URLItemSelectorReturnType;
import com.liferay.item.selector.criteria.image.criterion.ImageItemSelectorCriterion;
import com.liferay.item.selector.criteria.upload.criterion.UploadItemSelectorCriterion;
import com.liferay.item.selector.criteria.url.criterion.URLItemSelectorCriterion;
import com.liferay.portal.kernel.editor.configuration.BaseEditorConfigContributor;
import com.liferay.portal.kernel.editor.configuration.EditorConfigContributor;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.portlet.RequestBackedPortletURLFactory;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.PortletKeys;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.util.PropsValues;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.PortletURL;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Roberto Daz
 */
@Component(property = { "editor.config.key=contentEditor", "javax.portlet.name=" + BlogsPortletKeys.BLOGS,
        "javax.portlet.name=" + BlogsPortletKeys.BLOGS_ADMIN }, service = EditorConfigContributor.class)
public class BlogsContentEditorConfigContributor extends BaseEditorConfigContributor {

    @Override
    public void populateConfigJSONObject(JSONObject jsonObject, Map<String, Object> inputEditorTaglibAttributes,
            ThemeDisplay themeDisplay, RequestBackedPortletURLFactory requestBackedPortletURLFactory) {

        StringBundler sb = new StringBundler(6);

        sb.append("a[*](*); ");
        sb.append(getAllowedContentText());
        sb.append(" div(*); img[class, !src] {height, width}; ");
        sb.append(getAllowedContentLists());
        sb.append(" p {text-align}; ");
        sb.append(getAllowedContentTable());

        jsonObject.put("allowedContent", sb.toString());

        String namespace = GetterUtil
                .getString(inputEditorTaglibAttributes.get("liferay-ui:input-editor:namespace"));
        String name = GetterUtil.getString(inputEditorTaglibAttributes.get("liferay-ui:input-editor:name"));

        populateFileBrowserURL(jsonObject, themeDisplay, requestBackedPortletURLFactory,
                namespace + name + "selectItem");
    }

    @Reference(unbind = "-")
    public void setItemSelector(ItemSelector itemSelector) {
        _itemSelector = itemSelector;
    }

    protected String getAllowedContentLists() {
        return "li ol ul;";
    }

    protected String getAllowedContentTable() {
        return "table[border, cellpadding, cellspacing] {width}; tbody td " + "th[scope]; thead tr[scope];";
    }

    protected String getAllowedContentText() {
        return "b code em h1 h2 h3 h4 h5 h6 hr i pre strong u;";
    }

    protected void populateFileBrowserURL(JSONObject jsonObject, ThemeDisplay themeDisplay,
            RequestBackedPortletURLFactory requestBackedPortletURLFactory, String eventName) {

        List<ItemSelectorReturnType> blogsContentEditorDesiredItemSelectorReturnTypes = new ArrayList<>();

        blogsContentEditorDesiredItemSelectorReturnTypes.add(new FileEntryItemSelectorReturnType());

        blogsContentEditorDesiredItemSelectorReturnTypes.add(new URLItemSelectorReturnType());

        ItemSelectorCriterion blogsItemSelectorCriterion = new BlogsItemSelectorCriterion();

        blogsItemSelectorCriterion
                .setDesiredItemSelectorReturnTypes(blogsContentEditorDesiredItemSelectorReturnTypes);

        ItemSelectorCriterion imageItemSelectorCriterion = new ImageItemSelectorCriterion();

        imageItemSelectorCriterion
                .setDesiredItemSelectorReturnTypes(blogsContentEditorDesiredItemSelectorReturnTypes);

        ItemSelectorCriterion urlItemSelectorCriterion = new URLItemSelectorCriterion();

        List<ItemSelectorReturnType> urlDesiredItemSelectorReturnTypes = new ArrayList<>();

        urlDesiredItemSelectorReturnTypes.add(new URLItemSelectorReturnType());

        urlItemSelectorCriterion.setDesiredItemSelectorReturnTypes(urlDesiredItemSelectorReturnTypes);

        PortletURL uploadURL = requestBackedPortletURLFactory.createActionURL(PortletKeys.BLOGS);

        uploadURL.setParameter(ActionRequest.ACTION_NAME, "/blogs/upload_image");

        ItemSelectorCriterion uploadItemSelectorCriterion = new UploadItemSelectorCriterion(uploadURL.toString(),
                LanguageUtil.get(themeDisplay.getLocale(), "blog-images"), PropsValues.BLOGS_IMAGE_MAX_SIZE);

        List<ItemSelectorReturnType> uploadDesiredItemSelectorReturnTypes = new ArrayList<>();

        uploadDesiredItemSelectorReturnTypes.add(new FileEntryItemSelectorReturnType());

        uploadItemSelectorCriterion.setDesiredItemSelectorReturnTypes(uploadDesiredItemSelectorReturnTypes);

        PortletURL itemSelectorURL = _itemSelector.getItemSelectorURL(requestBackedPortletURLFactory, eventName,
                blogsItemSelectorCriterion, imageItemSelectorCriterion, urlItemSelectorCriterion,
                uploadItemSelectorCriterion);

        jsonObject.put("filebrowserImageBrowseLinkUrl", itemSelectorURL.toString());
        jsonObject.put("filebrowserImageBrowseUrl", itemSelectorURL.toString());
    }

    private ItemSelector _itemSelector;

}