Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » DataBinding and Validation Problems
DataBinding and Validation Problems [message #503339] Fri, 11 December 2009 23:36 Go to next message
Tim  is currently offline Tim Friend
Messages: 12
Registered: December 2009
Junior Member
Hello,

I have a problem with my validator and the databinding process,
i have the following problem:

I have a text widget who i want to bind to my model, the model have the property Integer, who should save the entered data about the text-widget ... so far, the databinding without validators works, now i want use a simpel validator.

The validator should only check, is the entered data a number and the range of the number, not more. But this does not work, as soon as the validator fails the first time, it checks not the rest of the entered data, e.g. the entered data: 123 ---> validator say Ok
123d --> say false
123ddddd --> validator do nothing, say nothing

The problem is in my opinion the property "Integer" in my model, if i change it to String, then all works perfect ... can someone help me ??
I use any converter.
Re: DataBinding and Validation Problems [message #503350 is a reply to message #503339] Sat, 12 December 2009 01:17 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Tim wrote:
> The validator should only check, is the entered data a number and the
> range of the number, not more. But this does not work, as soon as the
> validator fails the first time, it checks not the rest of the entered
> data, e.g. the entered data: 123 ---> validator say Ok
> 123d --> say false
> 123ddddd --> validator do nothing, say nothing
> The problem is in my opinion the property "Integer" in my model, if i
> change it to String, then all works perfect ... can someone help me ??
> I use any converter.

Could you paste the binding code you're using? I suspect you're putting
the validator at the wrong stage in the pipeline. For integer-to-string
conversion there is an after-get validator and converter provided for
you. The range check validator should be used in the after-convert stage.

You may also be interested in bug 183055 [1] which adds better support
for common validation constraints like integer range checks.

Matthew

[1] https://bugs.eclipse.org/bugs/show_bug.cgi?id=183055
Re: DataBinding and Validation Problems [message #503440 is a reply to message #503339] Mon, 14 December 2009 08:21 Go to previous message
Tim  is currently offline Tim Friend
Messages: 12
Registered: December 2009
Junior Member
Thanks Matthew, i have solve my problem:

package de.achmetow.databinding.validator;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ValidationProblem
{

  public static final String NAME_PROPERTY = "name_property";

  public static void main(String[] args)
  {
    Display display = new Display();
    final ViewModel viewModel = new ViewModel();
    Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable()
    {
      public void run()
      {
        final Shell shell = new View(viewModel).createShell();

        // The SWT event loop
        Display display = Display.getCurrent();
        while (!shell.isDisposed())
        {
          if (!display.readAndDispatch())
          {
            display.sleep();
          }
        }
      }
    });
  }

  // Minimal JavaBeans support

  public static abstract class AbstractModelObject
  {
    private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
        this);

    public void addPropertyChangeListener(PropertyChangeListener listener)
    {
      propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void addPropertyChangeListener(String propertyName,
        PropertyChangeListener listener)
    {
      propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener)
    {
      propertyChangeSupport.removePropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(String propertyName,
        PropertyChangeListener listener)
    {
      propertyChangeSupport
          .removePropertyChangeListener(propertyName, listener);
    }

    protected void firePropertyChange(String propertyName, Object oldValue,
        Object newValue)
    {
      propertyChangeSupport
          .firePropertyChange(propertyName, oldValue, newValue);
    }
  }

  public static class Person extends AbstractModelObject
  {

    // A property...
    private Integer duration;

    public Integer getDuration()
    {
      return duration;
    }

    public void setDuration(Integer duration)
    {
      firePropertyChange("duration", this.duration, this.duration = duration);
      System.out.println("Model: " + duration);
    }
  }

  static class ViewModel
  {
    // The model to bind
    private final Person person = new Person();

    public Person getPerson()
    {
      return person;
    }
  }

  static class MyValidator implements IValidator
  {

    public IStatus validate(Object value)
    {
      System.out.println("\nValidator works");
      Integer val = null;

      try
      {
        val = Integer.valueOf((String) value);
      }
      catch (NumberFormatException e)
      {
        System.out.println("convert failed");
      }
      if (val instanceof Integer)
      {
        if (val >= 0 && val <= 65000)
        {
          System.out.println("correct value: " + val.toString());
          return Status.OK_STATUS;
        }
        else
        {
          System.out.println("Validator failed !");
          return ValidationStatus.error("Error ...!!");
        }

      }
      else throw new RuntimeException("value is not an Integer!");
    }
  }

  // The GUI view
  static class View
  {
    private final ViewModel viewModel;
    private Text name;

    public View(ViewModel viewModel)
    {
      this.viewModel = viewModel;
    }

    public Shell createShell()
    {
      // Build a UI
      Display display = Display.getDefault();
      Shell shell = new Shell(display);
      shell.setLayout(new GridLayout(2, false));
      name = new Text(shell, SWT.BORDER);
      name.setLayoutData(new GridData(GridData.FILL_BOTH));
      Button b1 = new Button(shell, SWT.PUSH);
      b1.setText("Print Model");

      b1.addSelectionListener(new SelectionAdapter()
      {
        @Override
        public void widgetSelected(SelectionEvent e)
        {
          System.out.println("\nModelattribut: "
              + viewModel.getPerson().getDuration());
        }
      });

      // Bind it
      DataBindingContext bindingContext = new DataBindingContext();
      Person person = viewModel.getPerson();
      ISWTObservableValue widget = SWTObservables.observeText(name, SWT.Modify);

      IObservableValue model = BeansObservables
          .observeValue(person, "duration");
      UpdateValueStrategy strategy = new UpdateValueStrategy();
      strategy.setAfterGetValidator(new MyValidator());
      bindingContext.bindValue(widget, model, strategy, null);
      // Open and return the Shell
      shell.pack();
      shell.open();
      return shell;
    }
  }
}

Previous Topic:Update JFace Tooltip while its displayed
Next Topic:Snippet 21 and WizardPage
Goto Forum:
  


Current Time: Fri Mar 29 12:58:13 GMT 2024

Powered by FUDForum. Page generated in 0.03965 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top