Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Binding multiple TreeViewers to single model(databinding problem)
icon5.gif  Binding multiple TreeViewers to single model [message #494579] Sat, 31 October 2009 17:28 Go to next message
Sergey  is currently offline Sergey Friend
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 one of TreeViewers occur.
I've utilized createPartControl method in my Viewer for 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?


Thanks
Re: Binding multiple TreeViewers to single model [message #494581 is a reply to message #494579] Sat, 31 October 2009 17:35 Go to previous messageGo to next message
Sergey  is currently offline Sergey Friend
Messages: 6
Registered: October 2009
Junior Member
I modified snippet to try this out. But it doesnt work too.. Sad

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);
		}
	}
	
	
}

Re: Binding multiple TreeViewers to single model [message #494694 is a reply to message #494581] Mon, 02 November 2009 08:56 Go to previous message
Sergey  is currently offline Sergey Friend
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);
		}
	}
}
Previous Topic:RCP on OSX - Carbon, Cocoa, and SWT_AWT troubles...
Next Topic:Dynamically change the menu label
Goto Forum:
  


Current Time: Wed Nov 26 10:31:40 GMT 2014

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

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