Anonymous converter class in initDataBindings possible?

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Anonymous converter class in initDataBindings possible?

Postby niklas.skeppstedt » Fri Mar 06, 2009 7:29 am

Hi,

After adding databindings via the databindings editor to a composite someone else wrote I got compiler errors after saving it.
Turns out there was custom code inside initDataBindings() method. My question is if it should be possible to use anonymous converters inside initDataBinding method?

Enclosed is first the Runnable example app, then the result after editing in databindings editor and saving.

Runnable example:

Code: Select all
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class AnonymousConverterTest {

   private Label label;
   private Text text;
   protected Shell shell;

   /**
    * Launch the application
    * @param args
    */
   public static void main(String[] args)  {
      Display display = Display.getDefault();
      Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
         public void run() {
            try {
               AnonymousConverterTest window = new AnonymousConverterTest();
               window.open();
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }


   /**
    * Open the window
    */
   public void open() {
      final Display display = Display.getDefault();
      createContents();
      shell.open();
      shell.layout();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
            display.sleep();
      }
   }

   /**
    * Create contents of the window
    */
   protected void createContents() {
      shell = new Shell();
      shell.setSize(315, 270);
      shell.setText("SWT Application");

      text = new Text(shell, SWT.BORDER);
      text.setBounds(30, 47, 80, 25);

      final Label backwardsLabel = new Label(shell, SWT.NONE);
      backwardsLabel.setText("Backwards");
      backwardsLabel.setBounds(30, 93, 120, 13);

      label = new Label(shell, SWT.NONE);
      label.setText("Label");
      label.setBounds(30, 112, 81, 13);
      initDataBindings();
      //
   }
   public DataBindingContext initDataBindings() {
      IObservableValue labelTextObserveWidget = SWTObservables.observeText(label);
      IObservableValue textTextObserveWidget = SWTObservables.observeText(text, SWT.Modify);
      //
      //
      DataBindingContext bindingContext = new DataBindingContext();
      //
      UpdateValueStrategy strategy = new UpdateValueStrategy();
      strategy.setConverter(new Converter(String.class, String.class){
         public Object convert(Object arg) {
            String str = (String) arg;
            String converted = new String();
            int len = str.length();
            for( int i = len; --i >= 0; ) {
               converted += str.charAt(i);
            }
            return converted;
         }});
      bindingContext.bindValue(labelTextObserveWidget, textTextObserveWidget, null, strategy);
      //
      return bindingContext;
   }

}


Saved result after editing in databindings editor:

Code: Select all
   public DataBindingContext initDataBindings() {
      IObservableValue labelTextObserveWidget = SWTObservables.observeText(label);
      IObservableValue textTextObserveWidget = SWTObservables.observeText(text, SWT.Modify);
      IObservableValue dummyLabelFontObserveWidget = SWTObservables.observeFont(label);
      IObservableValue dummyBackwardsLabelFontObserveWidget = SWTObservables.observeFont(backwardsLabel);
      //
      //
      DataBindingContext bindingContext = new DataBindingContext();
      //
      UpdateValueStrategy strategy = new UpdateValueStrategy();
      strategy.setConverter(new AnonymousConverterTest.2());
      bindingContext.bindValue(labelTextObserveWidget, textTextObserveWidget, null, strategy);
      bindingContext.bindValue(dummyBackwardsLabelFontObserveWidget, dummyLabelFontObserveWidget, null, null);
      //
      return bindingContext;
   }


Problem is easily found and fixed by extracting the anonymous class to outside, but to do a really easy conversion it feels like a bit too much overhead?

Best regards,
/Niklas Skeppstedt
niklas.skeppstedt
 
Posts: 1
Joined: Fri Mar 06, 2009 3:54 am

Re: Anonymous converter class in initDataBindings possible?

Postby Eric Clayberg » Fri Mar 06, 2009 1:40 pm

niklas.skeppstedt wrote:My question is if it should be possible to use anonymous converters inside initDataBinding method?

No. That is not supported.

Any converters you use need to be in their own class, if you want to use them with our DataBinding support.

You should also not add arbitrary code to the initDataBinding() method as that method is completely regenerated every time it changes.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to SWT Designer

Who is online

Users browsing this forum: No registered users and 2 guests