view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/Validators.java @ 96:10ffc183f39e

Introduce some validators
author Omair Majid <omajid@redhat.com>
date Thu, 06 Feb 2014 15:32:06 -0500
parents
children
line wrap: on
line source

package com.redhat.thermostat.tools.eclipse.plugin.editor;

import java.util.Objects;

import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;

public class Validators {

    public static class NonEmptyStringRequired implements IValidator {

        private String errorMessage;

        public NonEmptyStringRequired(String errorMessage) {
            this.errorMessage = errorMessage;
        }

        @Override
        public IStatus validate(Object value) {
            Objects.requireNonNull(value);

            if (!(value instanceof String)) {
                throw new AssertionError("Expected String, but got '" + value + "' (" + value.getClass() + ").");
            }
            String text = (String) value;
            if (text.trim().length() == 0) {
                return ValidationStatus.error(errorMessage);
            } else {
                return ValidationStatus.ok();
            }
        }
    }
}