fixed NPE
[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 the
28  * regular java model reconciler and the comment spell checking strategy. 
29  * 
30  * @since 3.0 
31  */
32 public class JavaCompositeReconcilingStrategy  extends CompositeReconcilingStrategy {
33         
34         private ITextEditor fEditor;
35         private JavaReconcilingStrategy fJavaStrategy;
36
37         /**
38          * Creates a new Java reconciling strategy.
39          * 
40          * @param editor the editor of the strategy's reconciler
41          * @param documentPartitioning the document partitioning this strategy uses for configuration
42          */
43         public JavaCompositeReconcilingStrategy(ITextEditor editor, String documentPartitioning) {
44                 fEditor= editor;
45                 fJavaStrategy= new JavaReconcilingStrategy(editor);
46                 setReconcilingStrategies(new IReconcilingStrategy[] {
47                         fJavaStrategy,
48                         new SpellReconcileStrategy(editor, documentPartitioning, PreferenceConstants.getPreferenceStore())
49                 });
50         }
51         
52         /**
53          * Returns the problem requestor for the editor's input element.
54          * 
55          * @return the problem requestor for the editor's input element
56          */
57         private IProblemRequestorExtension getProblemRequestorExtension() {
58                 IDocumentProvider p= fEditor.getDocumentProvider();
59                 if (p == null) {
60                         // work around for https://bugs.eclipse.org/bugs/show_bug.cgi?id=51522
61                         p= PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider();
62                 }
63                 IAnnotationModel m= p.getAnnotationModel(fEditor.getEditorInput());
64                 if (m instanceof IProblemRequestorExtension)
65                         return (IProblemRequestorExtension) m;
66                 return null;
67         }
68         
69         /*
70          * @see org.eclipse.jface.text.reconciler.CompositeReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion, org.eclipse.jface.text.IRegion)
71          */
72         public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
73                 IProblemRequestorExtension e= getProblemRequestorExtension();
74                 if (e != null) {
75                         try {
76                                 e.beginReportingSequence();
77                                 super.reconcile(dirtyRegion, subRegion);
78                         } finally {
79                                 e.endReportingSequence();
80                         }
81                 } else {
82                         super.reconcile(dirtyRegion, subRegion);
83                 }
84         }
85         
86         /*
87          * @see org.eclipse.jface.text.reconciler.CompositeReconcilingStrategy#reconcile(org.eclipse.jface.text.IRegion)
88          */
89         public void reconcile(IRegion partition) {
90                 IProblemRequestorExtension e= getProblemRequestorExtension();
91                 if (e != null) {
92                         try {
93                                 e.beginReportingSequence();
94                                 super.reconcile(partition);
95                         } finally {
96                                 e.endReportingSequence();
97                         }
98                 } else {
99                         super.reconcile(partition);
100                 }
101         }
102         
103         /**
104          * Tells this strategy whether to inform its listeners.
105          * 
106          * @param notify <code>true</code> if listeners should be notified
107          */
108         public void notifyListeners(boolean notify) {
109                 fJavaStrategy.notifyListeners(notify);
110         }
111         
112         /*
113          * @see org.eclipse.jface.text.reconciler.CompositeReconcilingStrategy#initialReconcile()
114          */
115         public void initialReconcile() {
116                 IProblemRequestorExtension e= getProblemRequestorExtension();
117                 if (e != null) {
118                         try {
119                                 e.beginReportingSequence();
120                                 super.initialReconcile();
121                         } finally {
122                                 e.endReportingSequence();
123                         }
124                 } else {
125                         super.initialReconcile();
126                 }
127         }
128
129         /**
130          * Called before reconciling is started.
131          * 
132          * @since 3.0
133          */
134         public void aboutToBeReconciled() {
135                 fJavaStrategy.aboutToBeReconciled();
136                 
137         }
138 }