new version with WorkingCopy Management
[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.
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
12 package net.sourceforge.phpdt.internal.ui.text.java;
13
14 import net.sourceforge.phpdt.core.ICompilationUnit;
15 import net.sourceforge.phpdt.core.JavaModelException;
16 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.reconciler.DirtyRegion;
23 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
24 import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
25 import org.eclipse.jface.text.source.IAnnotationModel;
26 import org.eclipse.ui.texteditor.IDocumentProvider;
27 import org.eclipse.ui.texteditor.ITextEditor;
28
29 public class JavaReconcilingStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension {
30
31   private ITextEditor fEditor;
32
33   private IWorkingCopyManager fManager;
34   private IDocumentProvider fDocumentProvider;
35   private IProgressMonitor fProgressMonitor;
36   private boolean fNotify = true;
37
38   public JavaReconcilingStrategy(ITextEditor editor) {
39     fEditor = editor;
40     fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
41     fDocumentProvider = PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider();
42   }
43
44   private IProblemRequestorExtension getProblemRequestorExtension() {
45     IAnnotationModel model = fDocumentProvider.getAnnotationModel(fEditor.getEditorInput());
46     if (model instanceof IProblemRequestorExtension)
47       return (IProblemRequestorExtension) model;
48     return null;
49   }
50
51   private void reconcile() {
52     //    //    try {
53     //
54     //    /* fix for missing cancel flag communication */
55     //    IProblemRequestorExtension extension = getProblemRequestorExtension();
56     //    if (extension != null)
57     //      extension.setProgressMonitor(fProgressMonitor);
58     //
59     //    // reconcile
60     ////          synchronized (unit) {
61     ////            unit.reconcile(true, fProgressMonitor);
62     ////          }
63     //
64     //    Parser parser = new Parser();
65     //    parser.initializeScanner();
66     //    // actualParser.setFileToParse(fileToParse);
67     //    String text = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()).get();
68     //    parser.init(text);
69     //    parser.reportSyntaxError();
70     ////    checkAndReportBracketAnomalies(parser.problemReporter());
71     //
72     //    /* fix for missing cancel flag communication */
73     //    if (extension != null)
74     //      extension.setProgressMonitor(null);
75     //
76     //    // update participants
77     //    try {
78     //      if (fEditor instanceof IReconcilingParticipant && fNotify && !fProgressMonitor.isCanceled()) {
79     //        IReconcilingParticipant p = (IReconcilingParticipant) fEditor;
80     //        p.reconciled();
81     //      }
82     //    } finally {
83     //      fNotify = true;
84     //    }
85
86     // JDT implementation:
87     ICompilationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
88     if (unit != null) {
89       try {
90
91         /* fix for missing cancel flag communication */
92         IProblemRequestorExtension extension = getProblemRequestorExtension();
93         if (extension != null)
94           extension.setProgressMonitor(fProgressMonitor);
95
96         // reconcile
97         synchronized (unit) {
98           unit.reconcile(true, fProgressMonitor);
99         }
100
101         /* fix for missing cancel flag communication */
102         if (extension != null)
103           extension.setProgressMonitor(null);
104
105         // update participants
106         try {
107           if (fEditor instanceof IReconcilingParticipant && fNotify && !fProgressMonitor.isCanceled()) {
108             IReconcilingParticipant p = (IReconcilingParticipant) fEditor;
109             p.reconciled();
110           }
111         } finally {
112           fNotify = true;
113         }
114
115       } catch (JavaModelException x) {
116         // swallow exception
117       }
118     }
119   }
120
121   /*
122    * @see IReconcilingStrategy#reconcile(IRegion)
123    */
124   public void reconcile(IRegion partition) {
125     reconcile();
126   }
127
128   /*
129    * @see IReconcilingStrategy#reconcile(DirtyRegion, IRegion)
130    */
131   public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
132     reconcile();
133   }
134
135   /*
136    * @see IReconcilingStrategy#setDocument(IDocument)
137    */
138   public void setDocument(IDocument document) {
139   }
140
141   /*
142    * @see IReconcilingStrategyExtension#setProgressMonitor(IProgressMonitor)
143    */
144   public void setProgressMonitor(IProgressMonitor monitor) {
145     fProgressMonitor = monitor;
146   }
147
148   /*
149    * @see IReconcilingStrategyExtension#initialReconcile()
150    */
151   public void initialReconcile() {
152     reconcile();
153   }
154
155   /**
156    * Tells this strategy whether to inform its participants.
157    * 
158    * @param notify <code>true</code> if participant should be notified
159    */
160   public void notifyParticipants(boolean notify) {
161     fNotify = notify;
162   }
163 }