Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 103093
Collapse All | Expand All

(-)src/org/eclipse/mylar/internal/tasks/ui/editors/RepositoryTextViewer.java (-4 / +49 lines)
Lines 15-20 Link Here
15
import java.util.Arrays;
15
import java.util.Arrays;
16
import java.util.List;
16
import java.util.List;
17
17
18
import org.eclipse.core.runtime.NullProgressMonitor;
18
import org.eclipse.jface.text.IDocument;
19
import org.eclipse.jface.text.IDocument;
19
import org.eclipse.jface.text.TextAttribute;
20
import org.eclipse.jface.text.TextAttribute;
20
import org.eclipse.jface.text.hyperlink.DefaultHyperlinkPresenter;
21
import org.eclipse.jface.text.hyperlink.DefaultHyperlinkPresenter;
Lines 22-27 Link Here
22
import org.eclipse.jface.text.hyperlink.IHyperlinkPresenter;
23
import org.eclipse.jface.text.hyperlink.IHyperlinkPresenter;
23
import org.eclipse.jface.text.presentation.IPresentationReconciler;
24
import org.eclipse.jface.text.presentation.IPresentationReconciler;
24
import org.eclipse.jface.text.presentation.PresentationReconciler;
25
import org.eclipse.jface.text.presentation.PresentationReconciler;
26
import org.eclipse.jface.text.reconciler.IReconciler;
27
import org.eclipse.jface.text.reconciler.MonoReconciler;
25
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
28
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
26
import org.eclipse.jface.text.rules.IRule;
29
import org.eclipse.jface.text.rules.IRule;
27
import org.eclipse.jface.text.rules.IToken;
30
import org.eclipse.jface.text.rules.IToken;
Lines 29-36 Link Here
29
import org.eclipse.jface.text.rules.RuleBasedScanner;
32
import org.eclipse.jface.text.rules.RuleBasedScanner;
30
import org.eclipse.jface.text.rules.SingleLineRule;
33
import org.eclipse.jface.text.rules.SingleLineRule;
31
import org.eclipse.jface.text.rules.Token;
34
import org.eclipse.jface.text.rules.Token;
35
import org.eclipse.jface.text.source.AnnotationModel;
36
import org.eclipse.jface.text.source.IAnnotationModel;
32
import org.eclipse.jface.text.source.ISourceViewer;
37
import org.eclipse.jface.text.source.ISourceViewer;
33
import org.eclipse.jface.text.source.SourceViewer;
38
import org.eclipse.jface.text.source.SourceViewer;
39
import org.eclipse.jface.text.source.VerticalRuler;
34
import org.eclipse.mylar.tasks.core.TaskRepository;
40
import org.eclipse.mylar.tasks.core.TaskRepository;
35
import org.eclipse.mylar.tasks.ui.TasksUiPlugin;
41
import org.eclipse.mylar.tasks.ui.TasksUiPlugin;
36
import org.eclipse.swt.SWT;
42
import org.eclipse.swt.SWT;
Lines 39-58 Link Here
39
import org.eclipse.swt.widgets.Composite;
45
import org.eclipse.swt.widgets.Composite;
40
import org.eclipse.swt.widgets.Display;
46
import org.eclipse.swt.widgets.Display;
41
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
47
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
48
import org.eclipse.ui.forms.editor.FormEditor;
42
49
43
/**
50
/**
44
 * @author Rob Elves
51
 * @author Rob Elves
45
 */
52
 */
