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