Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » WindowBuilder » Having trouble with WindowBuilder Jface binding using a combo
Having trouble with WindowBuilder Jface binding using a combo [message #662258] Tue, 29 March 2011 22:14 Go to next message
Mark .
Messages: 12
Registered: July 2010
Junior Member
Hi all,

I'm new to Eclipse RCP and Windowbuilder and i'm having some trouble wrapping my head around the binding interface of Windowbuilder.

My goal is to populate a combo with a list of services. These services are contained in a model which also tracks the active service.

Essentially, the model looks like this:

class SourceModel
List<ServiceX> services;
ServiceX currentService;

interface ServiceX
getName() <--what I wish to see in the combo list
plus various interface methods


I have a View with a combo widget and I would like to populate this with the services list in the model. I have have achieved this with the binding designed in WindowBuilder. The input is bound to the services list using the getName() as the label provider.

Where I am stumped is using the currentService field to set the currently selected option in the combo. currentService is set in the constructor of the SourceModel so the combo should display that ServiceX as the selected.

Hopefully this makes sense.

Thanks in advance for any help.

[Updated on: Wed, 30 March 2011 06:18]

Report message to a moderator

Re: Having trouble with WindowBuilder Jface binding using a combo [message #664279 is a reply to message #662258] Fri, 08 April 2011 08:58 Go to previous messageGo to next message
Andrey Sablin is currently offline Andrey Sablin
Messages: 12
Registered: March 2011
Junior Member
You cant use Combo for this. Use JFace ComboViewer:
package com.instantiations.designer.test.rcp.databindings;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class App_Combo_test {
	private DataBindingContext m_bindingContext;

	public static interface ServiceX {
		public String getName();
	}

	List<ServiceX> services;

	private ServiceX currentService;

	public class Self {

		public ServiceX getCurrentService() {
			return currentService;
		}

		public void setCurrentService(ServiceX currentService) {
			App_Combo_test.this.currentService = currentService;
		}

	}

	Self self = new Self();

	protected Shell shell;
	private ComboViewer comboViewer;

	/**
	 * 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 {
					App_Combo_test window = new App_Combo_test();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		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(450, 300);
		shell.setText("SWT Application");

		Label label = new Label(shell, SWT.NONE);
		label.setBounds(10, 10, 269, 17);
		label.setText("Combo databinding test");

		comboViewer = new ComboViewer(shell, SWT.NONE);
		Combo combo = comboViewer.getCombo();
		combo.setBounds(10, 33, 185, 27);

		{
			ServiceX service1 = new ServiceX() {

				@Override
				public String getName() {
					return "service1";
				}
			};
			ServiceX service2 = new ServiceX() {

				@Override
				public String getName() {
					return "service2";
				}
			};
			ServiceX service3 = new ServiceX() {

				@Override
				public String getName() {
					return "service3";
				}
			};

			services = new ArrayList<ServiceX>();
			services.add(service1);
			services.add(service2);
			services.add(service3);

			self.setCurrentService(service2);
		}

		m_bindingContext = initDataBindings();

	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		comboViewer.setContentProvider(listContentProvider);
		//
		IObservableMap observeMap = PojoObservables.observeMap(
				listContentProvider.getKnownElements(), ServiceX.class, "name");
		comboViewer
				.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		//
		WritableList writableList = new WritableList(services, ServiceX.class);
		comboViewer.setInput(writableList);
		//
		IObservableValue comboViewerObserveSingleSelection = ViewersObservables
				.observeSingleSelection(comboViewer);
		IObservableValue selfCurrentServiceObserveValue = PojoObservables
				.observeValue(self, "currentService");
		bindingContext.bindValue(comboViewerObserveSingleSelection,
				selfCurrentServiceObserveValue, null, null);
		//
		return bindingContext;
	}

}
icon14.gif  Re: Having trouble with WindowBuilder Jface binding using a combo [message #666301 is a reply to message #664279] Tue, 19 April 2011 19:57 Go to previous message
Mark .
Messages: 12
Registered: July 2010
Junior Member
This worked perfectly andrey, thank you!

I was already using a JFace viewer but I was missing the selection binding in the final part of the binding creation, that is,

IObservableValue comboViewerObserveSingleSelection = ViewersObservables
.observeSingleSelection(comboViewer);
IObservableValue selfCurrentServiceObserveValue = PojoObservables
.observeValue(self, "currentService");
bindingContext.bindValue(comboViewerObserveSingleSelection,
selfCurrentServiceObserveValue, null, null);

Thanks again!
Previous Topic:Beansbinding assertion error
Next Topic:GWT Java Project Continuously Rebuilds
Goto Forum:
  


Current Time: Sun Sep 21 02:22:16 GMT 2014

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

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