fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / JavaReconcilingStrategy.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  * 
6  * Contributors: IBM Corporation - initial API and implementation
7  **********************************************************************************************************************************/
8
9 package net.sourceforge.phpdt.internal.ui.text.java;
10
11 import net.sourceforge.phpdt.core.ICompilationUnit;
12 import net.sourceforge.phpdt.core.JavaModelException;
13 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.jface.text.IDocument;
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.reconciler.IReconcilingStrategyExtension;
23 import org.eclipse.jface.text.source.IAnnotationModel;
24 import org.eclipse.ui.texteditor.IDocumentProvider;
25 import org.eclipse.ui.texteditor.ITextEditor;
26
27 public class JavaReconcilingStrategy implements IReconcilingStrategy,
28                 IReconcilingStrategyExtension {
29
30         private ITextEditor fEditor;
31
32         private IWorkingCopyManager fManager;
33
34         private IDocumentProvider fDocumentProvider;
35
36         private IProgressMonitor fProgressMonitor;
37
38         private boolean fNotify = true;
39
40         private IJavaReconcilingListener fJavaReconcilingListener;
41
42         private boolean fIsJavaReconcilingListener;
43
44         public JavaReconcilingStrategy(ITextEditor editor) {
45                 fEditor = editor;
46                 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
47                 fDocumentProvider = PHPeclipsePlugin.getDefault()
48                                 .getCompilationUnitDocumentProvider();
49                 fIsJavaReconcilingListener = fEditor instanceof IJavaReconcilingListener;
50                 if (fIsJavaReconcilingListener)
51                         fJavaReconcilingListener = (IJavaReconcilingListener) fEditor;
52         }
53
54         private IProblemRequestorExtension getProblemRequestorExtension() {
55                 IAnnotationModel model = fDocumentProvider.getAnnotationModel(fEditor
56                                 .getEditorInput());
57                 if (model instanceof IProblemRequestorExtension)
58                         return (IProblemRequestorExtension) model;
59                 return null;
60         }
61
62         private void reconcile() {
63                 // // try {
64                 //
65                 // /* fix for missing cancel flag communication */
66                 // IProblemRequestorExtension extension =
67                 // getProblemRequestorExtension();
68                 // if (extension != null)
69                 // extension.setProgressMonitor(fProgressMonitor);
70                 //
71                 // // reconcile
72                 // // synchronized (unit) {
73                 // // unit.reconcile(true, fProgressMonitor);
74                 // // }
75                 //
76                 // Parser parser = new Parser();
77                 // parser.initializeScanner();
78                 // // actualParser.setFileToParse(fileToParse);
79                 // String text =
80                 // fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()).get();
81                 // parser.init(text);
82                 // parser.reportSyntaxError();
83                 // // checkAndReportBracketAnomalies(parser.problemReporter());
84                 //
85                 // /* fix for missing cancel flag communication */
86                 // if (extension != null)
87                 // extension.setProgressMonitor(null);
88                 //
89                 // // update participants
90                 // try {
91                 // if (fEditor instanceof IReconcilingParticipant && fNotify &&
92                 // !fProgressMonitor.isCanceled()) {
93                 // IReconcilingParticipant p = (IReconcilingParticipant) fEditor;
94                 // p.reconciled();
95                 // }
96                 // } finally {
97                 // fNotify = true;
98                 // }
99
100                 // JDT implementation:
101                 try {
102                         ICompilationUnit unit = fManager.getWorkingCopy(fEditor
103                                         .getEditorInput());
104                         if (unit != null) {
105                                 try {
106
107                                         /* fix for missing cancel flag communication */
108                                         IProblemRequestorExtension extension = getProblemRequestorExtension();
109                                         if (extension != null)
110                                                 extension.setProgressMonitor(fProgressMonitor);
111
112                                         // reconcile
113                                         synchronized (unit) {
114                                                 unit.reconcile(true, fProgressMonitor);
115                                         }
116
117                                         /* fix for missing cancel flag communication */
118                                         if (extension != null)
119                                                 extension.setProgressMonitor(null);
120
121                                         // update participants
122                                         try {
123                                                 if (fEditor instanceof IReconcilingParticipant
124                                                                 && fNotify && !fProgressMonitor.isCanceled()) {
125                                                         IReconcilingParticipant p = (IReconcilingParticipant) fEditor;
126                                                         p.reconciled();
127                                                 }
128                                         } finally {
129                                                 fNotify = true;
130                                         }
131
132                                 } catch (JavaModelException x) {
133                                         // swallow exception
134                                 }
135                         }
136                 } finally {
137                         // Always notify listeners, see
138                         // https://bugs.eclipse.org/bugs/show_bug.cgi?id=55969 for the final
139                         // solution
140                         try {
141                                 if (fIsJavaReconcilingListener) {
142                                         IProgressMonitor pm = fProgressMonitor;
143                                         if (pm == null)
144                                                 pm = new NullProgressMonitor();
145                                         fJavaReconcilingListener.reconciled(null, !fNotify, pm);
146                                 }
147                         } finally {
148                                 fNotify = true;
149                         }
150
151                 }
152         }
153
154         /*
155          * @see IReconcilingStrategy#reconcile(IRegion)
156          */
157         public void reconcile(IRegion partition) {
158                 reconcile();
159         }
160
161         /*
162          * @see IReconcilingStrategy#reconcile(DirtyRegion, IRegion)
163          */
164         public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
165                 reconcile();
166         }
167
168         /*
169          * @see IReconcilingStrategy#setDocument(IDocument)
170          */
171         public void setDocument(IDocument document) {
172         }
173
174         /*
175          * @see IReconcilingStrategyExtension#setProgressMonitor(IProgressMonitor)
176          */
177         public void setProgressMonitor(IProgressMonitor monitor) {
178                 fProgressMonitor = monitor;
179         }
180
181         /*
182          * @see IReconcilingStrategyExtension#initialReconcile()
183          */
184         public void initialReconcile() {
185                 reconcile();
186         }
187
188         /**
189          * Tells this strategy whether to inform its participants.
190          * 
191          * @param notify
192          *            <code>true</code> if participant should be notified
193          */
194         public void notifyParticipants(boolean notify) {
195                 fNotify = notify;
196         }
197
198         /**
199          * Tells this strategy whether to inform its listeners.
200          * 
201          * @param notify
202          *            <code>true</code> if listeners should be notified
203          */
204         public void notifyListeners(boolean notify) {
205                 fNotify = notify;
206         }
207
208         /**
209          * Called before reconciling is started.
210          * 
211          * @since 3.0
212          */
213         public void aboutToBeReconciled() {
214                 if (fIsJavaReconcilingListener)
215                         fJavaReconcilingListener.aboutToBeReconciled();
216         }
217 }