1 11 package org.eclipse.pde.internal.ui.editor; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.jface.text.BadLocationException; 22 import org.eclipse.jface.text.IDocument; 23 import org.eclipse.jface.text.Position; 24 import org.eclipse.jface.text.source.Annotation; 25 import org.eclipse.jface.text.source.projection.ProjectionAnnotation; 26 import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; 27 import org.eclipse.pde.core.IModelChangedEvent; 28 import org.eclipse.pde.core.IModelChangedListener; 29 import org.eclipse.pde.internal.core.text.IEditingModel; 30 31 public abstract class AbstractFoldingStructureProvider implements 32 IFoldingStructureProvider, IModelChangedListener { 33 34 private PDESourcePage fEditor; 35 private IEditingModel fModel; 36 37 public AbstractFoldingStructureProvider(PDESourcePage editor, IEditingModel model) { 38 this.fEditor = editor; 39 this.fModel = model; 40 } 41 42 public void update() { 43 ProjectionAnnotationModel annotationModel = 44 (ProjectionAnnotationModel) fEditor.getAdapter(ProjectionAnnotationModel.class); 45 if (annotationModel == null) 46 return; 47 48 Set currentRegions = new HashSet (); 49 try { 50 addFoldingRegions(currentRegions, fModel); 51 updateFoldingRegions(annotationModel, currentRegions); 52 } catch (BadLocationException e) {} 53 } 54 55 public void updateFoldingRegions(ProjectionAnnotationModel model, Set currentRegions) { 56 Annotation[] deletions = computeDifferences(model, currentRegions); 57 58 Map additionsMap = new HashMap (); 59 for (Iterator iter = currentRegions.iterator(); iter.hasNext();) { 60 Object position = iter.next(); 61 additionsMap.put(new ProjectionAnnotation(false), position); 62 } 63 64 if ((deletions.length != 0 || additionsMap.size() != 0)) { 65 model.modifyAnnotations(deletions, additionsMap, new Annotation[] {}); 66 } 67 } 68 69 private Annotation[] computeDifferences(ProjectionAnnotationModel model, Set additions) { 70 List deletions = new ArrayList (); 71 for (Iterator iter = model.getAnnotationIterator(); iter.hasNext();) { 72 Object annotation = iter.next(); 73 if (annotation instanceof ProjectionAnnotation) { 74 Position position = model.getPosition((Annotation) annotation); 75 if (additions.contains(position)) { 76 additions.remove(position); 77 } else { 78 deletions.add(annotation); 79 } 80 } 81 } 82 return (Annotation[]) deletions.toArray(new Annotation[deletions.size()]); 83 } 84 85 public void initialize() { 86 update(); 87 } 88 89 public void modelChanged(IModelChangedEvent event) { 90 update(); 91 } 92 93 public void reconciled(IDocument document){ 94 update(); 95 } 96 97 } 98 | Popular Tags |