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.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(getJsHelpMouseHandler(
165                    widgetDialog,
166                    locKey,
167                    CmsEncoder.escape(locValue, cms.getRequestContext().getEncoding())));
168            }
169            result.append("></td>");
170            return result.toString();
171        }
172    }
173
174    /**
175     * @see org.opencms.widgets.A_CmsWidget#getHelpText(org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
176     */
177    @Override
178    public String getHelpText(I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
179
180        String helpId = getDisabledHelpKey(param);
181        Set<String> helpIdsShown = widgetDialog.getHelpMessageIds();
182        if (helpIdsShown.contains(helpId)) {
183            // help hey has already been included in output
184            return "";
185        }
186        helpIdsShown.add(helpId);
187
188        // calculate the key
189        String locValue = widgetDialog.getMessages().key(helpId, true);
190        if (locValue == null) {
191            // there was no help message found for this key, so return an empty string
192            return "";
193        } else {
194            if (widgetDialog.useNewStyle()) {
195                StringBuffer result = new StringBuffer(128);
196                result.append("<div class=\"help\" id=\"help");
197                result.append(helpId);
198                result.append("\"");
199                result.append(getJsHelpMouseHandler(widgetDialog, helpId, helpId));
200                result.append(">");
201                result.append(locValue);
202                result.append("</div>\n");
203                return result.toString();
204            } else {
205                return "";
206            }
207        }
208    }
209
210    /**
211     * @see org.opencms.widgets.I_CmsADEWidget#getInitCall()
212     */
213    public String getInitCall() {
214
215        return null;
216    }
217
218    /**
219     * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject)
220     */
221    public List<String> getJavaScriptResourceLinks(CmsObject cms) {
222
223        return null;
224    }
225
226    /**
227     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
228     */
229    public String getWidgetName() {
230
231        return CmsDisplayWidget.class.getName();
232    }
233
234    /**
235     * @see org.opencms.widgets.I_CmsADEWidget#isInternal()
236     */
237    public boolean isInternal() {
238
239        return true;
240    }
241
242    /**
243     * @see org.opencms.widgets.I_CmsWidget#newInstance()
244     */
245    public I_CmsWidget newInstance() {
246
247        return new CmsDisplayWidget(getConfiguration());
248    }
249
250    /**
251     * Returns the localized help key for the provided widget parameter.<p>
252     *
253     * @param param the widget parameter to return the localized help key for
254     *
255     * @return the localized help key for the provided widget parameter
256     */
257    private String getDisabledHelpKey(I_CmsWidgetParameter param) {
258
259        StringBuffer result = new StringBuffer(64);
260        result.append(LABEL_PREFIX);
261        result.append(param.getKey());
262        result.append(HELP_POSTFIX);
263        result.append(DISABLED_POSTFIX);
264        return result.toString();
265    }
266}