Instantiating ObservableList?

5.1k Views Asked by At

I am trying to understand the concept of ObservableList and Realms. I have tried to create an instance of an ObservableList like this:

  public ObservableList createObservableList() {
    ObservableList myObsList = new ObservableList(new ArrayList<String>(),
        "test") {
    };

    return myObsList;
  }

But when I call this method I get:

org.eclipse.core.runtime.AssertionFailedException: null argument:Realm cannot be null
 at org.eclipse.core.runtime.Assert.isNotNull(Assert.java:85)

I understand that this have something to do we the default realm is not set. But where do I find documentation on these concepts?

I have looked at this:

http://wiki.eclipse.org/JFace_Data_Binding/Observable

http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/databinding/observable/list/ObservableList.html

...but it contains very limited information/examples.

I also have the Eclipse EMF book but cannot find any examples of how to use eg. ObservableList

Where do I find tutorials/documentation on rules/concepts behind Observable?

I have now tried the following:

  public static ObservableList createObservableList() {
    ObservableList myObsList = null;
    Realm.getDefault().exec(new Runnable() {
      @Override
      public void run() {
        myObsList = new ObservableList(new ArrayList<String>(), "test") {
        };
      }
    });
    return myObsList;
  }

...but that does not work since myObsList must be final. Changing it to final makes it impossible to update it inside the run method.

Any suggestions?

4

There are 4 best solutions below

1
On

A Realm is not set be default. You can test, if a default realm is available:

if (Realm.getDefault() == null) {

and set a Realm (for the current thread) if necessary:

   Realm.setDefault(new Realm());
}

(JFace Databinding is a challenge...)

4
On

Hmm, I sympathise, the Eclipse API is a weird one to work with sometimes.

I found this in the JFaces FAQ:

http://wiki.eclipse.org/JFace_Data_Binding_FAQ#What_is_a_Realm.2C_and_do_I_need_to_care.3F

It looks like you can run something in a realm using:

Realm.exec(Runnable runnable)

so try creating the observable list from inside the Runnable block. Hope that helps.

See:

http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/databinding/observable/Realm.html

It would be nice if we had user friendly URL's Eclipse people !

0
On

You can use the static method

SWTObservables.getRealm(display)

to get the Realm associated with the UI Thread of the given display. It would definitely be nice for the API page of Realm to provide some hint on this.

0
On
  1. Make your main class to implement Runnable inerface.
  2. Move all logic to run() method
  3. In static main method call Realm.runWithDefault()

    Main main = new Main();  
    Realm realm = SWTObservables.getRealm(Display.getDefault());  
    //for JFace data binding  
    Realm.runWithDefault(realm, main);