1 11 package org.eclipse.jface.databinding.wizard; 12 13 import java.util.Iterator ; 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 38 public class WizardPageSupport { 39 40 private WizardPage wizardPage; 41 private DataBindingContext dbc; 42 private AggregateValidationStatus aggregateStatus; 43 private boolean uiChanged = false; 44 45 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 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 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 139 public void dispose() { 140 aggregateStatus.dispose(); 141 if (!uiChanged) { 142 for (Iterator 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 |