diff org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/validation/StringToDateValidator.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/validation/StringToDateValidator.d	Tue Apr 14 11:35:29 2009 +0200
@@ -0,0 +1,110 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2008 IBM Corporation and others.
+ * 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:
+ *     IBM Corporation - initial API and implementation
+ *     Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940
+ *******************************************************************************/
+
+module org.eclipse.core.internal.databinding.validation.StringToDateValidator;
+
+import java.lang.all;
+
+import java.util.Date;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.internal.databinding.conversion.DateConversionSupport;
+import org.eclipse.core.internal.databinding.conversion.StringToDateConverter;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+
+/**
+ * @since 1.0
+ */
+public class StringToDateValidator : IValidator {
+    private final StringToDateConverter converter;
+
+    /**
+     * @param converter
+     */
+    public this(StringToDateConverter converter) {
+        this.converter = converter;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
+     */
+    public IStatus validate(Object value) {
+        if ( null !is cast(String )value && (cast(String)value).trim().length() is 0) {
+            return Status.OK_STATUS;
+        }
+        Object convertedValue = converter.convert(value);
+        //The StringToDateConverter returns null if it can't parse the date.
+        if (convertedValue is null) {
+            return ValidationStatus.error(getErrorMessage());
+        }
+
+        return Status.OK_STATUS;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.eclipse.core.internal.databinding.validation.WrappedConverterValidator#getErrorMessage()
+     */
+    protected String getErrorMessage() {
+        Date sampleDate = new Date();
+
+        // FIXME We need to use the information from the
+        // converter, not use another instance of DateConversionSupport.
+        FormatUtil util = new FormatUtil();
+        StringBuffer samples = new StringBuffer();
+        for (int formatterIdx = 1; formatterIdx < util.numFormatters() - 2; formatterIdx++) {
+            samples.append('\'');
+            samples.append(util.format(sampleDate, formatterIdx));
+            samples.append("', "); //$NON-NLS-1$
+        }
+        samples.append('\'');
+        samples.append(util.format(sampleDate, 0));
+        samples.append('\'');
+        return BindingMessages.getStringcast(BindingMessages.EXAMPLES) + ": " + samples + ",..."; //$NON-NLS-1$//$NON-NLS-2$
+    }
+
+    private static class FormatUtil : DateConversionSupport {
+        /*
+         * (non-Javadoc)
+         *
+         * @see org.eclipse.core.internal.databinding.conversion.DateConversionSupport#numFormatters()
+         */
+        protected int numFormatters() {
+            return super.numFormatters();
+        }
+
+        /*
+         * (non-Javadoc)
+         *
+         * @see org.eclipse.core.internal.databinding.conversion.DateConversionSupport#format(java.util.Date)
+         */
+        protected String format(Date date) {
+            return super.format(date);
+        }
+
+        /*
+         * (non-Javadoc)
+         *
+         * @see org.eclipse.core.internal.databinding.conversion.DateConversionSupport#format(java.util.Date,
+         *      int)
+         */
+        protected String format(Date date, int formatterIdx) {
+            return super.format(date, formatterIdx);
+        }
+    }
+}