KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentExtension3;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITypedRegion;
25 import org.eclipse.jface.text.Region;
26 import org.eclipse.jface.text.TextUtilities;
27 import org.eclipse.jface.text.TypedRegion;
28
29
30 /**
31  * Standard implementation of {@link org.eclipse.jface.text.reconciler.IReconciler}.
32  * The reconciler is configured with a set of {@linkplain org.eclipse.jface.text.reconciler.IReconcilingStrategy reconciling strategies}
33  * each of which is responsible for a particular content type.
34  * <p>
35  * Usually, clients instantiate this class and configure it before using it.
36  * </p>
37  *
38  * @see org.eclipse.jface.text.IDocumentListener
39  * @see org.eclipse.jface.text.ITextInputListener
40  * @see org.eclipse.jface.text.reconciler.DirtyRegion
41  */

42 public class Reconciler extends AbstractReconciler implements IReconcilerExtension {
43
44     /** The map of reconciling strategies. */
45     private Map JavaDoc fStrategies;
46
47     /**
48      * The partitioning this reconciler uses.
49      *@since 3.0
50      */

51     private String JavaDoc fPartitioning;
52
53     /**
54      * Creates a new reconciler with the following configuration: it is
55      * an incremental reconciler with a standard delay of 500 milliseconds. There
56      * are no predefined reconciling strategies. The partitioning it uses
57      * is the default partitioning {@link IDocumentExtension3#DEFAULT_PARTITIONING}.
58      */

59     public Reconciler() {
60         super();
61         fPartitioning= IDocumentExtension3.DEFAULT_PARTITIONING;
62     }
63
64     /**
65      * Sets the document partitioning for this reconciler.
66      *
67      * @param partitioning the document partitioning for this reconciler
68      * @since 3.0
69      */

70     public void setDocumentPartitioning(String JavaDoc partitioning) {
71         Assert.isNotNull(partitioning);
72         fPartitioning= partitioning;
73     }
74
75     /*
76      * @see org.eclipse.jface.text.reconciler.IReconcilerExtension#getDocumentPartitioning()
77      * @since 3.0
78      */

79     public String JavaDoc getDocumentPartitioning() {
80         return fPartitioning;
81     }
82
83     /**
84      * Registers a given reconciling strategy for a particular content type.
85      * If there is already a strategy registered for this type, the new strategy
86      * is registered instead of the old one.
87      *
88      * @param strategy the reconciling strategy to register, or <code>null</code> to remove an existing one
89      * @param contentType the content type under which to register
90      */

91     public void setReconcilingStrategy(IReconcilingStrategy strategy, String JavaDoc contentType) {
92
93         Assert.isNotNull(contentType);
94
95         if (fStrategies == null)
96             fStrategies= new HashMap JavaDoc();
97
98         if (strategy == null)
99             fStrategies.remove(contentType);
100         else {
101             fStrategies.put(contentType, strategy);
102             if (strategy instanceof IReconcilingStrategyExtension && getProgressMonitor() != null) {
103                 IReconcilingStrategyExtension extension= (IReconcilingStrategyExtension) strategy;
104                 extension.setProgressMonitor(getProgressMonitor());
105             }
106         }
107     }
108
109     /*
110      * @see IReconciler#getReconcilingStrategy(String)
111      */

112     public IReconcilingStrategy getReconcilingStrategy(String JavaDoc contentType) {
113
114         Assert.isNotNull(contentType);
115
116         if (fStrategies == null)
117             return null;
118
119         return (IReconcilingStrategy) fStrategies.get(contentType);
120     }
121
122     /**
123      * Processes a dirty region. If the dirty region is <code>null</code> the whole
124      * document is consider being dirty. The dirty region is partitioned by the
125      * document and each partition is handed over to a reconciling strategy registered
126      * for the partition's content type.
127      *
128      * @param dirtyRegion the dirty region to be processed
129      * @see AbstractReconciler#process(DirtyRegion)
130      */

131     protected void process(DirtyRegion dirtyRegion) {
132
133         IRegion region= dirtyRegion;
134
135         if (region == null)
136             region= new Region(0, getDocument().getLength());
137
138         ITypedRegion[] regions= computePartitioning(region.getOffset(), region.getLength());
139
140         for (int i= 0; i < regions.length; i++) {
141             ITypedRegion r= regions[i];
142             IReconcilingStrategy s= getReconcilingStrategy(r.getType());
143             if (s == null)
144                 continue;
145
146             if(dirtyRegion != null)
147                 s.reconcile(dirtyRegion, r);
148             else
149                 s.reconcile(r);
150         }
151     }
152
153     /*
154      * @see AbstractReconciler#reconcilerDocumentChanged(IDocument)
155      * @since 2.0
156      */

157     protected void reconcilerDocumentChanged(IDocument document) {
158         if (fStrategies != null) {
159             Iterator JavaDoc e= fStrategies.values().iterator();
160             while (e.hasNext()) {
161                 IReconcilingStrategy strategy= (IReconcilingStrategy) e.next();
162                 strategy.setDocument(document);
163             }
164         }
165     }
166
167     /*
168      * @see AbstractReconciler#setProgressMonitor(IProgressMonitor)
169      * @since 2.0
170      */

171     public void setProgressMonitor(IProgressMonitor monitor) {
172         super.setProgressMonitor(monitor);
173
174         if (fStrategies != null) {
175             Iterator JavaDoc e= fStrategies.values().iterator();
176             while (e.hasNext()) {
177                 IReconcilingStrategy strategy= (IReconcilingStrategy) e.next();
178                 if (strategy instanceof IReconcilingStrategyExtension) {
179                     IReconcilingStrategyExtension extension= (IReconcilingStrategyExtension) strategy;
180                     extension.setProgressMonitor(monitor);
181                 }
182             }
183         }
184     }
185
186     /*
187      * @see AbstractReconciler#initialProcess()
188      * @since 2.0
189      */

190     protected void initialProcess() {
191         ITypedRegion[] regions= computePartitioning(0, getDocument().getLength());
192         for (int i= 0; i < regions.length; i++) {
193             ITypedRegion r= regions[i];
194             IReconcilingStrategy s= getReconcilingStrategy(r.getType());
195             if (s instanceof IReconcilingStrategyExtension) {
196                 IReconcilingStrategyExtension e= (IReconcilingStrategyExtension) s;
197                 e.initialReconcile();
198             }
199         }
200     }
201
202     /**
203      * Computes and returns the partitioning for the given region of the input document
204      * of the reconciler's connected text viewer.
205      *
206      * @param offset the region offset
207      * @param length the region length
208      * @return the computed partitioning
209      * @since 3.0
210      */

211     private ITypedRegion[] computePartitioning(int offset, int length) {
212         ITypedRegion[] regions= null;
213         try {
214             regions= TextUtilities.computePartitioning(getDocument(), getDocumentPartitioning(), offset, length, false);
215         } catch (BadLocationException x) {
216             regions= new TypedRegion[0];
217         }
218         return regions;
219     }
220 }
221
Popular Tags