Mercurial > projects > dwt-addons
comparison dwtx/jface/text/reconciler/Reconciler.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, 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.reconciler.Reconciler; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import java.util.ArrayList; | |
18 import java.util.HashMap; | |
19 import java.util.Iterator; | |
20 import java.util.List; | |
21 import java.util.Map; | |
22 | |
23 import dwtx.core.runtime.Assert; | |
24 import dwtx.core.runtime.IProgressMonitor; | |
25 import dwtx.jface.text.BadLocationException; | |
26 import dwtx.jface.text.IDocument; | |
27 import dwtx.jface.text.IDocumentExtension3; | |
28 import dwtx.jface.text.IRegion; | |
29 import dwtx.jface.text.ITypedRegion; | |
30 import dwtx.jface.text.Region; | |
31 import dwtx.jface.text.TextUtilities; | |
32 import dwtx.jface.text.TypedRegion; | |
33 | |
34 | |
35 /** | |
36 * Standard implementation of {@link dwtx.jface.text.reconciler.IReconciler}. | |
37 * The reconciler is configured with a set of {@linkplain dwtx.jface.text.reconciler.IReconcilingStrategy reconciling strategies} | |
38 * each of which is responsible for a particular content type. | |
39 * <p> | |
40 * Usually, clients instantiate this class and configure it before using it. | |
41 * </p> | |
42 * | |
43 * @see dwtx.jface.text.IDocumentListener | |
44 * @see dwtx.jface.text.ITextInputListener | |
45 * @see dwtx.jface.text.reconciler.DirtyRegion | |
46 */ | |
47 public class Reconciler : AbstractReconciler , IReconcilerExtension { | |
48 | |
49 /** The map of reconciling strategies. */ | |
50 private Map fStrategies; | |
51 | |
52 /** | |
53 * The partitioning this reconciler uses. | |
54 *@since 3.0 | |
55 */ | |
56 private String fPartitioning; | |
57 | |
58 /** | |
59 * Creates a new reconciler with the following configuration: it is | |
60 * an incremental reconciler with a standard delay of 500 milliseconds. There | |
61 * are no predefined reconciling strategies. The partitioning it uses | |
62 * is the default partitioning {@link IDocumentExtension3#DEFAULT_PARTITIONING}. | |
63 */ | |
64 public Reconciler() { | |
65 super(); | |
66 fPartitioning= IDocumentExtension3.DEFAULT_PARTITIONING; | |
67 } | |
68 | |
69 /** | |
70 * Sets the document partitioning for this reconciler. | |
71 * | |
72 * @param partitioning the document partitioning for this reconciler | |
73 * @since 3.0 | |
74 */ | |
75 public void setDocumentPartitioning(String partitioning) { | |
76 Assert.isNotNull(partitioning); | |
77 fPartitioning= partitioning; | |
78 } | |
79 | |
80 /* | |
81 * @see dwtx.jface.text.reconciler.IReconcilerExtension#getDocumentPartitioning() | |
82 * @since 3.0 | |
83 */ | |
84 public String getDocumentPartitioning() { | |
85 return fPartitioning; | |
86 } | |
87 | |
88 /** | |
89 * Registers a given reconciling strategy for a particular content type. | |
90 * If there is already a strategy registered for this type, the new strategy | |
91 * is registered instead of the old one. | |
92 * | |
93 * @param strategy the reconciling strategy to register, or <code>null</code> to remove an existing one | |
94 * @param contentType the content type under which to register | |
95 */ | |
96 public void setReconcilingStrategy(IReconcilingStrategy strategy, String contentType) { | |
97 | |
98 Assert.isNotNull(contentType); | |
99 | |
100 if (fStrategies is null) | |
101 fStrategies= new HashMap(); | |
102 | |
103 if (strategy is null) | |
104 fStrategies.remove(contentType); | |
105 else { | |
106 fStrategies.put(contentType, strategy); | |
107 if (strategy instanceof IReconcilingStrategyExtension && getProgressMonitor() !is null) { | |
108 IReconcilingStrategyExtension extension= (IReconcilingStrategyExtension) strategy; | |
109 extension.setProgressMonitor(getProgressMonitor()); | |
110 } | |
111 } | |
112 } | |
113 | |
114 /* | |
115 * @see IReconciler#getReconcilingStrategy(String) | |
116 */ | |
117 public IReconcilingStrategy getReconcilingStrategy(String contentType) { | |
118 | |
119 Assert.isNotNull(contentType); | |
120 | |
121 if (fStrategies is null) | |
122 return null; | |
123 | |
124 return (IReconcilingStrategy) fStrategies.get(contentType); | |
125 } | |
126 | |
127 /** | |
128 * Processes a dirty region. If the dirty region is <code>null</code> the whole | |
129 * document is consider being dirty. The dirty region is partitioned by the | |
130 * document and each partition is handed over to a reconciling strategy registered | |
131 * for the partition's content type. | |
132 * | |
133 * @param dirtyRegion the dirty region to be processed | |
134 * @see AbstractReconciler#process(DirtyRegion) | |
135 */ | |
136 protected void process(DirtyRegion dirtyRegion) { | |
137 | |
138 IRegion region= dirtyRegion; | |
139 | |
140 if (region is null) | |
141 region= new Region(0, getDocument().getLength()); | |
142 | |
143 ITypedRegion[] regions= computePartitioning(region.getOffset(), region.getLength()); | |
144 | |
145 for (int i= 0; i < regions.length; i++) { | |
146 ITypedRegion r= regions[i]; | |
147 IReconcilingStrategy s= getReconcilingStrategy(r.getType()); | |
148 if (s is null) | |
149 continue; | |
150 | |
151 if(dirtyRegion !is null) | |
152 s.reconcile(dirtyRegion, r); | |
153 else | |
154 s.reconcile(r); | |
155 } | |
156 } | |
157 | |
158 /* | |
159 * @see AbstractReconciler#reconcilerDocumentChanged(IDocument) | |
160 * @since 2.0 | |
161 */ | |
162 protected void reconcilerDocumentChanged(IDocument document) { | |
163 if (fStrategies !is null) { | |
164 Iterator e= fStrategies.values().iterator(); | |
165 while (e.hasNext()) { | |
166 IReconcilingStrategy strategy= (IReconcilingStrategy) e.next(); | |
167 strategy.setDocument(document); | |
168 } | |
169 } | |
170 } | |
171 | |
172 /* | |
173 * @see AbstractReconciler#setProgressMonitor(IProgressMonitor) | |
174 * @since 2.0 | |
175 */ | |
176 public void setProgressMonitor(IProgressMonitor monitor) { | |
177 super.setProgressMonitor(monitor); | |
178 | |
179 if (fStrategies !is null) { | |
180 Iterator e= fStrategies.values().iterator(); | |
181 while (e.hasNext()) { | |
182 IReconcilingStrategy strategy= (IReconcilingStrategy) e.next(); | |
183 if (strategy instanceof IReconcilingStrategyExtension) { | |
184 IReconcilingStrategyExtension extension= (IReconcilingStrategyExtension) strategy; | |
185 extension.setProgressMonitor(monitor); | |
186 } | |
187 } | |
188 } | |
189 } | |
190 | |
191 /* | |
192 * @see AbstractReconciler#initialProcess() | |
193 * @since 2.0 | |
194 */ | |
195 protected void initialProcess() { | |
196 ITypedRegion[] regions= computePartitioning(0, getDocument().getLength()); | |
197 List contentTypes= new ArrayList(regions.length); | |
198 for (int i= 0; i < regions.length; i++) { | |
199 String contentType= regions[i].getType(); | |
200 if( contentTypes.contains(contentType)) | |
201 continue; | |
202 contentTypes.add(contentType); | |
203 IReconcilingStrategy s= getReconcilingStrategy(contentType); | |
204 if (s instanceof IReconcilingStrategyExtension) { | |
205 IReconcilingStrategyExtension e= (IReconcilingStrategyExtension) s; | |
206 e.initialReconcile(); | |
207 } | |
208 } | |
209 } | |
210 | |
211 /** | |
212 * Computes and returns the partitioning for the given region of the input document | |
213 * of the reconciler's connected text viewer. | |
214 * | |
215 * @param offset the region offset | |
216 * @param length the region length | |
217 * @return the computed partitioning | |
218 * @since 3.0 | |
219 */ | |
220 private ITypedRegion[] computePartitioning(int offset, int length) { | |
221 ITypedRegion[] regions= null; | |
222 try { | |
223 regions= TextUtilities.computePartitioning(getDocument(), getDocumentPartitioning(), offset, length, false); | |
224 } catch (BadLocationException x) { | |
225 regions= new TypedRegion[0]; | |
226 } | |
227 return regions; | |
228 } | |
229 } |