Home » Eclipse Projects » JFace » [Databinding]Binding multiple TreeViewers to single model
[Databinding]Binding multiple TreeViewers to single model [message #494612] |
Sun, 01 November 2009 07:46 |
Sergey Messages: 6 Registered: October 2009 |
Junior Member |
|
|
Hi all!
I have some problem with databinding.
I find standalone example how to bind TreeViewer to model... It works nice... but what if i have multiple TreeViews?
I have ViewPart with TreeView, wich can be opened multiple times, and i must find a simple solution to update all of them when some changes in model or other TreeView occurs.
I've utilized createPartControl method in my Viewer like this:
Entry input = Model.getInstance().getRootEntry();
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setUseHashlookup(true);
ViewerSupport.bind(viewer, input, BeanProperties.list("children", Entry.class), BeanProperties.value(Entry.class, "name"));
Realm realm = SWTObservables.getRealm(viewer.getControl().getDisplay());
ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(BeanProperties.list("children",Entry.class).listFactory(realm), null);
if (viewer.getInput() != null)
viewer.setInput(null);
viewer.setContentProvider(contentProvider);
// viewer.setContentProvider(new DaysContentProvider(viewer));
viewer.setLabelProvider(new LabelProvider());
viewer.setInput(Model.getInstance().getRootEntry());
So it works only for one view, no changes in other! But why? Shuld i use different binding contexts and how can i do this?
Can someone help?
I also changed snippet to warkaround this problem:
package org.eclipse.jface.examples.databinding.snippets;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListTreeContentProvider;
import org.eclipse.jface.databinding.viewers.ViewerSupport;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.recaptool.business.Entry;
public class MySnippet {
private Shell shell;
private TreeViewer beanViewer;
private Tree tree;
private Bean input = createBean("input");
private static Bean createBean(String name) {
Bean input = new Bean(name);
for (int i = 0; i < 10; i++) {
List list = input.getList();
Bean root = new Bean("root");
ArrayList children = new ArrayList();
for(int j = 0; j < 3; j ++) {
Bean child = new Bean("root");
children.add(child);
}
root.setList(children);
list.add(root);
input.setList(list);
}
return input;
}
/**
* 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 {
MySnippet window = new MySnippet();
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();
}
}
private Bean getSelectedBean() {
IStructuredSelection selection = (IStructuredSelection) beanViewer
.getSelection();
if (selection.isEmpty())
return null;
return (Bean) selection.getFirstElement();
}
protected void createContents() {
shell = new Shell();
final GridLayout gridLayout_1 = new GridLayout();
gridLayout_1.numColumns = 2;
shell.setLayout(gridLayout_1);
shell.setSize(535, 397);
shell.setText("SWT Application");
final Composite group = new Composite(shell, SWT.NONE);
final RowLayout rowLayout = new RowLayout();
rowLayout.marginTop = 0;
rowLayout.marginRight = 0;
rowLayout.marginLeft = 0;
rowLayout.marginBottom = 0;
rowLayout.pack = false;
group.setLayout(rowLayout);
group
.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,
2, 1));
final Button addRootButton = new Button(group, SWT.NONE);
addRootButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
TreeItem selectedItem = beanViewer.getTree().getSelection()[0];
TreeItem parentItem = selectedItem.getParentItem();
Bean parent;
int index;
if (parentItem == null) {
parent = input;
index = beanViewer.getTree().indexOf(selectedItem);
} else {
parent = (Bean) parentItem.getData();
index = parentItem.indexOf(selectedItem);
}
List list = new ArrayList(parent.getList());
list.remove(index);
parent.setList(list);
}
});
addRootButton.setText("Delete");
// final Button setRoot = new Button(group, SWT.NONE);
// setRoot.addSelectionListener(new SelectionAdapter() {
// public void widgetSelected(final SelectionEvent e) {
// TreeItem selectedItem = beanViewer.getTree().getSelection()[0];
// beanViewer.setInput(selectedItem.getData());
// }
// });
// setRoot.setText("Set root");
beanViewer = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.BORDER);
beanViewer.setUseHashlookup(true);
tree = beanViewer.getTree();
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
ViewerSupport.bind(beanViewer, input, BeanProperties.list("list", Bean.class), BeanProperties.value(Bean.class, "text"));
Realm realm = SWTObservables.getRealm(beanViewer.getControl().getDisplay());
ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(BeanProperties.list("list",Bean.class).listFactory(realm), null);
if (beanViewer.getInput() != null)
beanViewer.setInput(null);
beanViewer.setContentProvider(contentProvider);
beanViewer.setLabelProvider(new MyLabelProvider());
beanViewer.setInput(input);
TreeViewer beanViewer2 = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.BORDER);
beanViewer2.setUseHashlookup(true);
tree = beanViewer2.getTree();
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
ViewerSupport.bind(beanViewer2, input, BeanProperties.list("list", Bean.class), BeanProperties.value(Bean.class, "text"));
if (beanViewer2.getInput() != null)
beanViewer2.setInput(null);
beanViewer2.setContentProvider(contentProvider);
beanViewer2.setLabelProvider(new MyLabelProvider());
// new MyLabelProvider(
// Properties.observeEach(contentProvider.getKnownElements(),
// new IValueProperty[] { BeanProperties.value(Bean.class, "text") }))
// );
beanViewer2.setInput(input);
}
class MyLabelProvider implements ILabelProvider {
@Override
public Image getImage(Object element) {
// Image im = new Image(MySnippet.this.shell.getDisplay(), "d:\\cd.png");
return null;
}
@Override
public String getText(Object element) {
return "blah";
}
@Override
public void addListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}
@Override
public void removeListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
}
static class Bean {
/* package */PropertyChangeSupport changeSupport = new PropertyChangeSupport(
this);
private String text;
private List list;
public Bean(String text) {
this.text = text;
list = new ArrayList();
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
public String getText() {
return text;
}
public void setText(String value) {
changeSupport.firePropertyChange("text", this.text,
this.text = value);
}
public List getList() {
if (list == null)
return null;
return new ArrayList(list);
}
public void setList(List list) {
if (list != null)
list = new ArrayList(list);
changeSupport.firePropertyChange("list", this.list,
this.list = list);
}
public boolean hasListeners(String propertyName) {
return changeSupport.hasListeners(propertyName);
}
}
}
Here 2 TreeViews binded to one model, but when i make changes in model only one treeview reflects them (when click delete button to delte beans from model).
Please, can someone help?
Sory for big post...
|
|
|
Re: [Databinding]Binding multiple TreeViewers to single model [message #494695 is a reply to message #494612] |
Mon, 02 November 2009 08:58 |
Sergey Messages: 6 Registered: October 2009 |
Junior Member |
|
|
Solved it. Here is modifyed snippet. It has some bugs, but i did it only for databindig test.
package org.eclipse.jface.examples.databinding.snippets;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.core.databinding.property.list.IListProperty;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListTreeContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ViewerSupport;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
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.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class Snippet019TreeViewerWithListFactory {
private Button pasteButton;
private Button copyButton;
private Shell shell;
private Button addChildBeanButton;
private Button removeBeanButton;
private Tree tree;
private Text beanText;
private TreeViewer v2;
private TreeViewer v1;
private Bean input = createBean("input");
private IObservableValue clipboard;
/**
* 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 {
Snippet019TreeViewerWithListFactory window = new Snippet019TreeViewerWithListFactory();
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();
final GridLayout gridLayout_1 = new GridLayout();
gridLayout_1.numColumns = 2;
shell.setLayout(gridLayout_1);
shell.setSize(535, 397);
shell.setText("SWT Application");
final Composite group = new Composite(shell, SWT.NONE);
final RowLayout rowLayout = new RowLayout();
rowLayout.marginTop = 0;
rowLayout.marginRight = 0;
rowLayout.marginLeft = 0;
rowLayout.marginBottom = 0;
rowLayout.pack = false;
group.setLayout(rowLayout);
group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
final Button addRootButton = new Button(group, SWT.NONE);
addRootButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
List list = input.getList();
Bean root = createBean("root");
list.add(root);
input.setList(list);
lastSelectedTreeViewer.setSelection(new StructuredSelection(root));
beanText.selectAll();
beanText.setFocus();
}
});
addRootButton.setText("Add Root");
addChildBeanButton = new Button(group, SWT.NONE);
addChildBeanButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
Bean parent = getSelectedBean();
List list = new ArrayList(parent.getList());
Bean child = new Bean("child");
list.add(child);
parent.setList(list);
lastSelectedTreeViewer.setSelection(new StructuredSelection(child));
beanText.selectAll();
beanText.setFocus();
}
});
addChildBeanButton.setText("Add Child");
removeBeanButton = new Button(group, SWT.NONE);
removeBeanButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
TreeItem selectedItem = lastSelectedTree.getSelection()[0];
TreeItem parentItem = selectedItem.getParentItem();
Bean parent;
int index;
if (parentItem == null) {
parent = input;
index = lastSelectedTree.indexOf(selectedItem);
} else {
parent = (Bean) parentItem.getData();
index = parentItem.indexOf(selectedItem);
}
List list = new ArrayList(parent.getList());
list.remove(index);
parent.setList(list);
}
});
removeBeanButton.setText("Remove");
copyButton = new Button(group, SWT.NONE);
copyButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
clipboard.setValue(getSelectedBean());
}
});
copyButton.setText("Copy");
pasteButton = new Button(group, SWT.NONE);
pasteButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
Bean copy = (Bean) clipboard.getValue();
if (copy == null)
return;
Bean parent = getSelectedBean();
if (parent == null)
parent = input;
List list = new ArrayList(parent.getList());
list.add(copy);
parent.setList(list);
lastSelectedTreeViewer.setSelection(new StructuredSelection(copy));
beanText.selectAll();
beanText.setFocus();
}
});
pasteButton.setText("Paste");
final Button refreshButton = new Button(group, SWT.NONE);
refreshButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
lastSelectedTreeViewer.refresh();
}
});
refreshButton.setText("Refresh");
v2 = createTreeViewer();
v1 = createTreeViewer();
final Label itemNameLabel = new Label(shell, SWT.NONE);
itemNameLabel.setText("Item Name");
beanText = new Text(shell, SWT.BORDER);
final GridData gd_beanValue = new GridData(SWT.FILL, SWT.CENTER, true, false);
beanText.setLayoutData(gd_beanValue);
initExtraBindings(v2);
initSelectionListeners(v2);
initExtraBindings(v1);
initSelectionListeners(v1);
}
private static Bean createBean(String name) {
Bean root = new Bean("root");
List rootList = root.getList();
for (int i = 0; i < 10; i++) {
Bean bean = new Bean("bean" + i);
List list = bean.getList();
for (int j = 0; j < 3; j++) {
list.add(new Bean("child" + j));
}
bean.setList(list);
rootList.add(bean);
}
root.setList(rootList);
return root;
}
private TreeViewer createTreeViewer() {
TreeViewer viewer;
viewer = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.BORDER);
viewer.setUseHashlookup(true);
tree = viewer.getTree();
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
return viewer;
}
private Bean selection;
private Tree lastSelectedTree;
private TreeViewer lastSelectedTreeViewer;
private void initSelectionListeners(final TreeViewer v) {
v.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
lastSelectedTree = v.getTree();
lastSelectedTreeViewer = v;
if (event.getSelection() instanceof IStructuredSelection) {
IStructuredSelection s = (IStructuredSelection) event.getSelection();
if (s.getFirstElement() instanceof Bean) {
selection = (Bean) s.getFirstElement();
}
}
}
});
// initial values. ugly, but its just workaround snippet :)
lastSelectedTree = v.getTree();
lastSelectedTreeViewer = v;
}
private Bean getSelectedBean() {
return selection;
}
public static void bind(AbstractTreeViewer viewer, Object input, IListProperty childrenProperty, IValueProperty labelProperty) {
bind(viewer, input, childrenProperty, new IValueProperty[] { labelProperty });
}
public static void bind(AbstractTreeViewer viewer, Object input, IListProperty childrenProperty, IValueProperty[] labelProperties) {
Realm realm = SWTObservables.getRealm(viewer.getControl().getDisplay());
ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(childrenProperty.listFactory(realm), null);
if (viewer.getInput() != null)
viewer.setInput(null);
viewer.setContentProvider(contentProvider);
viewer.setLabelProvider(new ObservableMapLabelProvider(Properties.observeEach(contentProvider.getKnownElements(), labelProperties)));
if (input != null)
viewer.setInput(input);
}
private void initExtraBindings(TreeViewer viewer) {
ViewerSupport.bind(viewer, input, BeanProperties.list("list", Bean.class), BeanProperties.value(Bean.class, "text"));
}
static class Bean {
/* package */PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private String text;
private LinkedList list;
public Bean(String text) {
this.text = text;
list = new LinkedList();
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
public String getText() {
return text;
}
public void setText(String value) {
changeSupport.firePropertyChange("text", this.text, this.text = value);
}
public List getList() {
if (list == null)
return null;
return new ArrayList(list);
}
public void setList(List list) {
if (list != null)
list = new LinkedList(list);
changeSupport.firePropertyChange("list", this.list, this.list = (LinkedList) list);
}
public boolean hasListeners(String propertyName) {
return changeSupport.hasListeners(propertyName);
}
}
}
|
|
|
Re: [Databinding]Binding multiple TreeViewers to single model [message #494875 is a reply to message #494695] |
Tue, 03 November 2009 02:39 |
Eclipse User |
|
|
|
Originally posted by: cheney_chen.i-len.com
Hi sergey,
Good snippet. But I have one problem for it, if binding multiple viewers
to single model in concurrent environment(e.g. use the case in eclipse
RAP framework, each viewer in different UI Thread, but binding to single
model), is it thread safely? Or need to synchronize the single model to
modify?
Can JFace DataBinding Team people or somebody else confirm it?
Best Regards,
Cheney
Sergey wrote:
> Solved it. Here is modifyed snippet. It has some bugs, but i did it only
> for databindig test.
>
>
> package org.eclipse.jface.examples.databinding.snippets;
>
> import java.beans.PropertyChangeListener;
> import java.beans.PropertyChangeSupport;
> import java.util.ArrayList;
> import java.util.LinkedList;
> import java.util.List;
>
> import org.eclipse.core.databinding.beans.BeanProperties;
> import org.eclipse.core.databinding.observable.Realm;
> import org.eclipse.core.databinding.observable.value.IObservableVal ue;
> import org.eclipse.core.databinding.property.Properties;
> import org.eclipse.core.databinding.property.list.IListProperty;
> import org.eclipse.core.databinding.property.value.IValueProperty;
> import org.eclipse.jface.databinding.swt.SWTObservables;
> import
> org.eclipse.jface.databinding.viewers.ObservableListTreeCont entProvider;
> import org.eclipse.jface.databinding.viewers.ObservableMapLabelProv ider;
> import org.eclipse.jface.databinding.viewers.ViewerSupport;
> import org.eclipse.jface.viewers.AbstractTreeViewer;
> import org.eclipse.jface.viewers.ISelectionChangedListener;
> import org.eclipse.jface.viewers.IStructuredSelection;
> import org.eclipse.jface.viewers.SelectionChangedEvent;
> import org.eclipse.jface.viewers.StructuredSelection;
> import org.eclipse.jface.viewers.TreeViewer;
> 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.layout.RowLayout;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Text;
> import org.eclipse.swt.widgets.Tree;
> import org.eclipse.swt.widgets.TreeItem;
>
> public class Snippet019TreeViewerWithListFactory {
>
> private Button pasteButton;
> private Button copyButton;
> private Shell shell;
> private Button addChildBeanButton;
> private Button removeBeanButton;
> private Tree tree;
> private Text beanText;
> private TreeViewer v2;
> private TreeViewer v1;
>
> private Bean input = createBean("input");
> private IObservableValue clipboard;
>
> /**
> * 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 {
> Snippet019TreeViewerWithListFactory window = new
> Snippet019TreeViewerWithListFactory();
> 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();
> final GridLayout gridLayout_1 = new GridLayout();
> gridLayout_1.numColumns = 2;
> shell.setLayout(gridLayout_1);
> shell.setSize(535, 397);
> shell.setText("SWT Application");
>
> final Composite group = new Composite(shell, SWT.NONE);
> final RowLayout rowLayout = new RowLayout();
> rowLayout.marginTop = 0;
> rowLayout.marginRight = 0;
> rowLayout.marginLeft = 0;
> rowLayout.marginBottom = 0;
> rowLayout.pack = false;
> group.setLayout(rowLayout);
> group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
> false, 2, 1));
>
> final Button addRootButton = new Button(group, SWT.NONE);
> addRootButton.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(final SelectionEvent e) {
> List list = input.getList();
> Bean root = createBean("root");
> list.add(root);
> input.setList(list);
>
> lastSelectedTreeViewer.setSelection(new
> StructuredSelection(root));
> beanText.selectAll();
> beanText.setFocus();
> }
> });
> addRootButton.setText("Add Root");
>
> addChildBeanButton = new Button(group, SWT.NONE);
> addChildBeanButton.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(final SelectionEvent e) {
> Bean parent = getSelectedBean();
> List list = new ArrayList(parent.getList());
> Bean child = new Bean("child");
> list.add(child);
> parent.setList(list);
>
> lastSelectedTreeViewer.setSelection(new
> StructuredSelection(child));
> beanText.selectAll();
> beanText.setFocus();
> }
> });
> addChildBeanButton.setText("Add Child");
>
> removeBeanButton = new Button(group, SWT.NONE);
> removeBeanButton.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(final SelectionEvent e) {
> TreeItem selectedItem = lastSelectedTree.getSelection()[0];
> TreeItem parentItem = selectedItem.getParentItem();
> Bean parent;
> int index;
> if (parentItem == null) {
> parent = input;
> index = lastSelectedTree.indexOf(selectedItem);
> } else {
> parent = (Bean) parentItem.getData();
> index = parentItem.indexOf(selectedItem);
> }
>
> List list = new ArrayList(parent.getList());
> list.remove(index);
> parent.setList(list);
> }
> });
> removeBeanButton.setText("Remove");
>
> copyButton = new Button(group, SWT.NONE);
> copyButton.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(final SelectionEvent e) {
> clipboard.setValue(getSelectedBean());
> }
> });
> copyButton.setText("Copy");
>
> pasteButton = new Button(group, SWT.NONE);
> pasteButton.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(final SelectionEvent e) {
> Bean copy = (Bean) clipboard.getValue();
> if (copy == null)
> return;
> Bean parent = getSelectedBean();
> if (parent == null)
> parent = input;
>
> List list = new ArrayList(parent.getList());
> list.add(copy);
> parent.setList(list);
>
> lastSelectedTreeViewer.setSelection(new
> StructuredSelection(copy));
> beanText.selectAll();
> beanText.setFocus();
> }
> });
> pasteButton.setText("Paste");
>
> final Button refreshButton = new Button(group, SWT.NONE);
> refreshButton.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(final SelectionEvent e) {
> lastSelectedTreeViewer.refresh();
> }
> });
> refreshButton.setText("Refresh");
>
> v2 = createTreeViewer();
> v1 = createTreeViewer();
>
> final Label itemNameLabel = new Label(shell, SWT.NONE);
> itemNameLabel.setText("Item Name");
>
> beanText = new Text(shell, SWT.BORDER);
> final GridData gd_beanValue = new GridData(SWT.FILL, SWT.CENTER,
> true, false);
> beanText.setLayoutData(gd_beanValue);
>
> initExtraBindings(v2);
> initSelectionListeners(v2);
>
> initExtraBindings(v1);
> initSelectionListeners(v1);
> }
>
> private static Bean createBean(String name) {
> Bean root = new Bean("root");
> List rootList = root.getList();
>
> for (int i = 0; i < 10; i++) {
> Bean bean = new Bean("bean" + i);
> List list = bean.getList();
> for (int j = 0; j < 3; j++) {
> list.add(new Bean("child" + j));
> }
> bean.setList(list);
> rootList.add(bean);
> }
> root.setList(rootList);
>
> return root;
> }
>
> private TreeViewer createTreeViewer() {
> TreeViewer viewer;
> viewer = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.BORDER);
> viewer.setUseHashlookup(true);
> tree = viewer.getTree();
> tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
> 2, 1));
> return viewer;
> }
>
> private Bean selection;
>
> private Tree lastSelectedTree;
> private TreeViewer lastSelectedTreeViewer;
>
> private void initSelectionListeners(final TreeViewer v) {
> v.addSelectionChangedListener(new ISelectionChangedListener() {
> @Override
> public void selectionChanged(SelectionChangedEvent event) {
> lastSelectedTree = v.getTree();
> lastSelectedTreeViewer = v;
> if (event.getSelection() instanceof IStructuredSelection) {
> IStructuredSelection s = (IStructuredSelection)
> event.getSelection();
> if (s.getFirstElement() instanceof Bean) {
> selection = (Bean) s.getFirstElement();
> }
> }
> }
> });
>
> // initial values. ugly, but its just workaround snippet :)
> lastSelectedTree = v.getTree();
> lastSelectedTreeViewer = v;
> }
>
> private Bean getSelectedBean() {
> return selection;
> }
>
> public static void bind(AbstractTreeViewer viewer, Object input,
> IListProperty childrenProperty, IValueProperty labelProperty) {
> bind(viewer, input, childrenProperty, new IValueProperty[] {
> labelProperty });
> }
>
> public static void bind(AbstractTreeViewer viewer, Object input,
> IListProperty childrenProperty, IValueProperty[] labelProperties) {
> Realm realm =
> SWTObservables.getRealm(viewer.getControl().getDisplay());
>
> ObservableListTreeContentProvider contentProvider = new
> ObservableListTreeContentProvider(childrenProperty.listFacto ry(realm),
> null);
> if (viewer.getInput() != null)
> viewer.setInput(null);
> viewer.setContentProvider(contentProvider);
> viewer.setLabelProvider(new
> ObservableMapLabelProvider(Properties.observeEach(contentPro vider.getKnownElements(),
> labelProperties)));
> if (input != null)
> viewer.setInput(input);
> }
>
> private void initExtraBindings(TreeViewer viewer) {
> ViewerSupport.bind(viewer, input, BeanProperties.list("list",
> Bean.class), BeanProperties.value(Bean.class, "text"));
> }
>
> static class Bean {
> /* package */PropertyChangeSupport changeSupport = new
> PropertyChangeSupport(this);
> private String text;
> private LinkedList list;
>
> public Bean(String text) {
> this.text = text;
> list = new LinkedList();
> }
>
> public void addPropertyChangeListener(PropertyChangeListener
> listener) {
> changeSupport.addPropertyChangeListener(listener);
> }
>
> public void removePropertyChangeListener(PropertyChangeListener
> listener) {
> changeSupport.removePropertyChangeListener(listener);
> }
>
> public String getText() {
> return text;
> }
>
> public void setText(String value) {
> changeSupport.firePropertyChange("text", this.text,
> this.text = value);
> }
>
> public List getList() {
> if (list == null)
> return null;
> return new ArrayList(list);
> }
>
> public void setList(List list) {
> if (list != null)
> list = new LinkedList(list);
> changeSupport.firePropertyChange("list", this.list,
> this.list = (LinkedList) list);
> }
>
> public boolean hasListeners(String propertyName) {
> return changeSupport.hasListeners(propertyName);
> }
> }
> }
>
|
|
|
Re: [Databinding]Binding multiple TreeViewers to single model [message #494974 is a reply to message #494875] |
Tue, 03 November 2009 12:41 |
Sergey Messages: 6 Registered: October 2009 |
Junior Member |
|
|
Yes, its very difficult to find good documentation about databinding.
Maybe this can help you:
Realm ("Kingdom") is the core concept of JFace Data Binding in regards to synchronization. A realm can be thought of as a special thread, or a lock, that serializes access to a set of observables in that realm. Each observable belongs to a Realm. It can only be accessed from that realm, and it will always fire change events on that realm. One important example of a realm is the SWT UI thread. Like for the SWT UI thread, you can execute code within a realm by using Realm.asyncExec(); in fact, the SWT realm implementation just delegates to Display.asyncExec(). This means that while the data binding framework can be used in a multi-threaded environment, each observable is essentially single-threaded. Java bean observables implement this contract on the observable side, but don't require it on the Java beans side: Even if a bean fires a PropertyChangeEvent on a different thread, the change events originating from the observable will happen within its realm. To bridge between observables in different realms, use a data binding context - you can bind two observables even if they belong to different realms and the bindings take care of this for you by using Realm.asyncExec() where necessary.
this is from
http://wiki.eclipse.org/JFace_Data_Binding/Realm
http://wiki.eclipse.org/JFace_Data_Binding
|
|
|
Re: [Databinding]Binding multiple TreeViewers to single model [message #495184 is a reply to message #494974] |
Wed, 04 November 2009 03:36 |
Eclipse User |
|
|
|
Originally posted by: cheney_chen.i-len.com
Hi Serger,
Thanks for your reply. Comments below...
Sergey wrote:
> Yes, its very difficult to find good documentation about databinding.
> Maybe this can help you:
>
> Realm ("Kingdom") is the core concept of JFace Data Binding in regards
> to synchronization. A realm can be thought of as a special thread, or a
> lock, that serializes access to a set of observables in that realm. Each
> observable belongs to a Realm. It can only be accessed from that realm,
> and it will always fire change events on that realm. One important
> example of a realm is the SWT UI thread. Like for the SWT UI thread, you
> can execute code within a realm by using Realm.asyncExec(); in fact, the
> SWT realm implementation just delegates to Display.asyncExec(). This
> means that while the data binding framework can be used in a
> multi-threaded environment, each observable is essentially
> single-threaded.Java bean observables implement this contract on the
> observable side, but don't require it on the Java beans side: Even if a
> bean fires a PropertyChangeEvent on a different thread,
"Each observable belongs to a Realm." So one model instance ( java bean
instance) can be related to multiple observable, and these observables
are created by Databinding API according to the model instance, and each
observable belongs a Realm, So they don't influence each other.
That is single model can thread safety when binding to different Realm,
is it right?
I will linked this thread to RAP Newsgroup, discuss "Binding multiple
Treeviewers in different session to single model", in order to get some
comments from RAP Team.
Best Regards,
Cheney
the change
> events originating from the observable will happen within its realm. To
> bridge between observables in different realms, use a data binding
> context - you can bind two observables even if they belong to different
> realms and the bindings take care of this for you by using
> Realm.asyncExec() where necessary.
> this is from
> http://wiki.eclipse.org/JFace_Data_Binding/Realm
> http://wiki.eclipse.org/JFace_Data_Binding
>
|
|
|
Re: [Databinding]Binding multiple TreeViewers to single model [message #495208 is a reply to message #495184] |
Wed, 04 November 2009 07:58 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
Cheney Chen schrieb:
> Hi Serger,
>
> Thanks for your reply. Comments below...
>
> Sergey wrote:
>> Yes, its very difficult to find good documentation about databinding.
>> Maybe this can help you:
>>
>> Realm ("Kingdom") is the core concept of JFace Data Binding in regards
>> to synchronization. A realm can be thought of as a special thread, or a
>> lock, that serializes access to a set of observables in that realm. Each
>> observable belongs to a Realm. It can only be accessed from that realm,
>> and it will always fire change events on that realm. One important
>> example of a realm is the SWT UI thread. Like for the SWT UI thread, you
>> can execute code within a realm by using Realm.asyncExec(); in fact, the
>> SWT realm implementation just delegates to Display.asyncExec(). This
>> means that while the data binding framework can be used in a
>> multi-threaded environment, each observable is essentially
>> single-threaded.Java bean observables implement this contract on the
>> observable side, but don't require it on the Java beans side: Even if a
>> bean fires a PropertyChangeEvent on a different thread,
>
> "Each observable belongs to a Realm." So one model instance ( java bean
> instance) can be related to multiple observable, and these observables
> are created by Databinding API according to the model instance, and each
> observable belongs a Realm, So they don't influence each other.
> That is single model can thread safety when binding to different Realm,
> is it right?
This is correct and I commented on the RAP thread as well (though I
think the situation there is bit different because multiple USERS are
looking at the same model).
When we talk about models and thread saftey you need to take into
consideration that the models themselves have to be thread safe which is
often NOT the case.
Tom
|
|
|
Re: [Databinding]Binding multiple TreeViewers to single model [message #495474 is a reply to message #495208] |
Thu, 05 November 2009 01:30 |
Eclipse User |
|
|
|
Originally posted by: cheney_chen.i-len.com
Hi Tom,
Thanks for your reply and good comments.
Best Regards,
Cheney
Tom Schindl wrote:
>
> This is correct and I commented on the RAP thread as well (though I
> think the situation there is bit different because multiple USERS are
> looking at the same model).
>
> When we talk about models and thread saftey you need to take into
> consideration that the models themselves have to be thread safe which is
> often NOT the case.
>
> Tom
|
|
| | |
Goto Forum:
Current Time: Fri Apr 26 01:35:21 GMT 2024
Powered by FUDForum. Page generated in 0.03854 seconds
|