001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.widgets;
029
030import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants;
031import org.opencms.file.CmsObject;
032import org.opencms.file.types.CmsResourceTypeImage;
033import org.opencms.i18n.CmsMessages;
034import org.opencms.json.JSONArray;
035import org.opencms.json.JSONException;
036import org.opencms.json.JSONObject;
037import org.opencms.main.OpenCms;
038import org.opencms.util.CmsStringUtil;
039
040/**
041 * ADE image gallery widget implementations.<p>
042 *
043 * @since 8.0.0
044 */
045public class CmsAdeImageGalleryWidget extends A_CmsAdeGalleryWidget {
046
047    /** The gallery name. */
048    private static final String GALLERY_NAME = "image";
049
050    /** The widget configuration. */
051    private CmsVfsImageWidgetConfiguration m_widgetConfiguration;
052
053    /**
054     * Constructor.<p>
055     */
056    public CmsAdeImageGalleryWidget() {
057
058        this("");
059    }
060
061    /**
062     * Creates a new gallery widget with the given configuration.<p>
063     *
064     * @param configuration the configuration to use
065     */
066    protected CmsAdeImageGalleryWidget(String configuration) {
067
068        super(configuration);
069    }
070
071    /**
072     * @see org.opencms.widgets.A_CmsAdeGalleryWidget#getGalleryName()
073     */
074    @Override
075    public String getGalleryName() {
076
077        return GALLERY_NAME;
078    }
079
080    /**
081     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
082     */
083    @Override
084    public String getWidgetName() {
085
086        return CmsAdeImageGalleryWidget.class.getName();
087    }
088
089    /**
090     * @see org.opencms.widgets.I_CmsWidget#newInstance()
091     */
092    public I_CmsWidget newInstance() {
093
094        return new CmsAdeImageGalleryWidget(getConfiguration());
095    }
096
097    /**
098     * @see org.opencms.widgets.A_CmsAdeGalleryWidget#getAdditionalGalleryInfo(org.opencms.file.CmsObject, java.lang.String, org.opencms.i18n.CmsMessages, org.opencms.widgets.I_CmsWidgetParameter)
099     */
100    @Override
101    protected JSONObject getAdditionalGalleryInfo(
102        CmsObject cms,
103        String resource,
104        CmsMessages messages,
105        I_CmsWidgetParameter param)
106    throws JSONException {
107
108        CmsVfsImageWidgetConfiguration config = getWidgetConfiguration(cms, messages, param);
109        JSONObject result = new JSONObject();
110        result.put(I_CmsGalleryProviderConstants.CONFIG_USE_FORMATS, config.isShowFormat());
111        result.put(I_CmsGalleryProviderConstants.CONFIG_IMAGE_FORMATS, new JSONArray(config.getFormatValues()));
112        String temp = config.getSelectFormatString();
113        String[] formatNames = new String[0];
114        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(temp)) {
115            formatNames = config.getSelectFormatString().split("\\|");
116        }
117        result.put(I_CmsGalleryProviderConstants.CONFIG_IMAGE_FORMAT_NAMES, new JSONArray(formatNames));
118        result.put(I_CmsGalleryProviderConstants.CONFIG_TAB_CONFIG, "selectDoc");
119        String uploadFolder = OpenCms.getWorkplaceManager().getRepositoryFolderHandler().getRepositoryFolder(
120            cms,
121            resource,
122            GALLERY_NAME + "gallery");
123        if (uploadFolder != null) {
124            result.put(I_CmsGalleryProviderConstants.CONFIG_UPLOAD_FOLDER, uploadFolder);
125        }
126        return result;
127    }
128
129    /**
130     * @see org.opencms.widgets.A_CmsAdeGalleryWidget#getGalleryStoragePrefix()
131     */
132    @Override
133    protected String getGalleryStoragePrefix() {
134
135        return "image";
136    }
137
138    /**
139     * @see org.opencms.widgets.A_CmsAdeGalleryWidget#getGalleryTypes()
140     */
141    @Override
142    protected String getGalleryTypes() {
143
144        return CmsResourceTypeImage.getStaticTypeName();
145    }
146
147    /**
148     * @see org.opencms.widgets.A_CmsAdeGalleryWidget#getOpenPreviewCall(org.opencms.widgets.I_CmsWidgetDialog, java.lang.String)
149     */
150    @Override
151    protected String getOpenPreviewCall(I_CmsWidgetDialog widgetDialog, String id) {
152
153        // using the 'cmsOpenImagePreview' function instead of 'cmsOpenPreview'
154        StringBuffer sb = new StringBuffer(64);
155        // using the 'cmsOpenImagePreview' function instead of 'cmsOpenPreview'
156        sb.append("javascript:cmsOpenImagePreview('").append(
157            widgetDialog.getMessages().key(Messages.GUI_BUTTON_PREVIEW_0));
158        sb.append("', '").append(OpenCms.getSystemInfo().getOpenCmsContext());
159        sb.append("', '").append(id);
160        sb.append("'); return false;");
161        return sb.toString();
162    }
163
164    /**
165     * @see org.opencms.widgets.A_CmsAdeGalleryWidget#getWidgetConfiguration(org.opencms.file.CmsObject, org.opencms.i18n.CmsMessages, org.opencms.widgets.I_CmsWidgetParameter)
166     */
167    @Override
168    protected CmsVfsImageWidgetConfiguration getWidgetConfiguration(
169        CmsObject cms,
170        CmsMessages messages,
171        I_CmsWidgetParameter param) {
172
173        if (m_widgetConfiguration == null) {
174            m_widgetConfiguration = new CmsVfsImageWidgetConfiguration(cms, messages, param, getConfiguration());
175        }
176        return m_widgetConfiguration;
177    }
178}