001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Map;
018: import java.util.Set;
019:
020: import org.eclipse.jface.text.BadLocationException;
021: import org.eclipse.jface.text.IDocument;
022: import org.eclipse.jface.text.Position;
023: import org.eclipse.jface.text.source.Annotation;
024: import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
025: import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
026: import org.eclipse.pde.core.IModelChangedEvent;
027: import org.eclipse.pde.core.IModelChangedListener;
028: import org.eclipse.pde.internal.core.text.IEditingModel;
029:
030: public abstract class AbstractFoldingStructureProvider implements
031: IFoldingStructureProvider, IModelChangedListener {
032:
033: private PDESourcePage fEditor;
034: private IEditingModel fModel;
035:
036: public AbstractFoldingStructureProvider(PDESourcePage editor,
037: IEditingModel model) {
038: this .fEditor = editor;
039: this .fModel = model;
040: }
041:
042: public void update() {
043: ProjectionAnnotationModel annotationModel = (ProjectionAnnotationModel) fEditor
044: .getAdapter(ProjectionAnnotationModel.class);
045: if (annotationModel == null)
046: return;
047:
048: Set currentRegions = new HashSet();
049: try {
050: addFoldingRegions(currentRegions, fModel);
051: updateFoldingRegions(annotationModel, currentRegions);
052: } catch (BadLocationException e) {
053: }
054: }
055:
056: public void updateFoldingRegions(ProjectionAnnotationModel model,
057: Set currentRegions) {
058: Annotation[] deletions = computeDifferences(model,
059: currentRegions);
060:
061: Map additionsMap = new HashMap();
062: for (Iterator iter = currentRegions.iterator(); iter.hasNext();) {
063: Object position = iter.next();
064: additionsMap.put(new ProjectionAnnotation(false), position);
065: }
066:
067: if ((deletions.length != 0 || additionsMap.size() != 0)) {
068: model.modifyAnnotations(deletions, additionsMap,
069: new Annotation[] {});
070: }
071: }
072:
073: private Annotation[] computeDifferences(
074: ProjectionAnnotationModel model, Set additions) {
075: List deletions = new ArrayList();
076: for (Iterator iter = model.getAnnotationIterator(); iter
077: .hasNext();) {
078: Object annotation = iter.next();
079: if (annotation instanceof ProjectionAnnotation) {
080: Position position = model
081: .getPosition((Annotation) annotation);
082: if (additions.contains(position)) {
083: additions.remove(position);
084: } else {
085: deletions.add(annotation);
086: }
087: }
088: }
089: return (Annotation[]) deletions
090: .toArray(new Annotation[deletions.size()]);
091: }
092:
093: public void initialize() {
094: update();
095: }
096:
097: public void modelChanged(IModelChangedEvent event) {
098: update();
099: }
100:
101: public void reconciled(IDocument document) {
102: update();
103: }
104:
105: }
|