001: /*******************************************************************************
002: * Copyright (c) 2000, 2004 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.jdt.internal.ui.examples.jspeditor;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.Iterator;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IProgressMonitor;
018:
019: import org.eclipse.jface.operation.IRunnableWithProgress;
020:
021: import org.eclipse.jface.text.IDocument;
022: import org.eclipse.jface.text.IRegion;
023: import org.eclipse.jface.text.Position;
024: import org.eclipse.jface.text.reconciler.DirtyRegion;
025: import org.eclipse.jface.text.reconciler.IReconcileStep;
026: import org.eclipse.jface.text.reconciler.IReconcileResult;
027: import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
028: import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
029: import org.eclipse.jface.text.source.Annotation;
030: import org.eclipse.jface.text.source.IAnnotationModel;
031: import org.eclipse.jface.text.source.ISourceViewer;
032:
033: import org.eclipse.ui.IEditorInput;
034: import org.eclipse.ui.IFileEditorInput;
035: import org.eclipse.ui.actions.WorkspaceModifyOperation;
036: import org.eclipse.ui.texteditor.ITextEditor;
037:
038: /**
039: * Reconciling strategy for Java parts in JSP files.
040: *
041: * @since 3.0
042: */
043: public class JspReconcilingStrategy implements IReconcilingStrategy,
044: IReconcilingStrategyExtension {
045:
046: private IReconcileStep fFirstStep;
047: private ITextEditor fTextEditor;
048: private IProgressMonitor fProgressMonitor;
049:
050: public JspReconcilingStrategy(ISourceViewer sourceViewer,
051: ITextEditor textEditor) {
052: fTextEditor = textEditor;
053: IReconcileStep javaReconcileStep = new JavaReconcileStep(
054: getFile());
055: fFirstStep = new Jsp2JavaReconcileStep(javaReconcileStep);
056: }
057:
058: /*
059: * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
060: */
061: public void setDocument(IDocument document) {
062: fFirstStep.setInputModel(new DocumentAdapter(document));
063: }
064:
065: /*
066: * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion, org.eclipse.jface.text.IRegion)
067: */
068: public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
069: removeTemporaryAnnotations();
070: process(fFirstStep.reconcile(dirtyRegion, subRegion));
071: }
072:
073: /*
074: * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
075: */
076: public void reconcile(IRegion partition) {
077: removeTemporaryAnnotations();
078: process(fFirstStep.reconcile(partition));
079: }
080:
081: /*
082: * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)
083: */
084: public void setProgressMonitor(IProgressMonitor monitor) {
085: fFirstStep.setProgressMonitor(monitor);
086: fProgressMonitor = monitor;
087:
088: }
089:
090: /*
091: * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#initialReconcile()
092: */
093: public void initialReconcile() {
094: fFirstStep.reconcile(null);
095:
096: }
097:
098: private void process(final IReconcileResult[] results) {
099:
100: if (results == null)
101: return;
102:
103: IRunnableWithProgress runnable = new WorkspaceModifyOperation(
104: null) {
105: /*
106: * @see org.eclipse.ui.actions.WorkspaceModifyOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
107: */
108: protected void execute(IProgressMonitor monitor)
109: throws CoreException, InvocationTargetException,
110: InterruptedException {
111: for (int i = 0; i < results.length; i++) {
112:
113: if (fProgressMonitor != null
114: && fProgressMonitor.isCanceled())
115: return;
116:
117: if (!(results[i] instanceof AnnotationAdapter))
118: continue;
119:
120: AnnotationAdapter result = (AnnotationAdapter) results[i];
121: Position pos = result.getPosition();
122:
123: Annotation annotation = result.createAnnotation();
124: getAnnotationModel().addAnnotation(annotation, pos);
125: }
126: }
127: };
128: try {
129: runnable.run(null);
130: } catch (InvocationTargetException e) {
131: e.printStackTrace();
132: } catch (InterruptedException e) {
133: e.printStackTrace();
134: }
135: }
136:
137: private IAnnotationModel getAnnotationModel() {
138: return fTextEditor.getDocumentProvider().getAnnotationModel(
139: fTextEditor.getEditorInput());
140: }
141:
142: /*
143: * XXX: A "real" implementation must be smarter
144: * i.e. don't remove and add the annotations
145: * which are the same.
146: */
147: private void removeTemporaryAnnotations() {
148: Iterator iter = getAnnotationModel().getAnnotationIterator();
149: while (iter.hasNext()) {
150: Object annotation = iter.next();
151: if (annotation instanceof Annotation) {
152: Annotation extension = (Annotation) annotation;
153: if (!extension.isPersistent())
154: getAnnotationModel().removeAnnotation(
155: (Annotation) annotation);
156: }
157: }
158: }
159:
160: private IFile getFile() {
161: IEditorInput input = fTextEditor.getEditorInput();
162: if (!(input instanceof IFileEditorInput))
163: return null;
164:
165: return ((IFileEditorInput) input).getFile();
166: }
167: }
|