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