blob: 6d20ce4e783395bf22e955f07d0bb171d7aa118e [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2013 Eclipse XWT Project.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Erdal Karaca - initial API and implementation
******************************************************************************/
package org.eclipse.xwt.e4.internal.converters;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.xwt.validation.AbstractValidationRule;
public class Instanceof extends AbstractValidationRule {
private Class<?> type;
public Class<?> getType() {
return type;
}
public void setType(Class<?> type) {
this.type = type;
}
private boolean convert(Object fromObject) {
if (fromObject == null) {
return false;
}
Class<?> objectType = null;
if (fromObject instanceof Class<?>) {
objectType = (Class<?>) fromObject;
} else {
objectType = fromObject.getClass();
}
return type == null ? false : type.isAssignableFrom(objectType);
}
@Override
public IStatus validateBack(Object value) {
return null;
}
@Override
public IStatus validate(Object value) {
return convert(value) ? Status.OK_STATUS : Status.CANCEL_STATUS;
}
@Override
public Phase getPhase() {
return Phase.AfterGet;
}
@Override
public Direction getBindingMode() {
return Direction.SourceToTarget;
}
}