46
public class RepositoryTextViewer extends SourceViewer {
53
public class RepositoryTextViewer extends SourceViewer {
47
54
55
	private static final int VERTICAL_RULER_WIDTH = 1; // ?
48
	private TaskRepository repository;
56
	private TaskRepository repository;
57
	private AnnotationModel annotationModel;
58
	private RepositoryViewerConfig repositoryViewerConfig;
49
59
50
	public RepositoryTextViewer(TaskRepository repository, Composite composite, int style) {
60
	public RepositoryTextViewer(TaskRepository repository, FormEditor editor, Composite composite, int style) {
51
		super(composite, null, style);
61
		super(composite, new VerticalRuler(VERTICAL_RULER_WIDTH), style);
52
		this.configure(new RepositoryViewerConfig());
62
63
		annotationModel = new AnnotationModel();
64
		getVerticalRuler().setModel(annotationModel);
65
		this.showAnnotations(true);
66
67
		repositoryViewerConfig = new RepositoryViewerConfig(editor);
68
		this.configure(repositoryViewerConfig);
53
		this.repository = repository;
69
		this.repository = repository;
54
	}
70
	}
55
71
72
	public void setDocument(IDocument doc) {
73
		annotationModel.connect(doc);
74
		super.setDocument(doc, annotationModel);
75
		repositoryViewerConfig.setAnnotationModel(getAnnotationModel());
76
	}
77
	
56
	public TaskRepository getRepository() {
78
	public TaskRepository getRepository() {
57
		return repository;
79
		return repository;
58
	}
80
	}
Lines 60-69 Link Here
60
	public void setRepository(TaskRepository repository) {
82
	public void setRepository(TaskRepository repository) {
61
		this.repository = repository;
83
		this.repository = repository;
62
	}
84
	}
63
85
	
64
	class RepositoryViewerConfig extends TextSourceViewerConfiguration {
86
	class RepositoryViewerConfig extends TextSourceViewerConfiguration {
65
87
66
		private RepositoryTextScanner scanner = null;
88
		private RepositoryTextScanner scanner = null;
89
		private FormEditor editor;
90
		private TaskSpellingReconcileStrategy strategy;
91
92
		public RepositoryViewerConfig(FormEditor editor) {
93
			this.editor = editor;
94
		}
95
96
		public void setAnnotationModel(IAnnotationModel model) {
97
			strategy.setAnnotationModel(model);
98
		}
67
99
68
		public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
100
		public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
69
			PresentationReconciler reconciler = new PresentationReconciler();
101
			PresentationReconciler reconciler = new PresentationReconciler();
Lines 96-101 Link Here
96
		public int getHyperlinkStateMask(ISourceViewer sourceViewer) {
128
		public int getHyperlinkStateMask(ISourceViewer sourceViewer) {
97
			return SWT.NONE;
129
			return SWT.NONE;
98
		}
130
		}
131
		
132
		public IReconciler getReconciler(ISourceViewer sourceViewer) {
133
			if (editor != null) {
134
				strategy= new TaskSpellingReconcileStrategy(editor);
135
				MonoReconciler reconciler= new MonoReconciler(strategy, false);
136
				reconciler.setIsIncrementalReconciler(false);
137
				reconciler.setProgressMonitor(new NullProgressMonitor());
138
				reconciler.setDelay(500);
139
				return reconciler;
140
			}
141
			return null;
142
		}
143
99
	}
144
	}
100
145
101
	static class RepositoryTextScanner extends RuleBasedScanner {
146
	static class RepositoryTextScanner extends RuleBasedScanner {
(-)src/org/eclipse/mylar/internal/tasks/ui/editors/TaskSpellingReconcileStrategy.java (+210 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 - 2006 Mylar committers 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
9
package org.eclipse.mylar.internal.tasks.ui.editors;
10
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.Iterator;
14
import java.util.List;
15
import java.util.Map;
16
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.Platform;
19
import org.eclipse.core.runtime.content.IContentTypeManager;
20
import org.eclipse.jdt.core.compiler.IProblem;
21
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider.ProblemAnnotation;
22
import org.eclipse.jdt.internal.ui.text.spelling.CoreSpellingProblem;
23
import org.eclipse.jdt.internal.ui.text.spelling.JavaSpellingProblem;
24
import org.eclipse.jface.text.BadLocationException;
25
import org.eclipse.jface.text.IDocument;
26
import org.eclipse.jface.text.IRegion;
27
import org.eclipse.jface.text.Position;
28
import org.eclipse.jface.text.Region;
29
import org.eclipse.jface.text.reconciler.DirtyRegion;
30
import org.eclipse.jface.text.reconciler.IReconcilerExtension;
31
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
32
import org.eclipse.jface.text.source.Annotation;
33
import org.eclipse.jface.text.source.IAnnotationModel;
34
import org.eclipse.ui.IEditorInput;
35
import org.eclipse.ui.editors.text.EditorsUI;
36
import org.eclipse.ui.forms.editor.FormEditor;
37
import org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector;
38
import org.eclipse.ui.texteditor.spelling.SpellingContext;
39
import org.eclipse.ui.texteditor.spelling.SpellingProblem;
40
41
/**
42
 * Adapted from
43
 * org.eclipse.jdt.internal.ui.text.spelling.PropertiesSpellingReconcileStrategy
44
 * 
45
 * @author Jeff Pound
46
 */
47
public class TaskSpellingReconcileStrategy implements IReconcilerExtension, IReconcilingStrategy {
48
49
	/**
50
	 * Spelling problem collector that forwards {@link SpellingProblem}s as
51
	 * {@link IProblem}s to the {@link org.eclipse.jdt.core.IProblemRequestor}.
52
	 */
53
	private class SpellingProblemCollector implements ISpellingProblemCollector {
54
55
		/** Annotation model */
56
		private IAnnotationModel fAnnotationModel;
57
58
		/** Annotations to add */
59
		private Map<ProblemAnnotation, Position> fAddAnnotations;
60
61
		/**
62
		 * Initializes this collector with the given annotation model.
63
		 * 
64
		 * @param annotationModel
65
		 *            the annotation model
66
		 */
67
		public SpellingProblemCollector(IAnnotationModel annotationModel) {
68
			fAnnotationModel = annotationModel;
69
		}
70
71
		/*
72
		 * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#accept(org.eclipse.ui.texteditor.spelling.SpellingProblem)
73
		 */
74
		public void accept(SpellingProblem problem) {
75
			try {
76
				int line = fDocument.getLineOfOffset(problem.getOffset()) + 1;
77
				String word = fDocument.get(problem.getOffset(), problem.getLength());
78
				boolean dictionaryMatch = false;
79
				boolean sentenceStart = false;
80
				if (problem instanceof JavaSpellingProblem) {
81
					dictionaryMatch = ((JavaSpellingProblem) problem).isDictionaryMatch();
82
					sentenceStart = ((JavaSpellingProblem) problem).isSentenceStart();
83
				}
84
				// see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=81514
85
				IEditorInput editorInput = editor.getEditorInput();
86
				if (editorInput != null) {
87
					CoreSpellingProblem iProblem = new CoreSpellingProblem(problem.getOffset(), problem.getOffset()
88
							+ problem.getLength() - 1, line, problem.getMessage(), word, dictionaryMatch,
89
							sentenceStart, fDocument, editorInput.getName());
90
					System.err.println(word);
91
					fAddAnnotations.put(new ProblemAnnotation(iProblem, null), new Position(problem.getOffset(),
92
							problem.getLength()));
93
				}
94
			} catch (BadLocationException x) {
95
				// drop this SpellingProblem
96
			}
97
		}
98
99
		/*
100
		 * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#beginCollecting()
101
		 */
102
		public void beginCollecting() {
103
			fAddAnnotations = new HashMap<ProblemAnnotation, Position>();
104
		}
105
106
		/*
107
		 * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#endCollecting()
108
		 */
109
		public void endCollecting() {
110
111
			List<Annotation> removeAnnotations = new ArrayList<Annotation>();
112
			for (Iterator iter = fAnnotationModel.getAnnotationIterator(); iter.hasNext();) {
113
				Annotation annotation = (Annotation) iter.next();
114
				if (ProblemAnnotation.SPELLING_ANNOTATION_TYPE.equals(annotation.getType()))
115
					removeAnnotations.add(annotation);
116
			}
117
118
//			if (fAnnotationModel instanceof IAnnotationModelExtension)
119
//				((IAnnotationModelExtension) fAnnotationModel).replaceAnnotations((Annotation[]) removeAnnotations
120
//						.toArray(new Annotation[removeAnnotations.size()]), fAddAnnotations);
121
//			else {
122
				for (Iterator iter = removeAnnotations.iterator(); iter.hasNext();)
123
					fAnnotationModel.removeAnnotation((Annotation) iter.next());
124
				for (Iterator iter = fAddAnnotations.keySet().iterator(); iter.hasNext();) {
125
					Annotation annotation = (Annotation) iter.next();
126
					fAnnotationModel.addAnnotation(annotation, (Position) fAddAnnotations.get(annotation));
127
				}
128
//			}
129
130
			fAddAnnotations = null;
131
		}
132
	}
133
134
	/** The id of the problem */
135
	public static final int SPELLING_PROBLEM_ID = 0x80000000;
136
137
	/** The document to operate on. */
138
	private IDocument fDocument;
139
140
	/** The progress monitor. */
141
	private IProgressMonitor fProgressMonitor;
142
143
	/**
144
	 * The spelling context containing the Java properties content type.
145
	 * <p>
146
	 * Since his reconcile strategy is for the Properties File editor which
147
	 * normally edits Java properties files we always use the Java properties
148
	 * file content type for performance reasons.
149
	 * </p>
150
	 * 
151
	 * @since 3.2
152
	 */
153
	private SpellingContext fSpellingContext;
154
155
	private FormEditor editor;
156
157
	private IAnnotationModel annotationModel;
158
159
	public TaskSpellingReconcileStrategy(FormEditor editor) {
160
		this.editor = editor;
161
		this.annotationModel = null;
162
		fSpellingContext = new SpellingContext();
163
		fSpellingContext.setContentType(Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT));
164
	}
165
166
	/*
167
	 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#initialReconcile()
168
	 */
169
	public void initialReconcile() {
170
		reconcile(new Region(0, fDocument.getLength()));
171
	}
172
173
	/*
174
	 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion,org.eclipse.jface.text.IRegion)
175
	 */
176
	public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
177
		reconcile(subRegion);
178
	}
179
180
	/*
181
	 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
182
	 */
183
	public void reconcile(IRegion region) {
184
		TaskSpellingReconcileStrategy.SpellingProblemCollector collector = new SpellingProblemCollector(annotationModel);
185
		EditorsUI.getSpellingService().check(fDocument, fSpellingContext, collector, fProgressMonitor);
186
	}
187
188
	/*
189
	 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
190
	 */
191
	public void setDocument(IDocument document) {
192
		fDocument = document;
193
	}
194
195
	/*
196
	 * @see org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)
197
	 */
198
	public void setProgressMonitor(IProgressMonitor monitor) {
199
		fProgressMonitor = monitor;
200
	}
201
202
	public String getDocumentPartitioning() {
203
		// ignore
204
		return null;
205
	}
206
207
	public void setAnnotationModel(IAnnotationModel model) {
208
		annotationModel = model;
209
	}
210
}

Return to bug 103093