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