Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fe331b5232fcb535bc668a34910f9b79a659099d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boris Bokowski, IBM Corporation - initial API and implementation
 *     Matthew Hall - bug 260329
 *******************************************************************************/

package org.eclipse.jface.examples.databinding.snippets;

import java.util.Date;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.wizard.WizardPageSupport;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Creates and opens a wizard dialog with two simple wizard pages.
 */
public class Snippet014WizardDialog {

	static class FirstWizardPage extends WizardPage {
		private final class SingleDigitValidator implements IValidator {
			public IStatus validate(Object value) {
				Integer i = (Integer) value;
				if (i == null) {
					return ValidationStatus
							.info("Please enter a value.");
				}
				if (i.intValue() < 0 || i.intValue() > 9) {
					return ValidationStatus
							.error("Value must be between 0 and 9.");
				}
				return ValidationStatus.ok();
			}
		}

		protected FirstWizardPage() {
			super("First", "First Page", ImageDescriptor
					.createFromImage(new Image(Display.getDefault(), 16, 16)));
		}

		public void createControl(Composite parent) {
			DataBindingContext dbc = new DataBindingContext();
			WizardPageSupport.create(this, dbc);
			Composite composite = new Composite(parent, SWT.NONE);
			Label label = new Label(composite, SWT.NONE);
			label.setText("Enter a number between 0 and 9:");
			Text text = new Text(composite, SWT.BORDER);
			
			dbc.bindValue(
							SWTObservables.observeText(text, SWT.Modify),
							((SampleWizard) getWizard()).getModel().intValue,
							new UpdateValueStrategy().setAfterConvertValidator(new SingleDigitValidator()),
							null);
			
			GridLayoutFactory.swtDefaults().numColumns(2).generateLayout(
					composite);
			setControl(composite);
		}
	}

	static class SecondWizardPage extends WizardPage {
		protected SecondWizardPage() {
			super("Second", "Second Page", ImageDescriptor
					.createFromImage(new Image(Display.getDefault(), 16, 16)));
		}

		public void createControl(Composite parent) {
			DataBindingContext dbc = new DataBindingContext();
			WizardPageSupport.create(this, dbc);
			Composite composite = new Composite(parent, SWT.NONE);
			Label label = new Label(composite, SWT.NONE);
			label.setText("Enter a date:");
			Text text = new Text(composite, SWT.BORDER);
			
			dbc.bindValue(
							SWTObservables.observeText(text, SWT.Modify),
							((SampleWizard) getWizard()).getModel().dateValue);

			GridLayoutFactory.swtDefaults().numColumns(2).generateLayout(
					composite);
			setControl(composite);
		}
	}

	static class SampleWizardModel {
		IObservableValue intValue = new WritableValue(null, Integer.class);
		IObservableValue dateValue = new WritableValue(null, Date.class);
	}

	static class SampleWizard extends Wizard {

		private SampleWizardModel model = new SampleWizardModel();

		public void addPages() {
			addPage(new FirstWizardPage());
			addPage(new SecondWizardPage());
		}

		public SampleWizardModel getModel() {
			return model;
		}
		
		public String getWindowTitle() {
			return "Data Binding Snippet014";
		}

		public boolean performFinish() {
			return true;
		}

	}

	public static void main(String[] args) {
		Display display = new Display();

		// note that the "runWithDefault" will be done for you if you are using
		// the
		// Workbench as opposed to just JFace/SWT.
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				IWizard wizard = new SampleWizard();
				WizardDialog dialog = new WizardDialog(null, wizard);
				dialog.open();
				// The SWT event loop
				Display display = Display.getCurrent();
				while (dialog.getShell() != null
						&& !dialog.getShell().isDisposed()) {
					if (!display.readAndDispatch()) {
						display.sleep();
					}
				}
			}
		});
	}

}

Back to the top