Eclipse Databinding with Eclipse RCP applications - Tutorial
// -- Main.java Eclipse 3.4 --
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.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
public class Main extends Composite {
private Spinner spinner = null;
private Spinner spinner2 = null;
public Main(Composite parent, int style) {
super(parent, style);
initialize();
Model data = new Model();
IObservableValue observe = PojoObservables.observeValue(data, "selection");
DataBindingContext bc = new DataBindingContext();
bc.bindValue(SWTObservables.observeSelection(spinner), observe, null, null);
bc.bindValue(SWTObservables.observeSelection(spinner2), observe, null, null);
}
private void initialize() {
this.setBounds(new Rectangle(0, 0, 300, 100));
FillLayout fillLayout = new FillLayout();
fillLayout.type = org.eclipse.swt.SWT.VERTICAL;
spinner = new Spinner(this, SWT.NONE);
spinner2 = new Spinner(this, SWT.NONE);
this.setLayout(fillLayout);
}
public static void main(String[] args) {
final Display display = Display.getDefault();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(new Point(300, 100));
new Main(shell, SWT.NONE);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
});
display.dispose();
}
class Model {
private int selection;
public void setSelection(int selection) {
this.selection = selection;
}
public int getSelection() {
return selection;
}
}
}
沒有留言:
張貼留言