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, 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.gwt.client.property; 029 030import org.opencms.gwt.client.Messages; 031import org.opencms.gwt.client.ui.CmsPopup; 032import org.opencms.gwt.client.ui.CmsScrollPanel; 033import org.opencms.gwt.client.ui.input.CmsTextArea; 034import org.opencms.gwt.client.ui.input.CmsTextBox; 035import org.opencms.gwt.client.ui.input.I_CmsFormWidget; 036import org.opencms.gwt.client.ui.input.I_CmsHasGhostValue; 037import org.opencms.gwt.client.ui.input.I_CmsStringModel; 038import org.opencms.gwt.client.ui.input.form.CmsBasicFormField; 039import org.opencms.gwt.client.util.CmsDebugLog; 040import org.opencms.gwt.client.util.CmsDomUtil; 041import org.opencms.gwt.client.util.CmsDomUtil.Style; 042import org.opencms.gwt.shared.CmsListInfoBean; 043import org.opencms.gwt.shared.property.CmsClientProperty; 044import org.opencms.gwt.shared.property.CmsClientProperty.Mode; 045import org.opencms.gwt.shared.property.CmsPathValue; 046import org.opencms.util.CmsPair; 047import org.opencms.util.CmsStringUtil; 048import org.opencms.util.CmsUUID; 049import org.opencms.xml.content.CmsXmlContentProperty; 050 051import java.util.ArrayList; 052import java.util.Collections; 053import java.util.HashMap; 054import java.util.List; 055import java.util.Map; 056 057import com.google.common.base.Joiner; 058import com.google.common.base.Objects; 059import com.google.common.base.Optional; 060import com.google.common.collect.BiMap; 061import com.google.common.collect.HashBiMap; 062import com.google.gwt.core.client.Scheduler; 063import com.google.gwt.core.client.Scheduler.RepeatingCommand; 064import com.google.gwt.dom.client.Element; 065import com.google.gwt.event.dom.client.FocusEvent; 066import com.google.gwt.event.dom.client.FocusHandler; 067import com.google.gwt.event.dom.client.HasFocusHandlers; 068import com.google.gwt.event.logical.shared.BeforeSelectionEvent; 069import com.google.gwt.event.logical.shared.BeforeSelectionHandler; 070import com.google.gwt.event.logical.shared.ValueChangeEvent; 071import com.google.gwt.event.logical.shared.ValueChangeHandler; 072import com.google.gwt.event.shared.EventBus; 073import com.google.gwt.event.shared.GwtEvent; 074import com.google.gwt.event.shared.HandlerRegistration; 075import com.google.gwt.event.shared.SimpleEventBus; 076import com.google.gwt.user.client.ui.Widget; 077 078/** 079 * The sitemap entry editor class for the VFS mode.<p> 080 * 081 * @since 8.0.0 082 */ 083public class CmsVfsModePropertyEditor extends A_CmsPropertyEditor { 084 085 /** The interval used for updating the height. */ 086 public static final int UPDATE_HEIGHT_INTERVAL = 70; 087 088 /** True when resizing of the dialog is disabled. */ 089 protected static boolean m_resizeDisabled; 090 091 /** The map of tab names. */ 092 private static BiMap<CmsClientProperty.Mode, String> tabs; 093 094 static { 095 tabs = HashBiMap.create(); 096 tabs.put(Mode.effective, CmsPropertyPanel.TAB_SIMPLE); 097 tabs.put(Mode.structure, CmsPropertyPanel.TAB_INDIVIDUAL); 098 tabs.put(Mode.resource, CmsPropertyPanel.TAB_SHARED); 099 } 100 101 /** The map of models of the fields. */ 102 Map<String, I_CmsStringModel> m_models = new HashMap<String, I_CmsStringModel>(); 103 104 /** Active field data. */ 105 private CmsActiveFieldData m_activeFieldData; 106 107 /** Field data which should be restored. */ 108 private CmsActiveFieldData m_fieldDataToBeRestored; 109 110 /** Flag to control whether the properties should be editable. */ 111 private boolean m_isReadOnly; 112 113 /** The previous tab index. */ 114 private int m_oldTabIndex = -1; 115 116 /** The panel used for editing the properties. */ 117 private CmsPropertyPanel m_panel; 118 119 /** The properties of the entry. */ 120 private Map<String, CmsClientProperty> m_properties; 121 122 /** Flag which indicates whether the resource properties should be editable. */ 123 private boolean m_showResourceProperties; 124 125 /** 126 * Creates a new sitemap entry editor instance for the VFS mode.<p> 127 * 128 * @param propConfig the property configuration 129 * @param handler the sitemap entry editor handler 130 */ 131 public CmsVfsModePropertyEditor(Map<String, CmsXmlContentProperty> propConfig, I_CmsPropertyEditorHandler handler) { 132 133 super(propConfig, handler); 134 m_properties = CmsClientProperty.makeLazyCopy(handler.getOwnProperties()); 135 } 136 137 /** 138 * Disables resizing.<p> 139 * 140 * @param disabled true if resizing should be disabled 141 */ 142 public static void disableResize(boolean disabled) { 143 144 m_resizeDisabled = disabled; 145 } 146 147 /** 148 * @see org.opencms.gwt.client.property.A_CmsPropertyEditor#buildFields() 149 */ 150 @Override 151 public void buildFields() { 152 153 internalBuildConfiguredFields(); 154 internalBuildFields(Mode.structure); 155 if (m_showResourceProperties) { 156 internalBuildFields(Mode.resource); 157 } 158 } 159 160 /** 161 * @see org.opencms.gwt.client.property.A_CmsPropertyEditor#createFormWidget(java.lang.String, java.util.Map, com.google.common.base.Optional) 162 */ 163 @Override 164 public I_CmsFormWidget createFormWidget( 165 String key, 166 Map<String, String> widgetParams, 167 Optional<String> defaultValue) { 168 169 I_CmsFormWidget widget = super.createFormWidget(key, widgetParams, defaultValue); 170 if (m_isReadOnly) { 171 widget.setEnabled(false); 172 } 173 return widget; 174 } 175 176 /** 177 * Focuses the file name field (delayed).<p> 178 */ 179 public void focusNameField() { 180 181 m_activeFieldData = new CmsActiveFieldData(null, "simple", A_CmsPropertyEditor.FIELD_URLNAME); 182 m_panel.tryToRestoreFieldData(m_activeFieldData); 183 184 } 185 186 /** 187 * Gets the active field data.<p> 188 * 189 * @return the active field data 190 */ 191 public CmsActiveFieldData getActiveFieldData() { 192 193 return m_activeFieldData; 194 } 195 196 /** 197 * @see org.opencms.gwt.client.property.A_CmsPropertyEditor#initializeWidgets(org.opencms.gwt.client.ui.CmsPopup) 198 */ 199 @Override 200 public void initializeWidgets(final CmsPopup dialog) { 201 202 super.initializeWidgets(dialog); 203 dialog.setCaption(null); 204 dialog.removePadding(); 205 m_panel.tryToRestoreFieldData(m_fieldDataToBeRestored); 206 207 Scheduler.get().scheduleFixedDelay(new RepeatingCommand() { 208 209 public boolean execute() { 210 211 if (!getPropertyPanel().getTabPanel().isAttached() || !getPropertyPanel().getTabPanel().isVisible()) { 212 return false; 213 } 214 if (!m_resizeDisabled) { 215 updateHeight(dialog); 216 } 217 return true; 218 } 219 }, UPDATE_HEIGHT_INTERVAL); 220 } 221 222 /** 223 * Sets the active field data to be restored.<p> 224 * 225 * @param fieldData the active field data to be restored 226 */ 227 public void restoreActiveFieldData(CmsActiveFieldData fieldData) { 228 229 m_fieldDataToBeRestored = fieldData; 230 231 } 232 233 /** 234 * Sets the "readonly" mode.<p> 235 * 236 * @param readonly if true, readonly mode will be enabled 237 */ 238 public void setReadOnly(boolean readonly) { 239 240 m_isReadOnly = readonly; 241 } 242 243 /** 244 * Sets the "show resource properties" flag which controls whether the resource value fields should be built.<p> 245 * 246 * @param showResourceProperties if true, the resource value fields will be build 247 */ 248 public void setShowResourceProperties(boolean showResourceProperties) { 249 250 m_showResourceProperties = showResourceProperties; 251 } 252 253 /** 254 * Returns the property panel.<p> 255 * 256 * @return the property panel 257 */ 258 protected CmsPropertyPanel getPropertyPanel() { 259 260 return m_panel; 261 } 262 263 /** 264 * Method which is called when the tab is switched.<p> 265 * 266 * @param toTab the tab to which the user is switching 267 */ 268 protected void handleSwitchTab(int toTab) { 269 270 switch (toTab) { 271 case 0: 272 rebuildSimpleTab(); 273 break; 274 case 1: 275 rebuildIndividualTab(); 276 break; 277 case 2: 278 rebuildSharedTab(); 279 break; 280 default: 281 break; 282 } 283 if ((m_disabledReason != null) && !m_nameOnlyDisabled) { 284 disableInput(m_disabledReason, m_nameOnlyDisabled); 285 } else if (m_nameOnlyDisabled) { 286 disableInput(m_disabledReason, true); 287 m_form.validateAllFields(); 288 } else { 289 m_form.validateAllFields(); 290 } 291 } 292 293 /** 294 * @see org.opencms.gwt.client.property.A_CmsPropertyEditor#setupFieldContainer() 295 */ 296 @Override 297 protected void setupFieldContainer() { 298 299 CmsListInfoBean info = m_handler.getPageInfo(); 300 m_panel = new CmsPropertyPanel(m_showResourceProperties, info); 301 String modeClass = m_handler.getModeClass(); 302 if (modeClass != null) { 303 m_panel.addStyleName(modeClass); 304 } 305 m_panel.addBeforeSelectionHandler(new BeforeSelectionHandler<Integer>() { 306 307 public void onBeforeSelection(BeforeSelectionEvent<Integer> event) { 308 309 int target = event.getItem().intValue(); 310 handleSwitchTab(target); 311 } 312 }); 313 m_form.setWidget(m_panel); 314 315 } 316 317 /** 318 * Updates the panel height depending on the content of the current tab.<p> 319 * 320 * @param dialog the dialog for which the height should be updated 321 */ 322 protected void updateHeight(CmsPopup dialog) { 323 324 int tabIndex = m_panel.getTabPanel().getSelectedIndex(); 325 boolean changedTab = tabIndex != m_oldTabIndex; 326 m_oldTabIndex = tabIndex; 327 CmsScrollPanel tabWidget = m_panel.getTabPanel().getWidget(tabIndex).getScrollPanel(); 328 Element innerElement = tabWidget.getWidget().getElement(); 329 int contentHeight = CmsDomUtil.getCurrentStyleInt(innerElement, Style.height); 330 int spaceLeft = dialog.getAvailableHeight(0); 331 int extraSpaceOutsideOfTab = 50; 332 int extraSpaceInTabOutsideOfScrollPanel = 38; 333 // The second and later tabs start out with a collapsed fieldset at the bottom, which takes more vertical space which is not included in the offsetHeight of the scroll panel content. 334 // So to not make things look weird, we use different sizing of the scroll panel for the first tab than for the others. 335 int extraSpaceInScrollPanel = tabIndex == 0 ? 4 : 10; 336 int newTabPanelHeight = Math.min( 337 spaceLeft, 338 contentHeight + extraSpaceOutsideOfTab + extraSpaceInTabOutsideOfScrollPanel + extraSpaceInScrollPanel); 339 int newTabHeight = newTabPanelHeight - extraSpaceOutsideOfTab; 340 int newScrollPanelHeight = newTabHeight - extraSpaceInTabOutsideOfScrollPanel; 341 boolean changedHeight = m_panel.getTabPanel().getOffsetHeight() != newTabPanelHeight; 342 if (changedHeight || changedTab) { 343 m_panel.getTabPanel().setHeight(newTabPanelHeight + "px"); 344 int selectedIndex = m_panel.getTabPanel().getSelectedIndex(); 345 CmsScrollPanel widget = m_panel.getTabPanel().getWidget(selectedIndex).getScrollPanel(); 346 widget.setHeight(newScrollPanelHeight + "px"); 347 widget.onResizeDescendant(); 348 } 349 } 350 351 /** 352 * Builds a single form field.<p> 353 * 354 * @param ownProps the entry's own properties 355 * @param propName the property name 356 * @param mode the mode which controls which kind of field will be built 357 */ 358 private void buildField( 359 Map<String, CmsClientProperty> ownProps, 360 final String propName, 361 CmsClientProperty.Mode mode) { 362 363 String entryId = m_handler.getId().toString(); 364 CmsXmlContentProperty propDef = m_propertyConfig.get(propName); 365 366 if (propDef == null) { 367 String widget = CmsClientProperty.PROPERTY_TEMPLATE.equals(propName) ? "template" : "string"; 368 propDef = new CmsXmlContentProperty( 369 propName, 370 "string", 371 widget, 372 "", 373 null, 374 null, 375 null, 376 null, 377 null, 378 null, 379 null); 380 } 381 382 if (mode != Mode.effective) { 383 propDef = propDef.withNiceName(propName); 384 } 385 386 CmsClientProperty ownProp = m_properties.get(propName); 387 CmsPathValue pathValue = CmsClientProperty.getPathValue(ownProp, mode).prepend(entryId + "/" + propName + "/"); 388 389 //CHECK: should fields other than NavText be really automatically allowed to be empty in navigation mode? 390 final String tab = tabs.get(mode); 391 final CmsBasicFormField field = CmsBasicFormField.createField( 392 propDef, 393 pathValue.getPath() + "#" + tab, 394 this, 395 Collections.<String, String> emptyMap(), 396 true); 397 398 CmsPair<String, String> defaultValueAndOrigin = getDefaultValueToDisplay(ownProp, mode); 399 String defaultValue = ""; 400 String origin = ""; 401 if (defaultValueAndOrigin != null) { 402 defaultValue = defaultValueAndOrigin.getFirst(); 403 origin = defaultValueAndOrigin.getSecond(); 404 } 405 final Widget w = (Widget)field.getWidget(); 406 I_CmsStringModel model = getStringModel(pathValue); 407 field.bind(model); 408 boolean ghost = CmsStringUtil.isEmptyOrWhitespaceOnly(pathValue.getValue()); 409 String initialValue = pathValue.getValue(); 410 if (w instanceof I_CmsHasGhostValue) { 411 ((I_CmsHasGhostValue)w).setGhostValue(defaultValue, ghost); 412 if (ghost) { 413 initialValue = null; 414 } 415 } 416 if (w instanceof HasFocusHandlers) { 417 try { 418 ((HasFocusHandlers)w).addFocusHandler(new FocusHandler() { 419 420 @SuppressWarnings("synthetic-access") 421 public void onFocus(FocusEvent event) { 422 423 if ((w instanceof CmsTextBox) || (w instanceof CmsTextArea)) { 424 m_activeFieldData = new CmsActiveFieldData(field, tab, propName); 425 } else { 426 // field received focus, but it doesn't make sense to restore it later 427 m_activeFieldData = null; 428 } 429 } 430 }); 431 } catch (Exception e) { 432 CmsDebugLog.consoleLog("" + e); 433 } 434 435 } 436 437 boolean isShowingGhost = ghost && !CmsStringUtil.isEmpty(defaultValue); 438 439 if (isShowingGhost) { 440 field.getLayoutData().put("info", origin); 441 } 442 if (!ghost || isShowingGhost) { 443 field.getLayoutData().put(CmsPropertyPanel.LD_DISPLAY_VALUE, "true"); 444 } 445 field.getLayoutData().put(CmsPropertyPanel.LD_PROPERTY, propName); 446 field.getLayoutData().put("tab", tab); 447 m_form.addField(tab, field, initialValue); 448 } 449 450 /** 451 * Creates a string model which uses a field of a CmsClientProperty for storing its value.<p> 452 * 453 * @param id the structure id 454 * @param propName the property id 455 * @param isStructure if true, the structure value field should be used, else the resource value field 456 * 457 * 458 * @return the new model object 459 */ 460 private I_CmsStringModel createStringModel(final CmsUUID id, final String propName, final boolean isStructure) { 461 462 final CmsClientProperty property = m_properties.get(propName); 463 464 return new I_CmsStringModel() { 465 466 private boolean m_active; 467 468 private EventBus m_eventBus = new SimpleEventBus(); 469 470 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { 471 472 return m_eventBus.addHandler(ValueChangeEvent.getType(), handler); 473 } 474 475 /** 476 * @see com.google.gwt.event.shared.HasHandlers#fireEvent(com.google.gwt.event.shared.GwtEvent) 477 */ 478 public void fireEvent(GwtEvent<?> event) { 479 480 m_eventBus.fireEvent(event); 481 } 482 483 public String getId() { 484 485 return Joiner.on("/").join(id.toString(), propName, isStructure ? "S" : "R"); 486 } 487 488 public String getValue() { 489 490 if (isStructure) { 491 return property.getStructureValue(); 492 } else { 493 return property.getResourceValue(); 494 } 495 } 496 497 public void setValue(String value, boolean notify) { 498 499 if (!m_active) { 500 m_active = true; 501 try { 502 String oldValue = getValue(); 503 boolean changed = !Objects.equal(value, oldValue); 504 if (isStructure) { 505 property.setStructureValue(value); 506 } else { 507 property.setResourceValue(value); 508 } 509 if (notify && changed) { 510 ValueChangeEvent.fire(this, value); 511 } 512 } finally { 513 m_active = false; 514 } 515 } 516 } 517 }; 518 } 519 520 /** 521 * Gets a pair of strings containing the default value to display for a given property and its source.<p> 522 * 523 * @param prop the property 524 * @param mode the mode 525 * 526 * @return a pair of the form (defaultValue, origin) 527 */ 528 private CmsPair<String, String> getDefaultValueToDisplay(CmsClientProperty prop, Mode mode) { 529 530 if ((mode == Mode.structure) && !CmsStringUtil.isEmpty(prop.getResourceValue())) { 531 String message = Messages.get().key(Messages.GUI_ORIGIN_SHARED_0); 532 533 return CmsPair.create(prop.getResourceValue(), message); 534 } 535 CmsClientProperty inheritedProperty = m_handler.getInheritedProperty(prop.getName()); 536 if (CmsClientProperty.isPropertyEmpty(inheritedProperty)) { 537 return null; 538 } 539 CmsPathValue pathValue = inheritedProperty.getPathValue(mode); 540 String message = Messages.get().key(Messages.GUI_ORIGIN_INHERITED_1, inheritedProperty.getOrigin()); 541 return CmsPair.create(pathValue.getValue(), message); 542 } 543 544 /** 545 * Creates a string model for a given property path value, and returns the same model if the same path value is passed in.<p> 546 * 547 * @param pathValue the path value 548 * 549 * @return the model for that path value 550 */ 551 private I_CmsStringModel getStringModel(CmsPathValue pathValue) { 552 553 String path = pathValue.getPath(); 554 I_CmsStringModel model = m_models.get(path); 555 if (model == null) { 556 String[] tokens = path.split("/"); 557 String id = tokens[0]; 558 String propName = tokens[1]; 559 boolean isStructure = tokens[2].equals("S"); 560 model = createStringModel(new CmsUUID(id), propName, isStructure); 561 m_models.put(path, model); 562 } 563 return model; 564 } 565 566 /** 567 * Builds the fields for the configured properties in the first tab.<p> 568 */ 569 private void internalBuildConfiguredFields() { 570 571 Map<String, CmsClientProperty> ownProps = m_handler.getOwnProperties(); 572 List<String> keys = new ArrayList<String>(m_propertyConfig.keySet()); 573 for (String propName : keys) { 574 buildField(ownProps, propName, Mode.effective); 575 } 576 } 577 578 /** 579 * 580 * Builds the fields for a given mode.<p> 581 * 582 * @param mode the mode 583 */ 584 private void internalBuildFields(Mode mode) { 585 586 Map<String, CmsClientProperty> ownProps = m_handler.getOwnProperties(); 587 for (String propName : m_handler.getAllPropertyNames()) { 588 buildField(ownProps, propName, mode); 589 } 590 } 591 592 /** 593 * Moves the given property name to the top of the keys if present.<p> 594 * 595 * @param keys the list of keys 596 * @param propertyName the property name to move 597 */ 598 private void moveToTop(List<String> keys, String propertyName) { 599 600 if (keys.contains(propertyName)) { 601 keys.remove(propertyName); 602 keys.add(0, propertyName); 603 } 604 } 605 606 /** 607 * Rebuilds the "individual" tab.<p> 608 */ 609 private void rebuildIndividualTab() { 610 611 m_form.removeGroup(CmsPropertyPanel.TAB_INDIVIDUAL); 612 CmsPropertyPanel panel = ((CmsPropertyPanel)m_form.getWidget()); 613 panel.clearTab(CmsPropertyPanel.TAB_INDIVIDUAL); 614 internalBuildFields(Mode.structure); 615 m_form.renderGroup(CmsPropertyPanel.TAB_INDIVIDUAL); 616 } 617 618 /** 619 * Rebuilds the "shared" tab.<p> 620 */ 621 private void rebuildSharedTab() { 622 623 m_form.removeGroup(CmsPropertyPanel.TAB_SHARED); 624 CmsPropertyPanel panel = ((CmsPropertyPanel)m_form.getWidget()); 625 panel.clearTab(CmsPropertyPanel.TAB_SHARED); 626 internalBuildFields(Mode.resource); 627 m_form.renderGroup(CmsPropertyPanel.TAB_SHARED); 628 } 629 630 /** 631 * Rebuilds the simple tab.<p> 632 */ 633 private void rebuildSimpleTab() { 634 635 m_form.removeGroup(CmsPropertyPanel.TAB_SIMPLE); 636 CmsPropertyPanel panel = ((CmsPropertyPanel)m_form.getWidget()); 637 panel.clearTab(CmsPropertyPanel.TAB_SIMPLE); 638 if (m_handler.hasEditableName()) { 639 m_form.addField(CmsPropertyPanel.TAB_SIMPLE, createUrlNameField()); 640 } 641 internalBuildConfiguredFields(); 642 m_form.renderGroup(CmsPropertyPanel.TAB_SIMPLE); 643 } 644}