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