blob: 85fb17f5e0a811281583887eb38687259d8dba6e [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - 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:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*/
package org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.validator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.eclipse.osbp.ecview.core.common.validation.IValidator;
import org.eclipse.osbp.runtime.common.dispose.AbstractDisposable;
import org.eclipse.osbp.runtime.common.i18n.II18nService;
import org.eclipse.osbp.runtime.common.validation.IStatus;
/**
* This class is responsible, to add Status instances that have been created by
* external validation services to the field.<br>
* The cached status will become reset, if the value changes.
*/
public class ExternalStatusAwareValidator extends AbstractDisposable implements
IValidator {
private List<IStatus> statusList;
private Object value;
private boolean initialized;
public void addStatus(IStatus status) {
// only add errors
if (!status.isError()) {
return;
}
if (statusList == null) {
statusList = new ArrayList<IStatus>();
}
if (!statusList.contains(status)) {
statusList.add(status);
}
}
public void removeStatus(IStatus status) {
if (statusList != null) {
statusList.remove(status);
}
}
public void reset() {
if (statusList != null) {
statusList.clear();
}
}
@Override
public void updateParameter(Object model) {
// nothing to do
}
@Override
public IStatus validateValue(Object value) {
Object oldValue = this.value;
this.value = value;
if (initialized) {
// if value changed, then clear the cached status
if (oldValue == null && this.value != null || oldValue != null
&& this.value == null) {
reset();
} else if (oldValue == null) {
// nothing to do
} else if (!oldValue.equals(this.value)) {
reset();
}
}
initialized = true;
return statusList != null && statusList.size() > 0 ? statusList.get(0)
: IStatus.OK;
}
@Override
public boolean isCheckValidType() {
return false;
}
@Override
public Class<?> getType() {
return Class.class;
}
@Override
public void setLocale(Locale locale) {
}
@Override
public void setI18nService(II18nService i18nService) {
}
@Override
protected void internalDispose() {
statusList.clear();
statusList = null;
}
@Override
public Set<IStatus> getCurrentStatus() {
return statusList != null ? new HashSet<IStatus>(statusList)
: Collections.<IStatus> emptySet();
}
}