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 GmbH & Co. KG, 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.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.i18n.CmsEncoder;
033import org.opencms.i18n.CmsMessages;
034import org.opencms.main.OpenCms;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.xml.content.I_CmsXmlContentHandler.DisplayType;
037import org.opencms.xml.types.A_CmsXmlContentValue;
038
039import java.util.List;
040import java.util.Locale;
041import java.util.Set;
042
043/**
044 * Provides a display only widget, for use on a widget dialog.<p>
045 *
046 * @since 6.0.0
047 */
048public class CmsDisplayWidget extends A_CmsWidget implements I_CmsADEWidget {
049
050    /** Key post fix, so you can display different help text if used a "normal" widget, and a display widget. */
051    private static final String DISABLED_POSTFIX = ".disabled";
052
053    /**
054     * Creates a new input widget.<p>
055     */
056    public CmsDisplayWidget() {
057
058        // empty constructor is required for class registration
059        this("");
060    }
061
062    /**
063     * Creates a new input widget with the given configuration.<p>
064     *
065     * @param configuration the configuration to use
066     */
067    public CmsDisplayWidget(String configuration) {
068
069        super(configuration);
070    }
071
072    /**
073     * @see org.opencms.widgets.I_CmsADEWidget#getConfiguration(org.opencms.file.CmsObject, org.opencms.xml.types.A_CmsXmlContentValue, org.opencms.i18n.CmsMessages, org.opencms.file.CmsResource, java.util.Locale)
074     */
075    public String getConfiguration(
076        CmsObject cms,
077        A_CmsXmlContentValue schemaType,
078        CmsMessages messages,
079        CmsResource resource,
080        Locale contentLocale) {
081
082        return getConfiguration();
083    }
084
085    /**
086     * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject)
087     */
088    public List<String> getCssResourceLinks(CmsObject cms) {
089
090        return null;
091    }
092
093    /**
094     * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType()
095     */
096    public DisplayType getDefaultDisplayType() {
097
098        return DisplayType.singleline;
099    }
100
101    /**
102     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
103     */
104    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
105
106        String value = param.getStringValue(cms);
107        String localizedValue = value;
108        if (CmsStringUtil.TRUE.equalsIgnoreCase(value) || CmsStringUtil.FALSE.equalsIgnoreCase(value)) {
109            boolean booleanValue = Boolean.valueOf(value).booleanValue();
110            if (booleanValue) {
111                localizedValue = Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.GUI_LABEL_TRUE_0);
112            } else {
113                localizedValue = Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.GUI_LABEL_FALSE_0);
114            }
115        }
116
117        String id = param.getId();
118        StringBuffer result = new StringBuffer(16);
119        result.append("<td class=\"xmlTd\">");
120        result.append("<span class=\"xmlInput textInput\" style=\"border: 0px solid black;\">");
121        if (CmsStringUtil.isNotEmpty(getConfiguration())) {
122            result.append(getConfiguration());
123        } else {
124            result.append(localizedValue);
125        }
126        result.append("</span>");
127        result.append("<input type=\"hidden\"");
128        result.append(" name=\"");
129        result.append(id);
130        result.append("\" id=\"");
131        result.append(id);
132        result.append("\" value=\"");
133        result.append(CmsEncoder.escapeXml(value));
134        result.append("\">");
135        result.append("</td>");
136
137        return result.toString();
138    }
139
140    /**
141     * @see org.opencms.widgets.A_CmsWidget#getHelpBubble(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
142     */
143    @Override
144    public String getHelpBubble(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
145
146        StringBuffer result = new StringBuffer(128);
147        String locKey = getDisabledHelpKey(param);
148        String locValue = widgetDialog.getMessages().key(locKey, true);
149        if (locValue == null) {
150            // there was no help message found for this key, so return a spacer cell
151            return widgetDialog.dialogHorizontalSpacer(16);
152        } else {
153            result.append("<td>");
154            result.append("<img name=\"img");
155            result.append(locKey);
156            result.append("\" id=\"img");
157            result.append(locKey);
158            result.append("\" src=\"");
159            result.append(OpenCms.getLinkManager().substituteLink(cms, "/system/workplace/resources/commons/help.png"));
160            result.append("\" alt=\"\" border=\"0\"");
161            if (widgetDialog.useNewStyle()) {
162                result.append(getJsHelpMouseHandler(widgetDialog, locKey, null));
163            } else {
164                result.append(
165                    getJsHelpMouseHandler(
166                        widgetDialog,
167                        locKey,
168                        CmsEncoder.escape(locValue, cms.getRequestContext().getEncoding())));
169            }
170            result.append("></td>");
171            return result.toString();
172        }
173    }
174
175    /**
176     * @see org.opencms.widgets.A_CmsWidget#getHelpText(org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
177     */
178    @Override
179    public String getHelpText(I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
180
181        String helpId = getDisabledHelpKey(param);
182        Set<String> helpIdsShown = widgetDialog.getHelpMessageIds();
183        if (helpIdsShown.contains(helpId)) {
184            // help hey has already been included in output
185            return "";
186        }
187        helpIdsShown.add(helpId);
188
189        // calculate the key
190        String locValue = widgetDialog.getMessages().key(helpId, true);
191        if (locValue == null) {
192            // there was no help message found for this key, so return an empty string
193            return "";
194        } else {
195            if (widgetDialog.useNewStyle()) {
196                StringBuffer result = new StringBuffer(128);
197                result.append("<div class=\"help\" id=\"help");
198                result.append(helpId);
199                result.append("\"");
200                result.append(getJsHelpMouseHandler(widgetDialog, helpId, helpId));
201                result.append(">");
202                result.append(locValue);
203                result.append("</div>\n");
204                return result.toString();
205            } else {
206                return "";
207            }
208        }
209    }
210
211    /**
212     * @see org.opencms.widgets.I_CmsADEWidget#getInitCall()
213     */
214    public String getInitCall() {
215
216        return null;
217    }
218
219    /**
220     * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject)
221     */
222    public List<String> getJavaScriptResourceLinks(CmsObject cms) {
223
224        return null;
225    }
226
227    /**
228     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
229     */
230    public String getWidgetName() {
231
232        return CmsDisplayWidget.class.getName();
233    }
234
235    /**
236     * @see org.opencms.widgets.I_CmsADEWidget#isInternal()
237     */
238    public boolean isInternal() {
239
240        return true;
241    }
242
243    /**
244     * @see org.opencms.widgets.I_CmsWidget#newInstance()
245     */
246    public I_CmsWidget newInstance() {
247
248        return new CmsDisplayWidget(getConfiguration());
249    }
250
251    /**
252     * Returns the localized help key for the provided widget parameter.<p>
253     *
254     * @param param the widget parameter to return the localized help key for
255     *
256     * @return the localized help key for the provided widget parameter
257     */
258    private String getDisabledHelpKey(I_CmsWidgetParameter param) {
259
260        StringBuffer result = new StringBuffer(64);
261        result.append(LABEL_PREFIX);
262        result.append(param.getKey());
263        result.append(HELP_POSTFIX);
264        result.append(DISABLED_POSTFIX);
265        return result.toString();
266    }
267}