blob: b6d97152acb15256df2b669e41e2f9404ac1212a [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2015, 2019 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.ecommons.io;
import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.variables.IStringVariable;
import org.eclipse.statet.ecommons.variables.core.ObservableVariable;
public class ObservableFileValidator extends FileValidator {
private class ResourceObservableValue extends AbstractObservableValue<IResource> {
private IResource value;
public ResourceObservableValue(final Realm realm) {
super(realm);
}
@Override
public Object getValueType() {
return IResource.class;
}
@Override
protected IResource doGetValue() {
return this.value;
}
@Override
protected void doSetValue(final IResource value) {
setExplicit(value);
checkExplicit();
}
void update(final IResource newValue) {
if ((newValue != null) ? !newValue.equals(this.value) : null != this.value) {
fireValueChange(Diffs.createValueDiff(this.value, this.value= newValue));
}
}
}
private class FileStoreObservableValue extends AbstractObservableValue<IFileStore> {
private IFileStore value;
public FileStoreObservableValue(final Realm realm) {
super(realm);
}
@Override
public Object getValueType() {
return IFileStore.class;
}
@Override
protected IFileStore doGetValue() {
return this.value;
}
@Override
protected void doSetValue(final IFileStore value) {
setExplicit(value);
checkExplicit();
}
void update(final IFileStore newValue) {
if ((newValue != null) ? !newValue.equals(this.value) : null != this.value) {
fireValueChange(Diffs.createValueDiff(this.value, this.value= newValue));
}
}
}
private final Realm realm;
private IChangeListener observableListener;
private ResourceObservableValue resourceObservable;
private FileStoreObservableValue fileStoreObservable;
public ObservableFileValidator(final Realm realm) {
this.realm= realm;
}
@Override
void checkVariable(final IStringVariable variable) {
if (variable instanceof ObservableVariable) {
if (this.observableListener == null) {
this.observableListener= new IChangeListener() {
@Override
public void handleChange(final ChangeEvent event) {
updateVariableResolution();
}
};
}
((ObservableVariable) variable).addChangeListener(this.observableListener);
}
}
@Override
protected void setStatus(final IStatus status) {
super.setStatus(status);
if (this.resourceObservable != null) {
this.resourceObservable.update(getWorkspaceResource());
}
if (this.fileStoreObservable != null) {
this.fileStoreObservable.update(getFileStore());
}
}
public IObservableValue<IResource> getWorkspaceResourceObservable() {
if (this.resourceObservable == null) {
this.resourceObservable= new ResourceObservableValue(this.realm);
this.resourceObservable.update(getWorkspaceResource());
}
return this.resourceObservable;
}
public IObservableValue<IFileStore> getFileStoreObservable() {
if (this.resourceObservable == null) {
this.fileStoreObservable= new FileStoreObservableValue(this.realm);
this.fileStoreObservable.update(getFileStore());
}
return this.fileStoreObservable;
}
}