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