2 * Copyright (c) 2003-2004 Christopher Lenz and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Christopher Lenz - initial API and implementation
11 * $Id: CssReconcilingStrategy.java,v 1.1 2004-09-02 18:11:48 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.text;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.Iterator;
19 import net.sourceforge.phpeclipse.css.ui.CssUI;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.Position;
26 import org.eclipse.jface.text.reconciler.DirtyRegion;
27 import org.eclipse.jface.text.reconciler.IReconcileResult;
28 import org.eclipse.jface.text.reconciler.IReconcileStep;
29 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
30 import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
31 import org.eclipse.jface.text.source.Annotation;
32 import org.eclipse.jface.text.source.IAnnotationModel;
33 import org.eclipse.ui.IEditorInput;
34 import org.eclipse.ui.actions.WorkspaceModifyOperation;
35 import org.eclipse.ui.texteditor.ITextEditor;
39 * Reconciling strategy for CSS style sheets. This class is responsible for
40 * keeping the parsed model in sync with the text.
42 public class CssReconcilingStrategy
43 implements IReconcilingStrategy, IReconcilingStrategyExtension {
45 // Instance Variables ------------------------------------------------------
48 * The associated text editor.
50 private ITextEditor editor;
53 * A progress monitor that should be used for long-running operations.
55 IProgressMonitor progressMonitor;
58 * The first (and only) reconcile step is the parsing of the style sheet.
60 private IReconcileStep firstStep;
62 // Constructors ------------------------------------------------------------
64 public CssReconcilingStrategy(ITextEditor editor) {
66 firstStep = new CssReconcileStep(editor);
69 // IReconcilingStrategy Implementation -------------------------------------
72 * @see IReconcilingStrategy#reconcile(DirtyRegion, IRegion)
74 public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
75 removeTemporaryAnnotations();
76 process(firstStep.reconcile(dirtyRegion, subRegion));
80 * @see IReconcilingStrategy#reconcile(IRegion)
82 public void reconcile(IRegion partition) {
83 removeTemporaryAnnotations();
84 process(firstStep.reconcile(partition));
88 * @see IReconcilingStrategy#setDocument(IDocument)
90 public void setDocument(IDocument document) {
92 firstStep.setInputModel(null); //new DocumentAdapter(document);
95 // IReconcilingStrategyExtension Implementation ----------------------------
98 * @see IReconcilingStrategyExtension#initialReconcile()
100 public void initialReconcile() {
101 process(firstStep.reconcile(null));
105 * @see IReconcilingStrategyExtension#setProgressMonitor(IProgressMonitor)
107 public void setProgressMonitor(IProgressMonitor monitor) {
108 firstStep.setProgressMonitor(monitor);
109 progressMonitor = monitor;
112 // Private Methods ---------------------------------------------------------
115 * Returns the annotation model for the editor input.
117 * @return the annotation model
119 IAnnotationModel getAnnotationModel() {
120 IEditorInput input = editor.getEditorInput();
121 return editor.getDocumentProvider().getAnnotationModel(input);
125 * Adds results of the reconcilation to the annotation model.
127 private void process(final IReconcileResult[] results) {
128 if (results == null) {
132 IRunnableWithProgress runnable = new WorkspaceModifyOperation() {
133 protected void execute(IProgressMonitor monitor) {
134 for (int i = 0; i < results.length; i++) {
135 if ((progressMonitor != null)
136 && (progressMonitor.isCanceled())) {
140 if (!(results[i] instanceof AnnotationAdapter)) {
144 AnnotationAdapter result = (AnnotationAdapter) results[i];
145 Position pos = result.getPosition();
146 Annotation annotation = result.createAnnotation();
147 getAnnotationModel().addAnnotation(annotation, pos);
154 } catch (InvocationTargetException e) {
156 } catch (InterruptedException e) {
160 if (editor instanceof IReconcilingParticipant) {
161 ((IReconcilingParticipant) editor).reconciled();
166 * TODO A "real" implementation must be smarter, i.e. don't remove and add
167 * the annotations which are the same.
169 private void removeTemporaryAnnotations() {
170 Iterator i = getAnnotationModel().getAnnotationIterator();
171 while (i.hasNext()) {
172 Annotation annotation = (Annotation) i.next();
173 if (!annotation.isPersistent()) {
174 getAnnotationModel().removeAnnotation(annotation);