Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet014WizardDialog.d @ 90:6086085e153d
Added databinding snippets. unmodified java sources.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:33:55 +0200 |
parents | |
children | 5d5bd660917f |
comparison
equal
deleted
inserted
replaced
89:0ca1aee7decf | 90:6086085e153d |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * Boris Bokowski, IBM Corporation - initial API and implementation | |
10 * Matthew Hall - bug 260329 | |
11 *******************************************************************************/ | |
12 | |
13 package org.eclipse.jface.examples.databinding.snippets; | |
14 | |
15 import java.util.Date; | |
16 | |
17 import org.eclipse.core.databinding.DataBindingContext; | |
18 import org.eclipse.core.databinding.UpdateValueStrategy; | |
19 import org.eclipse.core.databinding.observable.Realm; | |
20 import org.eclipse.core.databinding.observable.value.IObservableValue; | |
21 import org.eclipse.core.databinding.observable.value.WritableValue; | |
22 import org.eclipse.core.databinding.validation.IValidator; | |
23 import org.eclipse.core.databinding.validation.ValidationStatus; | |
24 import org.eclipse.core.runtime.IStatus; | |
25 import org.eclipse.jface.databinding.swt.SWTObservables; | |
26 import org.eclipse.jface.databinding.wizard.WizardPageSupport; | |
27 import org.eclipse.jface.layout.GridLayoutFactory; | |
28 import org.eclipse.jface.resource.ImageDescriptor; | |
29 import org.eclipse.jface.wizard.IWizard; | |
30 import org.eclipse.jface.wizard.Wizard; | |
31 import org.eclipse.jface.wizard.WizardDialog; | |
32 import org.eclipse.jface.wizard.WizardPage; | |
33 import org.eclipse.swt.SWT; | |
34 import org.eclipse.swt.graphics.Image; | |
35 import org.eclipse.swt.widgets.Composite; | |
36 import org.eclipse.swt.widgets.Display; | |
37 import org.eclipse.swt.widgets.Label; | |
38 import org.eclipse.swt.widgets.Text; | |
39 | |
40 /** | |
41 * Creates and opens a wizard dialog with two simple wizard pages. | |
42 */ | |
43 public class Snippet014WizardDialog { | |
44 | |
45 static class FirstWizardPage extends WizardPage { | |
46 private final class SingleDigitValidator implements IValidator { | |
47 public IStatus validate(Object value) { | |
48 Integer i = (Integer) value; | |
49 if (i == null) { | |
50 return ValidationStatus | |
51 .info("Please enter a value."); | |
52 } | |
53 if (i.intValue() < 0 || i.intValue() > 9) { | |
54 return ValidationStatus | |
55 .error("Value must be between 0 and 9."); | |
56 } | |
57 return ValidationStatus.ok(); | |
58 } | |
59 } | |
60 | |
61 protected FirstWizardPage() { | |
62 super("First", "First Page", ImageDescriptor | |
63 .createFromImage(new Image(Display.getDefault(), 16, 16))); | |
64 } | |
65 | |
66 public void createControl(Composite parent) { | |
67 DataBindingContext dbc = new DataBindingContext(); | |
68 WizardPageSupport.create(this, dbc); | |
69 Composite composite = new Composite(parent, SWT.NONE); | |
70 Label label = new Label(composite, SWT.NONE); | |
71 label.setText("Enter a number between 0 and 9:"); | |
72 Text text = new Text(composite, SWT.BORDER); | |
73 | |
74 dbc.bindValue( | |
75 SWTObservables.observeText(text, SWT.Modify), | |
76 ((SampleWizard) getWizard()).getModel().intValue, | |
77 new UpdateValueStrategy().setAfterConvertValidator(new SingleDigitValidator()), | |
78 null); | |
79 | |
80 GridLayoutFactory.swtDefaults().numColumns(2).generateLayout( | |
81 composite); | |
82 setControl(composite); | |
83 } | |
84 } | |
85 | |
86 static class SecondWizardPage extends WizardPage { | |
87 protected SecondWizardPage() { | |
88 super("Second", "Second Page", ImageDescriptor | |
89 .createFromImage(new Image(Display.getDefault(), 16, 16))); | |
90 } | |
91 | |
92 public void createControl(Composite parent) { | |
93 DataBindingContext dbc = new DataBindingContext(); | |
94 WizardPageSupport.create(this, dbc); | |
95 Composite composite = new Composite(parent, SWT.NONE); | |
96 Label label = new Label(composite, SWT.NONE); | |
97 label.setText("Enter a date:"); | |
98 Text text = new Text(composite, SWT.BORDER); | |
99 | |
100 dbc.bindValue( | |
101 SWTObservables.observeText(text, SWT.Modify), | |
102 ((SampleWizard) getWizard()).getModel().dateValue); | |
103 | |
104 GridLayoutFactory.swtDefaults().numColumns(2).generateLayout( | |
105 composite); | |
106 setControl(composite); | |
107 } | |
108 } | |
109 | |
110 static class SampleWizardModel { | |
111 IObservableValue intValue = new WritableValue(null, Integer.class); | |
112 IObservableValue dateValue = new WritableValue(null, Date.class); | |
113 } | |
114 | |
115 static class SampleWizard extends Wizard { | |
116 | |
117 private SampleWizardModel model = new SampleWizardModel(); | |
118 | |
119 public void addPages() { | |
120 addPage(new FirstWizardPage()); | |
121 addPage(new SecondWizardPage()); | |
122 } | |
123 | |
124 public SampleWizardModel getModel() { | |
125 return model; | |
126 } | |
127 | |
128 public String getWindowTitle() { | |
129 return "Data Binding Snippet014"; | |
130 } | |
131 | |
132 public boolean performFinish() { | |
133 return true; | |
134 } | |
135 | |
136 } | |
137 | |
138 public static void main(String[] args) { | |
139 Display display = new Display(); | |
140 | |
141 // note that the "runWithDefault" will be done for you if you are using | |
142 // the | |
143 // Workbench as opposed to just JFace/SWT. | |
144 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | |
145 public void run() { | |
146 IWizard wizard = new SampleWizard(); | |
147 WizardDialog dialog = new WizardDialog(null, wizard); | |
148 dialog.open(); | |
149 // The SWT event loop | |
150 Display display = Display.getCurrent(); | |
151 while (dialog.getShell() != null | |
152 && !dialog.getShell().isDisposed()) { | |
153 if (!display.readAndDispatch()) { | |
154 display.sleep(); | |
155 } | |
156 } | |
157 } | |
158 }); | |
159 } | |
160 | |
161 } |