Bug 886811
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPDocumentProvider.java
index fb4f623..5751336 100644 (file)
@@ -71,6 +71,8 @@ import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.editors.text.FileDocumentProvider;
 import org.eclipse.ui.part.FileEditorInput;
 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
+import org.eclipse.ui.texteditor.DefaultAnnotation;
+import org.eclipse.ui.texteditor.IAnnotationExtension;
 import org.eclipse.ui.texteditor.MarkerAnnotation;
 import org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel;
 
@@ -142,7 +144,7 @@ public class PHPDocumentProvider extends FileDocumentProvider {
     /*
      * @see AnnotationModel#addAnnotation(Annotation, Position, boolean)
      */
-    protected void addAnnotation(Annotation annotation, Position position, boolean fireModelChanged) {
+    protected void addAnnotation(Annotation annotation, Position position, boolean fireModelChanged) throws BadLocationException {
       super.addAnnotation(annotation, position, fireModelChanged);
 
       Object cached = fReverseMap.get(position);
@@ -185,65 +187,128 @@ public class PHPDocumentProvider extends FileDocumentProvider {
 
       return new Position(start, length);
     }
-
     /*
      * @see IProblemRequestor#endReporting()
      */
     public void endReporting() {
       if (!isActive())
         return;
-
+      
       if (fProgressMonitor != null && fProgressMonitor.isCanceled())
         return;
-
-      boolean isCanceled = false;
-      boolean temporaryProblemsChanged = false;
-      fPreviouslyOverlaid = fCurrentlyOverlaid;
-      fCurrentlyOverlaid = new ArrayList();
-
+      
+      
+      boolean isCanceled= false;
+      boolean temporaryProblemsChanged= false;
+      
       synchronized (fAnnotations) {
+        
+        fPreviouslyOverlaid= fCurrentlyOverlaid;
+        fCurrentlyOverlaid= new ArrayList();
 
         if (fGeneratedAnnotations.size() > 0) {
-          temporaryProblemsChanged = true;
+          temporaryProblemsChanged= true;      
           removeAnnotations(fGeneratedAnnotations, false, true);
           fGeneratedAnnotations.clear();
         }
-
+        
         if (fCollectedProblems != null && fCollectedProblems.size() > 0) {
-
-          Iterator e = fCollectedProblems.iterator();
+          
+          ICompilationUnit cu= getWorkingCopy(fInput);
+          Iterator e= fCollectedProblems.iterator();
           while (e.hasNext()) {
-
-            IProblem problem = (IProblem) e.next();
-
+            
+            IProblem problem= (IProblem) e.next();
+            
             if (fProgressMonitor != null && fProgressMonitor.isCanceled()) {
-              isCanceled = true;
+              isCanceled= true;
               break;
             }
-
-            Position position = createPositionFromProblem(problem);
+            
+            Position position= createPositionFromProblem(problem);
             if (position != null) {
-
-              ProblemAnnotation annotation = new ProblemAnnotation(problem);
-              overlayMarkers(position, annotation);
-              fGeneratedAnnotations.add(annotation);
-              addAnnotation(annotation, position, false);
-
-              temporaryProblemsChanged = true;
+              try {
+                ProblemAnnotation annotation= new ProblemAnnotation(problem, cu);
+                addAnnotation(annotation, position, false);
+                overlayMarkers(position, annotation);                                                          
+                fGeneratedAnnotations.add(annotation);
+                
+                temporaryProblemsChanged= true;
+              } catch (BadLocationException x) {
+                // ignore invalid position
+              }
             }
           }
-
+          
           fCollectedProblems.clear();
         }
-
+        
         removeMarkerOverlays(isCanceled);
         fPreviouslyOverlaid.clear();
-        fPreviouslyOverlaid = null;
+        fPreviouslyOverlaid= null;
       }
-
+      
       if (temporaryProblemsChanged)
-        fireModelChanged(new CompilationUnitAnnotationModelEvent(this, getResource(), false));
+        fireModelChanged();
     }
+    /*
+     * @see IProblemRequestor#endReporting()
+     */
+//    public void endReporting() {
+//      if (!isActive())
+//        return;
+//
+//      if (fProgressMonitor != null && fProgressMonitor.isCanceled())
+//        return;
+//
+//      boolean isCanceled = false;
+//      boolean temporaryProblemsChanged = false;
+//      fPreviouslyOverlaid = fCurrentlyOverlaid;
+//      fCurrentlyOverlaid = new ArrayList();
+//
+//      synchronized (fAnnotations) {
+//
+//        if (fGeneratedAnnotations.size() > 0) {
+//          temporaryProblemsChanged = true;
+//          removeAnnotations(fGeneratedAnnotations, false, true);
+//          fGeneratedAnnotations.clear();
+//        }
+//
+//        if (fCollectedProblems != null && fCollectedProblems.size() > 0) {
+//
+//          Iterator e = fCollectedProblems.iterator();
+//          while (e.hasNext()) {
+//
+//            IProblem problem = (IProblem) e.next();
+//
+//            if (fProgressMonitor != null && fProgressMonitor.isCanceled()) {
+//              isCanceled = true;
+//              break;
+//            }
+//
+//            Position position = createPositionFromProblem(problem);
+//            if (position != null) {
+//
+//              ProblemAnnotation annotation = new ProblemAnnotation(problem);
+//              overlayMarkers(position, annotation);
+//              fGeneratedAnnotations.add(annotation);
+//              addAnnotation(annotation, position, false);
+//
+//              temporaryProblemsChanged = true;
+//            }
+//          }
+//
+//          fCollectedProblems.clear();
+//        }
+//
+//        removeMarkerOverlays(isCanceled);
+//        fPreviouslyOverlaid.clear();
+//        fPreviouslyOverlaid = null;
+//      }
+//
+//      if (temporaryProblemsChanged)
+//        fireModelChanged(new CompilationUnitAnnotationModelEvent(this, getResource(), false));
+//    }
 
     private Object getAnnotations(Position position) {
       return fReverseMap.get(position);
@@ -470,30 +535,55 @@ public class PHPDocumentProvider extends FileDocumentProvider {
   /**
                 * Annotation representating an <code>IProblem</code>.
                 */
-  static protected class ProblemAnnotation extends Annotation implements IJavaAnnotation {
+  static protected class ProblemAnnotation extends Annotation implements IJavaAnnotation, IAnnotationExtension{
 
+    private static final String TASK_ANNOTATION_TYPE= "org.eclipse.ui.workbench.texteditor.task"; //$NON-NLS-1$
+    private static final String ERROR_ANNOTATION_TYPE= "org.eclipse.ui.workbench.texteditor.error"; //$NON-NLS-1$
+    private static final String WARNING_ANNOTATION_TYPE= "org.eclipse.ui.workbench.texteditor.warning"; //$NON-NLS-1$
+    private static final String INFO_ANNOTATION_TYPE= "org.eclipse.ui.workbench.texteditor.info"; //$NON-NLS-1$
+    
     //    private static Image fgQuickFixImage;
     //    private static Image fgQuickFixErrorImage;
     //    private static boolean fgQuickFixImagesInitialized = false;
 
+    private ICompilationUnit fCompilationUnit;
     private List fOverlaids;
     private IProblem fProblem;
     private Image fImage;
     //    private boolean fQuickFixImagesInitialized = false;
-    private AnnotationType fType;
-
-    public ProblemAnnotation(IProblem problem) {
-
-      fProblem = problem;
-      setLayer(MarkerAnnotation.PROBLEM_LAYER + 1);
-
-      if (IProblem.Task == fProblem.getID())
-        fType = AnnotationType.TASK;
-      else if (fProblem.isWarning())
-        fType = AnnotationType.WARNING;
-      else
-        fType = AnnotationType.ERROR;
+//    private AnnotationType fType;
+    private String fType;
+
+    public ProblemAnnotation(IProblem problem, ICompilationUnit cu) {
+      fProblem= problem;
+      fCompilationUnit= cu;
+      
+      if (IProblem.Task == fProblem.getID()) {
+        fType= TASK_ANNOTATION_TYPE;
+        setLayer(DefaultAnnotation.TASK_LAYER + 1);
+      } else if (fProblem.isWarning()) {
+        fType= WARNING_ANNOTATION_TYPE;
+        setLayer(DefaultAnnotation.WARNING_LAYER + 1);
+      } else if (fProblem.isError()) {
+        fType= ERROR_ANNOTATION_TYPE;
+        setLayer(DefaultAnnotation.ERROR_LAYER + 1);
+      } else {
+        fType= INFO_ANNOTATION_TYPE;
+        setLayer(DefaultAnnotation.INFO_LAYER + 1);
+      }
     }
+//    public ProblemAnnotation(IProblem problem) {
+//
+//      fProblem = problem;
+//      setLayer(MarkerAnnotation.PROBLEM_LAYER + 1);
+//
+//      if (IProblem.Task == fProblem.getID())
+//        fType = AnnotationType.TASK;
+//      else if (fProblem.isWarning())
+//        fType = AnnotationType.WARNING;
+//      else
+//        fType = AnnotationType.ERROR;
+//    }
 
     private void initializeImages() {
       // http://bugs.eclipse.org/bugs/show_bug.cgi?id=18936
@@ -562,11 +652,17 @@ public class PHPDocumentProvider extends FileDocumentProvider {
       return isProblem() ? fProblem.getID() : -1;
     }
 
+//    /*
+//     * @see IJavaAnnotation#isProblem()
+//     */
+//    public boolean isProblem() {
+//      return fType == AnnotationType.WARNING || fType == AnnotationType.ERROR;
+//    }
     /*
      * @see IJavaAnnotation#isProblem()
      */
     public boolean isProblem() {
-      return fType == AnnotationType.WARNING || fType == AnnotationType.ERROR;
+      return  WARNING_ANNOTATION_TYPE.equals(fType)  || ERROR_ANNOTATION_TYPE.equals(fType);
     }
 
     /*
@@ -612,8 +708,48 @@ public class PHPDocumentProvider extends FileDocumentProvider {
       return null;
     }
 
+    
     public AnnotationType getAnnotationType() {
-      return fType;
+      if (ERROR_ANNOTATION_TYPE.equals(fType)) {
+        return AnnotationType.ERROR;
+      }
+      if (WARNING_ANNOTATION_TYPE.equals(fType)) {
+        return AnnotationType.WARNING;
+      }
+      if (TASK_ANNOTATION_TYPE.equals(fType)) {
+        return AnnotationType.TASK;
+      }
+//      if (INFO_ANNOTATION_TYPE.equals(fType)) {
+//        return AnnotationType.INFO;
+//      }
+      return AnnotationType.UNKNOWN;
+    }
+    
+    /*
+     * @see IAnnotationExtension#getMarkerType()
+     */
+    public String getMarkerType() {
+      if (isProblem() || INFO_ANNOTATION_TYPE.equals(fType))
+        return IMarker.PROBLEM;
+      else
+        return IMarker.TASK;
+    }
+    /*
+     * @see IAnnotationExtension#getSeverity()
+     */
+    public int getSeverity() {
+      if (ERROR_ANNOTATION_TYPE.equals(fType))
+        return IMarker.SEVERITY_ERROR;
+      if (WARNING_ANNOTATION_TYPE.equals(fType))
+        return IMarker.SEVERITY_WARNING;
+      return IMarker.SEVERITY_INFO;
+    }
+
+    /*
+     * @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getCompilationUnit()
+     */
+    public ICompilationUnit getCompilationUnit() {
+      return fCompilationUnit;
     }
   };
   /**
@@ -1120,60 +1256,116 @@ public class PHPDocumentProvider extends FileDocumentProvider {
   }
 
   /*
-   * @see AbstractDocumentProvider#resetDocument(Object)
+   * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#doResetDocument(java.lang.Object, org.eclipse.core.runtime.IProgressMonitor)
    */
-  public void resetDocument(Object element) throws CoreException {
+  protected void doResetDocument(Object element, IProgressMonitor monitor) throws CoreException {
     if (element == null)
       return;
-
-    ElementInfo elementInfo = getElementInfo(element);
+    
+    ElementInfo elementInfo= getElementInfo(element);          
     if (elementInfo instanceof CompilationUnitInfo) {
-      CompilationUnitInfo info = (CompilationUnitInfo) elementInfo;
-
+      CompilationUnitInfo info= (CompilationUnitInfo) elementInfo;
+      
       IDocument document;
-      IStatus status = null;
-
+      IStatus status= null;
+      
       try {
-
-        ICompilationUnit original = (ICompilationUnit) info.fCopy.getOriginalElement();
-        IResource resource = original.getResource();
+        
+        ICompilationUnit original= (ICompilationUnit) info.fCopy.getOriginalElement();
+        IResource resource= original.getResource();
         if (resource instanceof IFile) {
-
-          IFile file = (IFile) resource;
-
+          
+          IFile file= (IFile) resource;
+          
           try {
-            refreshFile(file);
+            refreshFile(file, monitor);
           } catch (CoreException x) {
-            handleCoreException(x, PHPEditorMessages.getString("PHPDocumentProvider.error.resetDocument")); //$NON-NLS-1$
+            handleCoreException(x, PHPEditorMessages.getString("CompilationUnitDocumentProvider.error.resetDocument")); //$NON-NLS-1$
           }
-
-          IFileEditorInput input = new FileEditorInput(file);
-          document = super.createDocument(input);
-
+          
+          IFileEditorInput input= new FileEditorInput(file);
+          document= super.createDocument(input);
+          
         } else {
-          document = new Document();
+          document= createEmptyDocument();
         }
-
+        
       } catch (CoreException x) {
-        document = new Document();
-        status = x.getStatus();
+        document= createEmptyDocument();
+        status= x.getStatus();
       }
-
+      
       fireElementContentAboutToBeReplaced(element);
-
+      
       removeUnchangedElementListeners(element, info);
       info.fDocument.set(document.get());
-      info.fCanBeSaved = false;
-      info.fStatus = status;
+      info.fCanBeSaved= false;
+      info.fStatus= status;
       addUnchangedElementListeners(element, info);
-
+      
       fireElementContentReplaced(element);
       fireElementDirtyStateChanged(element, false);
-
+      
     } else {
-      super.resetDocument(element);
+      super.doResetDocument(element, monitor);
     }
   }
+  
+  /*
+   * @see AbstractDocumentProvider#resetDocument(Object)
+   */
+//  public void resetDocument(Object element) throws CoreException {
+//    if (element == null)
+//      return;
+//
+//    ElementInfo elementInfo = getElementInfo(element);
+//    if (elementInfo instanceof CompilationUnitInfo) {
+//      CompilationUnitInfo info = (CompilationUnitInfo) elementInfo;
+//
+//      IDocument document;
+//      IStatus status = null;
+//
+//      try {
+//
+//        ICompilationUnit original = (ICompilationUnit) info.fCopy.getOriginalElement();
+//        IResource resource = original.getResource();
+//        if (resource instanceof IFile) {
+//
+//          IFile file = (IFile) resource;
+//
+//          try {
+//            refreshFile(file);
+//          } catch (CoreException x) {
+//            handleCoreException(x, PHPEditorMessages.getString("PHPDocumentProvider.error.resetDocument")); //$NON-NLS-1$
+//          }
+//
+//          IFileEditorInput input = new FileEditorInput(file);
+//          document = super.createDocument(input);
+//
+//        } else {
+//          document = new Document();
+//        }
+//
+//      } catch (CoreException x) {
+//        document = new Document();
+//        status = x.getStatus();
+//      }
+//
+//      fireElementContentAboutToBeReplaced(element);
+//
+//      removeUnchangedElementListeners(element, info);
+//      info.fDocument.set(document.get());
+//      info.fCanBeSaved = false;
+//      info.fStatus = status;
+//      addUnchangedElementListeners(element, info);
+//
+//      fireElementContentReplaced(element);
+//      fireElementDirtyStateChanged(element, false);
+//
+//    } else {
+//      super.resetDocument(element);
+//    }
+//  }
   /**
         * Saves the content of the given document to the given element.
         * This is only performed when this provider initiated the save.