1 11 package org.eclipse.jface.text.reconciler; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 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 42 public class Reconciler extends AbstractReconciler implements IReconcilerExtension { 43 44 45 private Map fStrategies; 46 47 51 private String fPartitioning; 52 53 59 public Reconciler() { 60 super(); 61 fPartitioning= IDocumentExtension3.DEFAULT_PARTITIONING; 62 } 63 64 70 public void setDocumentPartitioning(String partitioning) { 71 Assert.isNotNull(partitioning); 72 fPartitioning= partitioning; 73 } 74 75 79 public String getDocumentPartitioning() { 80 return fPartitioning; 81 } 82 83 91 public void setReconcilingStrategy(IReconcilingStrategy strategy, String contentType) { 92 93 Assert.isNotNull(contentType); 94 95 if (fStrategies == null) 96 fStrategies= new HashMap (); 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 112 public IReconcilingStrategy getReconcilingStrategy(String contentType) { 113 114 Assert.isNotNull(contentType); 115 116 if (fStrategies == null) 117 return null; 118 119 return (IReconcilingStrategy) fStrategies.get(contentType); 120 } 121 122 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 157 protected void reconcilerDocumentChanged(IDocument document) { 158 if (fStrategies != null) { 159 Iterator e= fStrategies.values().iterator(); 160 while (e.hasNext()) { 161 IReconcilingStrategy strategy= (IReconcilingStrategy) e.next(); 162 strategy.setDocument(document); 163 } 164 } 165 } 166 167 171 public void setProgressMonitor(IProgressMonitor monitor) { 172 super.setProgressMonitor(monitor); 173 174 if (fStrategies != null) { 175 Iterator 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 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 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 |