i having trouble understanding databinding in eclipse rcp application. can't figure out how it's supposed work..
here's want do
i have view list of texts should displayed. texts "stored" in simple list<string>
provided via singleton (for demonstration purpose ;)). whoever uses listprovider
list of texts. when list changes want view automatically update table contents.
here's i've done far
my list provider observable
notify observers when list changes.
in view create observer watch list provider , when observer notified refresh view.
code listprovider
:
public class listprovider extends observable { private stack<string> hist; private static listprovider instance; // singleton implementation public static listprovidergetinstance() { if (null == instance) { instance = new listprovider } return instance; } public void add(string text) { if (this.hist.size() == max_size) { this.hist.remove(max_size-1); } this.hist.add(0, text); // notify observers this.setchanged(); this.notifyobservers(); } }
code view:
public class testview extends viewpart { private tableviewer viewer; [...] class viewcontentprovider implements istructuredcontentprovider { public void inputchanged(viewer v, object oldinput, object newinput) { } public void dispose() { } public object[] getelements(object parent) { return clipboardhistory.getinstance().getlist().toarray(); } } public void createpartcontrol(composite parent) { viewer = new tableviewer(parent, swt.multi | swt.h_scroll | swt.v_scroll); icontentprovider contentprovider = new viewcontentprovider(); viewer.setcontentprovider(contentprovider); viewer.setlabelprovider(new viewlabelprovider()); viewer.setinput(getviewsite()); observer listobserver = new observer() { @override public void update(observable o, object arg) { // update table viewer viewer.refresh(); } }; listprovider.getinstance().addobserver(listobserver); } }
question
is using observer
, observable
"right" way go or missing something? not sure i've grasped concept of rcp databinding...
i recommend reading jface databinding , make use of viewersupport
, observablelistcontentprovider
, beanproperties
simplfy job.
you can see working example jface databinding snippet: model tableviewer binding reference, binding done @ last of viewersupport
:
(...) tableviewer peopleviewer = new tableviewer(committers); viewersupport.bind(peopleviewer, new writablelist(viewmodel.getpeople(), person.class), beanproperties.value(person.class, "name"));
with helpers won't need implement observer
list, , if want viewer update labels when data modified take in same example propertychangesupport
, allows refresh viewer on every change on element of list notify through it. it's important use beanproperties
binding.
Comments
Post a Comment