001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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 GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://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.file.CmsObject;
031import org.opencms.i18n.CmsEncoder;
032import org.opencms.workplace.CmsWorkplace;
033
034import java.util.Map;
035
036/**
037 * Provides a widget that creates a rich input field using the matching component, for use on a widget dialog.<p>
038 *
039 * The matching component is determined by checking the installed editors for the best matching component to use.<p>
040 *
041 * @since 6.0.1
042 */
043public abstract class A_CmsHtmlWidget extends A_CmsWidget {
044
045    /** The configured Html widget options. */
046    private CmsHtmlWidgetOption m_htmlWidgetOption;
047
048    /**
049     * Creates a new html editing widget.<p>
050     */
051    public A_CmsHtmlWidget() {
052
053        // empty constructor is required for class registration
054        super();
055    }
056
057    /**
058     * Creates a new html editing widget with the given configuration.<p>
059     *
060     * @param configuration the configuration to use
061     */
062    public A_CmsHtmlWidget(CmsHtmlWidgetOption configuration) {
063
064        super();
065        m_htmlWidgetOption = configuration;
066    }
067
068    /**
069     * Creates a new html editing widget with the given configuration.<p>
070     *
071     * @param configuration the configuration to use
072     */
073    public A_CmsHtmlWidget(String configuration) {
074
075        super(configuration);
076    }
077
078    /**
079     * @see org.opencms.widgets.A_CmsWidget#getConfiguration()
080     */
081    @Override
082    public String getConfiguration() {
083
084        if (super.getConfiguration() != null) {
085            return super.getConfiguration();
086        }
087        return CmsHtmlWidgetOption.createConfigurationString(getHtmlWidgetOption());
088    }
089
090    /**
091     * Returns the configured Html widget options.<p>
092     *
093     * @return the configured Html widget options
094     */
095    public CmsHtmlWidgetOption getHtmlWidgetOption() {
096
097        return m_htmlWidgetOption;
098    }
099
100    /**
101     * @see org.opencms.widgets.I_CmsWidget#setConfiguration(java.lang.String)
102     */
103    @Override
104    public void setConfiguration(String configuration) {
105
106        super.setConfiguration(configuration);
107        m_htmlWidgetOption = new CmsHtmlWidgetOption(configuration);
108    }
109
110    /**
111     * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
112     */
113    @Override
114    public void setEditorValue(
115        CmsObject cms,
116        Map<String, String[]> formParameters,
117        I_CmsWidgetDialog widgetDialog,
118        I_CmsWidgetParameter param) {
119
120        String[] values = formParameters.get(param.getId());
121        if ((values != null) && (values.length > 0)) {
122            String val = CmsEncoder.decode(values[0], CmsEncoder.ENCODING_UTF_8);
123            param.setStringValue(cms, val);
124        }
125    }
126
127    /**
128     * Sets the configured Html widget options.<p>
129     *
130     * @param htmlWidgetOption the configured Html widget options
131     */
132    public void setHtmlWidgetOption(CmsHtmlWidgetOption htmlWidgetOption) {
133
134        m_htmlWidgetOption = htmlWidgetOption;
135    }
136
137    /**
138     * Returns the HTML for the OpenCms specific button row for galleries and links.<p>
139     *
140     * Use this method to generate a button row with OpenCms specific dialog buttons in the
141     * {@link org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)}
142     * method to obtain the buttons.<p>
143     *
144     * Overwrite the method if the integrated editor needs a specific button generation
145     * (e.g. add format select or toggle source code button) or if some buttons should not be available.<p>
146     *
147     * @param widgetDialog the dialog where the widget is used on
148     * @param paramId the id of the current widget
149     * @return the html String for the OpenCms specific button row
150     */
151    protected String buildOpenCmsButtonRow(I_CmsWidgetDialog widgetDialog, String paramId) {
152
153        StringBuffer result = new StringBuffer(2048);
154        // flag indicating if at least one button is active
155        boolean buttonsActive = false;
156
157        // generate button row start HTML
158        result.append(buildOpenCmsButtonRow(CmsWorkplace.HTML_START, widgetDialog));
159
160        // build the link buttons
161        if (getHtmlWidgetOption().showLinkDialog()) {
162            result.append(
163                widgetDialog.button(
164                    "javascript:setActiveEditor('"
165                        + paramId
166                        + "');openLinkDialog('"
167                        + Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.GUI_BUTTON_LINKTO_0)
168                        + "');",
169                    null,
170                    "link",
171                    "button.linkto",
172                    widgetDialog.getButtonStyle()));
173            buttonsActive = true;
174        }
175        if (getHtmlWidgetOption().showAnchorDialog()) {
176            result.append(
177                widgetDialog.button(
178                    "javascript:setActiveEditor('"
179                        + paramId
180                        + "');openAnchorDialog('"
181                        + Messages.get().getBundle(widgetDialog.getLocale()).key(
182                            Messages.ERR_EDITOR_MESSAGE_NOSELECTION_0)
183                        + "');",
184                    null,
185                    "anchor",
186                    Messages.GUI_BUTTON_ANCHOR_0,
187                    widgetDialog.getButtonStyle()));
188            buttonsActive = true;
189        }
190
191        if (!buttonsActive) {
192            // no active buttons to show, return empty String
193            return "";
194        }
195
196        // generate button row end HTML
197        result.append(buildOpenCmsButtonRow(CmsWorkplace.HTML_END, widgetDialog));
198
199        // show the active buttons
200        return result.toString();
201
202    }
203
204    /**
205     * Returns the start or end HTML for the OpenCms specific button row.<p>
206     *
207     * Use this method to generate the start and end html for the button row.<p>
208     *
209     * Overwrite the method if the integrated editor needs a specific layout for the button row start or end html.<p>
210     *
211     * @param segment the HTML segment (START / END)
212     * @param widgetDialog the dialog where the widget is used on
213     * @return the html String for the OpenCms specific button row
214     */
215    protected String buildOpenCmsButtonRow(int segment, I_CmsWidgetDialog widgetDialog) {
216
217        StringBuffer result = new StringBuffer(256);
218
219        if (segment == CmsWorkplace.HTML_START) {
220            // generate line and start row HTML
221            result.append(widgetDialog.buttonBarHorizontalLine());
222            result.append(widgetDialog.buttonBar(CmsWorkplace.HTML_START));
223            result.append(widgetDialog.buttonBarStartTab(0, 0));
224        } else {
225            // close button row and generate end line
226            result.append(widgetDialog.buttonBar(CmsWorkplace.HTML_END));
227            result.append(widgetDialog.buttonBarHorizontalLine());
228        }
229
230        return result.toString();
231    }
232}