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