KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > reconciler > AbstractReconcileStep


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.jface.text.reconciler;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collection JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.reconciler.DirtyRegion;
22
23
24 /**
25  * Abstract implementation of a reconcile step.
26  *
27  * @since 3.0
28  */

29 public abstract class AbstractReconcileStep implements IReconcileStep {
30
31     private IReconcileStep fNextStep;
32     private IReconcileStep fPreviousStep;
33     private IProgressMonitor fProgressMonitor;
34     protected IReconcilableModel fInputModel;
35
36     /**
37      * Creates an intermediate reconcile step which adds
38      * the given step to the pipe.
39      *
40      * @param step the reconcile step
41      */

42     public AbstractReconcileStep(IReconcileStep step) {
43         Assert.isNotNull(step);
44         fNextStep= step;
45         fNextStep.setPreviousStep(this);
46     }
47
48     /**
49      * Creates the last reconcile step of the pipe.
50      */

51     public AbstractReconcileStep() {
52     }
53
54     public boolean isLastStep() {
55         return fNextStep == null;
56     }
57
58     public boolean isFirstStep() {
59         return fPreviousStep == null;
60     }
61
62     /*
63      * @see org.eclipse.text.reconcilerpipe.IReconcilerResultCollector#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)
64      */

65     public void setProgressMonitor(IProgressMonitor monitor) {
66         fProgressMonitor= monitor;
67
68         if (!isLastStep())
69             fNextStep.setProgressMonitor(monitor);
70     }
71
72     /*
73      * @see org.eclipse.jface.text.reconciler.IReconcileStep#getProgressMonitor()
74      */

75     public IProgressMonitor getProgressMonitor() {
76         return fProgressMonitor;
77     }
78
79     /*
80      * @see IReconcileStep#reconcile(IRegion)
81      */

82     public final IReconcileResult[] reconcile(IRegion partition) {
83         IReconcileResult[] result= reconcileModel(null, partition);
84         if (!isLastStep()) {
85             fNextStep.setInputModel(getModel());
86             IReconcileResult[] nextResult= fNextStep.reconcile(partition);
87             return merge(result, convertToInputModel(nextResult));
88         }
89         return result;
90     }
91
92     /*
93      * @see IReconcileStep#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion, org.eclipse.jface.text.IRegion)
94      */

95     public final IReconcileResult[] reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
96         IReconcileResult[] result= reconcileModel(dirtyRegion, subRegion);
97         if (!isLastStep()) {
98             fNextStep.setInputModel(getModel());
99             IReconcileResult[] nextResult= fNextStep.reconcile(dirtyRegion, subRegion);
100             return merge(result, convertToInputModel(nextResult));
101         }
102         return result;
103     }
104
105
106     /**
107      * Reconciles the model of this reconcile step. The
108      * result is based on the input model.
109      *
110      * @param dirtyRegion the document region which has been changed
111      * @param subRegion the sub region in the dirty region which should be reconciled
112      * @return an array with reconcile results
113      */

114     abstract protected IReconcileResult[] reconcileModel(DirtyRegion dirtyRegion, IRegion subRegion);
115
116     /**
117      * Adapts the given an array with reconcile results to
118      * this step's input model and returns it.
119      *
120      * @param inputResults an array with reconcile results
121      * @return an array with the reconcile results adapted to the input model
122      */

123     protected IReconcileResult[] convertToInputModel(IReconcileResult[] inputResults) {
124         return inputResults;
125     }
126
127     /**
128      * Merges the two reconcile result arrays.
129      *
130      * @param results1 an array with reconcile results
131      * @param results2 an array with reconcile results
132      * @return an array with the merged reconcile results
133      */

134     private IReconcileResult[] merge(IReconcileResult[] results1, IReconcileResult[] results2) {
135         if (results1 == null)
136             return results2;
137
138         if (results2 == null)
139             return results1;
140
141         // XXX: not yet performance optimized
142
Collection JavaDoc collection= new ArrayList JavaDoc(Arrays.asList(results1));
143         collection.addAll(Arrays.asList(results2));
144         return (IReconcileResult[])collection.toArray(new IReconcileResult[collection.size()]);
145     }
146
147     /*
148      * @see IProgressMonitor#isCanceled()
149      */

150     protected final boolean isCanceled() {
151         return fProgressMonitor != null && fProgressMonitor.isCanceled();
152     }
153
154     /*
155      * @see IReconcileStep#setPreviousStep(IReconcileStep)
156      */

157     public void setPreviousStep(IReconcileStep step) {
158         Assert.isNotNull(step);
159         Assert.isTrue(fPreviousStep == null);
160         fPreviousStep= step;
161     }
162
163     /*
164      * @see IReconcileStep#setInputModel(Object)
165      */

166     public void setInputModel(IReconcilableModel inputModel) {
167         fInputModel= inputModel;
168
169         if (!isLastStep())
170             fNextStep.setInputModel(getModel());
171     }
172
173     /**
174      * Returns the reconcilable input model.
175      *
176      * @return the reconcilable input model.
177      */

178     public IReconcilableModel getInputModel() {
179         return fInputModel;
180     }
181
182     /**
183      * Returns the reconcilable model.
184      *
185      * @return the reconcilable model
186      */

187     abstract public IReconcilableModel getModel();
188 }
189
Popular Tags