Fix import
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / JavaCompositeReconcilingStrategy.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation 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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13 import net.sourceforge.phpdt.internal.ui.text.java.IProblemRequestorExtension;
14 import net.sourceforge.phpdt.internal.ui.text.java.JavaReconcilingStrategy;
15 import net.sourceforge.phpdt.internal.ui.text.spelling.SpellReconcileStrategy;
16 import net.sourceforge.phpdt.ui.PreferenceConstants;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.reconciler.DirtyRegion;
21 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
22 import org.eclipse.jface.text.source.IAnnotationModel;
23 import org.eclipse.ui.texteditor.IDocumentProvider;
24 import org.eclipse.ui.texteditor.ITextEditor;
25
26 /**
27  * Reconciling strategy for Java code. This is a composite strategy containing
28  * the regular java model reconciler and the comment spell checking strategy.
29  * 
30  * @since 3.0
31  */
32 public class JavaCompositeReconcilingStrategy extends
33                 CompositeReconcilingStrategy {
34
35         private ITextEditor fEditor;
36
37         private JavaReconcilingStrategy fJavaStrategy;
38
39         /**
40          * Creates a new Java reconciling strategy.
41          * 
42          * @param editor
43          *            the editor of the strategy's reconciler
44          * @param documentPartitioning
45          *            the document partitioning this strategy uses for configuration
46          */
47         public JavaCompositeReconcilingStrategy(ITextEditor editor,
48                         String documentPartitioning) {
49                 fEditor = editor;
50                 fJavaStrategy = new JavaReconcilingStrategy(editor);
51                 setReconcilingStrategies(new IReconcilingStrategy[] {
52                                 fJavaStrategy,
53                                 new SpellReconcileStrategy(editor, documentPartitioning,
54                                                 PreferenceConstants.getPreferenceStore()) });
55         }
56
57         /**
58          * Returns the problem requestor for the editor's input element.
59          * 
60          * @return the problem requestor for the editor's input element
61          */
62         private IProblemRequestorExtension getProblemRequestorExtension() {
63                 IDocumentProvider p = fEditor.getDocumentProvider();
64                 if (p == null) {
65                         try {
66                                 // work around for
67                                 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=51522
68                                 p = PHPeclipsePlugin.getDefault()
69                                                 .getCompilationUnitDocumentProvider();
70                         } catch (NullPointerException npe) {
71                                 return null;
72                         }
73                 }
74                 IAnnotationModel m = p.getAnnotationModel(fEditor.getEditorInput());
75                 if (m instanceof IProblemRequestorExtension)
76                         return (IProblemRequestorExtension) m;
77                 return null;
78         }
79
80         /*
81          * @see org.eclipse.jface.text.reconciler.CompositeReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion,
82          *      org.eclipse.jface.text.IRegion)
83          */
84         public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
85                 IProblemRequestorExtension e = getProblemRequestorExtension();
86                 if (e != null) {
87                         try {
88                                 e.beginReportingSequence();
89                                 super.reconcile(dirtyRegion, subRegion);
90                         } finally {
91                                 e.endReportingSequence();
92                         }
93                 } else {
94                         super.reconcile(dirtyRegion, subRegion);
95                 }
96         }
97
98         /*
99          * @see org.eclipse.jface.text.reconciler.CompositeReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
100          */
101         public void reconcile(IRegion partition) {
102                 IProblemRequestorExtension e = getProblemRequestorExtension();
103                 if (e != null) {
104                         try {
105                                 e.beginReportingSequence();
106                                 super.reconcile(partition);
107                         } finally {
108                                 e.endReportingSequence();
109                         }
110                 } else {
111                         super.reconcile(partition);
112                 }
113         }
114
115         /**
116          * Tells this strategy whether to inform its listeners.
117          * 
118          * @param notify
119          *            <code>true</code> if listeners should be notified
120          */
121         public void notifyListeners(boolean notify) {
122                 fJavaStrategy.notifyListeners(notify);
123         }
124
125         /*
126          * @see org.eclipse.jface.text.reconciler.CompositeReconcilingStrategy#initialReconcile()
127          */
128         public void initialReconcile() {
129                 IProblemRequestorExtension e = getProblemRequestorExtension();
130                 if (e != null) {
131                         try {
132                                 e.beginReportingSequence();
133                                 super.initialReconcile();
134                         } finally {
135                                 e.endReportingSequence();
136                         }
137                 } else {
138                         super.initialReconcile();
139                 }
140         }
141
142         /**
143          * Called before reconciling is started.
144          * 
145          * @since 3.0
146          */
147         public void aboutToBeReconciled() {
148                 fJavaStrategy.aboutToBeReconciled();
149
150         }
151 }