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 = getApiKey(cms, sitePath); 118 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(apiKey)) { 119 conf.put(CONFIG_API_KEY, apiKey); 120 } 121 } catch (CmsException e) { 122 LOG.error(e.getLocalizedMessage(), e); 123 } 124 } 125 config = conf.toString(); 126 } catch (JSONException e) { 127 config = DEFAULT_CONFIG; 128 LOG.error(e.getLocalizedMessage(), e); 129 } 130 131 return config; 132 } 133 134 /** 135 * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject) 136 */ 137 public List<String> getCssResourceLinks(CmsObject cms) { 138 139 return null; 140 } 141 142 /** 143 * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType() 144 */ 145 public DisplayType getDefaultDisplayType() { 146 147 return DisplayType.singleline; 148 } 149 150 /** 151 * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 152 */ 153 public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 154 155 String value = param.getStringValue(cms); 156 String localizedValue = value; 157 if (CmsStringUtil.TRUE.equalsIgnoreCase(value) || CmsStringUtil.FALSE.equalsIgnoreCase(value)) { 158 boolean booleanValue = Boolean.valueOf(value).booleanValue(); 159 if (booleanValue) { 160 localizedValue = Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.GUI_LABEL_TRUE_0); 161 } else { 162 localizedValue = Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.GUI_LABEL_FALSE_0); 163 } 164 } 165 166 String id = param.getId(); 167 StringBuffer result = new StringBuffer(16); 168 result.append("<td class=\"xmlTd\">"); 169 result.append("<span class=\"xmlInput textInput\" style=\"border: 0px solid black;\">"); 170 if (CmsStringUtil.isNotEmpty(getConfiguration())) { 171 result.append(getConfiguration()); 172 } else { 173 result.append(localizedValue); 174 } 175 result.append("</span>"); 176 result.append("<input type=\"hidden\""); 177 result.append(" name=\""); 178 result.append(id); 179 result.append("\" id=\""); 180 result.append(id); 181 result.append("\" value=\""); 182 result.append(CmsEncoder.escapeXml(value)); 183 result.append("\">"); 184 result.append("</td>"); 185 186 return result.toString(); 187 } 188 189 /** 190 * @see org.opencms.widgets.A_CmsWidget#getHelpBubble(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 191 */ 192 @Override 193 public String getHelpBubble(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 194 195 StringBuffer result = new StringBuffer(128); 196 String locKey = getDisabledHelpKey(param); 197 String locValue = widgetDialog.getMessages().key(locKey, true); 198 if (locValue == null) { 199 // there was no help message found for this key, so return a spacer cell 200 return widgetDialog.dialogHorizontalSpacer(16); 201 } else { 202 result.append("<td>"); 203 result.append("<img name=\"img"); 204 result.append(locKey); 205 result.append("\" id=\"img"); 206 result.append(locKey); 207 result.append("\" src=\""); 208 result.append(OpenCms.getLinkManager().substituteLink(cms, "/system/workplace/resources/commons/help.png")); 209 result.append("\" alt=\"\" border=\"0\""); 210 if (widgetDialog.useNewStyle()) { 211 result.append(getJsHelpMouseHandler(widgetDialog, locKey, null)); 212 } else { 213 result.append( 214 getJsHelpMouseHandler( 215 widgetDialog, 216 locKey, 217 CmsEncoder.escape(locValue, cms.getRequestContext().getEncoding()))); 218 } 219 result.append("></td>"); 220 return result.toString(); 221 } 222 } 223 224 /** 225 * @see org.opencms.widgets.A_CmsWidget#getHelpText(org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 226 */ 227 @Override 228 public String getHelpText(I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 229 230 String helpId = getDisabledHelpKey(param); 231 Set<String> helpIdsShown = widgetDialog.getHelpMessageIds(); 232 if (helpIdsShown.contains(helpId)) { 233 // help hey has already been included in output 234 return ""; 235 } 236 helpIdsShown.add(helpId); 237 238 // calculate the key 239 String locValue = widgetDialog.getMessages().key(helpId, true); 240 if (locValue == null) { 241 // there was no help message found for this key, so return an empty string 242 return ""; 243 } else { 244 if (widgetDialog.useNewStyle()) { 245 StringBuffer result = new StringBuffer(128); 246 result.append("<div class=\"help\" id=\"help"); 247 result.append(helpId); 248 result.append("\""); 249 result.append(getJsHelpMouseHandler(widgetDialog, helpId, helpId)); 250 result.append(">"); 251 result.append(locValue); 252 result.append("</div>\n"); 253 return result.toString(); 254 } else { 255 return ""; 256 } 257 } 258 } 259 260 /** 261 * @see org.opencms.widgets.I_CmsADEWidget#getInitCall() 262 */ 263 public String getInitCall() { 264 265 return null; 266 } 267 268 /** 269 * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject) 270 */ 271 public List<String> getJavaScriptResourceLinks(CmsObject cms) { 272 273 return null; 274 } 275 276 /** 277 * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName() 278 */ 279 public String getWidgetName() { 280 281 return CmsLocationPickerWidget.class.getName(); 282 } 283 284 /** 285 * @see org.opencms.widgets.I_CmsADEWidget#isInternal() 286 */ 287 public boolean isInternal() { 288 289 return true; 290 } 291 292 /** 293 * @see org.opencms.widgets.I_CmsWidget#newInstance() 294 */ 295 public I_CmsWidget newInstance() { 296 297 return new CmsLocationPickerWidget(getConfiguration()); 298 } 299 300 /** 301 * Get the correct google api key. 302 * Tries to read a workplace key first. 303 * 304 * @param cms CmsObject 305 * @param sitePath site path 306 * @return key value 307 * @throws CmsException exception 308 */ 309 private String getApiKey(CmsObject cms, String sitePath) throws CmsException { 310 311 String res = cms.readPropertyObject( 312 sitePath, 313 CmsPropertyDefinition.PROPERTY_GOOGLE_API_KEY_WORKPLACE, 314 true).getValue(); 315 316 if (CmsStringUtil.isEmptyOrWhitespaceOnly(res)) { 317 res = cms.readPropertyObject(sitePath, CmsPropertyDefinition.PROPERTY_GOOGLE_API_KEY, true).getValue(); 318 } 319 return res; 320 321 } 322 323 /** 324 * Returns the localized help key for the provided widget parameter.<p> 325 * 326 * @param param the widget parameter to return the localized help key for 327 * 328 * @return the localized help key for the provided widget parameter 329 */ 330 private String getDisabledHelpKey(I_CmsWidgetParameter param) { 331 332 StringBuffer result = new StringBuffer(64); 333 result.append(LABEL_PREFIX); 334 result.append(param.getKey()); 335 result.append(HELP_POSTFIX); 336 result.append(DISABLED_POSTFIX); 337 return result.toString(); 338 } 339}