diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/databinding/FloatValidator.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/databinding/FloatValidator.java new file mode 100644 index 0000000000000000000000000000000000000000..7a8faa99f824f72f2a3464a1c8ef40376fd4437d --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/databinding/FloatValidator.java @@ -0,0 +1,52 @@ +/*--------------------------------------------------------------------------+ +$Id$ +| | +| Copyright 2012 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.databinding; + +import static org.eclipse.core.databinding.validation.ValidationStatus.cancel; +import static org.eclipse.core.runtime.Status.OK_STATUS; + +import org.eclipse.core.databinding.validation.IValidator; +import org.eclipse.core.runtime.IStatus; + +/** + * Validator for String to Float conversion. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating GREEN Hash: 66FE26465159F0F367C315559BA03FF0 + */ +public class FloatValidator implements IValidator { + + /** Singleton instance. */ + public static final FloatValidator FLOAT_VALIDATOR = new FloatValidator(); + + /** {@inheritDoc} */ + @Override + public IStatus validate(Object value) { + if(value instanceof String) { + try { + Float.valueOf((String)value); + return OK_STATUS; + } catch(NumberFormatException nfex) { + // ignore + } + } + return cancel("Illegal float value!"); + } +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/databinding/IntValidator.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/databinding/IntValidator.java new file mode 100644 index 0000000000000000000000000000000000000000..fb546e3be9de233cf5bcc95f417e43a0ee50f2c6 --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/databinding/IntValidator.java @@ -0,0 +1,52 @@ +/*--------------------------------------------------------------------------+ +$Id$ +| | +| Copyright 2012 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.databinding; + +import static org.eclipse.core.databinding.validation.ValidationStatus.cancel; +import static org.eclipse.core.runtime.Status.OK_STATUS; + +import org.eclipse.core.databinding.validation.IValidator; +import org.eclipse.core.runtime.IStatus; + +/** + * Validator for String to Int conversion. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating GREEN Hash: F33EF54821FADE10F1B6AA4D5B495CE4 + */ +public class IntValidator implements IValidator { + + /** Singleton instance. */ + public static final IntValidator INT_VALIDATOR = new IntValidator(); + + /** {@inheritDoc} */ + @Override + public IStatus validate(Object value) { + if(value instanceof String) { + try { + Integer.valueOf((String)value); + return OK_STATUS; + } catch(NumberFormatException nfex) { + // ignore + } + } + return cancel("Illegal int value!"); + } +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/databinding/NumberPositiveZeroValidator.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/databinding/NumberPositiveZeroValidator.java new file mode 100644 index 0000000000000000000000000000000000000000..498d6b0f8c7d6d4922093d7b40066482332f3292 --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/databinding/NumberPositiveZeroValidator.java @@ -0,0 +1,49 @@ +/*--------------------------------------------------------------------------+ +$Id$ +| | +| Copyright 2011 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.databinding; + +import org.eclipse.core.databinding.validation.IValidator; +import org.eclipse.core.databinding.validation.ValidationStatus; +import org.eclipse.core.runtime.IStatus; + +/** + * {@link IValidator} for checking that a numeric value is positive or zero. + * + * @author hattendorf + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating GREEN Hash: 104C55D6C8A6E1C1AFDD2F64DB56CC20 + */ +public class NumberPositiveZeroValidator implements IValidator { + + /** Singleton instance. */ + public static final NumberPositiveZeroValidator INSTANCE = new NumberPositiveZeroValidator(); + + /** {@inheritDoc} */ + @Override + public IStatus validate(Object value) { + if(!(value instanceof Number)) { + return ValidationStatus.cancel("Internal: Expected a Number, but had a " + + value.getClass()); + } + if(((Number)value).doubleValue() < 0) { + return ValidationStatus.cancel("Value must be positive or zero!"); + } + return ValidationStatus.ok(); + } +}