KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > databinding > wizard > WizardPageSupport


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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.databinding.wizard;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.core.databinding.AggregateValidationStatus;
16 import org.eclipse.core.databinding.Binding;
17 import org.eclipse.core.databinding.DataBindingContext;
18 import org.eclipse.core.databinding.observable.ChangeEvent;
19 import org.eclipse.core.databinding.observable.IChangeListener;
20 import org.eclipse.core.databinding.observable.list.IListChangeListener;
21 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
22 import org.eclipse.core.databinding.observable.list.ListDiff;
23 import org.eclipse.core.databinding.observable.list.ListDiffEntry;
24 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
25 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.jface.wizard.WizardPage;
28
29 /**
30  * Connects the validation result from the given data binding context to the
31  * given wizard page, updating the wizard page's completion state and its error
32  * message accordingly.
33  *
34  * This class is not intended to be extended by clients.
35  *
36  * @since 1.1
37  */

38 public class WizardPageSupport {
39
40     private WizardPage wizardPage;
41     private DataBindingContext dbc;
42     private AggregateValidationStatus aggregateStatus;
43     private boolean uiChanged = false;
44
45     /**
46      * Connect the validation result from the given data binding context to the
47      * given wizard page. Upon creation, the wizard page support will use the
48      * context's validation result to determine whether the page is complete.
49      * The page's error message will not be set at this time ensuring that the
50      * wizard page does not show an error right away. Upon any validation result
51      * change, {@link WizardPage#setPageComplete(boolean)} will be called
52      * reflecting the new validation result, and the wizard page's error message
53      * will be updated according to the current validation result.
54      *
55      * @param wizardPage
56      * @param dbc
57      * @return an instance of WizardPageSupport
58      */

59     public static WizardPageSupport create(WizardPage wizardPage,
60             DataBindingContext dbc) {
61         return new WizardPageSupport(wizardPage, dbc);
62     }
63
64     private WizardPageSupport(WizardPage wizardPage, DataBindingContext dbc) {
65         this.wizardPage = wizardPage;
66         this.dbc = dbc;
67         init();
68     }
69
70     private IChangeListener uiChangeListener = new IChangeListener() {
71         public void handleChange(ChangeEvent event) {
72             handleUIChanged();
73         }
74     };
75     private IListChangeListener bindingsListener = new IListChangeListener() {
76         public void handleListChange(ListChangeEvent event) {
77             ListDiff diff = event.diff;
78             ListDiffEntry[] differences = diff.getDifferences();
79             for (int i = 0; i < differences.length; i++) {
80                 ListDiffEntry listDiffEntry = differences[i];
81                 Binding binding = (Binding) listDiffEntry.getElement();
82                 if (listDiffEntry.isAddition()) {
83                     binding.getTarget().addChangeListener(uiChangeListener);
84                 } else {
85                     binding.getTarget().removeChangeListener(uiChangeListener);
86                 }
87             }
88         }
89     };
90     private IStatus currentStatus;
91
92     protected void init() {
93         aggregateStatus = new AggregateValidationStatus(dbc.getBindings(),
94                 AggregateValidationStatus.MAX_SEVERITY);
95         aggregateStatus.addValueChangeListener(new IValueChangeListener() {
96             public void handleValueChange(ValueChangeEvent event) {
97
98                 currentStatus = (IStatus) event.diff.getNewValue();
99                 handleStatusChanged();
100             }
101         });
102         currentStatus = (IStatus) aggregateStatus.getValue();
103         handleStatusChanged();
104         dbc.getBindings().addListChangeListener(bindingsListener);
105         for (Iterator JavaDoc it = dbc.getBindings().iterator(); it.hasNext();) {
106             Binding binding = (Binding) it.next();
107             binding.getTarget().addChangeListener(uiChangeListener);
108         }
109     }
110
111     protected void handleUIChanged() {
112         uiChanged = true;
113         if (currentStatus != null) {
114             handleStatusChanged();
115         }
116         dbc.getBindings().removeListChangeListener(bindingsListener);
117         for (Iterator JavaDoc it = dbc.getBindings().iterator(); it.hasNext();) {
118             Binding binding = (Binding) it.next();
119             binding.getTarget().removeChangeListener(uiChangeListener);
120         }
121     }
122
123     protected void handleStatusChanged() {
124         if (currentStatus != null
125                 && currentStatus.getSeverity() == IStatus.ERROR) {
126             wizardPage.setPageComplete(false);
127             wizardPage.setErrorMessage(uiChanged ? currentStatus.getMessage()
128                     : null);
129         } else {
130             wizardPage.setPageComplete(true);
131             wizardPage.setErrorMessage(null);
132         }
133     }
134
135     /**
136      * Disposes of this wizard page support object, removing any listeners it
137      * may have attached.
138      */

139     public void dispose() {
140         aggregateStatus.dispose();
141         if (!uiChanged) {
142             for (Iterator JavaDoc it = dbc.getBindings().iterator(); it.hasNext();) {
143                 Binding binding = (Binding) it.next();
144                 binding.getTarget().removeChangeListener(uiChangeListener);
145             }
146             dbc.getBindings().removeListChangeListener(bindingsListener);
147         }
148         aggregateStatus = null;
149         dbc = null;
150         uiChangeListener = null;
151         bindingsListener = null;
152         wizardPage = null;
153     }
154 }
155
Popular Tags