78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007, 2008 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 * IBM Corporation - initial API and implementation
|
|
10 * Boris Bokowski - bug 218269
|
|
11 * Matthew Hall - bug 218269
|
|
12 * Ashley Cambrell - bug 199179
|
|
13 *******************************************************************************/
|
|
14 module org.eclipse.jface.databinding.wizard.WizardPageSupport;
|
|
15
|
|
16 import java.lang.all;
|
|
17
|
|
18 import java.util.Iterator;
|
|
19
|
|
20 import org.eclipse.core.databinding.AggregateValidationStatus;
|
|
21 import org.eclipse.core.databinding.DataBindingContext;
|
|
22 import org.eclipse.core.databinding.ValidationStatusProvider;
|
|
23 import org.eclipse.core.databinding.observable.ChangeEvent;
|
|
24 import org.eclipse.core.databinding.observable.IChangeListener;
|
|
25 import org.eclipse.core.databinding.observable.IObservable;
|
|
26 import org.eclipse.core.databinding.observable.list.IListChangeListener;
|
|
27 import org.eclipse.core.databinding.observable.list.IObservableList;
|
|
28 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
|
|
29 import org.eclipse.core.databinding.observable.list.ListDiff;
|
|
30 import org.eclipse.core.databinding.observable.list.ListDiffEntry;
|
|
31 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
|
|
32 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
|
|
33 import org.eclipse.core.databinding.util.Policy;
|
|
34 import org.eclipse.core.runtime.AssertionFailedException;
|
|
35 import org.eclipse.core.runtime.IStatus;
|
|
36 import org.eclipse.core.runtime.MultiStatus;
|
|
37 import org.eclipse.core.runtime.Status;
|
|
38 import org.eclipse.jface.dialogs.IMessageProvider;
|
|
39 import org.eclipse.jface.wizard.WizardPage;
|
|
40
|
|
41 /**
|
|
42 * Connects the validation result from the given data binding context to the
|
|
43 * given wizard page, updating the wizard page's completion state and its error
|
|
44 * message accordingly.
|
|
45 *
|
|
46 * @noextend This class is not intended to be subclassed by clients.
|
|
47 *
|
|
48 * @since 1.1
|
|
49 */
|
|
50 public class WizardPageSupport {
|
|
51
|
|
52 private WizardPage wizardPage;
|
|
53 private DataBindingContext dbc;
|
|
54 private AggregateValidationStatus aggregateStatus;
|
|
55 private bool uiChanged = false;
|
|
56
|
|
57 /**
|
|
58 * Connect the validation result from the given data binding context to the
|
|
59 * given wizard page. Upon creation, the wizard page support will use the
|
|
60 * context's validation result to determine whether the page is complete.
|
|
61 * The page's error message will not be set at this time ensuring that the
|
|
62 * wizard page does not show an error right away. Upon any validation result
|
|
63 * change, {@link WizardPage#setPageComplete(bool)} will be called
|
|
64 * reflecting the new validation result, and the wizard page's error message
|
|
65 * will be updated according to the current validation result.
|
|
66 *
|
|
67 * @param wizardPage
|
|
68 * @param dbc
|
|
69 * @return an instance of WizardPageSupport
|
|
70 */
|
|
71 public static WizardPageSupport create(WizardPage wizardPage,
|
|
72 DataBindingContext dbc) {
|
|
73 return new WizardPageSupport(wizardPage, dbc);
|
|
74 }
|
|
75
|
|
76 private this(WizardPage wizardPage, DataBindingContext dbc) {
|
85
|
77 uiChangeListener = new UiChangeListener();
|
|
78 validationStatusProvidersListener = new ValidationStatusProvidersListener();
|
|
79 validationStatusProviderTargetsListener = new ValidationStatusProviderTargetsListener();
|
78
|
80 this.wizardPage = wizardPage;
|
|
81 this.dbc = dbc;
|
|
82 init();
|
|
83 }
|
|
84
|
85
|
85 private IChangeListener uiChangeListener;
|
|
86 class UiChangeListener : IChangeListener {
|
78
|
87 public void handleChange(ChangeEvent event) {
|
|
88 handleUIChanged();
|
|
89 }
|
|
90 };
|
85
|
91 private IListChangeListener validationStatusProvidersListener;
|
|
92 class ValidationStatusProvidersListener : IListChangeListener {
|
78
|
93 public void handleListChange(ListChangeEvent event) {
|
|
94 ListDiff diff = event.diff;
|
|
95 ListDiffEntry[] differences = diff.getDifferences();
|
|
96 for (int i = 0; i < differences.length; i++) {
|
|
97 ListDiffEntry listDiffEntry = differences[i];
|
|
98 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) listDiffEntry
|
|
99 .getElement();
|
|
100 IObservableList targets = validationStatusProvider.getTargets();
|
|
101 if (listDiffEntry.isAddition()) {
|
|
102 targets
|
|
103 .addListChangeListener(validationStatusProviderTargetsListener);
|
|
104 for (Iterator it = targets.iterator(); it.hasNext();) {
|
|
105 (cast(IObservable) it.next())
|
|
106 .addChangeListener(uiChangeListener);
|
|
107 }
|
|
108 } else {
|
|
109 targets
|
|
110 .removeListChangeListener(validationStatusProviderTargetsListener);
|
|
111 for (Iterator it = targets.iterator(); it.hasNext();) {
|
|
112 (cast(IObservable) it.next())
|
|
113 .removeChangeListener(uiChangeListener);
|
|
114 }
|
|
115 }
|
|
116 }
|
|
117 }
|
|
118 };
|
85
|
119 private IListChangeListener validationStatusProviderTargetsListener;
|
|
120 class ValidationStatusProviderTargetsListener : IListChangeListener {
|
78
|
121 public void handleListChange(ListChangeEvent event) {
|
|
122 ListDiff diff = event.diff;
|
|
123 ListDiffEntry[] differences = diff.getDifferences();
|
|
124 for (int i = 0; i < differences.length; i++) {
|
|
125 ListDiffEntry listDiffEntry = differences[i];
|
|
126 IObservable target = cast(IObservable) listDiffEntry.getElement();
|
|
127 if (listDiffEntry.isAddition()) {
|
|
128 target.addChangeListener(uiChangeListener);
|
|
129 } else {
|
|
130 target.removeChangeListener(uiChangeListener);
|
|
131 }
|
|
132 }
|
|
133 }
|
|
134 };
|
|
135 private IStatus currentStatus;
|
|
136
|
|
137 protected void init() {
|
|
138 aggregateStatus = new AggregateValidationStatus(dbc
|
|
139 .getValidationStatusProviders(),
|
|
140 AggregateValidationStatus.MAX_SEVERITY);
|
|
141 aggregateStatus.addValueChangeListener(new class() IValueChangeListener {
|
|
142 public void handleValueChange(ValueChangeEvent event) {
|
|
143
|
|
144 currentStatus = cast(IStatus) event.diff.getNewValue();
|
|
145 handleStatusChanged();
|
|
146 }
|
|
147 });
|
|
148 currentStatus = cast(IStatus) aggregateStatus.getValue();
|
|
149 handleStatusChanged();
|
|
150 dbc.getValidationStatusProviders().addListChangeListener(
|
|
151 validationStatusProvidersListener);
|
|
152 for (Iterator it = dbc.getValidationStatusProviders().iterator(); it
|
|
153 .hasNext();) {
|
|
154 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) it
|
|
155 .next();
|
|
156 IObservableList targets = validationStatusProvider.getTargets();
|
|
157 targets
|
|
158 .addListChangeListener(validationStatusProviderTargetsListener);
|
|
159 for (Iterator iter = targets.iterator(); iter.hasNext();) {
|
|
160 (cast(IObservable) iter.next()).addChangeListener(uiChangeListener);
|
|
161 }
|
|
162 }
|
|
163 }
|
|
164
|
|
165 protected void handleUIChanged() {
|
|
166 uiChanged = true;
|
|
167 if (currentStatus !is null) {
|
|
168 handleStatusChanged();
|
|
169 }
|
|
170 dbc.getValidationStatusProviders().removeListChangeListener(
|
|
171 validationStatusProvidersListener);
|
|
172 for (Iterator it = dbc.getValidationStatusProviders().iterator(); it
|
|
173 .hasNext();) {
|
|
174 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) it
|
|
175 .next();
|
|
176 IObservableList targets = validationStatusProvider.getTargets();
|
|
177 targets
|
|
178 .removeListChangeListener(validationStatusProviderTargetsListener);
|
|
179 for (Iterator iter = targets.iterator(); iter.hasNext();) {
|
|
180 (cast(IObservable) iter.next())
|
|
181 .removeChangeListener(uiChangeListener);
|
|
182 }
|
|
183 }
|
|
184 }
|
|
185
|
|
186 protected void handleStatusChanged() {
|
|
187 if (currentStatus !is null
|
|
188 && currentStatus.getSeverity() is IStatus.ERROR) {
|
|
189 wizardPage.setPageComplete(false);
|
|
190 wizardPage.setMessage(null);
|
|
191 wizardPage.setErrorMessage(uiChanged ? currentStatus.getMessage()
|
|
192 : null);
|
|
193 if (currentStatusHasException()) {
|
|
194 handleStatusException();
|
|
195 }
|
|
196 } else if (currentStatus !is null
|
|
197 && currentStatus.getSeverity() !is IStatus.OK) {
|
|
198 int severity = currentStatus.getSeverity();
|
|
199 wizardPage.setPageComplete((severity & IStatus.CANCEL) !is 0);
|
|
200 int type;
|
|
201 switch (severity) {
|
|
202 case IStatus.OK:
|
|
203 type = IMessageProvider.NONE;
|
|
204 break;
|
|
205 case IStatus.CANCEL:
|
|
206 type = IMessageProvider.NONE;
|
|
207 break;
|
|
208 case IStatus.INFO:
|
|
209 type = IMessageProvider.INFORMATION;
|
|
210 break;
|
|
211 case IStatus.WARNING:
|
|
212 type = IMessageProvider.WARNING;
|
|
213 break;
|
|
214 case IStatus.ERROR:
|
|
215 type = IMessageProvider.ERROR;
|
|
216 break;
|
|
217 default:
|
|
218 throw new AssertionFailedException(
|
|
219 "incomplete switch statement"); //$NON-NLS-1$
|
|
220 }
|
|
221 wizardPage.setErrorMessage(null);
|
|
222 wizardPage.setMessage(currentStatus.getMessage(), type);
|
|
223 } else {
|
|
224 wizardPage.setPageComplete(true);
|
|
225 wizardPage.setMessage(null);
|
|
226 wizardPage.setErrorMessage(null);
|
|
227 }
|
|
228 }
|
|
229
|
|
230 private bool currentStatusHasException() {
|
|
231 bool hasException = false;
|
|
232 if (currentStatus.getException() !is null) {
|
|
233 hasException = true;
|
|
234 }
|
|
235 if (null !is cast(MultiStatus)currentStatus) {
|
|
236 MultiStatus multiStatus = cast(MultiStatus) currentStatus;
|
|
237
|
|
238 for (int i = 0; i < multiStatus.getChildren().length; i++) {
|
|
239 IStatus status = multiStatus.getChildren()[i];
|
|
240 if (status.getException() !is null) {
|
|
241 hasException = true;
|
|
242 break;
|
|
243 }
|
|
244 }
|
|
245 }
|
|
246 return hasException;
|
|
247 }
|
|
248
|
|
249 /**
|
|
250 * This is called when a Override to provide custom exception handling and
|
|
251 * reporting.
|
|
252 */
|
|
253 protected void handleStatusException() {
|
|
254 if (currentStatus.getException() !is null) {
|
|
255 logThrowable(currentStatus.getException());
|
|
256 } else if (null !is cast(MultiStatus)currentStatus) {
|
|
257 MultiStatus multiStatus = cast(MultiStatus) currentStatus;
|
|
258 for (int i = 0; i < multiStatus.getChildren().length; i++) {
|
|
259 IStatus status = multiStatus.getChildren()[i];
|
|
260 if (status.getException() !is null) {
|
|
261 logThrowable(status.getException());
|
|
262 }
|
|
263 }
|
|
264 }
|
|
265 }
|
|
266
|
|
267 private void logThrowable(Throwable throwable) {
|
|
268 Policy
|
|
269 .getLog()
|
|
270 .log(
|
|
271 new Status(
|
|
272 IStatus.ERROR,
|
|
273 Policy.JFACE_DATABINDING,
|
|
274 IStatus.OK,
|
85
|
275 Format("Unhandled exception: {}", throwable.msg), throwable)); //$NON-NLS-1$
|
78
|
276 }
|
|
277
|
|
278 /**
|
|
279 * Disposes of this wizard page support object, removing any listeners it
|
|
280 * may have attached.
|
|
281 */
|
|
282 public void dispose() {
|
|
283 aggregateStatus.dispose();
|
|
284 if (!uiChanged) {
|
|
285 for (Iterator it = dbc.getValidationStatusProviders().iterator(); it
|
|
286 .hasNext();) {
|
|
287 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) it
|
|
288 .next();
|
|
289 IObservableList targets = validationStatusProvider.getTargets();
|
|
290 targets
|
|
291 .removeListChangeListener(validationStatusProviderTargetsListener);
|
|
292 for (Iterator iter = targets.iterator(); iter.hasNext();) {
|
|
293 (cast(IObservable) iter.next())
|
|
294 .removeChangeListener(uiChangeListener);
|
|
295 }
|
|
296 }
|
|
297 dbc.getValidationStatusProviders().removeListChangeListener(
|
|
298 validationStatusProvidersListener);
|
|
299 }
|
|
300 aggregateStatus = null;
|
|
301 dbc = null;
|
|
302 uiChangeListener = null;
|
|
303 validationStatusProvidersListener = null;
|
|
304 validationStatusProviderTargetsListener = null;
|
|
305 wizardPage = null;
|
|
306 }
|
|
307 }
|