001/* 002 * File : $Source$ 003 * Date : $Date$ 004 * Version: $Revision$ 005 * 006 * This library is part of OpenCms - 007 * the Open Source Content Management System 008 * 009 * Copyright (C) 2002 - 2009 Alkacon Software (http://www.alkacon.com) 010 * 011 * This library is free software; you can redistribute it and/or 012 * modify it under the terms of the GNU Lesser General Public 013 * License as published by the Free Software Foundation; either 014 * version 2.1 of the License, or (at our option) any later version. 015 * 016 * This library is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 * Lesser General Public License for more details. 020 * 021 * For further information about Alkacon Software, please see the 022 * company website: http://www.alkacon.com 023 * 024 * For further information about OpenCms, please see the 025 * project website: http://www.opencms.org 026 * 027 * You should have received a copy of the GNU Lesser General Public 028 * License along with this library; if not, write to the Free Software 029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 030 */ 031 032package org.opencms.ade.postupload.client.ui; 033 034import org.opencms.ade.postupload.client.Messages; 035import org.opencms.ade.postupload.shared.CmsPostUploadDialogBean; 036import org.opencms.ade.postupload.shared.CmsPostUploadDialogPanelBean; 037import org.opencms.gwt.client.property.CmsPropertySubmitHandler; 038import org.opencms.gwt.client.property.CmsSimplePropertyEditor; 039import org.opencms.gwt.client.property.I_CmsPropertyEditorHandler; 040import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor; 041import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle; 042import org.opencms.gwt.client.ui.input.I_CmsFormField; 043import org.opencms.gwt.client.ui.input.form.A_CmsFormFieldPanel; 044import org.opencms.gwt.client.ui.input.form.CmsForm; 045import org.opencms.gwt.client.ui.input.form.I_CmsFormHandler; 046import org.opencms.gwt.shared.property.CmsPropertyModification; 047import org.opencms.xml.content.CmsXmlContentProperty; 048 049import java.util.Map; 050import java.util.function.Function; 051 052import com.google.gwt.core.client.Scheduler; 053import com.google.gwt.core.client.Scheduler.ScheduledCommand; 054import com.google.gwt.dom.client.Style.Unit; 055import com.google.gwt.event.dom.client.ClickEvent; 056import com.google.gwt.event.dom.client.ClickHandler; 057import com.google.gwt.user.client.ui.FlowPanel; 058 059import elemental2.core.JsString; 060 061/** 062 * Panel for the property dialog.<p> 063 */ 064public class CmsUploadPropertyPanel extends FlowPanel implements I_CmsFormHandler { 065 066 /** The upload property dialog containing this panel. */ 067 CmsUploadPropertyDialog m_dialog; 068 069 /** The property editor handler instance. */ 070 I_CmsPropertyEditorHandler m_propertyEditorHandler; 071 072 /** The original file extension. */ 073 private String m_originalExtension; 074 075 /** The property editor instance. */ 076 private CmsSimplePropertyEditor m_propertyEditor; 077 078 /** The path relative resource path. */ 079 private String m_resourcePath; 080 081 /** The values. */ 082 private CmsPostUploadDialogPanelBean m_values; 083 084 /** 085 * Public constructor.<p> 086 * 087 * @param dialog the dialog which this panel is added to 088 * @param options the data to fill UI component options 089 * @param values the bean with the current values 090 */ 091 public CmsUploadPropertyPanel( 092 CmsUploadPropertyDialog dialog, 093 CmsPostUploadDialogBean options, 094 CmsPostUploadDialogPanelBean values) { 095 096 m_values = values; 097 m_dialog = dialog; 098 m_resourcePath = values.getInfoBean().getSubTitle(); 099 initializePropertyEditor(); 100 I_CmsFormField filenameField = getFilenameField(); 101 m_originalExtension = getExtension(filenameField.getModelValue()); 102 if (options.hasImage()) { 103 CmsImagePreview preview = new CmsImagePreview(); 104 preview.getElement().getStyle().setMarginTop(5, Unit.PX); 105 add(preview); 106 if (values.getPreviewLink() != null) { 107 preview.setFullPreview(values.getPermalink()); 108 preview.setImageUrl(values.getPreviewLink()); 109 preview.setHighResImageUrl(values.getHighResPreviewLink()); 110 preview.setInfo1(values.getPreviewInfo1()); 111 preview.setInfo2(values.getPreviewInfo2()); 112 } else { 113 preview.hide(); 114 } 115 116 } 117 118 // height may change on click 119 addDomHandler(new ClickHandler() { 120 121 public void onClick(ClickEvent event) { 122 123 m_dialog.updateHeight(); 124 } 125 }, ClickEvent.getType()); 126 } 127 128 /** 129 * Gets the property editor instance.<p> 130 * 131 * @return the property editor instance 132 */ 133 public CmsSimplePropertyEditor getPropertyEditor() { 134 135 return m_propertyEditor; 136 } 137 138 /** 139 * Returns the resourcePath.<p> 140 * 141 * @return the resourcePath 142 */ 143 public String getResourcePath() { 144 145 return m_resourcePath; 146 } 147 148 /** 149 * Returns the content bean (values) of the current dialog.<p> 150 * 151 * @return the content bean (values) of the current dialog 152 */ 153 public CmsPostUploadDialogPanelBean getUpdatedValues() { 154 155 CmsPostUploadDialogPanelBean bean = new CmsPostUploadDialogPanelBean( 156 m_values.getStructureId(), 157 m_values.getInfoBean()); 158 159 if (!m_values.equals(bean)) { 160 m_values = bean; 161 } 162 return m_values; 163 } 164 165 /** 166 * @see org.opencms.gwt.client.ui.input.form.I_CmsFormHandler#isSubmitting() 167 */ 168 public boolean isSubmitting() { 169 170 // TODO Auto-generated method stub 171 return false; 172 } 173 174 /** 175 * @see org.opencms.gwt.client.ui.input.form.I_CmsFormHandler#onSubmitValidationResult(org.opencms.gwt.client.ui.input.form.CmsForm, boolean) 176 */ 177 public void onSubmitValidationResult(CmsForm form, boolean ok) { 178 179 if (ok) { 180 I_CmsFormField filenameField = getFilenameField(); 181 182 String value = filenameField.getWidget().getFormValueAsString(); 183 String extension = getExtension(value); 184 185 if (!extension.equalsIgnoreCase(m_originalExtension)) { 186 187 String titleKey = Messages.GUI_DIALOG_CHANGE_FILE_EXTENSION_WARNING_TITLE_2; 188 String textKey = Messages.GUI_DIALOG_CHANGE_FILE_EXTENSION_WARNING_TEXT_0; 189 String yesKey = Messages.GUI_DIALOG_BUTTON_KEEP_1; 190 String noKey = Messages.GUI_DIALOG_BUTTON_CHANGE_1; 191 if ("".equals(extension)) { 192 titleKey = Messages.GUI_DIALOG_CHANGE_FILE_EXTENSION_TO_EMPTY_WARNING_TITLE_2; 193 noKey = Messages.GUI_DIALOG_BUTTON_CHANGE_TO_EMPTY_1; 194 } 195 196 CmsYesNoDialog confirmation = new CmsYesNoDialog( 197 Messages.get().key(titleKey, m_originalExtension, extension), 198 Messages.get().key(textKey), 199 (dialog, useOriginalExtension) -> { 200 dialog.hide(); 201 if (useOriginalExtension) { 202 String correctedValue = value.substring(0, value.length() - extension.length()) 203 + m_originalExtension; 204 filenameField.getModel().setValue(correctedValue, false); 205 } 206 form.handleSubmit(new CmsPropertySubmitHandler(m_propertyEditorHandler)); 207 }); 208 Function<String, String> format = s -> { 209 if ("".equals(s)) { 210 return "\"\""; 211 } 212 if (s.length() > 10) { 213 return s.substring(0, 10) + JsString.fromCodePoint(8230 /* ellipsis */); 214 } 215 return s; 216 }; 217 confirmation.getNoButton().setText(Messages.get().key(noKey, format.apply(extension))); 218 confirmation.getNoButton().setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE); 219 confirmation.getYesButton().setText(Messages.get().key(yesKey, format.apply(m_originalExtension))); 220 confirmation.getYesButton().setButtonStyle(ButtonStyle.TEXT, ButtonColor.GREEN); 221 confirmation.center(); 222 } else { 223 form.handleSubmit(new CmsPropertySubmitHandler(m_propertyEditorHandler)); 224 } 225 } 226 } 227 228 /** 229 * @see org.opencms.gwt.client.ui.input.form.I_CmsFormHandler#onValidationResult(org.opencms.gwt.client.ui.input.form.CmsForm, boolean) 230 */ 231 public void onValidationResult(CmsForm form, boolean ok) { 232 233 // do nothing for now 234 } 235 236 /** 237 * Sets up the property editor.<p> 238 */ 239 protected void initializePropertyEditor() { 240 241 Map<String, CmsXmlContentProperty> propertyConfig = m_values.getPropertyDefinitions(); 242 m_propertyEditorHandler = new CmsUploadPropertyEditorHandler(m_dialog, m_values); 243 CmsSimplePropertyEditor propertyEditor = new CmsUploadPropertyEditor( 244 m_values.getStructureId(), 245 propertyConfig, 246 m_propertyEditorHandler); 247 propertyEditor.getForm().setFormHandler(this); 248 m_propertyEditor = propertyEditor; 249 m_propertyEditor.initializeWidgets(null); 250 A_CmsFormFieldPanel propertiesPanel = m_propertyEditor.getForm().getWidget(); 251 add(propertiesPanel); 252 } 253 254 /** 255 * @see com.google.gwt.user.client.ui.Widget#onLoad() 256 */ 257 @Override 258 protected void onLoad() { 259 260 super.onLoad(); 261 Scheduler.get().scheduleDeferred(new ScheduledCommand() { 262 263 public void execute() { 264 265 if (m_dialog != null) { 266 m_dialog.updateHeight(); 267 } 268 } 269 }); 270 } 271 272 private String getExtension(String filename) { 273 274 int dotPos = filename.lastIndexOf('.'); 275 if (dotPos == -1) { 276 return ""; 277 } else { 278 return filename.substring(dotPos); 279 } 280 } 281 282 private I_CmsFormField getFilenameField() { 283 284 return m_propertyEditor.getForm().getFields().values().stream().filter( 285 field -> field.getId().contains(CmsPropertyModification.FILE_NAME_PROPERTY)).findFirst().orElse(null); 286 } 287 288}