KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > internal > ValidationErrorList


1 /*******************************************************************************
2  * Copyright (c) 2006 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
12 package org.eclipse.jface.internal.databinding.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.internal.databinding.provisional.Binding;
19 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
20 import org.eclipse.jface.internal.databinding.provisional.observable.IChangeListener;
21 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable;
22 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiff;
23 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiffEntry;
24 import org.eclipse.jface.internal.databinding.provisional.observable.list.ObservableList;
25 import org.eclipse.jface.internal.databinding.provisional.observable.list.WritableList;
26 import org.eclipse.jface.internal.databinding.provisional.observable.value.IObservableValue;
27 import org.eclipse.jface.internal.databinding.provisional.validation.ValidationError;
28
29 /**
30  * @since 1.0
31  *
32  */

33 public class ValidationErrorList extends ObservableList {
34
35     private boolean isDirty = true;
36
37     private final WritableList bindings;
38
39     private final boolean usePartialErrors;
40
41     private List JavaDoc dependencies = new ArrayList JavaDoc();
42
43     private IChangeListener markDirtyChangeListener = new IChangeListener() {
44         public void handleChange(IObservable source) {
45             markDirty();
46         }
47     };
48
49     /**
50      * @param bindings
51      * @param usePartialErrors
52      */

53     public ValidationErrorList(WritableList bindings,
54             boolean usePartialErrors) {
55         super(new ArrayList JavaDoc(), ValidationError.class);
56         this.bindings = bindings;
57         this.usePartialErrors = usePartialErrors;
58         bindings.addChangeListener(markDirtyChangeListener);
59     }
60
61     protected void getterCalled() {
62         recompute();
63         super.getterCalled();
64     }
65
66     private void markDirty() {
67         // since we are dirty, we don't need to listen anymore
68
removeElementChangeListener();
69         final List JavaDoc oldList = wrappedList;
70         // lazy computation of diff
71
ListDiff listDiff = new ListDiff() {
72             ListDiffEntry[] cachedDifferences = null;
73             public ListDiffEntry[] getDifferences() {
74                 if(cachedDifferences==null) {
75                     recompute();
76                     cachedDifferences = Diffs.computeListDiff(oldList, wrappedList).getDifferences();
77                 }
78                 return cachedDifferences;
79             }
80         };
81         wrappedList = new ArrayList JavaDoc();
82         isDirty = true;
83         fireListChange(listDiff);
84     }
85
86     private void recompute() {
87         if (isDirty) {
88             List JavaDoc newContents = new ArrayList JavaDoc();
89             for (Iterator JavaDoc it = bindings.iterator(); it.hasNext();) {
90                 Binding binding = (Binding) it.next();
91                 IObservableValue validationError = usePartialErrors ? binding
92                         .getPartialValidationError() : binding
93                         .getValidationError();
94                 dependencies.add(validationError);
95                 validationError.addChangeListener(markDirtyChangeListener);
96                 Object JavaDoc validationErrorValue = validationError.getValue();
97                 if (validationErrorValue != null) {
98                     newContents.add(validationErrorValue);
99                 }
100             }
101             wrappedList.addAll(newContents);
102             isDirty = false;
103         }
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see org.eclipse.jface.internal.databinding.provisional.observable.list.ObservableList#dispose()
110      */

111     public void dispose() {
112         bindings.removeChangeListener(markDirtyChangeListener);
113         removeElementChangeListener();
114         super.dispose();
115     }
116
117     private void removeElementChangeListener() {
118         for (Iterator JavaDoc it = dependencies.iterator(); it.hasNext();) {
119             IObservableValue observableValue = (IObservableValue) it.next();
120             observableValue.removeChangeListener(markDirtyChangeListener);
121         }
122     }
123
124     public Object JavaDoc getElementType() {
125         return ValidationError.class;
126     }
127
128 }
129
Popular Tags