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