KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > AbstractFoldingStructureProvider


1 /*******************************************************************************
2  * Copyright (c) 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.pde.internal.ui.editor;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
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 JavaDoc currentRegions = new HashSet JavaDoc();
49         try {
50             addFoldingRegions(currentRegions, fModel);
51             updateFoldingRegions(annotationModel, currentRegions);
52         } catch (BadLocationException e) {}
53     }
54
55     public void updateFoldingRegions(ProjectionAnnotationModel model, Set JavaDoc currentRegions) {
56         Annotation[] deletions = computeDifferences(model, currentRegions);
57
58         Map JavaDoc additionsMap = new HashMap JavaDoc();
59         for (Iterator JavaDoc iter = currentRegions.iterator(); iter.hasNext();) {
60             Object JavaDoc 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 JavaDoc additions) {
70         List JavaDoc deletions = new ArrayList JavaDoc();
71         for (Iterator JavaDoc iter = model.getAnnotationIterator(); iter.hasNext();) {
72             Object JavaDoc 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