| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.eclipse.osbp.ecview.core.common.editpart.IEmbeddableEditpart; |
| import org.eclipse.osbp.ecview.core.common.model.core.YField; |
| import org.eclipse.osbp.ecview.core.common.presentation.IFieldPresentation; |
| import org.eclipse.osbp.ecview.core.common.validation.IFieldValidationManager; |
| import org.eclipse.osbp.ecview.core.common.validation.IValidator; |
| import org.eclipse.osbp.runtime.common.validation.IStatus; |
| import org.eclipse.osbp.runtime.designer.api.IDesignerService.DesignEvent; |
| import org.eclipse.osbp.runtime.designer.api.IDesignerService.EventType; |
| import org.eclipse.osbp.runtime.designer.api.IDesignerService.IDesignListener; |
| import org.eclipse.osbp.runtime.designer.api.IWidgetDesignConfigurator; |
| import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal.ValidatorAdapter; |
| import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.validator.ExternalStatusAwareValidator; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import com.vaadin.data.Validator; |
| import com.vaadin.data.util.converter.Converter; |
| import com.vaadin.server.ErrorMessage; |
| import com.vaadin.ui.AbstractField; |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.Field; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * An abstract implementation of the {@link IFieldPresentation}. |
| * |
| * @param <A> |
| * the generic type |
| */ |
| public abstract class AbstractFieldWidgetPresenter<A extends Component> extends AbstractVaadinWidgetPresenter<A> |
| implements IFieldPresentation<A>, IDesignListener { |
| |
| /** The Constant LOGGER. */ |
| private static final Logger LOGGER = LoggerFactory.getLogger(AbstractFieldWidgetPresenter.class); |
| |
| /** The validators. */ |
| private Map<IValidator, ValidatorAdapter> validators; |
| |
| /** The external status validator. */ |
| private ExternalStatusAwareValidator externalStatusValidator; |
| |
| /** The converter. */ |
| private Converter<?, ?> converter; |
| |
| public AbstractFieldWidgetPresenter() { |
| super(null); |
| } |
| |
| /** |
| * Instantiates a new abstract field widget presenter. |
| * |
| * @param editpart |
| * the editpart |
| */ |
| public AbstractFieldWidgetPresenter(IEmbeddableEditpart editpart) { |
| super(editpart); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.ecview.core.common.presentation.IFieldPresentation# |
| * addValidator(org.eclipse.osbp.ecview.core.common.validation.IValidator) |
| */ |
| @Override |
| public void addValidator(IValidator validator) { |
| if (validators == null) { |
| validators = new HashMap<IValidator, ValidatorAdapter>(2); |
| } |
| |
| if (!validators.containsKey(validator)) { |
| validators.put(validator, new ValidatorAdapter(validator)); |
| } |
| |
| doUpdateValidator(new Event(Event.ADD, validators.get(validator))); |
| } |
| |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.ecview.core.common.presentation.IFieldPresentation# |
| * removeValidator(org.eclipse.osbp.ecview.core.common.validation. |
| * IValidator) |
| */ |
| @Override |
| public void removeValidator(IValidator validator) { |
| if (validators == null) { |
| return; |
| } |
| |
| ValidatorAdapter adapter = validators.remove(validator); |
| if (adapter != null) { |
| doUpdateValidator(new Event(Event.REMOVE, adapter)); |
| } |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.ecview.core.common.presentation.IFieldPresentation# |
| * setConverter(java.lang.Object) |
| */ |
| @Override |
| public void setConverter(Object object) { |
| if (object instanceof Converter) { |
| converter = (Converter<?, ?>) object; |
| doUpdateConverter(converter); |
| } else if (object == null) { |
| converter = getDefaultConverter(); |
| doUpdateConverter(converter); |
| } else { |
| LOGGER.error("Converter is not a valid Vaadin converter! " + object); |
| } |
| } |
| |
| /** |
| * Gets the converter. |
| * |
| * @return the converter |
| */ |
| @SuppressWarnings("rawtypes") |
| protected Converter getConverter() { |
| if (converter == null) { |
| // fallback if converter could not be initialized eager |
| converter = getDefaultConverter(); |
| } |
| return converter; |
| } |
| |
| /** |
| * Returns a proper default converter that is used instead of |
| * <code>null</code>. |
| * |
| * @return the default converter |
| */ |
| protected Converter<?, ?> getDefaultConverter() { |
| return null; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common. |
| * AbstractVaadinWidgetPresenter#getCastedModel() |
| */ |
| protected YField getCastedModel() { |
| return (YField) getModel(); |
| } |
| |
| /** |
| * Needs to set the converter to the field. |
| * |
| * @param converter |
| * the converter |
| */ |
| @SuppressWarnings({ "rawtypes", "unchecked" }) |
| protected void doUpdateConverter(Converter converter) { |
| if (doGetField() != null) { |
| AbstractField<?> field = (AbstractField<?>) doGetField(); |
| field.setConverter(converter); |
| } |
| } |
| |
| /** |
| * Gets the validators. |
| * |
| * @return the validators |
| */ |
| public Map<IValidator, ValidatorAdapter> getValidators() { |
| return validators != null ? java.util.Collections.unmodifiableMap(validators) |
| : Collections.<IValidator, ValidatorAdapter>emptyMap(); |
| } |
| |
| /** |
| * Should be implemented by subclasses to update their validator. |
| * |
| * @param event |
| * - the event |
| */ |
| protected void doUpdateValidator(Event event) { |
| Field<?> field = doGetField(); |
| if (field != null) { |
| switch (event.getType()) { |
| case Event.ADD: |
| field.addValidator((Validator) event.getValidator()); |
| break; |
| case Event.REMOVE: |
| field.removeValidator((Validator) event.getValidator()); |
| break; |
| } |
| } |
| } |
| |
| /** |
| * Is called by subclasses to attach all available validators at the given |
| * field. |
| * |
| * @param field |
| * - the field all validators should be added to |
| */ |
| protected void attachValidators(Field<?> field) { |
| if (validators == null) { |
| return; |
| } |
| |
| for (Validator validator : validators.values()) { |
| field.addValidator(validator); |
| } |
| } |
| |
| /** |
| * Is called by subclasses to detach all validators that are registered at |
| * this instance. |
| * |
| * @param field |
| * - the field all validators should be added to |
| */ |
| protected void detachValidators(Field<?> field) { |
| if (validators == null) { |
| return; |
| } |
| |
| for (Validator validator : validators.values()) { |
| field.removeValidator(validator); |
| } |
| } |
| |
| /** |
| * Returns the field of the current presentation. May return |
| * <code>null</code>. Should be implemented by sub classes. |
| * |
| * @return the field |
| */ |
| protected abstract Field<?> doGetField(); |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common. |
| * AbstractVaadinWidgetPresenter#notify(org.eclipse.osbp.runtime.designer. |
| * api.IDesignerService.DesignEvent) |
| */ |
| @Override |
| public void notify(DesignEvent event) { |
| super.notify(event); |
| |
| IWidgetDesignConfigurator service = getViewContext().getService(IWidgetDesignConfigurator.class.getName()); |
| if (service != null) { |
| if (event.getType() == EventType.ENABLED) { |
| service.configure(getWidget(), getCastedModel(), true); |
| } else { |
| service.configure(getWidget(), getCastedModel(), false); |
| } |
| } |
| } |
| |
| /** |
| * Called by subclasses to initialize the field with validators, tabindex |
| * and more general stuff. |
| * |
| * @param field |
| * - the field to be initialized. |
| */ |
| @SuppressWarnings("unchecked") |
| protected void initializeField(Field<?> field) { |
| attachValidators(field); |
| |
| // initialize the converter if not set before |
| if (field instanceof AbstractField) { |
| AbstractField<?> casted = (AbstractField<?>) field; |
| if (casted.getConverter() == null) { |
| casted.setConverter(getConverter()); |
| } |
| } |
| |
| super.initialize(field, getCastedModel()); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.ecview.core.common.presentation.IFieldPresentation# |
| * addExternalStatus(org.eclipse.osbp.runtime.common.validation.IStatus) |
| */ |
| @Override |
| public void addExternalStatus(IStatus status) { |
| if (externalStatusValidator == null) { |
| externalStatusValidator = new ExternalStatusAwareValidator(); |
| addValidator(externalStatusValidator); |
| } |
| |
| externalStatusValidator.addStatus(status); |
| |
| if (doGetField() != null) { |
| doGetField().markAsDirty(); |
| } |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.ecview.core.common.presentation.IFieldPresentation# |
| * removeExternalStatus(org.eclipse.osbp.runtime.common.validation.IStatus) |
| */ |
| @Override |
| public void removeExternalStatus(IStatus status) { |
| if (externalStatusValidator != null) { |
| externalStatusValidator.removeStatus(status); |
| |
| if (doGetField() != null) { |
| doGetField().markAsDirty(); |
| } |
| } |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.ecview.core.common.presentation.IFieldPresentation# |
| * resetExternalStatus() |
| */ |
| @Override |
| public void resetExternalStatus() { |
| if (externalStatusValidator != null) { |
| externalStatusValidator.reset(); |
| |
| if (doGetField() != null) { |
| doGetField().markAsDirty(); |
| } |
| } |
| } |
| |
| /** |
| * Reports the validation error to the IFieldValidationManager. |
| * |
| * @param message |
| * the message |
| */ |
| protected void reportValidationError(ErrorMessage message) { |
| // error message is only used as a fallback for later |
| |
| IFieldValidationManager service = getViewContext().getService(IFieldValidationManager.class.getName()); |
| if (service != null) { |
| List<IStatus> statuses = new ArrayList<IStatus>(); |
| if (validators != null) { |
| for (IValidator validator : validators.keySet()) { |
| statuses.addAll(validator.getCurrentStatus()); |
| } |
| } |
| if (externalStatusValidator != null) { |
| statuses.addAll(externalStatusValidator.getCurrentStatus()); |
| } |
| service.registerResult(getModel(), statuses); |
| } |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common. |
| * AbstractVaadinWidgetPresenter#internalDispose() |
| */ |
| @Override |
| protected void internalDispose() { |
| if (externalStatusValidator != null) { |
| externalStatusValidator.dispose(); |
| externalStatusValidator = null; |
| } |
| |
| converter = null; |
| |
| super.internalDispose(); |
| } |
| } |