comparison dwtx/jface/text/reconciler/AbstractReconcileStep.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.jface.text.reconciler.AbstractReconcileStep;
14
15 import dwt.dwthelper.utils;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20
21 import dwtx.core.runtime.Assert;
22 import dwtx.core.runtime.IProgressMonitor;
23 import dwtx.jface.text.IRegion;
24
25
26 /**
27 * Abstract implementation of a reconcile step.
28 *
29 * @since 3.0
30 */
31 public abstract class AbstractReconcileStep : IReconcileStep {
32
33 private IReconcileStep fNextStep;
34 private IReconcileStep fPreviousStep;
35 private IProgressMonitor fProgressMonitor;
36 protected IReconcilableModel fInputModel;
37
38 /**
39 * Creates an intermediate reconcile step which adds
40 * the given step to the pipe.
41 *
42 * @param step the reconcile step
43 */
44 public AbstractReconcileStep(IReconcileStep step) {
45 Assert.isNotNull(step);
46 fNextStep= step;
47 fNextStep.setPreviousStep(this);
48 }
49
50 /**
51 * Creates the last reconcile step of the pipe.
52 */
53 public AbstractReconcileStep() {
54 }
55
56 public bool isLastStep() {
57 return fNextStep is null;
58 }
59
60 public bool isFirstStep() {
61 return fPreviousStep is null;
62 }
63
64 /*
65 * @see dwtx.text.reconcilerpipe.IReconcilerResultCollector#setProgressMonitor(dwtx.core.runtime.IProgressMonitor)
66 */
67 public void setProgressMonitor(IProgressMonitor monitor) {
68 fProgressMonitor= monitor;
69
70 if (!isLastStep())
71 fNextStep.setProgressMonitor(monitor);
72 }
73
74 /*
75 * @see dwtx.jface.text.reconciler.IReconcileStep#getProgressMonitor()
76 */
77 public IProgressMonitor getProgressMonitor() {
78 return fProgressMonitor;
79 }
80
81 /*
82 * @see IReconcileStep#reconcile(IRegion)
83 */
84 public final IReconcileResult[] reconcile(IRegion partition) {
85 IReconcileResult[] result= reconcileModel(null, partition);
86 if (!isLastStep()) {
87 fNextStep.setInputModel(getModel());
88 IReconcileResult[] nextResult= fNextStep.reconcile(partition);
89 return merge(result, convertToInputModel(nextResult));
90 }
91 return result;
92 }
93
94 /*
95 * @see IReconcileStep#reconcile(dwtx.jface.text.reconciler.DirtyRegion, dwtx.jface.text.IRegion)
96 */
97 public final IReconcileResult[] reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
98 IReconcileResult[] result= reconcileModel(dirtyRegion, subRegion);
99 if (!isLastStep()) {
100 fNextStep.setInputModel(getModel());
101 IReconcileResult[] nextResult= fNextStep.reconcile(dirtyRegion, subRegion);
102 return merge(result, convertToInputModel(nextResult));
103 }
104 return result;
105 }
106
107
108 /**
109 * Reconciles the model of this reconcile step. The
110 * result is based on the input model.
111 *
112 * @param dirtyRegion the document region which has been changed
113 * @param subRegion the sub region in the dirty region which should be reconciled
114 * @return an array with reconcile results
115 */
116 abstract protected IReconcileResult[] reconcileModel(DirtyRegion dirtyRegion, IRegion subRegion);
117
118 /**
119 * Adapts the given an array with reconcile results to
120 * this step's input model and returns it.
121 *
122 * @param inputResults an array with reconcile results
123 * @return an array with the reconcile results adapted to the input model
124 */
125 protected IReconcileResult[] convertToInputModel(IReconcileResult[] inputResults) {
126 return inputResults;
127 }
128
129 /**
130 * Merges the two reconcile result arrays.
131 *
132 * @param results1 an array with reconcile results
133 * @param results2 an array with reconcile results
134 * @return an array with the merged reconcile results
135 */
136 private IReconcileResult[] merge(IReconcileResult[] results1, IReconcileResult[] results2) {
137 if (results1 is null)
138 return results2;
139
140 if (results2 is null)
141 return results1;
142
143 // XXX: not yet performance optimized
144 Collection collection= new ArrayList(Arrays.asList(results1));
145 collection.addAll(Arrays.asList(results2));
146 return (IReconcileResult[])collection.toArray(new IReconcileResult[collection.size()]);
147 }
148
149 /*
150 * @see IProgressMonitor#isCanceled()
151 */
152 protected final bool isCanceled() {
153 return fProgressMonitor !is null && fProgressMonitor.isCanceled();
154 }
155
156 /*
157 * @see IReconcileStep#setPreviousStep(IReconcileStep)
158 */
159 public void setPreviousStep(IReconcileStep step) {
160 Assert.isNotNull(step);
161 Assert.isTrue(fPreviousStep is null);
162 fPreviousStep= step;
163 }
164
165 /*
166 * @see IReconcileStep#setInputModel(Object)
167 */
168 public void setInputModel(IReconcilableModel inputModel) {
169 fInputModel= inputModel;
170
171 if (!isLastStep())
172 fNextStep.setInputModel(getModel());
173 }
174
175 /**
176 * Returns the reconcilable input model.
177 *
178 * @return the reconcilable input model.
179 */
180 public IReconcilableModel getInputModel() {
181 return fInputModel;
182 }
183
184 /**
185 * Returns the reconcilable model.
186 *
187 * @return the reconcilable model
188 */
189 abstract public IReconcilableModel getModel();
190 }