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