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) {
|
|
77 this.wizardPage = wizardPage;
|
|
78 this.dbc = dbc;
|
|
79 init();
|
|
80 }
|
|
81
|
|
82 private IChangeListener uiChangeListener = new class() IChangeListener {
|
|
83 public void handleChange(ChangeEvent event) {
|
|
84 handleUIChanged();
|
|
85 }
|
|
86 };
|
|
87 private IListChangeListener validationStatusProvidersListener = new class() IListChangeListener {
|
|
88 public void handleListChange(ListChangeEvent event) {
|
|
89 ListDiff diff = event.diff;
|
|
90 ListDiffEntry[] differences = diff.getDifferences();
|
|
91 for (int i = 0; i < differences.length; i++) {
|
|
92 ListDiffEntry listDiffEntry = differences[i];
|
|
93 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) listDiffEntry
|
|
94 .getElement();
|
|
95 IObservableList targets = validationStatusProvider.getTargets();
|
|
96 if (listDiffEntry.isAddition()) {
|
|
97 targets
|
|
98 .addListChangeListener(validationStatusProviderTargetsListener);
|
|
99 for (Iterator it = targets.iterator(); it.hasNext();) {
|
|
100 (cast(IObservable) it.next())
|
|
101 .addChangeListener(uiChangeListener);
|
|
102 }
|
|
103 } else {
|
|
104 targets
|
|
105 .removeListChangeListener(validationStatusProviderTargetsListener);
|
|
106 for (Iterator it = targets.iterator(); it.hasNext();) {
|
|
107 (cast(IObservable) it.next())
|
|
108 .removeChangeListener(uiChangeListener);
|
|
109 }
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113 };
|
|
114 private IListChangeListener validationStatusProviderTargetsListener = new class() IListChangeListener {
|
|
115 public void handleListChange(ListChangeEvent event) {
|
|
116 ListDiff diff = event.diff;
|
|
117 ListDiffEntry[] differences = diff.getDifferences();
|
|
118 for (int i = 0; i < differences.length; i++) {
|
|
119 ListDiffEntry listDiffEntry = differences[i];
|
|
120 IObservable target = cast(IObservable) listDiffEntry.getElement();
|
|
121 if (listDiffEntry.isAddition()) {
|
|
122 target.addChangeListener(uiChangeListener);
|
|
123 } else {
|
|
124 target.removeChangeListener(uiChangeListener);
|
|
125 }
|
|
126 }
|
|
127 }
|
|
128 };
|
|
129 private IStatus currentStatus;
|
|
130
|
|
131 protected void init() {
|
|
132 aggregateStatus = new AggregateValidationStatus(dbc
|
|
133 .getValidationStatusProviders(),
|
|
134 AggregateValidationStatus.MAX_SEVERITY);
|
|
135 aggregateStatus.addValueChangeListener(new class() IValueChangeListener {
|
|
136 public void handleValueChange(ValueChangeEvent event) {
|
|
137
|
|
138 currentStatus = cast(IStatus) event.diff.getNewValue();
|
|
139 handleStatusChanged();
|
|
140 }
|
|
141 });
|
|
142 currentStatus = cast(IStatus) aggregateStatus.getValue();
|
|
143 handleStatusChanged();
|
|
144 dbc.getValidationStatusProviders().addListChangeListener(
|
|
145 validationStatusProvidersListener);
|
|
146 for (Iterator it = dbc.getValidationStatusProviders().iterator(); it
|
|
147 .hasNext();) {
|
|
148 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) it
|
|
149 .next();
|
|
150 IObservableList targets = validationStatusProvider.getTargets();
|
|
151 targets
|
|
152 .addListChangeListener(validationStatusProviderTargetsListener);
|
|
153 for (Iterator iter = targets.iterator(); iter.hasNext();) {
|
|
154 (cast(IObservable) iter.next()).addChangeListener(uiChangeListener);
|
|
155 }
|
|
156 }
|
|
157 }
|
|
158
|
|
159 protected void handleUIChanged() {
|
|
160 uiChanged = true;
|
|
161 if (currentStatus !is null) {
|
|
162 handleStatusChanged();
|
|
163 }
|
|
164 dbc.getValidationStatusProviders().removeListChangeListener(
|
|
165 validationStatusProvidersListener);
|
|
166 for (Iterator it = dbc.getValidationStatusProviders().iterator(); it
|
|
167 .hasNext();) {
|
|
168 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) it
|
|
169 .next();
|
|
170 IObservableList targets = validationStatusProvider.getTargets();
|
|
171 targets
|
|
172 .removeListChangeListener(validationStatusProviderTargetsListener);
|
|
173 for (Iterator iter = targets.iterator(); iter.hasNext();) {
|
|
174 (cast(IObservable) iter.next())
|
|
175 .removeChangeListener(uiChangeListener);
|
|
176 }
|
|
177 }
|
|
178 }
|
|
179
|
|
180 protected void handleStatusChanged() {
|
|
181 if (currentStatus !is null
|
|
182 && currentStatus.getSeverity() is IStatus.ERROR) {
|
|
183 wizardPage.setPageComplete(false);
|
|
184 wizardPage.setMessage(null);
|
|
185 wizardPage.setErrorMessage(uiChanged ? currentStatus.getMessage()
|
|
186 : null);
|
|
187 if (currentStatusHasException()) {
|
|
188 handleStatusException();
|
|
189 }
|
|
190 } else if (currentStatus !is null
|
|
191 && currentStatus.getSeverity() !is IStatus.OK) {
|
|
192 int severity = currentStatus.getSeverity();
|
|
193 wizardPage.setPageComplete((severity & IStatus.CANCEL) !is 0);
|
|
194 int type;
|
|
195 switch (severity) {
|
|
196 case IStatus.OK:
|
|
197 type = IMessageProvider.NONE;
|
|
198 break;
|
|
199 case IStatus.CANCEL:
|
|
200 type = IMessageProvider.NONE;
|
|
201 break;
|
|
202 case IStatus.INFO:
|
|
203 type = IMessageProvider.INFORMATION;
|
|
204 break;
|
|
205 case IStatus.WARNING:
|
|
206 type = IMessageProvider.WARNING;
|
|
207 break;
|
|
208 case IStatus.ERROR:
|
|
209 type = IMessageProvider.ERROR;
|
|
210 break;
|
|
211 default:
|
|
212 throw new AssertionFailedException(
|
|
213 "incomplete switch statement"); //$NON-NLS-1$
|
|
214 }
|
|
215 wizardPage.setErrorMessage(null);
|
|
216 wizardPage.setMessage(currentStatus.getMessage(), type);
|
|
217 } else {
|
|
218 wizardPage.setPageComplete(true);
|
|
219 wizardPage.setMessage(null);
|
|
220 wizardPage.setErrorMessage(null);
|
|
221 }
|
|
222 }
|
|
223
|
|
224 private bool currentStatusHasException() {
|
|
225 bool hasException = false;
|
|
226 if (currentStatus.getException() !is null) {
|
|
227 hasException = true;
|
|
228 }
|
|
229 if (null !is cast(MultiStatus)currentStatus) {
|
|
230 MultiStatus multiStatus = cast(MultiStatus) currentStatus;
|
|
231
|
|
232 for (int i = 0; i < multiStatus.getChildren().length; i++) {
|
|
233 IStatus status = multiStatus.getChildren()[i];
|
|
234 if (status.getException() !is null) {
|
|
235 hasException = true;
|
|
236 break;
|
|
237 }
|
|
238 }
|
|
239 }
|
|
240 return hasException;
|
|
241 }
|
|
242
|
|
243 /**
|
|
244 * This is called when a Override to provide custom exception handling and
|
|
245 * reporting.
|
|
246 */
|
|
247 protected void handleStatusException() {
|
|
248 if (currentStatus.getException() !is null) {
|
|
249 logThrowable(currentStatus.getException());
|
|
250 } else if (null !is cast(MultiStatus)currentStatus) {
|
|
251 MultiStatus multiStatus = cast(MultiStatus) currentStatus;
|
|
252 for (int i = 0; i < multiStatus.getChildren().length; i++) {
|
|
253 IStatus status = multiStatus.getChildren()[i];
|
|
254 if (status.getException() !is null) {
|
|
255 logThrowable(status.getException());
|
|
256 }
|
|
257 }
|
|
258 }
|
|
259 }
|
|
260
|
|
261 private void logThrowable(Throwable throwable) {
|
|
262 Policy
|
|
263 .getLog()
|
|
264 .log(
|
|
265 new Status(
|
|
266 IStatus.ERROR,
|
|
267 Policy.JFACE_DATABINDING,
|
|
268 IStatus.OK,
|
|
269 "Unhandled exception: " + throwable.getMessage(), throwable)); //$NON-NLS-1$
|
|
270 }
|
|
271
|
|
272 /**
|
|
273 * Disposes of this wizard page support object, removing any listeners it
|
|
274 * may have attached.
|
|
275 */
|
|
276 public void dispose() {
|
|
277 aggregateStatus.dispose();
|
|
278 if (!uiChanged) {
|
|
279 for (Iterator it = dbc.getValidationStatusProviders().iterator(); it
|
|
280 .hasNext();) {
|
|
281 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) it
|
|
282 .next();
|
|
283 IObservableList targets = validationStatusProvider.getTargets();
|
|
284 targets
|
|
285 .removeListChangeListener(validationStatusProviderTargetsListener);
|
|
286 for (Iterator iter = targets.iterator(); iter.hasNext();) {
|
|
287 (cast(IObservable) iter.next())
|
|
288 .removeChangeListener(uiChangeListener);
|
|
289 }
|
|
290 }
|
|
291 dbc.getValidationStatusProviders().removeListChangeListener(
|
|
292 validationStatusProvidersListener);
|
|
293 }
|
|
294 aggregateStatus = null;
|
|
295 dbc = null;
|
|
296 uiChangeListener = null;
|
|
297 validationStatusProvidersListener = null;
|
|
298 validationStatusProviderTargetsListener = null;
|
|
299 wizardPage = null;
|
|
300 }
|
|
301 }
|