replace deprecated org.eclipse.jface.util.ListenerList
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / phpeditor / PHPDocumentProvider.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
3 /**********************************************************************
4  Copyright (c) 2000, 2002 IBM Corp. and others.
5  All rights reserved. This program and the accompanying materials
6  are made available under the terms of the Common Public License v1.0
7  which accompanies this distribution, and is available at
8  http://www.eclipse.org/legal/cpl-v10.html
9
10  Contributors:
11  IBM Corporation - Initial implementation
12  www.phpeclipse.de
13  **********************************************************************/
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18
19 import net.sourceforge.phpdt.core.ICompilationUnit;
20 import net.sourceforge.phpdt.core.IProblemRequestor;
21 import net.sourceforge.phpdt.core.JavaCore;
22 import net.sourceforge.phpdt.core.JavaModelException;
23 import net.sourceforge.phpdt.core.compiler.IProblem;
24 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
25 import net.sourceforge.phpdt.internal.ui.text.java.IProblemRequestorExtension;
26 import net.sourceforge.phpdt.internal.ui.text.spelling.SpellReconcileStrategy.SpellProblem;
27 import net.sourceforge.phpdt.ui.PreferenceConstants;
28 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
29 import net.sourceforge.phpeclipse.ui.WebUI;
30
31 import org.eclipse.core.resources.IFile;
32 import org.eclipse.core.resources.IMarker;
33 import org.eclipse.core.resources.IResource;
34 import org.eclipse.core.resources.IResourceRuleFactory;
35 import org.eclipse.core.resources.ResourcesPlugin;
36 import org.eclipse.core.runtime.CoreException;
37 import org.eclipse.core.runtime.IProgressMonitor;
38 import org.eclipse.core.runtime.jobs.ISchedulingRule;
39 import org.eclipse.jface.preference.IPreferenceStore;
40 import org.eclipse.jface.text.Assert;
41 import org.eclipse.jface.text.BadLocationException;
42 import org.eclipse.jface.text.DefaultLineTracker;
43 import org.eclipse.jface.text.Document;
44 import org.eclipse.jface.text.IDocument;
45 import org.eclipse.jface.text.ILineTracker;
46 import org.eclipse.jface.text.ISynchronizable;
47 import org.eclipse.jface.text.Position;
48 import org.eclipse.jface.text.source.Annotation;
49 import org.eclipse.jface.text.source.AnnotationModelEvent;
50 import org.eclipse.jface.text.source.IAnnotationAccessExtension;
51 import org.eclipse.jface.text.source.IAnnotationModel;
52 import org.eclipse.jface.text.source.IAnnotationModelListener;
53 import org.eclipse.jface.text.source.IAnnotationModelListenerExtension;
54 import org.eclipse.jface.text.source.IAnnotationPresentation;
55 import org.eclipse.jface.text.source.ImageUtilities;
56 import org.eclipse.jface.util.IPropertyChangeListener;
57 //incastrix
58 //import org.eclipse.jface.util.ListenerList;
59 import org.eclipse.core.runtime.ListenerList;
60 import org.eclipse.jface.util.PropertyChangeEvent;
61 import org.eclipse.swt.SWT;
62 import org.eclipse.swt.graphics.GC;
63 import org.eclipse.swt.graphics.Image;
64 import org.eclipse.swt.graphics.Rectangle;
65 import org.eclipse.swt.widgets.Canvas;
66 import org.eclipse.swt.widgets.Display;
67 import org.eclipse.ui.IFileEditorInput;
68 import org.eclipse.ui.editors.text.EditorsUI;
69 import org.eclipse.ui.editors.text.ForwardingDocumentProvider;
70 import org.eclipse.ui.editors.text.TextFileDocumentProvider;
71 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
72 import org.eclipse.ui.texteditor.AnnotationPreference;
73 import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
74 import org.eclipse.ui.texteditor.IDocumentProvider;
75 import org.eclipse.ui.texteditor.MarkerAnnotation;
76 import org.eclipse.ui.texteditor.MarkerUtilities;
77 import org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel;
78
79 /**
80  * The PHPDocumentProvider provides the IDocuments used by java editors.
81  */
82
83 public class PHPDocumentProvider extends TextFileDocumentProvider implements
84                 ICompilationUnitDocumentProvider {
85         /**
86          * Here for visibility issues only.
87          */
88
89         /**
90          * Bundle of all required informations to allow working copy management.
91          */
92         /**
93          * Bundle of all required informations to allow working copy management.
94          */
95         static protected class CompilationUnitInfo extends FileInfo {
96                 public ICompilationUnit fCopy;
97         }
98
99         /**
100          * Annotation model dealing with java marker annotations and temporary
101          * problems. Also acts as problem requestor for its compilation unit.
102          * Initialiy inactive. Must explicitly be activated.
103          */
104         protected static class CompilationUnitAnnotationModel extends
105                         ResourceMarkerAnnotationModel implements IProblemRequestor,
106                         IProblemRequestorExtension {
107
108                 private static class ProblemRequestorState {
109                         boolean fInsideReportingSequence = false;
110
111                         List fReportedProblems;
112                 }
113
114                 private ThreadLocal fProblemRequestorState = new ThreadLocal();
115
116                 private int fStateCount = 0;
117
118                 private ICompilationUnit fCompilationUnit;
119
120                 private List fGeneratedAnnotations;
121
122                 private IProgressMonitor fProgressMonitor;
123
124                 private boolean fIsActive = false;
125
126                 private ReverseMap fReverseMap = new ReverseMap();
127
128                 private List fPreviouslyOverlaid = null;
129
130                 private List fCurrentlyOverlaid = new ArrayList();
131
132                 public CompilationUnitAnnotationModel(IResource resource) {
133                         super(resource);
134                 }
135
136                 public void setCompilationUnit(ICompilationUnit unit) {
137                         fCompilationUnit = unit;
138                 }
139
140                 protected MarkerAnnotation createMarkerAnnotation(IMarker marker) {
141                         String markerType = MarkerUtilities.getMarkerType(marker);
142                         if (markerType != null
143                                         && markerType
144                                                         .startsWith(JavaMarkerAnnotation.JAVA_MARKER_TYPE_PREFIX))
145                                 return new JavaMarkerAnnotation(marker);
146                         return super.createMarkerAnnotation(marker);
147                 }
148
149                 /*
150                  * @see org.eclipse.jface.text.source.AnnotationModel#createAnnotationModelEvent()
151                  */
152                 protected AnnotationModelEvent createAnnotationModelEvent() {
153                         return new CompilationUnitAnnotationModelEvent(this, getResource());
154                 }
155
156                 protected Position createPositionFromProblem(IProblem problem) {
157                         int start = problem.getSourceStart();
158                         if (start < 0)
159                                 return null;
160
161                         int length = problem.getSourceEnd() - problem.getSourceStart() + 1;
162                         if (length < 0)
163                                 return null;
164
165                         return new Position(start, length);
166                 }
167
168                 /*
169                  * @see IProblemRequestor#beginReporting()
170                  */
171                 public void beginReporting() {
172                         ProblemRequestorState state = (ProblemRequestorState) fProblemRequestorState
173                                         .get();
174                         if (state == null)
175                                 internalBeginReporting(false);
176                 }
177
178                 /*
179                  * @see net.sourceforge.phpdt.internal.ui.text.java.IProblemRequestorExtension#beginReportingSequence()
180                  */
181                 public void beginReportingSequence() {
182                         ProblemRequestorState state = (ProblemRequestorState) fProblemRequestorState
183                                         .get();
184                         if (state == null)
185                                 internalBeginReporting(true);
186                 }
187
188                 /**
189                  * Sets up the infrastructure necessary for problem reporting.
190                  * 
191                  * @param insideReportingSequence
192                  *            <code>true</code> if this method call is issued from
193                  *            inside a reporting sequence
194                  */
195                 private void internalBeginReporting(boolean insideReportingSequence) {
196                         if (fCompilationUnit != null) {
197                                 // &&
198                                 // fCompilationUnit.getJavaProject().isOnClasspath(fCompilationUnit))
199                                 // {
200                                 ProblemRequestorState state = new ProblemRequestorState();
201                                 state.fInsideReportingSequence = insideReportingSequence;
202                                 state.fReportedProblems = new ArrayList();
203                                 synchronized (getLockObject()) {
204                                         fProblemRequestorState.set(state);
205                                         ++fStateCount;
206                                 }
207                         }
208                 }
209
210                 /*
211                  * @see IProblemRequestor#acceptProblem(IProblem)
212                  */
213                 public void acceptProblem(IProblem problem) {
214                         if (isActive()) {
215                                 ProblemRequestorState state = (ProblemRequestorState) fProblemRequestorState
216                                                 .get();
217                                 if (state != null)
218                                         state.fReportedProblems.add(problem);
219                         }
220                 }
221
222                 /*
223                  * @see IProblemRequestor#endReporting()
224                  */
225                 public void endReporting() {
226                         ProblemRequestorState state = (ProblemRequestorState) fProblemRequestorState
227                                         .get();
228                         if (state != null && !state.fInsideReportingSequence)
229                                 internalEndReporting(state);
230                 }
231
232                 /*
233                  * @see net.sourceforge.phpdt.internal.ui.text.java.IProblemRequestorExtension#endReportingSequence()
234                  */
235                 public void endReportingSequence() {
236                         ProblemRequestorState state = (ProblemRequestorState) fProblemRequestorState
237                                         .get();
238                         if (state != null && state.fInsideReportingSequence)
239                                 internalEndReporting(state);
240                 }
241
242                 private void internalEndReporting(ProblemRequestorState state) {
243                         int stateCount = 0;
244                         synchronized (getLockObject()) {
245                                 --fStateCount;
246                                 stateCount = fStateCount;
247                                 fProblemRequestorState.set(null);
248                         }
249
250                         if (stateCount == 0 && isActive())
251                                 reportProblems(state.fReportedProblems);
252                 }
253
254                 /**
255                  * Signals the end of problem reporting.
256                  */
257                 private void reportProblems(List reportedProblems) {
258                         if (fProgressMonitor != null && fProgressMonitor.isCanceled())
259                                 return;
260
261                         boolean temporaryProblemsChanged = false;
262
263                         synchronized (getLockObject()) {
264
265                                 boolean isCanceled = false;
266
267                                 fPreviouslyOverlaid = fCurrentlyOverlaid;
268                                 fCurrentlyOverlaid = new ArrayList();
269
270                                 if (fGeneratedAnnotations.size() > 0) {
271                                         temporaryProblemsChanged = true;
272                                         removeAnnotations(fGeneratedAnnotations, false, true);
273                                         fGeneratedAnnotations.clear();
274                                 }
275
276                                 if (reportedProblems != null && reportedProblems.size() > 0) {
277
278                                         Iterator e = reportedProblems.iterator();
279                                         while (e.hasNext()) {
280
281                                                 if (fProgressMonitor != null
282                                                                 && fProgressMonitor.isCanceled()) {
283                                                         isCanceled = true;
284                                                         break;
285                                                 }
286
287                                                 IProblem problem = (IProblem) e.next();
288                                                 Position position = createPositionFromProblem(problem);
289                                                 if (position != null) {
290
291                                                         try {
292                                                                 ProblemAnnotation annotation = new ProblemAnnotation(
293                                                                                 problem, fCompilationUnit);
294                                                                 overlayMarkers(position, annotation);
295                                                                 addAnnotation(annotation, position, false);
296                                                                 fGeneratedAnnotations.add(annotation);
297
298                                                                 temporaryProblemsChanged = true;
299                                                         } catch (BadLocationException x) {
300                                                                 // ignore invalid position
301                                                         }
302                                                 }
303                                         }
304                                 }
305
306                                 removeMarkerOverlays(isCanceled);
307                                 fPreviouslyOverlaid = null;
308                         }
309
310                         if (temporaryProblemsChanged)
311                                 fireModelChanged();
312                 }
313
314                 private void removeMarkerOverlays(boolean isCanceled) {
315                         if (isCanceled) {
316                                 fCurrentlyOverlaid.addAll(fPreviouslyOverlaid);
317                         } else if (fPreviouslyOverlaid != null) {
318                                 Iterator e = fPreviouslyOverlaid.iterator();
319                                 while (e.hasNext()) {
320                                         JavaMarkerAnnotation annotation = (JavaMarkerAnnotation) e
321                                                         .next();
322                                         annotation.setOverlay(null);
323                                 }
324                         }
325                 }
326
327                 /**
328                  * Overlays value with problem annotation.
329                  * 
330                  * @param problemAnnotation
331                  */
332                 private void setOverlay(Object value,
333                                 ProblemAnnotation problemAnnotation) {
334                         if (value instanceof JavaMarkerAnnotation) {
335                                 JavaMarkerAnnotation annotation = (JavaMarkerAnnotation) value;
336                                 if (annotation.isProblem()) {
337                                         annotation.setOverlay(problemAnnotation);
338                                         fPreviouslyOverlaid.remove(annotation);
339                                         fCurrentlyOverlaid.add(annotation);
340                                 }
341                         } else {
342                         }
343                 }
344
345                 private void overlayMarkers(Position position,
346                                 ProblemAnnotation problemAnnotation) {
347                         Object value = getAnnotations(position);
348                         if (value instanceof List) {
349                                 List list = (List) value;
350                                 for (Iterator e = list.iterator(); e.hasNext();)
351                                         setOverlay(e.next(), problemAnnotation);
352                         } else {
353                                 setOverlay(value, problemAnnotation);
354                         }
355                 }
356
357                 /**
358                  * Tells this annotation model to collect temporary problems from now
359                  * on.
360                  */
361                 private void startCollectingProblems() {
362                         fGeneratedAnnotations = new ArrayList();
363                 }
364
365                 /**
366                  * Tells this annotation model to no longer collect temporary problems.
367                  */
368                 private void stopCollectingProblems() {
369                         if (fGeneratedAnnotations != null)
370                                 removeAnnotations(fGeneratedAnnotations, true, true);
371                         fGeneratedAnnotations = null;
372                 }
373
374                 /*
375                  * @see IProblemRequestor#isActive()
376                  */
377                 public boolean isActive() {
378                         return fIsActive;
379                 }
380
381                 /*
382                  * @see IProblemRequestorExtension#setProgressMonitor(IProgressMonitor)
383                  */
384                 public void setProgressMonitor(IProgressMonitor monitor) {
385                         fProgressMonitor = monitor;
386                 }
387
388                 /*
389                  * @see IProblemRequestorExtension#setIsActive(boolean)
390                  */
391                 public void setIsActive(boolean isActive) {
392                         if (fIsActive != isActive) {
393                                 fIsActive = isActive;
394                                 if (fIsActive)
395                                         startCollectingProblems();
396                                 else
397                                         stopCollectingProblems();
398                         }
399                 }
400
401                 private Object getAnnotations(Position position) {
402                         return fReverseMap.get(position);
403                 }
404
405                 /*
406                  * @see AnnotationModel#addAnnotation(Annotation, Position, boolean)
407                  */
408                 protected void addAnnotation(Annotation annotation, Position position,
409                                 boolean fireModelChanged) throws BadLocationException {
410                         super.addAnnotation(annotation, position, fireModelChanged);
411
412                         Object cached = fReverseMap.get(position);
413                         if (cached == null)
414                                 fReverseMap.put(position, annotation);
415                         else if (cached instanceof List) {
416                                 List list = (List) cached;
417                                 list.add(annotation);
418                         } else if (cached instanceof Annotation) {
419                                 List list = new ArrayList(2);
420                                 list.add(cached);
421                                 list.add(annotation);
422                                 fReverseMap.put(position, list);
423                         }
424                 }
425
426                 /*
427                  * @see AnnotationModel#removeAllAnnotations(boolean)
428                  */
429                 protected void removeAllAnnotations(boolean fireModelChanged) {
430                         super.removeAllAnnotations(fireModelChanged);
431                         fReverseMap.clear();
432                 }
433
434                 /*
435                  * @see AnnotationModel#removeAnnotation(Annotation, boolean)
436                  */
437                 protected void removeAnnotation(Annotation annotation,
438                                 boolean fireModelChanged) {
439                         Position position = getPosition(annotation);
440                         Object cached = fReverseMap.get(position);
441                         if (cached instanceof List) {
442                                 List list = (List) cached;
443                                 list.remove(annotation);
444                                 if (list.size() == 1) {
445                                         fReverseMap.put(position, list.get(0));
446                                         list.clear();
447                                 }
448                         } else if (cached instanceof Annotation) {
449                                 fReverseMap.remove(position);
450                         }
451                         super.removeAnnotation(annotation, fireModelChanged);
452                 }
453         }
454
455         protected static class GlobalAnnotationModelListener implements
456                         IAnnotationModelListener, IAnnotationModelListenerExtension {
457
458                 private ListenerList fListenerList;
459
460                 public GlobalAnnotationModelListener() {
461                         fListenerList = new ListenerList();
462                 }
463
464                 public void addListener(IAnnotationModelListener listener) {
465                         fListenerList.add(listener);
466                 }
467
468                 /**
469                  * @see IAnnotationModelListenerExtension#modelChanged(AnnotationModelEvent)
470                  */
471                 public void modelChanged(AnnotationModelEvent event) {
472                         Object[] listeners = fListenerList.getListeners();
473                         for (int i = 0; i < listeners.length; i++) {
474                                 Object curr = listeners[i];
475                                 if (curr instanceof IAnnotationModelListenerExtension) {
476                                         ((IAnnotationModelListenerExtension) curr)
477                                                         .modelChanged(event);
478                                 }
479                         }
480                 }
481
482                 /**
483                  * @see IAnnotationModelListener#modelChanged(IAnnotationModel)
484                  */
485                 public void modelChanged(IAnnotationModel model) {
486                         Object[] listeners = fListenerList.getListeners();
487                         for (int i = 0; i < listeners.length; i++) {
488                                 ((IAnnotationModelListener) listeners[i]).modelChanged(model);
489                         }
490                 }
491
492                 public void removeListener(IAnnotationModelListener listener) {
493                         fListenerList.remove(listener);
494                 }
495         }
496
497         /**
498          * Annotation representating an <code>IProblem</code>.
499          */
500         static public class ProblemAnnotation extends Annotation implements
501                         IJavaAnnotation, IAnnotationPresentation {
502
503                 private static final String SPELLING_ANNOTATION_TYPE = "org.eclipse.ui.workbench.texteditor.spelling";
504
505                 // XXX: To be fully correct these constants should be non-static
506                 /**
507                  * The layer in which task problem annotations are located.
508                  */
509                 private static final int TASK_LAYER;
510
511                 /**
512                  * The layer in which info problem annotations are located.
513                  */
514                 private static final int INFO_LAYER;
515
516                 /**
517                  * The layer in which warning problem annotations representing are
518                  * located.
519                  */
520                 private static final int WARNING_LAYER;
521
522                 /**
523                  * The layer in which error problem annotations representing are
524                  * located.
525                  */
526                 private static final int ERROR_LAYER;
527
528                 static {
529                         AnnotationPreferenceLookup lookup = EditorsUI
530                                         .getAnnotationPreferenceLookup();
531                         TASK_LAYER = computeLayer(
532                                         "org.eclipse.ui.workbench.texteditor.task", lookup); //$NON-NLS-1$
533                         INFO_LAYER = computeLayer("net.sourceforge.phpdt.ui.info", lookup); //$NON-NLS-1$
534                         WARNING_LAYER = computeLayer(
535                                         "net.sourceforge.phpdt.ui.warning", lookup); //$NON-NLS-1$
536                         ERROR_LAYER = computeLayer("net.sourceforge.phpdt.ui.error", lookup); //$NON-NLS-1$
537                 }
538
539                 private static int computeLayer(String annotationType,
540                                 AnnotationPreferenceLookup lookup) {
541                         Annotation annotation = new Annotation(annotationType, false, null);
542                         AnnotationPreference preference = lookup
543                                         .getAnnotationPreference(annotation);
544                         if (preference != null)
545                                 return preference.getPresentationLayer() + 1;
546                         else
547                                 return IAnnotationAccessExtension.DEFAULT_LAYER + 1;
548                 }
549
550                 // private static Image fgQuickFixImage;
551                 // private static Image fgQuickFixErrorImage;
552                 // private static boolean fgQuickFixImagesInitialized= false;
553
554                 private ICompilationUnit fCompilationUnit;
555
556                 private List fOverlaids;
557
558                 private IProblem fProblem;
559
560                 private Image fImage;
561
562                 private boolean fQuickFixImagesInitialized = false;
563
564                 private int fLayer = IAnnotationAccessExtension.DEFAULT_LAYER;
565
566                 public ProblemAnnotation(IProblem problem, ICompilationUnit cu) {
567
568                         fProblem = problem;
569                         fCompilationUnit = cu;
570
571                         if (SpellProblem.Spelling == fProblem.getID()) {
572                                 setType(SPELLING_ANNOTATION_TYPE);
573                                 fLayer = WARNING_LAYER;
574                         } else if (IProblem.Task == fProblem.getID()) {
575                                 setType(JavaMarkerAnnotation.TASK_ANNOTATION_TYPE);
576                                 fLayer = TASK_LAYER;
577                         } else if (fProblem.isWarning()) {
578                                 setType(JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE);
579                                 fLayer = WARNING_LAYER;
580                         } else if (fProblem.isError()) {
581                                 setType(JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE);
582                                 fLayer = ERROR_LAYER;
583                         } else {
584                                 setType(JavaMarkerAnnotation.INFO_ANNOTATION_TYPE);
585                                 fLayer = INFO_LAYER;
586                         }
587                 }
588
589                 /*
590                  * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
591                  */
592                 public int getLayer() {
593                         return fLayer;
594                 }
595
596                 private void initializeImages() {
597                         // http://bugs.eclipse.org/bugs/show_bug.cgi?id=18936
598                         // if (!fQuickFixImagesInitialized) {
599                         // if (isProblem() && indicateQuixFixableProblems() &&
600                         // JavaCorrectionProcessor.hasCorrections(this)) { // no light bulb
601                         // for tasks
602                         // if (!fgQuickFixImagesInitialized) {
603                         // fgQuickFixImage=
604                         // JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_PROBLEM);
605                         // fgQuickFixErrorImage=
606                         // JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_ERROR);
607                         // fgQuickFixImagesInitialized= true;
608                         // }
609                         // if (JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(getType()))
610                         // fImage= fgQuickFixErrorImage;
611                         // else
612                         // fImage= fgQuickFixImage;
613                         // }
614                         // fQuickFixImagesInitialized= true;
615                         // }
616                 }
617
618                 private boolean indicateQuixFixableProblems() {
619                         return PreferenceConstants.getPreferenceStore().getBoolean(
620                                         PreferenceConstants.EDITOR_CORRECTION_INDICATION);
621                 }
622
623                 /*
624                  * @see Annotation#paint
625                  */
626                 public void paint(GC gc, Canvas canvas, Rectangle r) {
627                         initializeImages();
628                         if (fImage != null)
629                                 ImageUtilities.drawImage(fImage, gc, canvas, r, SWT.CENTER,
630                                                 SWT.TOP);
631                 }
632
633                 /*
634                  * @see IJavaAnnotation#getImage(Display)
635                  */
636                 public Image getImage(Display display) {
637                         initializeImages();
638                         return fImage;
639                 }
640
641                 /*
642                  * @see IJavaAnnotation#getMessage()
643                  */
644                 public String getText() {
645                         return fProblem.getMessage();
646                 }
647
648                 /*
649                  * @see IJavaAnnotation#getArguments()
650                  */
651                 public String[] getArguments() {
652                         return isProblem() ? fProblem.getArguments() : null;
653                 }
654
655                 /*
656                  * @see IJavaAnnotation#getId()
657                  */
658                 public int getId() {
659                         return fProblem.getID();
660                 }
661
662                 /*
663                  * @see IJavaAnnotation#isProblem()
664                  */
665                 public boolean isProblem() {
666                         String type = getType();
667                         return JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE.equals(type)
668                                         || JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(type)
669                                         || SPELLING_ANNOTATION_TYPE.equals(type);
670                 }
671
672                 /*
673                  * @see IJavaAnnotation#hasOverlay()
674                  */
675                 public boolean hasOverlay() {
676                         return false;
677                 }
678
679                 /*
680                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.IJavaAnnotation#getOverlay()
681                  */
682                 public IJavaAnnotation getOverlay() {
683                         return null;
684                 }
685
686                 /*
687                  * @see IJavaAnnotation#addOverlaid(IJavaAnnotation)
688                  */
689                 public void addOverlaid(IJavaAnnotation annotation) {
690                         if (fOverlaids == null)
691                                 fOverlaids = new ArrayList(1);
692                         fOverlaids.add(annotation);
693                 }
694
695                 /*
696                  * @see IJavaAnnotation#removeOverlaid(IJavaAnnotation)
697                  */
698                 public void removeOverlaid(IJavaAnnotation annotation) {
699                         if (fOverlaids != null) {
700                                 fOverlaids.remove(annotation);
701                                 if (fOverlaids.size() == 0)
702                                         fOverlaids = null;
703                         }
704                 }
705
706                 /*
707                  * @see IJavaAnnotation#getOverlaidIterator()
708                  */
709                 public Iterator getOverlaidIterator() {
710                         if (fOverlaids != null)
711                                 return fOverlaids.iterator();
712                         return null;
713                 }
714
715                 /*
716                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.IJavaAnnotation#getCompilationUnit()
717                  */
718                 public ICompilationUnit getCompilationUnit() {
719                         return fCompilationUnit;
720                 }
721         }
722
723         /**
724          * Internal structure for mapping positions to some value. The reason for
725          * this specific structure is that positions can change over time. Thus a
726          * lookup is based on value and not on hash value.
727          */
728         protected static class ReverseMap {
729
730                 static class Entry {
731                         Position fPosition;
732
733                         Object fValue;
734                 }
735
736                 private int fAnchor = 0;
737
738                 private List fList = new ArrayList(2);
739
740                 public ReverseMap() {
741                 }
742
743                 public void clear() {
744                         fList.clear();
745                 }
746
747                 public Object get(Position position) {
748
749                         Entry entry;
750
751                         // behind anchor
752                         int length = fList.size();
753                         for (int i = fAnchor; i < length; i++) {
754                                 entry = (Entry) fList.get(i);
755                                 if (entry.fPosition.equals(position)) {
756                                         fAnchor = i;
757                                         return entry.fValue;
758                                 }
759                         }
760
761                         // before anchor
762                         for (int i = 0; i < fAnchor; i++) {
763                                 entry = (Entry) fList.get(i);
764                                 if (entry.fPosition.equals(position)) {
765                                         fAnchor = i;
766                                         return entry.fValue;
767                                 }
768                         }
769
770                         return null;
771                 }
772
773                 private int getIndex(Position position) {
774                         Entry entry;
775                         int length = fList.size();
776                         for (int i = 0; i < length; i++) {
777                                 entry = (Entry) fList.get(i);
778                                 if (entry.fPosition.equals(position))
779                                         return i;
780                         }
781                         return -1;
782                 }
783
784                 public void put(Position position, Object value) {
785                         int index = getIndex(position);
786                         if (index == -1) {
787                                 Entry entry = new Entry();
788                                 entry.fPosition = position;
789                                 entry.fValue = value;
790                                 fList.add(entry);
791                         } else {
792                                 Entry entry = (Entry) fList.get(index);
793                                 entry.fValue = value;
794                         }
795                 }
796
797                 public void remove(Position position) {
798                         int index = getIndex(position);
799                         if (index > -1)
800                                 fList.remove(index);
801                 }
802         }
803
804         /**
805          * Document that can also be used by a background reconciler.
806          */
807         protected static class PartiallySynchronizedDocument extends Document {
808
809                 /*
810                  * @see IDocumentExtension#startSequentialRewrite(boolean)
811                  */
812                 synchronized public void startSequentialRewrite(boolean normalized) {
813                         super.startSequentialRewrite(normalized);
814                 }
815
816                 /*
817                  * @see IDocumentExtension#stopSequentialRewrite()
818                  */
819                 synchronized public void stopSequentialRewrite() {
820                         super.stopSequentialRewrite();
821                 }
822
823                 /*
824                  * @see IDocument#get()
825                  */
826                 synchronized public String get() {
827                         return super.get();
828                 }
829
830                 /*
831                  * @see IDocument#get(int, int)
832                  */
833                 synchronized public String get(int offset, int length)
834                                 throws BadLocationException {
835                         return super.get(offset, length);
836                 }
837
838                 /*
839                  * @see IDocument#getChar(int)
840                  */
841                 synchronized public char getChar(int offset)
842                                 throws BadLocationException {
843                         return super.getChar(offset);
844                 }
845
846                 /*
847                  * @see IDocument#replace(int, int, String)
848                  */
849                 synchronized public void replace(int offset, int length, String text)
850                                 throws BadLocationException {
851                         super.replace(offset, length, text);
852                 }
853
854                 /*
855                  * @see IDocument#set(String)
856                  */
857                 synchronized public void set(String text) {
858                         super.set(text);
859                 }
860         };
861
862         //
863         // private static PHPPartitionScanner HTML_PARTITION_SCANNER = null;
864         //
865         // private static PHPPartitionScanner PHP_PARTITION_SCANNER = null;
866         // private static PHPPartitionScanner SMARTY_PARTITION_SCANNER = null;
867         //
868         // // private final static String[] TYPES= new String[] {
869         // PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC,
870         // PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
871         // private final static String[] TYPES =
872         // new String[] {
873         // IPHPPartitionScannerConstants.PHP,
874         // IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
875         // IPHPPartitionScannerConstants.HTML,
876         // IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
877         // IPHPPartitionScannerConstants.JAVASCRIPT,
878         // IPHPPartitionScannerConstants.CSS,
879         // IPHPPartitionScannerConstants.SMARTY,
880         // IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT };
881         // private static PHPPartitionScanner XML_PARTITION_SCANNER = null;
882
883         /* Preference key for temporary problems */
884         private final static String HANDLE_TEMPORARY_PROBLEMS = PreferenceConstants.EDITOR_EVALUTE_TEMPORARY_PROBLEMS;
885
886         /** Indicates whether the save has been initialized by this provider */
887         private boolean fIsAboutToSave = false;
888
889         /** The save policy used by this provider */
890         private ISavePolicy fSavePolicy;
891
892         /** Internal property changed listener */
893         private IPropertyChangeListener fPropertyListener;
894
895         /** annotation model listener added to all created CU annotation models */
896         private GlobalAnnotationModelListener fGlobalAnnotationModelListener;
897
898         public PHPDocumentProvider() {
899                 // IDocumentProvider provider= new TextFileDocumentProvider(new
900                 // JavaStorageDocumentProvider());
901                 IDocumentProvider provider = new TextFileDocumentProvider();
902                 provider = new ForwardingDocumentProvider(
903                                 IPHPPartitions.PHP_PARTITIONING,
904                                 new JavaDocumentSetupParticipant(), provider);
905                 setParentDocumentProvider(provider);
906
907                 fGlobalAnnotationModelListener = new GlobalAnnotationModelListener();
908                 fPropertyListener = new IPropertyChangeListener() {
909                         public void propertyChange(PropertyChangeEvent event) {
910                                 if (HANDLE_TEMPORARY_PROBLEMS.equals(event.getProperty()))
911                                         enableHandlingTemporaryProblems();
912                         }
913                 };
914                 WebUI.getDefault().getPreferenceStore()
915                                 .addPropertyChangeListener(fPropertyListener);
916
917         }
918
919         /**
920          * Sets the document provider's save policy.
921          */
922         public void setSavePolicy(ISavePolicy savePolicy) {
923                 fSavePolicy = savePolicy;
924         }
925
926         /**
927          * Creates a compilation unit from the given file.
928          * 
929          * @param file
930          *            the file from which to create the compilation unit
931          */
932         protected ICompilationUnit createCompilationUnit(IFile file) {
933                 Object element = JavaCore.create(file);
934                 if (element instanceof ICompilationUnit)
935                         return (ICompilationUnit) element;
936                 return null;
937         }
938
939         /*
940          * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createEmptyFileInfo()
941          */
942         protected FileInfo createEmptyFileInfo() {
943                 return new CompilationUnitInfo();
944         }
945
946         /*
947          * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createAnnotationModel(org.eclipse.core.resources.IFile)
948          */
949         protected IAnnotationModel createAnnotationModel(IFile file) {
950                 return new CompilationUnitAnnotationModel(file);
951         }
952
953         /*
954          * @see AbstractDocumentProvider#createElementInfo(Object)
955          */
956         // protected ElementInfo createElementInfo(Object element) throws
957         // CoreException {
958         //
959         // if (!(element instanceof IFileEditorInput))
960         // return super.createElementInfo(element);
961         //
962         // IFileEditorInput input = (IFileEditorInput) element;
963         // ICompilationUnit original = createCompilationUnit(input.getFile());
964         // if (original != null) {
965         //
966         // try {
967         //
968         // try {
969         // refreshFile(input.getFile());
970         // } catch (CoreException x) {
971         // handleCoreException(x,
972         // PHPEditorMessages.getString("PHPDocumentProvider.error.createElementInfo"));
973         // //$NON-NLS-1$
974         // }
975         //
976         // IAnnotationModel m = createCompilationUnitAnnotationModel(input);
977         // IProblemRequestor r = m instanceof IProblemRequestor ?
978         // (IProblemRequestor) m : null;
979         // ICompilationUnit c = (ICompilationUnit)
980         // original.getSharedWorkingCopy(getProgressMonitor(), fBufferFactory, r);
981         //
982         // DocumentAdapter a = null;
983         // try {
984         // a = (DocumentAdapter) c.getBuffer();
985         // } catch (ClassCastException x) {
986         // IStatus status = new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID,
987         // PHPStatusConstants.TEMPLATE_IO_EXCEPTION, "Shared working copy has wrong
988         // buffer", x); //$NON-NLS-1$
989         // throw new CoreException(status);
990         // }
991         //
992         // _FileSynchronizer f = new _FileSynchronizer(input);
993         // f.install();
994         //
995         // CompilationUnitInfo info = new CompilationUnitInfo(a.getDocument(), m, f,
996         // c);
997         // info.setModificationStamp(computeModificationStamp(input.getFile()));
998         // info.fStatus = a.getStatus();
999         // info.fEncoding = getPersistedEncoding(input);
1000         //
1001         // if (r instanceof IProblemRequestorExtension) {
1002         // IProblemRequestorExtension extension = (IProblemRequestorExtension) r;
1003         // extension.setIsActive(isHandlingTemporaryProblems());
1004         // }
1005         // m.addAnnotationModelListener(fGlobalAnnotationModelListener);
1006         //
1007         // return info;
1008         //
1009         // } catch (JavaModelException x) {
1010         // throw new CoreException(x.getStatus());
1011         // }
1012         // } else {
1013         // return super.createElementInfo(element);
1014         // }
1015         // }
1016         /*
1017          * @see AbstractDocumentProvider#disposeElementInfo(Object, ElementInfo)
1018          */
1019         // protected void disposeElementInfo(Object element, ElementInfo info) {
1020         //
1021         // if (info instanceof CompilationUnitInfo) {
1022         // CompilationUnitInfo cuInfo = (CompilationUnitInfo) info;
1023         // cuInfo.fCopy.destroy();
1024         // cuInfo.fModel.removeAnnotationModelListener(fGlobalAnnotationModelListener);
1025         // }
1026         //
1027         // super.disposeElementInfo(element, info);
1028         // }
1029         /*
1030          * @see AbstractDocumentProvider#doSaveDocument(IProgressMonitor, Object,
1031          *      IDocument, boolean)
1032          */
1033         // protected void doSaveDocument(IProgressMonitor monitor, Object element,
1034         // IDocument document, boolean overwrite)
1035         // throws CoreException {
1036         //
1037         // ElementInfo elementInfo = getElementInfo(element);
1038         // if (elementInfo instanceof CompilationUnitInfo) {
1039         // CompilationUnitInfo info = (CompilationUnitInfo) elementInfo;
1040         //
1041         // // update structure, assumes lock on info.fCopy
1042         // info.fCopy.reconcile();
1043         //
1044         // ICompilationUnit original = (ICompilationUnit)
1045         // info.fCopy.getOriginalElement();
1046         // IResource resource = original.getResource();
1047         //
1048         // if (resource == null) {
1049         // // underlying resource has been deleted, just recreate file, ignore the
1050         // rest
1051         // super.doSaveDocument(monitor, element, document, overwrite);
1052         // return;
1053         // }
1054         //
1055         // if (resource != null && !overwrite)
1056         // checkSynchronizationState(info.fModificationStamp, resource);
1057         //
1058         // if (fSavePolicy != null)
1059         // fSavePolicy.preSave(info.fCopy);
1060         //
1061         // // inform about the upcoming content change
1062         // fireElementStateChanging(element);
1063         // try {
1064         // fIsAboutToSave = true;
1065         // // commit working copy
1066         // info.fCopy.commit(overwrite, monitor);
1067         // } catch (CoreException x) {
1068         // // inform about the failure
1069         // fireElementStateChangeFailed(element);
1070         // throw x;
1071         // } catch (RuntimeException x) {
1072         // // inform about the failure
1073         // fireElementStateChangeFailed(element);
1074         // throw x;
1075         // } finally {
1076         // fIsAboutToSave = false;
1077         // }
1078         //
1079         // // If here, the dirty state of the editor will change to "not dirty".
1080         // // Thus, the state changing flag will be reset.
1081         //
1082         // AbstractMarkerAnnotationModel model = (AbstractMarkerAnnotationModel)
1083         // info.fModel;
1084         // model.updateMarkers(info.fDocument);
1085         //
1086         // if (resource != null)
1087         // info.setModificationStamp(computeModificationStamp(resource));
1088         //
1089         // if (fSavePolicy != null) {
1090         // ICompilationUnit unit = fSavePolicy.postSave(original);
1091         // if (unit != null) {
1092         // IResource r = unit.getResource();
1093         // IMarker[] markers = r.findMarkers(IMarker.MARKER, true,
1094         // IResource.DEPTH_ZERO);
1095         // if (markers != null && markers.length > 0) {
1096         // for (int i = 0; i < markers.length; i++)
1097         // model.updateMarker(markers[i], info.fDocument, null);
1098         // }
1099         // }
1100         // }
1101         //
1102         // } else {
1103         // super.doSaveDocument(monitor, element, document, overwrite);
1104         // }
1105         // }
1106         /*
1107          * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createFileInfo(java.lang.Object)
1108          */
1109         protected FileInfo createFileInfo(Object element) throws CoreException {
1110                 if (!(element instanceof IFileEditorInput))
1111                         return null;
1112
1113                 IFileEditorInput input = (IFileEditorInput) element;
1114                 ICompilationUnit original = createCompilationUnit(input.getFile());
1115                 if (original == null)
1116                         return null;
1117
1118                 FileInfo info = super.createFileInfo(element);
1119                 if (!(info instanceof CompilationUnitInfo))
1120                         return null;
1121
1122                 CompilationUnitInfo cuInfo = (CompilationUnitInfo) info;
1123                 setUpSynchronization(cuInfo);
1124
1125                 IProblemRequestor requestor = cuInfo.fModel instanceof IProblemRequestor ? (IProblemRequestor) cuInfo.fModel
1126                                 : null;
1127
1128                 original.becomeWorkingCopy(requestor, getProgressMonitor());
1129                 cuInfo.fCopy = original;
1130
1131                 if (cuInfo.fModel instanceof CompilationUnitAnnotationModel) {
1132                         CompilationUnitAnnotationModel model = (CompilationUnitAnnotationModel) cuInfo.fModel;
1133                         model.setCompilationUnit(cuInfo.fCopy);
1134                 }
1135
1136                 if (cuInfo.fModel != null)
1137                         cuInfo.fModel
1138                                         .addAnnotationModelListener(fGlobalAnnotationModelListener);
1139
1140                 if (requestor instanceof IProblemRequestorExtension) {
1141                         IProblemRequestorExtension extension = (IProblemRequestorExtension) requestor;
1142                         extension.setIsActive(isHandlingTemporaryProblems());
1143                 }
1144
1145                 return cuInfo;
1146         }
1147
1148         private void setUpSynchronization(CompilationUnitInfo cuInfo) {
1149                 IDocument document = cuInfo.fTextFileBuffer.getDocument();
1150                 IAnnotationModel model = cuInfo.fModel;
1151
1152                 if (document instanceof ISynchronizable
1153                                 && model instanceof ISynchronizable) {
1154                         Object lock = ((ISynchronizable) document).getLockObject();
1155                         ((ISynchronizable) model).setLockObject(lock);
1156                 }
1157         }
1158
1159         /*
1160          * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#disposeFileInfo(java.lang.Object,
1161          *      org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo)
1162          */
1163         protected void disposeFileInfo(Object element, FileInfo info) {
1164                 if (info instanceof CompilationUnitInfo) {
1165                         CompilationUnitInfo cuInfo = (CompilationUnitInfo) info;
1166
1167                         try {
1168                                 cuInfo.fCopy.discardWorkingCopy();
1169                         } catch (JavaModelException x) {
1170                                 handleCoreException(x, x.getMessage());
1171                         }
1172
1173                         if (cuInfo.fModel != null)
1174                                 cuInfo.fModel
1175                                                 .removeAnnotationModelListener(fGlobalAnnotationModelListener);
1176                 }
1177                 super.disposeFileInfo(element, info);
1178         }
1179
1180         protected void commitWorkingCopy(IProgressMonitor monitor, Object element,
1181                         CompilationUnitInfo info, boolean overwrite) throws CoreException {
1182                 synchronized (info.fCopy) {
1183                         info.fCopy.reconcile();
1184                 }
1185
1186                 IDocument document = info.fTextFileBuffer.getDocument();
1187                 IResource resource = info.fCopy.getResource();
1188
1189                 Assert.isTrue(resource instanceof IFile);
1190                 if (!resource.exists()) {
1191                         // underlying resource has been deleted, just recreate file, ignore
1192                         // the rest
1193                         createFileFromDocument(monitor, (IFile) resource, document);
1194                         return;
1195                 }
1196
1197                 if (fSavePolicy != null)
1198                         fSavePolicy.preSave(info.fCopy);
1199
1200                 try {
1201
1202                         fIsAboutToSave = true;
1203                         info.fCopy.commitWorkingCopy(overwrite, monitor);
1204
1205                 } catch (CoreException x) {
1206                         // inform about the failure
1207                         fireElementStateChangeFailed(element);
1208                         throw x;
1209                 } catch (RuntimeException x) {
1210                         // inform about the failure
1211                         fireElementStateChangeFailed(element);
1212                         throw x;
1213                 } finally {
1214                         fIsAboutToSave = false;
1215                 }
1216
1217                 // If here, the dirty state of the editor will change to "not dirty".
1218                 // Thus, the state changing flag will be reset.
1219                 if (info.fModel instanceof AbstractMarkerAnnotationModel) {
1220                         AbstractMarkerAnnotationModel model = (AbstractMarkerAnnotationModel) info.fModel;
1221                         model.updateMarkers(document);
1222                 }
1223
1224                 if (fSavePolicy != null) {
1225                         ICompilationUnit unit = fSavePolicy.postSave(info.fCopy);
1226                         if (unit != null
1227                                         && info.fModel instanceof AbstractMarkerAnnotationModel) {
1228                                 IResource r = unit.getResource();
1229                                 IMarker[] markers = r.findMarkers(IMarker.MARKER, true,
1230                                                 IResource.DEPTH_ZERO);
1231                                 if (markers != null && markers.length > 0) {
1232                                         AbstractMarkerAnnotationModel model = (AbstractMarkerAnnotationModel) info.fModel;
1233                                         for (int i = 0; i < markers.length; i++)
1234                                                 model.updateMarker(document, markers[i], null);
1235                                 }
1236                         }
1237                 }
1238
1239         }
1240
1241         /*
1242          * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createSaveOperation(java.lang.Object,
1243          *      org.eclipse.jface.text.IDocument, boolean)
1244          */
1245         protected DocumentProviderOperation createSaveOperation(
1246                         final Object element, final IDocument document,
1247                         final boolean overwrite) throws CoreException {
1248                 // final FileInfo info= getFileInfo(element);
1249                 // if (info instanceof CompilationUnitInfo) {
1250                 // return new DocumentProviderOperation() {
1251                 // /*
1252                 // * @see
1253                 // org.eclipse.ui.editors.text.TextFileDocumentProvider.DocumentProviderOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
1254                 // */
1255                 // protected void execute(IProgressMonitor monitor) throws CoreException
1256                 // {
1257                 // commitWorkingCopy(monitor, element, (CompilationUnitInfo) info,
1258                 // overwrite);
1259                 // }
1260                 // /*
1261                 // * @see
1262                 // org.eclipse.ui.editors.text.TextFileDocumentProvider.DocumentProviderOperation#getSchedulingRule()
1263                 // */
1264                 // public ISchedulingRule getSchedulingRule() {
1265                 // if (info.fElement instanceof IFileEditorInput) {
1266                 // IFile file= ((IFileEditorInput) info.fElement).getFile();
1267                 // IResourceRuleFactory ruleFactory=
1268                 // ResourcesPlugin.getWorkspace().getRuleFactory();
1269                 // if (file == null || !file.exists())
1270                 // return ruleFactory.createRule(file);
1271                 // else
1272                 // return ruleFactory.modifyRule(file);
1273                 // } else
1274                 // return null;
1275                 // }
1276                 // };
1277                 // }
1278                 // return null;
1279                 final FileInfo info = getFileInfo(element);
1280                 if (info instanceof CompilationUnitInfo) {
1281                         return new DocumentProviderOperation() {
1282                                 /*
1283                                  * @see org.eclipse.ui.editors.text.TextFileDocumentProvider.DocumentProviderOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
1284                                  */
1285                                 protected void execute(IProgressMonitor monitor)
1286                                                 throws CoreException {
1287                                         commitWorkingCopy(monitor, element,
1288                                                         (CompilationUnitInfo) info, overwrite);
1289                                 }
1290
1291                                 /*
1292                                  * @see org.eclipse.ui.editors.text.TextFileDocumentProvider.DocumentProviderOperation#getSchedulingRule()
1293                                  */
1294                                 public ISchedulingRule getSchedulingRule() {
1295                                         if (info.fElement instanceof IFileEditorInput) {
1296                                                 IFile file = ((IFileEditorInput) info.fElement)
1297                                                                 .getFile();
1298                                                 return computeSchedulingRule(file);
1299                                         } else
1300                                                 return null;
1301                                 }
1302                         };
1303                 }
1304                 return null;
1305         }
1306
1307         /*
1308          * (non-Javadoc) Method declared on AbstractDocumentProvider
1309          */
1310         // protected IDocument createDocument(Object element) throws CoreException {
1311         // if (element instanceof IEditorInput) {
1312         // Document document = new PartiallySynchronizedDocument();
1313         // if (setDocumentContent(document, (IEditorInput) element,
1314         // getEncoding(element))) {
1315         // initializeDocument(document, (IEditorInput) element);
1316         //
1317         // //
1318         // // IDocument document = super.createDocument(element);
1319         // // if (document != null) {
1320         // // IDocumentPartitioner partitioner = null;
1321         // // if (element instanceof FileEditorInput) {
1322         // // IFile file = (IFile) ((FileEditorInput)
1323         // element).getAdapter(IFile.class);
1324         // // String filename = file.getLocation().toString();
1325         // // String extension = filename.substring(filename.lastIndexOf("."),
1326         // filename.length());
1327         // // // System.out.println(extension);
1328         // // if (extension.equalsIgnoreCase(".html") ||
1329         // extension.equalsIgnoreCase(".htm")) {
1330         // // // html
1331         // // partitioner = createHTMLPartitioner();
1332         // // } else if (extension.equalsIgnoreCase(".xml")) {
1333         // // // xml
1334         // // partitioner = createXMLPartitioner();
1335         // // } else if (extension.equalsIgnoreCase(".js")) {
1336         // // // javascript
1337         // // partitioner = createJavaScriptPartitioner();
1338         // // } else if (extension.equalsIgnoreCase(".css")) {
1339         // // // cascading style sheets
1340         // // partitioner = createCSSPartitioner();
1341         // // } else if (extension.equalsIgnoreCase(".tpl")) {
1342         // // // smarty ?
1343         // // partitioner = createSmartyPartitioner();
1344         // // } else if (extension.equalsIgnoreCase(".inc")) {
1345         // // // php include files ?
1346         // // partitioner = createIncludePartitioner();
1347         // // }
1348         // // }
1349         // //
1350         // // if (partitioner == null) {
1351         // // partitioner = createPHPPartitioner();
1352         // // }
1353         // // document.setDocumentPartitioner(partitioner);
1354         // // partitioner.connect(document);
1355         // }
1356         // return document;
1357         // }
1358         // return null;
1359         // }
1360         // /**
1361         // * Return a partitioner for .html files.
1362         // */
1363         // private IDocumentPartitioner createHTMLPartitioner() {
1364         // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
1365         // }
1366         //
1367         // private IDocumentPartitioner createIncludePartitioner() {
1368         // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
1369         // }
1370         //
1371         // private IDocumentPartitioner createJavaScriptPartitioner() {
1372         // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
1373         // }
1374         /**
1375          * Creates a line tracker working with the same line delimiters as the
1376          * document of the given element. Assumes the element to be managed by this
1377          * document provider.
1378          * 
1379          * @param element
1380          *            the element serving as blue print
1381          * @return a line tracker based on the same line delimiters as the element's
1382          *         document
1383          */
1384         public ILineTracker createLineTracker(Object element) {
1385                 return new DefaultLineTracker();
1386         }
1387
1388         // /**
1389         // * Return a partitioner for .php files.
1390         // */
1391         // private IDocumentPartitioner createPHPPartitioner() {
1392         // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
1393         // }
1394         //
1395         // private IDocumentPartitioner createSmartyPartitioner() {
1396         // return new DefaultPartitioner(getSmartyPartitionScanner(), TYPES);
1397         // }
1398         //
1399         // private IDocumentPartitioner createXMLPartitioner() {
1400         // return new DefaultPartitioner(getXMLPartitionScanner(), TYPES);
1401         // }
1402         //
1403         // /**
1404         // * Return a scanner for creating html partitions.
1405         // */
1406         // private PHPPartitionScanner getHTMLPartitionScanner() {
1407         // if (HTML_PARTITION_SCANNER == null)
1408         // HTML_PARTITION_SCANNER = new
1409         // PHPPartitionScanner(IPHPPartitionScannerConstants.HTML_FILE);
1410         // return HTML_PARTITION_SCANNER;
1411         // }
1412         // /**
1413         // * Return a scanner for creating php partitions.
1414         // */
1415         // private PHPPartitionScanner getPHPPartitionScanner() {
1416         // if (PHP_PARTITION_SCANNER == null)
1417         // PHP_PARTITION_SCANNER = new
1418         // PHPPartitionScanner(IPHPPartitionScannerConstants.PHP_FILE);
1419         // return PHP_PARTITION_SCANNER;
1420         // }
1421         //
1422         // /**
1423         // * Return a scanner for creating smarty partitions.
1424         // */
1425         // private PHPPartitionScanner getSmartyPartitionScanner() {
1426         // if (SMARTY_PARTITION_SCANNER == null)
1427         // SMARTY_PARTITION_SCANNER = new
1428         // PHPPartitionScanner(IPHPPartitionScannerConstants.SMARTY_FILE);
1429         // return SMARTY_PARTITION_SCANNER;
1430         // }
1431         //
1432         // /**
1433         // * Return a scanner for creating xml partitions.
1434         // */
1435         // private PHPPartitionScanner getXMLPartitionScanner() {
1436         // if (XML_PARTITION_SCANNER == null)
1437         // XML_PARTITION_SCANNER = new
1438         // PHPPartitionScanner(IPHPPartitionScannerConstants.XML_FILE);
1439         // return XML_PARTITION_SCANNER;
1440         // }
1441
1442         // protected void initializeDocument(IDocument document, IEditorInput
1443         // editorInput) {
1444         // if (document != null) {
1445         // JavaTextTools tools = PHPeclipsePlugin.getDefault().getJavaTextTools();
1446         // IDocumentPartitioner partitioner = null;
1447         // if (editorInput != null && editorInput instanceof FileEditorInput) {
1448         // IFile file = (IFile) ((FileEditorInput)
1449         // editorInput).getAdapter(IFile.class);
1450         // String filename = file.getLocation().toString();
1451         // String extension = filename.substring(filename.lastIndexOf("."),
1452         // filename.length());
1453         // partitioner = tools.createDocumentPartitioner(extension);
1454         // } else {
1455         // partitioner = tools.createDocumentPartitioner(".php");
1456         // }
1457         // document.setDocumentPartitioner(partitioner);
1458         // partitioner.connect(document);
1459         // }
1460         // }
1461
1462         /*
1463          * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#doResetDocument(java.lang.Object,
1464          *      org.eclipse.core.runtime.IProgressMonitor)
1465          */
1466         // protected void doResetDocument(Object element, IProgressMonitor monitor)
1467         // throws CoreException {
1468         // if (element == null)
1469         // return;
1470         //
1471         // ElementInfo elementInfo= getElementInfo(element);
1472         // if (elementInfo instanceof CompilationUnitInfo) {
1473         // CompilationUnitInfo info= (CompilationUnitInfo) elementInfo;
1474         //
1475         // IDocument document;
1476         // IStatus status= null;
1477         //
1478         // try {
1479         //
1480         // ICompilationUnit original= (ICompilationUnit)
1481         // info.fCopy.getOriginalElement();
1482         // IResource resource= original.getResource();
1483         // if (resource instanceof IFile) {
1484         //
1485         // IFile file= (IFile) resource;
1486         //
1487         // try {
1488         // refreshFile(file, monitor);
1489         // } catch (CoreException x) {
1490         // handleCoreException(x,
1491         // PHPEditorMessages.getString("CompilationUnitDocumentProvider.error.resetDocument"));
1492         // //$NON-NLS-1$
1493         // }
1494         //
1495         // IFileEditorInput input= new FileEditorInput(file);
1496         // document= super.createDocument(input);
1497         //
1498         // } else {
1499         // document= createEmptyDocument();
1500         // }
1501         //
1502         // } catch (CoreException x) {
1503         // document= createEmptyDocument();
1504         // status= x.getStatus();
1505         // }
1506         //
1507         // fireElementContentAboutToBeReplaced(element);
1508         //
1509         // removeUnchangedElementListeners(element, info);
1510         // info.fDocument.set(document.get());
1511         // info.fCanBeSaved= false;
1512         // info.fStatus= status;
1513         // addUnchangedElementListeners(element, info);
1514         //
1515         // fireElementContentReplaced(element);
1516         // fireElementDirtyStateChanged(element, false);
1517         //
1518         // } else {
1519         // super.doResetDocument(element, monitor);
1520         // }
1521         // }
1522         /*
1523          * @see AbstractDocumentProvider#resetDocument(Object)
1524          */
1525         // public void resetDocument(Object element) throws CoreException {
1526         // if (element == null)
1527         // return;
1528         //
1529         // ElementInfo elementInfo = getElementInfo(element);
1530         // if (elementInfo instanceof CompilationUnitInfo) {
1531         // CompilationUnitInfo info = (CompilationUnitInfo) elementInfo;
1532         //
1533         // IDocument document;
1534         // IStatus status = null;
1535         //
1536         // try {
1537         //
1538         // ICompilationUnit original = (ICompilationUnit)
1539         // info.fCopy.getOriginalElement();
1540         // IResource resource = original.getResource();
1541         // if (resource instanceof IFile) {
1542         //
1543         // IFile file = (IFile) resource;
1544         //
1545         // try {
1546         // refreshFile(file);
1547         // } catch (CoreException x) {
1548         // handleCoreException(x,
1549         // PHPEditorMessages.getString("PHPDocumentProvider.error.resetDocument"));
1550         // //$NON-NLS-1$
1551         // }
1552         //
1553         // IFileEditorInput input = new FileEditorInput(file);
1554         // document = super.createDocument(input);
1555         //
1556         // } else {
1557         // document = new Document();
1558         // }
1559         //
1560         // } catch (CoreException x) {
1561         // document = new Document();
1562         // status = x.getStatus();
1563         // }
1564         //
1565         // fireElementContentAboutToBeReplaced(element);
1566         //
1567         // removeUnchangedElementListeners(element, info);
1568         // info.fDocument.set(document.get());
1569         // info.fCanBeSaved = false;
1570         // info.fStatus = status;
1571         // addUnchangedElementListeners(element, info);
1572         //
1573         // fireElementContentReplaced(element);
1574         // fireElementDirtyStateChanged(element, false);
1575         //
1576         // } else {
1577         // super.resetDocument(element);
1578         // }
1579         // }
1580         /**
1581          * Saves the content of the given document to the given element. This is
1582          * only performed when this provider initiated the save.
1583          * 
1584          * @param monitor
1585          *            the progress monitor
1586          * @param element
1587          *            the element to which to save
1588          * @param document
1589          *            the document to save
1590          * @param overwrite
1591          *            <code>true</code> if the save should be enforced
1592          */
1593         public void saveDocumentContent(IProgressMonitor monitor, Object element,
1594                         IDocument document, boolean overwrite) throws CoreException {
1595                 if (!fIsAboutToSave)
1596                         return;
1597                 super.saveDocument(monitor, element, document, overwrite);
1598                 // if (!fIsAboutToSave)
1599                 // return;
1600                 //
1601                 // if (element instanceof IFileEditorInput) {
1602                 // IFileEditorInput input = (IFileEditorInput) element;
1603                 // try {
1604                 // String encoding = getEncoding(element);
1605                 // if (encoding == null)
1606                 // encoding = ResourcesPlugin.getEncoding();
1607                 // InputStream stream = new
1608                 // ByteArrayInputStream(document.get().getBytes(encoding));
1609                 // IFile file = input.getFile();
1610                 // file.setContents(stream, overwrite, true, monitor);
1611                 // } catch (IOException x) {
1612                 // IStatus s = new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID,
1613                 // IStatus.OK, x.getMessage(), x);
1614                 // throw new CoreException(s);
1615                 // }
1616                 // }
1617         }
1618
1619         /**
1620          * Returns the underlying resource for the given element.
1621          * 
1622          * @param the
1623          *            element
1624          * @return the underlying resource of the given element
1625          */
1626         // public IResource getUnderlyingResource(Object element) {
1627         // if (element instanceof IFileEditorInput) {
1628         // IFileEditorInput input = (IFileEditorInput) element;
1629         // return input.getFile();
1630         // }
1631         // return null;
1632         // }
1633         /*
1634          * @see net.sourceforge.phpdt.internal.ui.javaeditor.ICompilationUnitDocumentProvider#getWorkingCopy(java.lang.Object)
1635          */
1636         public ICompilationUnit getWorkingCopy(Object element) {
1637                 FileInfo fileInfo = getFileInfo(element);
1638                 if (fileInfo instanceof CompilationUnitInfo) {
1639                         CompilationUnitInfo info = (CompilationUnitInfo) fileInfo;
1640                         return info.fCopy;
1641                 }
1642                 return null;
1643         }
1644
1645         /*
1646          * @see net.sourceforge.phpdt.internal.ui.javaeditor.ICompilationUnitDocumentProvider#shutdown()
1647          */
1648         public void shutdown() {
1649                 WebUI.getDefault().getPreferenceStore()
1650                                 .removePropertyChangeListener(fPropertyListener);
1651                 Iterator e = getConnectedElementsIterator();
1652                 while (e.hasNext())
1653                         disconnect(e.next());
1654         }
1655
1656         /**
1657          * Returns the preference whether handling temporary problems is enabled.
1658          */
1659         protected boolean isHandlingTemporaryProblems() {
1660                 IPreferenceStore store = WebUI.getDefault()
1661                                 .getPreferenceStore();
1662                 return store.getBoolean(HANDLE_TEMPORARY_PROBLEMS);
1663         }
1664
1665         /**
1666          * Switches the state of problem acceptance according to the value in the
1667          * preference store.
1668          */
1669         protected void enableHandlingTemporaryProblems() {
1670                 boolean enable = isHandlingTemporaryProblems();
1671                 for (Iterator iter = getFileInfosIterator(); iter.hasNext();) {
1672                         FileInfo info = (FileInfo) iter.next();
1673                         if (info.fModel instanceof IProblemRequestorExtension) {
1674                                 IProblemRequestorExtension extension = (IProblemRequestorExtension) info.fModel;
1675                                 extension.setIsActive(enable);
1676                         }
1677                 }
1678         }
1679
1680         /**
1681          * Adds a listener that reports changes from all compilation unit annotation
1682          * models.
1683          */
1684         public void addGlobalAnnotationModelListener(
1685                         IAnnotationModelListener listener) {
1686                 fGlobalAnnotationModelListener.addListener(listener);
1687         }
1688
1689         /**
1690          * Removes the listener.
1691          */
1692         public void removeGlobalAnnotationModelListener(
1693                         IAnnotationModelListener listener) {
1694                 fGlobalAnnotationModelListener.removeListener(listener);
1695         }
1696
1697         /**
1698          * Computes the scheduling rule needed to create or modify a resource. If
1699          * the resource exists, its modify rule is returned. If it does not, the
1700          * resource hierarchy is iterated towards the workspace root to find the
1701          * first parent of <code>toCreateOrModify</code> that exists. Then the
1702          * 'create' rule for the last non-existing resource is returned.
1703          * <p>
1704          * XXX This is a workaround for
1705          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=67601
1706          * IResourceRuleFactory.createRule should iterate the hierarchy itself.
1707          * </p>
1708          * <p>
1709          * XXX to be replaced by call to
1710          * TextFileDocumentProvider.computeSchedulingRule after 3.0
1711          * </p>
1712          * 
1713          * @param toCreateOrModify
1714          *            the resource to create or modify
1715          * @return the minimal scheduling rule needed to modify or create a resource
1716          */
1717         protected ISchedulingRule computeSchedulingRule(IResource toCreateOrModify) {
1718                 IResourceRuleFactory factory = ResourcesPlugin.getWorkspace()
1719                                 .getRuleFactory();
1720                 if (toCreateOrModify.exists()) {
1721                         return factory.modifyRule(toCreateOrModify);
1722                 } else {
1723                         IResource parent = toCreateOrModify;
1724                         do {
1725                                 toCreateOrModify = parent;
1726                                 parent = toCreateOrModify.getParent();
1727                         } while (parent != null && !parent.exists());
1728
1729                         return factory.createRule(toCreateOrModify);
1730                 }
1731         }
1732 }