committing phphelp to Plug-ins for feature to fix build dependence for phpmanual.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPUnitEditor.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
3 import java.text.MessageFormat;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9
10 import net.sourceforge.phpdt.core.ICompilationUnit;
11 import net.sourceforge.phpdt.core.IJavaElement;
12 import net.sourceforge.phpdt.core.IJavaProject;
13 import net.sourceforge.phpdt.core.IMember;
14 import net.sourceforge.phpdt.core.ISourceRange;
15 import net.sourceforge.phpdt.core.ISourceReference;
16 import net.sourceforge.phpdt.core.JavaCore;
17 import net.sourceforge.phpdt.core.JavaModelException;
18 import net.sourceforge.phpdt.core.dom.CompilationUnit;
19 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
20 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
21 import net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction;
22 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
23 import net.sourceforge.phpdt.internal.ui.actions.IndentAction;
24 import net.sourceforge.phpdt.internal.ui.actions.RemoveBlockCommentAction;
25 import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference;
26 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
27 import net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner;
28 import net.sourceforge.phpdt.internal.ui.text.JavaIndenter;
29 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
30 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager;
31 import net.sourceforge.phpdt.internal.ui.text.SmartSemicolonAutoEditStrategy;
32 import net.sourceforge.phpdt.internal.ui.text.comment.CommentFormattingContext;
33 import net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener;
34 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
35 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
36 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI.ExitFlags;
37 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
38 import net.sourceforge.phpdt.ui.PreferenceConstants;
39 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
40 import net.sourceforge.phpdt.ui.text.JavaTextTools;
41 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
42 import net.sourceforge.phpeclipse.phpeditor.actions.RTrimAction;
43 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
44
45 import org.eclipse.core.resources.IFile;
46 import org.eclipse.core.resources.IWorkspaceRoot;
47 import org.eclipse.core.resources.ResourcesPlugin;
48 import org.eclipse.core.runtime.CoreException;
49 import org.eclipse.core.runtime.IPath;
50 import org.eclipse.core.runtime.IProgressMonitor;
51 import org.eclipse.core.runtime.IStatus;
52 import org.eclipse.core.runtime.Preferences;
53 import org.eclipse.jface.action.Action;
54 import org.eclipse.jface.action.IAction;
55 import org.eclipse.jface.action.IMenuManager;
56 import org.eclipse.jface.dialogs.ErrorDialog;
57 import org.eclipse.jface.dialogs.IMessageProvider;
58 import org.eclipse.jface.dialogs.MessageDialog;
59 import org.eclipse.jface.preference.IPreferenceStore;
60 import org.eclipse.jface.preference.PreferenceConverter;
61 import org.eclipse.jface.text.BadLocationException;
62 import org.eclipse.jface.text.DocumentCommand;
63 import org.eclipse.jface.text.IAutoEditStrategy;
64 import org.eclipse.jface.text.IDocument;
65 import org.eclipse.jface.text.ILineTracker;
66 import org.eclipse.jface.text.IRegion;
67 import org.eclipse.jface.text.ITextOperationTarget;
68 import org.eclipse.jface.text.ITextViewerExtension;
69 import org.eclipse.jface.text.ITypedRegion;
70 import org.eclipse.jface.text.IWidgetTokenKeeper;
71 import org.eclipse.jface.text.contentassist.ContentAssistant;
72 import org.eclipse.jface.text.contentassist.IContentAssistant;
73 import org.eclipse.jface.text.formatter.FormattingContextProperties;
74 import org.eclipse.jface.text.formatter.IFormattingContext;
75 import org.eclipse.jface.text.source.IOverviewRuler;
76 import org.eclipse.jface.text.source.ISourceViewer;
77 import org.eclipse.jface.text.source.IVerticalRuler;
78 import org.eclipse.jface.text.source.SourceViewerConfiguration;
79 import org.eclipse.jface.util.ListenerList;
80 import org.eclipse.jface.util.PropertyChangeEvent;
81 import org.eclipse.jface.window.Window;
82 import org.eclipse.swt.SWT;
83 import org.eclipse.swt.custom.VerifyKeyListener;
84 import org.eclipse.swt.events.VerifyEvent;
85 import org.eclipse.swt.graphics.Color;
86 import org.eclipse.swt.graphics.Point;
87 import org.eclipse.swt.graphics.RGB;
88 import org.eclipse.swt.widgets.Composite;
89 import org.eclipse.swt.widgets.Display;
90 import org.eclipse.swt.widgets.Shell;
91 import org.eclipse.ui.IEditorInput;
92 import org.eclipse.ui.IEditorPart;
93 import org.eclipse.ui.IFileEditorInput;
94 import org.eclipse.ui.IWorkbenchPage;
95 import org.eclipse.ui.IWorkbenchWindow;
96 import org.eclipse.ui.PlatformUI;
97 import org.eclipse.ui.actions.ActionContext;
98 import org.eclipse.ui.actions.ActionGroup;
99 import org.eclipse.ui.dialogs.SaveAsDialog;
100 import org.eclipse.ui.editors.text.IStorageDocumentProvider;
101 import org.eclipse.ui.part.FileEditorInput;
102 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
103 import org.eclipse.ui.texteditor.ContentAssistAction;
104 import org.eclipse.ui.texteditor.IDocumentProvider;
105 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
106 import org.eclipse.ui.texteditor.TextOperationAction;
107
108 /*******************************************************************************
109  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
110  * program and the accompanying materials are made available under the terms of
111  * the Common Public License v1.0 which accompanies this distribution, and is
112  * available at http://www.eclipse.org/legal/cpl-v10.html
113  *
114  * Contributors: IBM Corporation - Initial implementation
115  * www.phpeclipse.de
116  ******************************************************************************/
117 /**
118  * PHP specific text editor.
119  */
120 public class PHPUnitEditor extends PHPEditor { // implements
121         // IJavaReconcilingListener {
122         interface ITextConverter {
123                 void customizeDocumentCommand(IDocument document,
124                                 DocumentCommand command);
125         };
126
127         // class AdaptedSourceViewer extends JavaSourceViewer {
128         // private List fTextConverters;
129         //
130         // private boolean fIgnoreTextConverters = false;
131         //
132         // // private JavaCorrectionAssistant fCorrectionAssistant;
133         // public AdaptedSourceViewer(Composite parent, IVerticalRuler
134         // verticalRuler,
135         // IOverviewRuler overviewRuler, boolean showAnnotationsOverview,
136         // int styles, IPreferenceStore store) {
137         // super(parent, verticalRuler, overviewRuler, showAnnotationsOverview,
138         // styles, store);
139         // }
140         //
141         // // public AdaptedSourceViewer(Composite parent,
142         // // IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
143         // // boolean showAnnotationsOverview, int styles) {
144         // // super(parent, verticalRuler, overviewRuler,
145         // // showAnnotationsOverview, styles);
146         // // }
147         // public IContentAssistant getContentAssistant() {
148         // return fContentAssistant;
149         // }
150         //
151         // /*
152         // * @see ITextOperationTarget#doOperation(int)
153         // */
154         // public void doOperation(int operation) {
155         // if (getTextWidget() == null)
156         // return;
157         // switch (operation) {
158         // case CONTENTASSIST_PROPOSALS:
159         // String msg = fContentAssistant.showPossibleCompletions();
160         // setStatusLineErrorMessage(msg);
161         // return;
162         // // case CORRECTIONASSIST_PROPOSALS:
163         // // fCorrectionAssistant.showPossibleCompletions();
164         // // return;
165         // case UNDO:
166         // fIgnoreTextConverters = true;
167         // break;
168         // case REDO:
169         // fIgnoreTextConverters = true;
170         // break;
171         // }
172         // super.doOperation(operation);
173         // }
174         //
175         // /*
176         // * @see ITextOperationTarget#canDoOperation(int)
177         // */
178         // public boolean canDoOperation(int operation) {
179         // // if (operation == CORRECTIONASSIST_PROPOSALS)
180         // // return isEditable();
181         // return super.canDoOperation(operation);
182         // }
183         //
184         // /*
185         // * @see TextViewer#handleDispose()
186         // */
187         // protected void handleDispose() {
188         // // if (fCorrectionAssistant != null) {
189         // // fCorrectionAssistant.uninstall();
190         // // fCorrectionAssistant= null;
191         // // }
192         // super.handleDispose();
193         // }
194         //
195         // public void insertTextConverter(ITextConverter textConverter, int index)
196         // {
197         // throw new UnsupportedOperationException();
198         // }
199         //
200         // public void addTextConverter(ITextConverter textConverter) {
201         // if (fTextConverters == null) {
202         // fTextConverters = new ArrayList(1);
203         // fTextConverters.add(textConverter);
204         // } else if (!fTextConverters.contains(textConverter))
205         // fTextConverters.add(textConverter);
206         // }
207         //
208         // public void removeTextConverter(ITextConverter textConverter) {
209         // if (fTextConverters != null) {
210         // fTextConverters.remove(textConverter);
211         // if (fTextConverters.size() == 0)
212         // fTextConverters = null;
213         // }
214         // }
215         //
216         // /*
217         // * @see TextViewer#customizeDocumentCommand(DocumentCommand)
218         // */
219         // protected void customizeDocumentCommand(DocumentCommand command) {
220         // super.customizeDocumentCommand(command);
221         // if (!fIgnoreTextConverters && fTextConverters != null) {
222         // for (Iterator e = fTextConverters.iterator(); e.hasNext();)
223         // ((ITextConverter) e.next()).customizeDocumentCommand(getDocument(),
224         // command);
225         // }
226         // fIgnoreTextConverters = false;
227         // }
228         //
229         // // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
230         // public void updateIndentationPrefixes() {
231         // SourceViewerConfiguration configuration = getSourceViewerConfiguration();
232         // String[] types = configuration.getConfiguredContentTypes(this);
233         // for (int i = 0; i < types.length; i++) {
234         // String[] prefixes = configuration.getIndentPrefixes(this, types[i]);
235         // if (prefixes != null && prefixes.length > 0)
236         // setIndentPrefixes(prefixes, types[i]);
237         // }
238         // }
239         //
240         // /*
241         // * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
242         // */
243         // public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
244         // if (WorkbenchHelp.isContextHelpDisplayed())
245         // return false;
246         // return super.requestWidgetToken(requester);
247         // }
248         //
249         // // /*
250         // // * @see
251         // org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
252         // // */
253         // // public void configure(SourceViewerConfiguration configuration) {
254         // // super.configure(configuration);
255         // // // fCorrectionAssistant= new
256         // // // JavaCorrectionAssistant(CompilationUnitEditor.this);
257         // // // fCorrectionAssistant.install(this);
258         // // //TODO install SmartBracesAutoEditStrategy
259         // // // prependAutoEditStrategy(new SmartBracesAutoEditStrategy(this),
260         // // // IDocument.DEFAULT_CONTENT_TYPE);
261         // // }
262         // public void configure(SourceViewerConfiguration configuration) {
263         // super.configure(configuration);
264         // // fCorrectionAssistant= new
265         // JavaCorrectionAssistant(CompilationUnitEditor.this);
266         // // fCorrectionAssistant.install(this);
267         // IAutoEditStrategy smartSemi= new
268         // SmartSemicolonAutoEditStrategy(IPHPPartitions.PHP_PARTITIONING);
269         // prependAutoEditStrategy(smartSemi, IDocument.DEFAULT_CONTENT_TYPE);
270         // prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_DQ);
271         // prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_SQ);
272         // // prependAutoEditStrategy(smartSemi, IPHPPartitions.JAVA_CHARACTER);
273         // }
274         // };
275         class AdaptedSourceViewer extends JavaSourceViewer {
276
277                 private List fTextConverters;
278
279                 private boolean fIgnoreTextConverters = false;
280
281                 // private JavaCorrectionAssistant fCorrectionAssistant;
282
283                 public AdaptedSourceViewer(Composite parent,
284                                 IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
285                                 boolean showAnnotationsOverview, int styles,
286                                 IPreferenceStore store) {
287                         super(parent, verticalRuler, overviewRuler,
288                                         showAnnotationsOverview, styles, store);
289                 }
290
291                 public IContentAssistant getContentAssistant() {
292                         return fContentAssistant;
293                 }
294
295                 /*
296                  * @see ITextOperationTarget#doOperation(int)
297                  */
298                 public void doOperation(int operation) {
299
300                         if (getTextWidget() == null)
301                                 return;
302
303                         switch (operation) {
304                         case CONTENTASSIST_PROPOSALS:
305                                 String msg = fContentAssistant.showPossibleCompletions();
306                                 setStatusLineErrorMessage(msg);
307                                 return;
308                                 // case CORRECTIONASSIST_PROPOSALS:
309                                 // msg = fCorrectionAssistant.showPossibleCompletions();
310                                 // setStatusLineErrorMessage(msg);
311                                 // return;
312                         case UNDO:
313                                 fIgnoreTextConverters = true;
314                                 super.doOperation(operation);
315                                 fIgnoreTextConverters = false;
316                                 return;
317                         case REDO:
318                                 fIgnoreTextConverters = true;
319                                 super.doOperation(operation);
320                                 fIgnoreTextConverters = false;
321                                 return;
322                         }
323
324                         super.doOperation(operation);
325                 }
326
327                 /*
328                  * @see ITextOperationTarget#canDoOperation(int)
329                  */
330                 public boolean canDoOperation(int operation) {
331                         // if (operation == CORRECTIONASSIST_PROPOSALS)
332                         // return isEditable();
333
334                         return super.canDoOperation(operation);
335                 }
336
337                 /*
338                  * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
339                  * @since 3.0
340                  */
341                 public void unconfigure() {
342                         // if (fCorrectionAssistant != null) {
343                         // fCorrectionAssistant.uninstall();
344                         // fCorrectionAssistant = null;
345                         // }
346                         super.unconfigure();
347                 }
348
349                 public void insertTextConverter(ITextConverter textConverter, int index) {
350                         throw new UnsupportedOperationException();
351                 }
352
353                 public void addTextConverter(ITextConverter textConverter) {
354                         if (fTextConverters == null) {
355                                 fTextConverters = new ArrayList(1);
356                                 fTextConverters.add(textConverter);
357                         } else if (!fTextConverters.contains(textConverter))
358                                 fTextConverters.add(textConverter);
359                 }
360
361                 public void removeTextConverter(ITextConverter textConverter) {
362                         if (fTextConverters != null) {
363                                 fTextConverters.remove(textConverter);
364                                 if (fTextConverters.size() == 0)
365                                         fTextConverters = null;
366                         }
367                 }
368
369                 /*
370                  * @see TextViewer#customizeDocumentCommand(DocumentCommand)
371                  */
372                 protected void customizeDocumentCommand(DocumentCommand command) {
373                         super.customizeDocumentCommand(command);
374                         if (!fIgnoreTextConverters && fTextConverters != null) {
375                                 for (Iterator e = fTextConverters.iterator(); e.hasNext();)
376                                         ((ITextConverter) e.next()).customizeDocumentCommand(
377                                                         getDocument(), command);
378                         }
379                 }
380
381                 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
382                 public void updateIndentationPrefixes() {
383                         SourceViewerConfiguration configuration = getSourceViewerConfiguration();
384                         String[] types = configuration.getConfiguredContentTypes(this);
385                         for (int i = 0; i < types.length; i++) {
386                                 String[] prefixes = configuration.getIndentPrefixes(this,
387                                                 types[i]);
388                                 if (prefixes != null && prefixes.length > 0)
389                                         setIndentPrefixes(prefixes, types[i]);
390                         }
391                 }
392
393                 /*
394                  * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
395                  */
396                 public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
397                         if (PlatformUI.getWorkbench().getHelpSystem()
398                                         .isContextHelpDisplayed())
399                                 return false;
400                         return super.requestWidgetToken(requester);
401                 }
402
403                 /*
404                  * @see IWidgetTokenOwnerExtension#requestWidgetToken(IWidgetTokenKeeper,
405                  *      int)
406                  * @since 3.0
407                  */
408                 public boolean requestWidgetToken(IWidgetTokenKeeper requester,
409                                 int priority) {
410                         if (PlatformUI.getWorkbench().getHelpSystem()
411                                         .isContextHelpDisplayed())
412                                 return false;
413                         return super.requestWidgetToken(requester, priority);
414                 }
415
416                 /*
417                  * @see org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
418                  */
419                 public void configure(SourceViewerConfiguration configuration) {
420                         super.configure(configuration);
421                         // fCorrectionAssistant = new
422                         // JavaCorrectionAssistant(CompilationUnitEditor.this);
423                         // fCorrectionAssistant.install(this);
424                         IAutoEditStrategy smartSemi = new SmartSemicolonAutoEditStrategy(
425                                         IPHPPartitions.PHP_PARTITIONING);
426                         prependAutoEditStrategy(smartSemi, IDocument.DEFAULT_CONTENT_TYPE);
427                         prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_DQ);
428                         prependAutoEditStrategy(smartSemi, IPHPPartitions.PHP_STRING_SQ);
429                         prependAutoEditStrategy(smartSemi,
430                                         IPHPPartitions.PHP_STRING_HEREDOC);
431                 }
432
433                 /*
434                  * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
435                  * @since 3.0
436                  */
437                 public IFormattingContext createFormattingContext() {
438                         IFormattingContext context = new CommentFormattingContext();
439
440                         Map preferences;
441                         IJavaElement inputJavaElement = getInputJavaElement();
442                         IJavaProject javaProject = inputJavaElement != null ? inputJavaElement
443                                         .getJavaProject()
444                                         : null;
445                         if (javaProject == null)
446                                 preferences = new HashMap(JavaCore.getOptions());
447                         else
448                                 preferences = new HashMap(javaProject.getOptions(true));
449
450                         context.storeToMap(PreferenceConstants.getPreferenceStore(),
451                                         preferences, false);
452                         context.setProperty(
453                                         FormattingContextProperties.CONTEXT_PREFERENCES,
454                                         preferences);
455
456                         return context;
457                 }
458         }
459
460         /**
461          * Remembers data related to the current selection to be able to restore it
462          * later.
463          * 
464          * @since 3.0
465          */
466         private class RememberedSelection {
467                 /** The remembered selection start. */
468                 private RememberedOffset fStartOffset = new RememberedOffset();
469
470                 /** The remembered selection end. */
471                 private RememberedOffset fEndOffset = new RememberedOffset();
472
473                 /**
474                  * Remember current selection.
475                  */
476                 public void remember() {
477                         /*
478                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52257 This method
479                          * may be called inside an async call posted to the UI thread, so
480                          * protect against intermediate disposal of the editor.
481                          */
482                         ISourceViewer viewer = getSourceViewer();
483                         if (viewer != null) {
484                                 IRegion selection = getSignedSelection(viewer);
485                                 int startOffset = selection.getOffset();
486                                 int endOffset = startOffset + selection.getLength();
487
488                                 fStartOffset.setOffset(startOffset);
489                                 fEndOffset.setOffset(endOffset);
490                         }
491                 }
492
493                 /**
494                  * Restore remembered selection.
495                  */
496                 public void restore() {
497                         /*
498                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52257 This method
499                          * may be called inside an async call posted to the UI thread, so
500                          * protect against intermediate disposal of the editor.
501                          */
502                         if (getSourceViewer() == null)
503                                 return;
504
505                         try {
506
507                                 int startOffset, endOffset;
508                                 int revealStartOffset, revealEndOffset;
509                                 if (showsHighlightRangeOnly()) {
510                                         IJavaElement newStartElement = fStartOffset.getElement();
511                                         startOffset = fStartOffset
512                                                         .getRememberedOffset(newStartElement);
513                                         revealStartOffset = fStartOffset.getRevealOffset(
514                                                         newStartElement, startOffset);
515                                         if (revealStartOffset == -1)
516                                                 startOffset = -1;
517
518                                         IJavaElement newEndElement = fEndOffset.getElement();
519                                         endOffset = fEndOffset.getRememberedOffset(newEndElement);
520                                         revealEndOffset = fEndOffset.getRevealOffset(newEndElement,
521                                                         endOffset);
522                                         if (revealEndOffset == -1)
523                                                 endOffset = -1;
524                                 } else {
525                                         startOffset = fStartOffset.getOffset();
526                                         revealStartOffset = startOffset;
527                                         endOffset = fEndOffset.getOffset();
528                                         revealEndOffset = endOffset;
529                                 }
530
531                                 if (startOffset == -1) {
532                                         startOffset = endOffset; // fallback to caret offset
533                                         revealStartOffset = revealEndOffset;
534                                 }
535
536                                 if (endOffset == -1) {
537                                         endOffset = startOffset; // fallback to other offset
538                                         revealEndOffset = revealStartOffset;
539                                 }
540
541                                 IJavaElement element;
542                                 if (endOffset == -1) {
543                                         // fallback to element selection
544                                         element = fEndOffset.getElement();
545                                         if (element == null)
546                                                 element = fStartOffset.getElement();
547                                         if (element != null)
548                                                 setSelection(element);
549                                         return;
550                                 }
551
552                                 if (isValidSelection(revealStartOffset, revealEndOffset
553                                                 - revealStartOffset)
554                                                 && isValidSelection(startOffset, endOffset
555                                                                 - startOffset))
556                                         selectAndReveal(startOffset, endOffset - startOffset,
557                                                         revealStartOffset, revealEndOffset
558                                                                         - revealStartOffset);
559                         } finally {
560                                 fStartOffset.clear();
561                                 fEndOffset.clear();
562                         }
563                 }
564
565                 private boolean isValidSelection(int offset, int length) {
566                         IDocumentProvider provider = getDocumentProvider();
567                         if (provider != null) {
568                                 IDocument document = provider.getDocument(getEditorInput());
569                                 if (document != null) {
570                                         int end = offset + length;
571                                         int documentLength = document.getLength();
572                                         return 0 <= offset && offset <= documentLength && 0 <= end
573                                                         && end <= documentLength;
574                                 }
575                         }
576                         return false;
577                 }
578
579         }
580
581         /**
582          * Remembers additional data for a given offset to be able restore it later.
583          * 
584          * @since 3.0
585          */
586         private class RememberedOffset {
587                 /** Remembered line for the given offset */
588                 private int fLine;
589
590                 /** Remembered column for the given offset */
591                 private int fColumn;
592
593                 /** Remembered Java element for the given offset */
594                 private IJavaElement fElement;
595
596                 /** Remembered Java element line for the given offset */
597                 private int fElementLine;
598
599                 /**
600                  * Store visual properties of the given offset.
601                  * 
602                  * @param offset
603                  *            Offset in the document
604                  */
605                 public void setOffset(int offset) {
606                         try {
607                                 IDocument document = getSourceViewer().getDocument();
608                                 fLine = document.getLineOfOffset(offset);
609                                 fColumn = offset - document.getLineOffset(fLine);
610                                 fElement = getElementAt(offset, true);
611
612                                 fElementLine = -1;
613                                 if (fElement instanceof IMember) {
614                                         ISourceRange range = ((IMember) fElement).getNameRange();
615                                         if (range != null)
616                                                 fElementLine = document.getLineOfOffset(range
617                                                                 .getOffset());
618                                 }
619                                 if (fElementLine == -1)
620                                         fElementLine = document
621                                                         .getLineOfOffset(getOffset(fElement));
622                         } catch (BadLocationException e) {
623                                 // should not happen
624                                 PHPeclipsePlugin.log(e);
625                                 clear();
626                         } catch (JavaModelException e) {
627                                 // should not happen
628                                 PHPeclipsePlugin.log(e.getStatus());
629                                 clear();
630                         }
631                 }
632
633                 /**
634                  * Return offset recomputed from stored visual properties.
635                  * 
636                  * @return Offset in the document
637                  */
638                 public int getOffset() {
639                         IJavaElement newElement = getElement();
640
641                         int offset = getRememberedOffset(newElement);
642
643                         if (offset != -1 && !containsOffset(newElement, offset)
644                                         && (offset == 0 || !containsOffset(newElement, offset - 1)))
645                                 return -1;
646
647                         return offset;
648                 }
649
650                 /**
651                  * Return offset recomputed from stored visual properties.
652                  * 
653                  * @param newElement
654                  *            Enclosing element
655                  * @return Offset in the document
656                  */
657                 public int getRememberedOffset(IJavaElement newElement) {
658                         try {
659                                 if (newElement == null)
660                                         return -1;
661
662                                 IDocument document = getSourceViewer().getDocument();
663                                 int newElementLine = -1;
664                                 if (newElement instanceof IMember) {
665                                         ISourceRange range = ((IMember) newElement).getNameRange();
666                                         if (range != null)
667                                                 newElementLine = document.getLineOfOffset(range
668                                                                 .getOffset());
669                                 }
670                                 if (newElementLine == -1)
671                                         newElementLine = document
672                                                         .getLineOfOffset(getOffset(newElement));
673                                 if (newElementLine == -1)
674                                         return -1;
675
676                                 int newLine = fLine + newElementLine - fElementLine;
677                                 if (newLine < 0 || newLine >= document.getNumberOfLines())
678                                         return -1;
679                                 int maxColumn = document.getLineLength(newLine);
680                                 String lineDelimiter = document.getLineDelimiter(newLine);
681                                 if (lineDelimiter != null)
682                                         maxColumn = maxColumn - lineDelimiter.length();
683                                 int offset;
684                                 if (fColumn > maxColumn)
685                                         offset = document.getLineOffset(newLine) + maxColumn;
686                                 else
687                                         offset = document.getLineOffset(newLine) + fColumn;
688
689                                 return offset;
690                         } catch (BadLocationException e) {
691                                 // should not happen
692                                 PHPeclipsePlugin.log(e);
693                                 return -1;
694                         } catch (JavaModelException e) {
695                                 // should not happen
696                                 PHPeclipsePlugin.log(e.getStatus());
697                                 return -1;
698                         }
699                 }
700
701                 /**
702                  * Returns the offset used to reveal the given element based on the
703                  * given selection offset.
704                  * 
705                  * @param element
706                  *            the element
707                  * @param offset
708                  *            the selection offset
709                  * @return the offset to reveal the given element based on the given
710                  *         selection offset
711                  */
712                 public int getRevealOffset(IJavaElement element, int offset) {
713                         if (element == null || offset == -1)
714                                 return -1;
715
716                         if (containsOffset(element, offset)) {
717                                 if (offset > 0) {
718                                         IJavaElement alternateElement = getElementAt(offset, false);
719                                         if (element.getHandleIdentifier().equals(
720                                                         alternateElement.getParent().getHandleIdentifier()))
721                                                 return offset - 1; // Solves test case 2 from
722                                                                                         // https://bugs.eclipse.org/bugs/show_bug.cgi?id=47727#c3
723                                 }
724                                 return offset;
725                         } else if (offset > 0 && containsOffset(element, offset - 1))
726                                 return offset - 1; // Solves test case 1 from
727                                                                         // https://bugs.eclipse.org/bugs/show_bug.cgi?id=47727#c3
728
729                         return -1;
730                 }
731
732                 /**
733                  * Return Java element recomputed from stored visual properties.
734                  * 
735                  * @return Java element
736                  */
737                 public IJavaElement getElement() {
738                         if (fElement == null)
739                                 return null;
740
741                         return findElement(fElement);
742                 }
743
744                 /**
745                  * Clears the stored position
746                  */
747                 public void clear() {
748                         fLine = -1;
749                         fColumn = -1;
750                         fElement = null;
751                         fElementLine = -1;
752                 }
753
754                 /**
755                  * Does the given Java element contain the given offset?
756                  * 
757                  * @param element
758                  *            Java element
759                  * @param offset
760                  *            Offset
761                  * @return <code>true</code> iff the Java element contains the offset
762                  */
763                 private boolean containsOffset(IJavaElement element, int offset) {
764                         int elementOffset = getOffset(element);
765                         int elementLength = getLength(element);
766                         return (elementOffset > -1 && elementLength > -1) ? (offset >= elementOffset && offset < elementOffset
767                                         + elementLength)
768                                         : false;
769                 }
770
771                 /**
772                  * Returns the offset of the given Java element.
773                  * 
774                  * @param element
775                  *            Java element
776                  * @return Offset of the given Java element
777                  */
778                 private int getOffset(IJavaElement element) {
779                         if (element instanceof ISourceReference) {
780                                 ISourceReference sr = (ISourceReference) element;
781                                 try {
782                                         ISourceRange srcRange = sr.getSourceRange();
783                                         if (srcRange != null)
784                                                 return srcRange.getOffset();
785                                 } catch (JavaModelException e) {
786                                 }
787                         }
788                         return -1;
789                 }
790
791                 /**
792                  * Returns the length of the given Java element.
793                  * 
794                  * @param element
795                  *            Java element
796                  * @return Length of the given Java element
797                  */
798                 private int getLength(IJavaElement element) {
799                         if (element instanceof ISourceReference) {
800                                 ISourceReference sr = (ISourceReference) element;
801                                 try {
802                                         ISourceRange srcRange = sr.getSourceRange();
803                                         if (srcRange != null)
804                                                 return srcRange.getLength();
805                                 } catch (JavaModelException e) {
806                                 }
807                         }
808                         return -1;
809                 }
810
811                 /**
812                  * Returns the updated java element for the old java element.
813                  * 
814                  * @param element
815                  *            Old Java element
816                  * @return Updated Java element
817                  */
818                 private IJavaElement findElement(IJavaElement element) {
819
820                         if (element == null)
821                                 return null;
822
823                         IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
824                                         .getWorkingCopyManager();
825                         ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
826
827                         if (unit != null) {
828                                 try {
829
830                                         synchronized (unit) {
831                                                 // unit.reconcile(ICompilationUnit.NO_AST, false, null,
832                                                 // null);
833                                                 unit.reconcile();
834                                         }
835                                         IJavaElement[] findings = unit.findElements(element);
836                                         if (findings != null && findings.length > 0)
837                                                 return findings[0];
838
839                                 } catch (JavaModelException x) {
840                                         PHPeclipsePlugin.log(x.getStatus());
841                                         // nothing found, be tolerant and go on
842                                 }
843                         }
844
845                         return null;
846                 }
847
848         }
849
850         static class TabConverter implements ITextConverter {
851                 private int fTabRatio;
852
853                 private ILineTracker fLineTracker;
854
855                 public TabConverter() {
856                 }
857
858                 public void setNumberOfSpacesPerTab(int ratio) {
859                         fTabRatio = ratio;
860                 }
861
862                 public void setLineTracker(ILineTracker lineTracker) {
863                         fLineTracker = lineTracker;
864                 }
865
866                 private int insertTabString(StringBuffer buffer, int offsetInLine) {
867                         if (fTabRatio == 0)
868                                 return 0;
869                         int remainder = offsetInLine % fTabRatio;
870                         remainder = fTabRatio - remainder;
871                         for (int i = 0; i < remainder; i++)
872                                 buffer.append(' ');
873                         return remainder;
874                 }
875
876                 public void customizeDocumentCommand(IDocument document,
877                                 DocumentCommand command) {
878                         String text = command.text;
879                         if (text == null)
880                                 return;
881                         int index = text.indexOf('\t');
882                         if (index > -1) {
883                                 StringBuffer buffer = new StringBuffer();
884                                 fLineTracker.set(command.text);
885                                 int lines = fLineTracker.getNumberOfLines();
886                                 try {
887                                         for (int i = 0; i < lines; i++) {
888                                                 int offset = fLineTracker.getLineOffset(i);
889                                                 int endOffset = offset + fLineTracker.getLineLength(i);
890                                                 String line = text.substring(offset, endOffset);
891                                                 int position = 0;
892                                                 if (i == 0) {
893                                                         IRegion firstLine = document
894                                                                         .getLineInformationOfOffset(command.offset);
895                                                         position = command.offset - firstLine.getOffset();
896                                                 }
897                                                 int length = line.length();
898                                                 for (int j = 0; j < length; j++) {
899                                                         char c = line.charAt(j);
900                                                         if (c == '\t') {
901                                                                 position += insertTabString(buffer, position);
902                                                         } else {
903                                                                 buffer.append(c);
904                                                                 ++position;
905                                                         }
906                                                 }
907                                         }
908                                         command.text = buffer.toString();
909                                 } catch (BadLocationException x) {
910                                 }
911                         }
912                 }
913         };
914
915         private static class ExitPolicy implements LinkedPositionUI.ExitPolicy {
916                 final char fExitCharacter;
917
918                 public ExitPolicy(char exitCharacter) {
919                         fExitCharacter = exitCharacter;
920                 }
921
922                 /*
923                  * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitPolicy#doExit(org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionManager,
924                  *      org.eclipse.swt.events.VerifyEvent, int, int)
925                  */
926                 public ExitFlags doExit(LinkedPositionManager manager,
927                                 VerifyEvent event, int offset, int length) {
928                         if (event.character == fExitCharacter) {
929                                 if (manager.anyPositionIncludes(offset, length))
930                                         return new ExitFlags(LinkedPositionUI.COMMIT
931                                                         | LinkedPositionUI.UPDATE_CARET, false);
932                                 else
933                                         return new ExitFlags(LinkedPositionUI.COMMIT, true);
934                         }
935                         // Fix for #1380415 (toshihiro) start 
936                     switch (event.keyCode) {
937                     case SWT.ARROW_UP:
938                     case SWT.ARROW_DOWN:
939                       return new ExitFlags(LinkedPositionUI.COMMIT, true);
940                     case SWT.ARROW_LEFT:
941                     case SWT.ARROW_RIGHT:
942                        if (!manager.anyPositionIncludes(offset, length))
943                          return new ExitFlags(LinkedPositionUI.COMMIT, true);
944                        break;
945                     }
946                     // #1380415 end 
947                         switch (event.character) {
948                         case '\b':
949                                 if (manager.getFirstPosition().length == 0)
950                                         return new ExitFlags(0, false);
951                                 else
952                                         return null;
953                         case '\n':
954                         case '\r':
955                                 return new ExitFlags(LinkedPositionUI.COMMIT, true);
956                         default:
957                                 return null;
958                         }
959                 }
960         }
961
962         private static class BracketLevel {
963                 int fOffset;
964
965                 int fLength;
966
967                 LinkedPositionManager fManager;
968
969                 LinkedPositionUI fEditor;
970         };
971
972         private class BracketInserter implements VerifyKeyListener,
973                         LinkedPositionUI.ExitListener {
974                 private boolean fCloseBracketsPHP = true;
975
976                 private boolean fCloseStringsPHPDQ = true;
977
978                 private boolean fCloseStringsPHPSQ = true;
979
980                 private int fOffset;
981
982                 private int fLength;
983
984                 public void setCloseBracketsPHPEnabled(boolean enabled) {
985                         fCloseBracketsPHP = enabled;
986                 }
987
988                 public void setCloseStringsPHPDQEnabled(boolean enabled) {
989                         fCloseStringsPHPDQ = enabled;
990                 }
991
992                 public void setCloseStringsPHPSQEnabled(boolean enabled) {
993                         fCloseStringsPHPSQ = enabled;
994                 }
995
996                 private boolean hasIdentifierToTheRight(IDocument document, int offset) {
997                         try {
998                                 int end = offset;
999                                 IRegion endLine = document.getLineInformationOfOffset(end);
1000                                 int maxEnd = endLine.getOffset() + endLine.getLength();
1001                                 while (end != maxEnd
1002                                                 && Character.isWhitespace(document.getChar(end)))
1003                                         ++end;
1004                                 return end != maxEnd
1005                                                 && Scanner.isPHPIdentifierPart(document.getChar(end));
1006                         } catch (BadLocationException e) {
1007                                 // be conservative
1008                                 return true;
1009                         }
1010                 }
1011
1012                 private boolean hasIdentifierToTheLeft(IDocument document, int offset) {
1013                         try {
1014                                 int start = offset;
1015                                 IRegion startLine = document.getLineInformationOfOffset(start);
1016                                 int minStart = startLine.getOffset();
1017                                 while (start != minStart
1018                                                 && Character.isWhitespace(document.getChar(start - 1)))
1019                                         --start;
1020                                 return start != minStart
1021                                                 && Scanner.isPHPIdentifierPart(document
1022                                                                 .getChar(start - 1));
1023                         } catch (BadLocationException e) {
1024                                 return true;
1025                         }
1026                 }
1027
1028                 private boolean hasCharacterToTheLeft(IDocument document, int offset,
1029                                 char character) {
1030                         try {
1031                                 int start = offset;
1032                                 IRegion startLine = document.getLineInformationOfOffset(start);
1033                                 int minStart = startLine.getOffset();
1034                                 while (start != minStart
1035                                                 && Character.isWhitespace(document.getChar(start - 1)))
1036                                         --start;
1037                                 return start != minStart
1038                                                 && document.getChar(start - 1) == character;
1039                         } catch (BadLocationException e) {
1040                                 return false;
1041                         }
1042                 }
1043
1044                 private boolean hasCharacterToTheRight(IDocument document, int offset,
1045                                 char character) {
1046                         try {
1047                                 int end = offset;
1048                                 IRegion endLine = document.getLineInformationOfOffset(end);
1049                                 int maxEnd = endLine.getOffset() + endLine.getLength();
1050                                 while (end != maxEnd
1051                                                 && Character.isWhitespace(document.getChar(end)))
1052                                         ++end;
1053                                 return end != maxEnd && document.getChar(end) == character;
1054                         } catch (BadLocationException e) {
1055                                 // be conservative
1056                                 return true;
1057                         }
1058                 }
1059
1060                 /*
1061                  * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
1062                  */
1063                 public void verifyKey(VerifyEvent event) {
1064                         if (!event.doit)
1065                                 return;
1066                         final ISourceViewer sourceViewer = getSourceViewer();
1067                         IDocument document = sourceViewer.getDocument();
1068                         final Point selection = sourceViewer.getSelectedRange();
1069                         final int offset = selection.x;
1070                         final int length = selection.y;
1071                         try {
1072                                 ITypedRegion partition = document.getPartition(offset);
1073                                 String type = partition.getType();
1074                                 if (type.equals(IPHPPartitions.PHP_PARTITIONING)
1075                                                 || type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
1076                                         // you will get IDocument.DEFAULT_CONTENT_TYPE for both PHP
1077                                         // and HTML area
1078                                         switch (event.character) {
1079                                         case '(':
1080                                                 if (hasCharacterToTheRight(document, offset + length,
1081                                                                 '('))
1082                                                         return;
1083                                                 // fall through
1084                                         case '[':
1085                                                 if (!fCloseBracketsPHP)
1086                                                         return;
1087                                                 if (hasIdentifierToTheRight(document, offset + length))
1088                                                         return;
1089                                                 // fall through
1090                                         case '{':
1091                                                 if (!fCloseBracketsPHP)
1092                                                         return;
1093                                                 if (hasIdentifierToTheRight(document, offset + length))
1094                                                         return;
1095                                                 // fall through
1096                                         case '"':
1097                                                 if (event.character == '"') {
1098                                                         if (!fCloseStringsPHPDQ)
1099                                                                 return;
1100                                                         // changed for statements like echo "" print ""
1101                                                         // if (hasIdentifierToTheLeft(document, offset)
1102                                                         // ||
1103                                                         // hasIdentifierToTheRight(document, offset +
1104                                                         // length))
1105                                                         if (hasIdentifierToTheRight(document, offset
1106                                                                         + length))
1107                                                                 return;
1108                                                 }
1109                                                 // ITypedRegion partition=
1110                                                 // document.getPartition(offset);
1111                                                 // if (!
1112                                                 // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
1113                                                 // &&
1114                                                 // (partition.getOffset() != offset))
1115                                                 // return;
1116                                                 final char characterDQ = event.character;
1117                                                 final char closingCharacterDQ = getPeerCharacter(characterDQ);
1118                                                 final StringBuffer bufferDQ = new StringBuffer();
1119                                                 bufferDQ.append(characterDQ);
1120                                                 bufferDQ.append(closingCharacterDQ);
1121                                                 document.replace(offset, length, bufferDQ.toString());
1122                                                 LinkedPositionManager managerDQ = new LinkedPositionManager(
1123                                                                 document);
1124                                                 managerDQ.addPosition(offset + 1, 0);
1125                                                 fOffset = offset;
1126                                                 fLength = 2;
1127                                                 LinkedPositionUI editorDQ = new LinkedPositionUI(
1128                                                                 sourceViewer, managerDQ);
1129                                                 editorDQ.setCancelListener(this);
1130                                                 editorDQ.setExitPolicy(new ExitPolicy(
1131                                                                 closingCharacterDQ));
1132                                                 editorDQ.setFinalCaretOffset(offset + 2);
1133                                                 editorDQ.enter();
1134                                                 IRegion newSelectionDQ = editorDQ.getSelectedRegion();
1135                                                 sourceViewer.setSelectedRange(newSelectionDQ
1136                                                                 .getOffset(), newSelectionDQ.getLength());
1137                                                 event.doit = false;
1138                                                 break;
1139                                         case '\'':
1140                                                 if (event.character == '\'') {
1141                                                         if (!fCloseStringsPHPSQ)
1142                                                                 return;
1143                                                         // changed for statements like echo "" print ""
1144                                                         // if (hasIdentifierToTheLeft(document, offset)
1145                                                         // ||
1146                                                         // hasIdentifierToTheRight(document, offset +
1147                                                         // length))
1148                                                         if (hasIdentifierToTheRight(document, offset
1149                                                                         + length))
1150                                                                 return;
1151                                                 }
1152                                                 // ITypedRegion partition=
1153                                                 // document.getPartition(offset);
1154                                                 // if (!
1155                                                 // IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
1156                                                 // &&
1157                                                 // (partition.getOffset() != offset))
1158                                                 // return;
1159                                                 final char characterSQ = event.character;
1160                                                 final char closingCharacterSQ = getPeerCharacter(characterSQ);
1161                                                 final StringBuffer bufferSQ = new StringBuffer();
1162                                                 bufferSQ.append(characterSQ);
1163                                                 bufferSQ.append(closingCharacterSQ);
1164                                                 document.replace(offset, length, bufferSQ.toString());
1165                                                 LinkedPositionManager managerSQ = new LinkedPositionManager(
1166                                                                 document);
1167                                                 managerSQ.addPosition(offset + 1, 0);
1168                                                 fOffset = offset;
1169                                                 fLength = 2;
1170                                                 LinkedPositionUI editorSQ = new LinkedPositionUI(
1171                                                                 sourceViewer, managerSQ);
1172                                                 editorSQ.setCancelListener(this);
1173                                                 editorSQ.setExitPolicy(new ExitPolicy(
1174                                                                 closingCharacterSQ));
1175                                                 editorSQ.setFinalCaretOffset(offset + 2);
1176                                                 editorSQ.enter();
1177                                                 IRegion newSelectionSQ = editorSQ.getSelectedRegion();
1178                                                 sourceViewer.setSelectedRange(newSelectionSQ
1179                                                                 .getOffset(), newSelectionSQ.getLength());
1180                                                 event.doit = false;
1181                                         case '\r': { // insert linebreaks and new closing brace
1182                                                                         // after brace and return
1183                                                 if (!fCloseBracketsPHP) {
1184                                                         return;
1185                                                 }
1186                                                 if (hasCharacterToTheLeft(document, offset, '{')
1187                                                                 && hasCharacterToTheRight(document, offset, '}')) {
1188                                                         String lineDelimiter = StubUtility
1189                                                                         .getLineDelimiterFor(document);
1190                                                         int caretPos = sourceViewer.getTextWidget()
1191                                                                         .getCaretOffset();
1192                                                         final StringBuffer buffer = new StringBuffer(
1193                                                                         lineDelimiter);
1194                                                         // get indentation
1195                                                         IRegion line = document
1196                                                                         .getLineInformationOfOffset(offset);
1197                                                         String currentLine = document.get(line.getOffset(),
1198                                                                         line.getLength());
1199                                                         int index = 0;
1200                                                         int max = currentLine.length();
1201                                                         StringBuffer indent = new StringBuffer();
1202                                                         while (index < max
1203                                                                         && Character.isWhitespace(currentLine
1204                                                                                         .charAt(index))) {
1205                                                                 indent.append(currentLine.charAt(index));
1206                                                                 index++;
1207                                                         }
1208                                                         buffer.append(indent);
1209                                                         JavaHeuristicScanner scanner = new JavaHeuristicScanner(
1210                                                                         document);
1211                                                         JavaIndenter indenter = new JavaIndenter(document,
1212                                                                         scanner);
1213                                                         buffer.append(indenter.createIndent(1));
1214                                                         int cursorPos = buffer.length();
1215                                                         buffer.append(lineDelimiter);
1216                                                         buffer.append(indent);
1217                                                         document.replace(offset, length, buffer.toString());
1218                                                         sourceViewer.getTextWidget().setCaretOffset(
1219                                                                         caretPos + cursorPos);
1220                                                         event.doit = false;
1221                                                 }
1222                                         }
1223                                         }
1224                                 }
1225                         } catch (BadLocationException e) {
1226                         }
1227                 }
1228
1229                 /*
1230                  * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitListener#exit(boolean)
1231                  */
1232                 public void exit(boolean accept) {
1233                         if (accept)
1234                                 return;
1235                         // remove brackets
1236                         try {
1237                                 final ISourceViewer sourceViewer = getSourceViewer();
1238                                 IDocument document = sourceViewer.getDocument();
1239                                 document.replace(fOffset, fLength, null);
1240                         } catch (BadLocationException e) {
1241                         }
1242                 }
1243         }
1244
1245         /** The editor's save policy */
1246         protected ISavePolicy fSavePolicy;
1247
1248         /**
1249          * Listener to annotation model changes that updates the error tick in the
1250          * tab image
1251          */
1252         private JavaEditorErrorTickUpdater fJavaEditorErrorTickUpdater;
1253
1254         /** The editor's paint manager */
1255         // private PaintManager fPaintManager;
1256         /** The editor's bracket painter */
1257         // private BracketPainter fBracketPainter;
1258         /** The editor's bracket matcher */
1259         private PHPPairMatcher fBracketMatcher;
1260
1261         /** The editor's line painter */
1262         // private LinePainter fLinePainter;
1263         /** The editor's print margin ruler painter */
1264         // private PrintMarginPainter fPrintMarginPainter;
1265         /** The editor's problem painter */
1266         // private ProblemPainter fProblemPainter;
1267         /** The editor's tab converter */
1268         private TabConverter fTabConverter;
1269
1270         /** History for structure select action */
1271         // private SelectionHistory fSelectionHistory;
1272         /** The preference property change listener for php core. */
1273         // private IPropertyChangeListener fPropertyChangeListener = new
1274         // PropertyChangeListener();
1275         /** The remembered java element */
1276         private IJavaElement fRememberedElement;
1277
1278         /**
1279          * The remembered selection.
1280          * 
1281          * @since 3.0
1282          */
1283         private RememberedSelection fRememberedSelection = new RememberedSelection();
1284
1285         /** The remembered php element offset */
1286         private int fRememberedElementOffset;
1287
1288         /** The bracket inserter. */
1289         private BracketInserter fBracketInserter = new BracketInserter();
1290
1291         /** The standard action groups added to the menu */
1292         private GenerateActionGroup fGenerateActionGroup;
1293
1294         private CompositeActionGroup fContextMenuGroup;
1295
1296         // private class PropertyChangeListener implements IPropertyChangeListener {
1297         // /*
1298         // * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
1299         // */
1300         // public void
1301         // propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent
1302         // event) {
1303         // handlePreferencePropertyChanged(event);
1304         // }
1305         // }
1306         /* Preference key for code formatter tab size */
1307         private final static String CODE_FORMATTER_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE;
1308
1309         /** Preference key for matching brackets */
1310         // private final static String MATCHING_BRACKETS =
1311         // PreferenceConstants.EDITOR_MATCHING_BRACKETS;
1312         /** Preference key for matching brackets color */
1313         // private final static String MATCHING_BRACKETS_COLOR =
1314         // PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
1315         /** Preference key for highlighting current line */
1316         // private final static String CURRENT_LINE =
1317         // PreferenceConstants.EDITOR_CURRENT_LINE;
1318         /** Preference key for highlight color of current line */
1319         // private final static String CURRENT_LINE_COLOR =
1320         // PreferenceConstants.EDITOR_CURRENT_LINE_COLOR;
1321         /** Preference key for showing print marging ruler */
1322         // private final static String PRINT_MARGIN =
1323         // PreferenceConstants.EDITOR_PRINT_MARGIN;
1324         /** Preference key for print margin ruler color */
1325         // private final static String PRINT_MARGIN_COLOR =
1326         // PreferenceConstants.EDITOR_PRINT_MARGIN_COLOR;
1327         /** Preference key for print margin ruler column */
1328         // private final static String PRINT_MARGIN_COLUMN =
1329         // PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN;
1330         /** Preference key for inserting spaces rather than tabs */
1331         private final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
1332
1333         /** Preference key for error indication */
1334         // private final static String ERROR_INDICATION =
1335         // PreferenceConstants.EDITOR_PROBLEM_INDICATION;
1336         /** Preference key for error color */
1337         // private final static String ERROR_INDICATION_COLOR =
1338         // PreferenceConstants.EDITOR_PROBLEM_INDICATION_COLOR;
1339         /** Preference key for warning indication */
1340         // private final static String WARNING_INDICATION =
1341         // PreferenceConstants.EDITOR_WARNING_INDICATION;
1342         /** Preference key for warning color */
1343         // private final static String WARNING_INDICATION_COLOR =
1344         // PreferenceConstants.EDITOR_WARNING_INDICATION_COLOR;
1345         /** Preference key for task indication */
1346         private final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
1347
1348         /** Preference key for task color */
1349         private final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
1350
1351         /** Preference key for bookmark indication */
1352         private final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
1353
1354         /** Preference key for bookmark color */
1355         private final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
1356
1357         /** Preference key for search result indication */
1358         private final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
1359
1360         /** Preference key for search result color */
1361         private final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
1362
1363         /** Preference key for unknown annotation indication */
1364         private final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
1365
1366         /** Preference key for unknown annotation color */
1367         private final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
1368
1369         /** Preference key for linked position color */
1370         private final static String LINKED_POSITION_COLOR = PreferenceConstants.EDITOR_LINKED_POSITION_COLOR;
1371
1372         /** Preference key for shwoing the overview ruler */
1373         private final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
1374
1375         /** Preference key for error indication in overview ruler */
1376         private final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
1377
1378         /** Preference key for warning indication in overview ruler */
1379         private final static String WARNING_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
1380
1381         /** Preference key for task indication in overview ruler */
1382         private final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
1383
1384         /** Preference key for bookmark indication in overview ruler */
1385         private final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
1386
1387         /** Preference key for search result indication in overview ruler */
1388         private final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
1389
1390         /** Preference key for unknown annotation indication in overview ruler */
1391         private final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
1392
1393         /** Preference key for automatically closing double quoted strings */
1394         private final static String CLOSE_STRINGS_DQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_DQ_PHP;
1395
1396         /** Preference key for automatically closing single quoted strings */
1397         private final static String CLOSE_STRINGS_SQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_SQ_PHP;
1398
1399         /** Preference key for automatically wrapping Java strings */
1400         // private final static String WRAP_STRINGS =
1401         // PreferenceConstants.EDITOR_WRAP_STRINGS_DQ;
1402         /** Preference key for automatically closing brackets and parenthesis */
1403         private final static String CLOSE_BRACKETS_PHP = PreferenceConstants.EDITOR_CLOSE_BRACKETS_PHP;
1404
1405         /** Preference key for automatically closing phpdocs and comments */
1406         private final static String CLOSE_JAVADOCS = PreferenceConstants.EDITOR_CLOSE_JAVADOCS;
1407
1408         /** Preference key for automatically adding phpdoc tags */
1409         private final static String ADD_JAVADOC_TAGS = PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS;
1410
1411         /** Preference key for automatically formatting phpdocs */
1412         // private final static String FORMAT_JAVADOCS =
1413         // PreferenceConstants.EDITOR_FORMAT_JAVADOCS;
1414         /** Preference key for automatically closing strings */
1415         private final static String CLOSE_STRINGS_HTML = PreferenceConstants.EDITOR_CLOSE_STRINGS_HTML;
1416
1417         /** Preference key for automatically closing brackets and parenthesis */
1418         private final static String CLOSE_BRACKETS_HTML = PreferenceConstants.EDITOR_CLOSE_BRACKETS_HTML;
1419
1420         /** Preference key for smart paste */
1421         private final static String SMART_PASTE = PreferenceConstants.EDITOR_SMART_PASTE;
1422
1423         // private final static class AnnotationInfo {
1424         // public String fColorPreference;
1425         // public String fOverviewRulerPreference;
1426         // public String fEditorPreference;
1427         // };
1428         // private final static Map ANNOTATION_MAP;
1429         // static {
1430         //
1431         // AnnotationInfo info;
1432         // ANNOTATION_MAP = new HashMap();
1433         //
1434         // info = new AnnotationInfo();
1435         // info.fColorPreference = TASK_INDICATION_COLOR;
1436         // info.fOverviewRulerPreference = TASK_INDICATION_IN_OVERVIEW_RULER;
1437         // info.fEditorPreference = TASK_INDICATION;
1438         // ANNOTATION_MAP.put(AnnotationType.TASK, info);
1439         //
1440         // info = new AnnotationInfo();
1441         // info.fColorPreference = ERROR_INDICATION_COLOR;
1442         // info.fOverviewRulerPreference = ERROR_INDICATION_IN_OVERVIEW_RULER;
1443         // info.fEditorPreference = ERROR_INDICATION;
1444         // ANNOTATION_MAP.put(AnnotationType.ERROR, info);
1445         //
1446         // info = new AnnotationInfo();
1447         // info.fColorPreference = WARNING_INDICATION_COLOR;
1448         // info.fOverviewRulerPreference = WARNING_INDICATION_IN_OVERVIEW_RULER;
1449         // info.fEditorPreference = WARNING_INDICATION;
1450         // ANNOTATION_MAP.put(AnnotationType.WARNING, info);
1451         //
1452         // info = new AnnotationInfo();
1453         // info.fColorPreference = BOOKMARK_INDICATION_COLOR;
1454         // info.fOverviewRulerPreference = BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
1455         // info.fEditorPreference = BOOKMARK_INDICATION;
1456         // ANNOTATION_MAP.put(AnnotationType.BOOKMARK, info);
1457         //
1458         // info = new AnnotationInfo();
1459         // info.fColorPreference = SEARCH_RESULT_INDICATION_COLOR;
1460         // info.fOverviewRulerPreference =
1461         // SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
1462         // info.fEditorPreference = SEARCH_RESULT_INDICATION;
1463         // ANNOTATION_MAP.put(AnnotationType.SEARCH, info);
1464         //
1465         // info = new AnnotationInfo();
1466         // info.fColorPreference = UNKNOWN_INDICATION_COLOR;
1467         // info.fOverviewRulerPreference = UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
1468         // info.fEditorPreference = UNKNOWN_INDICATION;
1469         // ANNOTATION_MAP.put(AnnotationType.UNKNOWN, info);
1470         // };
1471         //
1472         // private final static AnnotationType[] ANNOTATION_LAYERS =
1473         // new AnnotationType[] {
1474         // AnnotationType.UNKNOWN,
1475         // AnnotationType.BOOKMARK,
1476         // AnnotationType.TASK,
1477         // AnnotationType.SEARCH,
1478         // AnnotationType.WARNING,
1479         // AnnotationType.ERROR };
1480         /**
1481          * Creates a new php unit editor.
1482          */
1483
1484         /**
1485          * Reconciling listeners.
1486          * 
1487          * @since 3.0
1488          */
1489         private ListenerList fReconcilingListeners = new ListenerList();
1490
1491         /**
1492          * Mutex for the reconciler. See
1493          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63898 for a description of
1494          * the problem.
1495          * <p>
1496          * TODO remove once the underlying problem is solved.
1497          * </p>
1498          */
1499         private final Object fReconcilerLock = new Object();
1500
1501         public PHPUnitEditor() {
1502                 super();
1503                 setDocumentProvider(PHPeclipsePlugin.getDefault()
1504                                 .getCompilationUnitDocumentProvider());
1505                 setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
1506                 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
1507                 setOutlinerContextMenuId("#PHPOutlinerContext"); //$NON-NLS-1$
1508                 // don't set help contextId, we install our own help context
1509                 fSavePolicy = null;
1510                 fJavaEditorErrorTickUpdater = new JavaEditorErrorTickUpdater(this);
1511         }
1512
1513         /*
1514          * @see AbstractTextEditor#createActions()
1515          */
1516         protected void createActions() {
1517                 super.createActions();
1518                 Action action;
1519                 // Action action= new
1520                 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1521                 // "CorrectionAssistProposal.", this, CORRECTIONASSIST_PROPOSALS);
1522                 // //$NON-NLS-1$
1523                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.CORRECTION_ASSIST_PROPOSALS);
1524                 // setAction("CorrectionAssistProposal", action); //$NON-NLS-1$
1525                 // markAsStateDependentAction("CorrectionAssistProposal", true);
1526                 // //$NON-NLS-1$
1527                 // // WorkbenchHelp.setHelp(action,
1528                 // IJavaHelpContextIds.QUICK_FIX_ACTION);
1529                 action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(),
1530                                 "ContentAssistProposal.", this); //$NON-NLS-1$
1531                 action
1532                                 .setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
1533                 setAction("ContentAssistProposal", action); //$NON-NLS-1$
1534                 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
1535                 // WorkbenchHelp.setHelp(action,
1536                 // IJavaHelpContextIds.CONTENT_ASSIST_ACTION);
1537                 // action = new
1538                 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1539                 // "ContentAssistContextInformation.", this,
1540                 // ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION); //$NON-NLS-1$
1541                 // action
1542                 // .setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
1543                 // setAction("ContentAssistContextInformation", action); //$NON-NLS-1$
1544                 // markAsStateDependentAction("ContentAssistContextInformation", true);
1545                 // //$NON-NLS-1$
1546                 // WorkbenchHelp.setHelp(action,
1547                 // IJavaHelpContextIds.PARAMETER_HINTS_ACTION);
1548                 // action= new
1549                 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
1550                 // "ContentAssistCompletePrefix.", this, CONTENTASSIST_COMPLETE_PREFIX);
1551                 // //$NON-NLS-1$
1552                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_COMPLETE_PREFIX);
1553                 // setAction("ContentAssistCompletePrefix", action); //$NON-NLS-1$
1554                 // markAsStateDependentAction("ContentAssistCompletePrefix", true);
1555                 // //$NON-NLS-1$
1556                 // // WorkbenchHelp.setHelp(action,
1557                 // IJavaHelpContextIds.PARAMETER_HINTS_ACTION);
1558                 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1559                                 "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$
1560                 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
1561                 setAction("Comment", action); //$NON-NLS-1$
1562                 markAsStateDependentAction("Comment", true); //$NON-NLS-1$
1563                 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.COMMENT_ACTION);
1564                 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1565                                 "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX); //$NON-NLS-1$
1566                 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
1567                 setAction("Uncomment", action); //$NON-NLS-1$
1568                 markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
1569                 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.UNCOMMENT_ACTION);
1570
1571                 action = new ToggleCommentAction(PHPEditorMessages.getResourceBundle(),
1572                                 "ToggleComment.", this); //$NON-NLS-1$
1573                 action
1574                                 .setActionDefinitionId(PHPEditorActionDefinitionIds.TOGGLE_COMMENT);
1575                 setAction("ToggleComment", action); //$NON-NLS-1$
1576                 markAsStateDependentAction("ToggleComment", true); //$NON-NLS-1$
1577                 // WorkbenchHelp.setHelp(action,
1578                 // IJavaHelpContextIds.TOGGLE_COMMENT_ACTION);
1579                 configureToggleCommentAction();
1580
1581                 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(),
1582                                 "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
1583                 action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
1584                 setAction("Format", action); //$NON-NLS-1$
1585                 markAsStateDependentAction("Format", true); //$NON-NLS-1$
1586                 markAsSelectionDependentAction("Format", true); //$NON-NLS-1$
1587                 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.FORMAT_ACTION);
1588
1589                 // action = new
1590                 // AddBlockCommentAction(PHPEditorMessages.getResourceBundle(),
1591                 // "AddBlockComment.", this); //$NON-NLS-1$
1592                 // action
1593                 // .setActionDefinitionId(PHPEditorActionDefinitionIds.ADD_BLOCK_COMMENT);
1594                 // setAction("AddBlockComment", action); //$NON-NLS-1$
1595                 // markAsStateDependentAction("AddBlockComment", true); //$NON-NLS-1$
1596                 // markAsSelectionDependentAction("AddBlockComment", true);
1597                 // //$NON-NLS-1$
1598                 // // WorkbenchHelp.setHelp(action,
1599                 // // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1600                 // action = new RemoveBlockCommentAction(
1601                 // PHPEditorMessages.getResourceBundle(), "RemoveBlockComment.", this);
1602                 // //$NON-NLS-1$
1603                 // action
1604                 // .setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_BLOCK_COMMENT);
1605                 // setAction("RemoveBlockComment", action); //$NON-NLS-1$
1606                 // markAsStateDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1607                 // markAsSelectionDependentAction("RemoveBlockComment", true);
1608                 // //$NON-NLS-1$
1609                 // WorkbenchHelp.setHelp(action,
1610                 // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1611                 action = new IndentAction(PHPEditorMessages.getResourceBundle(),
1612                                 "Indent.", this, false); //$NON-NLS-1$
1613                 action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
1614                 setAction("Indent", action); //$NON-NLS-1$
1615                 markAsStateDependentAction("Indent", true); //$NON-NLS-1$
1616                 markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
1617                 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
1618                 //
1619                 // action= new IndentAction(PHPEditorMessages.getResourceBundle(),
1620                 // "Indent.", this, true); //$NON-NLS-1$
1621                 // setAction("IndentOnTab", action); //$NON-NLS-1$
1622                 // markAsStateDependentAction("IndentOnTab", true); //$NON-NLS-1$
1623                 // markAsSelectionDependentAction("IndentOnTab", true); //$NON-NLS-1$
1624                 //
1625
1626                 action = new AddBlockCommentAction(PHPEditorMessages
1627                                 .getResourceBundle(), "AddBlockComment.", this); //$NON-NLS-1$
1628                 action
1629                                 .setActionDefinitionId(PHPEditorActionDefinitionIds.ADD_BLOCK_COMMENT);
1630                 setAction("AddBlockComment", action); //$NON-NLS-1$
1631                 markAsStateDependentAction("AddBlockComment", true); //$NON-NLS-1$
1632                 markAsSelectionDependentAction("AddBlockComment", true); //$NON-NLS-1$
1633                 // WorkbenchHelp.setHelp(action,
1634                 // IJavaHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
1635
1636                 action = new RemoveBlockCommentAction(PHPEditorMessages
1637                                 .getResourceBundle(), "RemoveBlockComment.", this); //$NON-NLS-1$
1638                 action
1639                                 .setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_BLOCK_COMMENT);
1640                 setAction("RemoveBlockComment", action); //$NON-NLS-1$
1641                 markAsStateDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1642                 markAsSelectionDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
1643                 // WorkbenchHelp.setHelp(action,
1644                 // IJavaHelpContextIds.REMOVE_BLOCK_COMMENT_ACTION);
1645
1646                 // action= new IndentAction(PHPEditorMessages.getResourceBundle(),
1647                 // "Indent.", this, false); //$NON-NLS-1$
1648                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.INDENT);
1649                 // setAction("Indent", action); //$NON-NLS-1$
1650                 // markAsStateDependentAction("Indent", true); //$NON-NLS-1$
1651                 // markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
1652                 // // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.INDENT_ACTION);
1653                 //
1654                 action = new IndentAction(PHPEditorMessages.getResourceBundle(),
1655                                 "Indent.", this, true); //$NON-NLS-1$
1656                 setAction("IndentOnTab", action); //$NON-NLS-1$
1657                 markAsStateDependentAction("IndentOnTab", true); //$NON-NLS-1$
1658                 markAsSelectionDependentAction("IndentOnTab", true); //$NON-NLS-1$
1659
1660                 if (getPreferenceStore().getBoolean(
1661                                 PreferenceConstants.EDITOR_SMART_TAB)) {
1662                         // don't replace Shift Right - have to make sure their enablement is
1663                         // mutually exclusive
1664                         // removeActionActivationCode(ITextEditorActionConstants.SHIFT_RIGHT);
1665                         setActionActivationCode("IndentOnTab", '\t', -1, SWT.NONE); //$NON-NLS-1$
1666                 }
1667                 fGenerateActionGroup = new GenerateActionGroup(this,
1668                                 ITextEditorActionConstants.GROUP_EDIT);
1669                 // ActionGroup rg= new RefactorActionGroup(this,
1670                 // ITextEditorActionConstants.GROUP_EDIT);
1671
1672                 // fActionGroups.addGroup(rg);
1673                 fActionGroups.addGroup(fGenerateActionGroup);
1674
1675                 // We have to keep the context menu group separate to have better
1676                 // control over positioning
1677                 fContextMenuGroup = new CompositeActionGroup(
1678                                 new ActionGroup[] { fGenerateActionGroup
1679                                 // rg,
1680                                 // new LocalHistoryActionGroup(this,
1681                                 // ITextEditorActionConstants.GROUP_EDIT)
1682                                 });
1683
1684         }
1685
1686         /*
1687          * @see JavaEditor#getElementAt(int)
1688          */
1689         protected IJavaElement getElementAt(int offset) {
1690                 return getElementAt(offset, true);
1691         }
1692
1693         /**
1694          * Returns the most narrow element including the given offset. If
1695          * <code>reconcile</code> is <code>true</code> the editor's input
1696          * element is reconciled in advance. If it is <code>false</code> this
1697          * method only returns a result if the editor's input element does not need
1698          * to be reconciled.
1699          * 
1700          * @param offset
1701          *            the offset included by the retrieved element
1702          * @param reconcile
1703          *            <code>true</code> if working copy should be reconciled
1704          */
1705         protected IJavaElement getElementAt(int offset, boolean reconcile) {
1706                 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
1707                                 .getWorkingCopyManager();
1708                 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
1709                 if (unit != null) {
1710                         try {
1711                                 if (reconcile) {
1712                                         synchronized (unit) {
1713                                                 unit.reconcile();
1714                                         }
1715                                         return unit.getElementAt(offset);
1716                                 } else if (unit.isConsistent())
1717                                         return unit.getElementAt(offset);
1718                         } catch (JavaModelException x) {
1719                                 PHPeclipsePlugin.log(x.getStatus());
1720                                 // nothing found, be tolerant and go on
1721                         }
1722                 }
1723                 return null;
1724         }
1725
1726         /*
1727          * @see JavaEditor#getCorrespondingElement(IJavaElement)
1728          */
1729         protected IJavaElement getCorrespondingElement(IJavaElement element) {
1730                 try {
1731                         return EditorUtility.getWorkingCopy(element, true);
1732                 } catch (JavaModelException x) {
1733                         PHPeclipsePlugin.log(x.getStatus());
1734                         // nothing found, be tolerant and go on
1735                 }
1736                 return null;
1737         }
1738
1739         public void createPartControl(Composite parent) {
1740                 super.createPartControl(parent);
1741                 // fPaintManager = new PaintManager(getSourceViewer());
1742                 LinePainter linePainter;
1743                 linePainter = new LinePainter(getSourceViewer());
1744                 linePainter.setHighlightColor(new Color(Display.getCurrent(), 225, 235,
1745                                 224));
1746                 // fPaintManager.addPainter(linePainter);
1747                 // if (isBracketHighlightingEnabled())
1748                 // startBracketHighlighting();
1749                 // if (isLineHighlightingEnabled())
1750                 // startLineHighlighting();
1751                 // if (isPrintMarginVisible())
1752                 // showPrintMargin();
1753                 // Iterator e = ANNOTATION_MAP.keySet().iterator();
1754                 // while (e.hasNext()) {
1755                 // AnnotationType type = (AnnotationType) e.next();
1756                 // if (isAnnotationIndicationEnabled(type))
1757                 // startAnnotationIndication(type);
1758                 // }
1759                 if (isTabConversionEnabled())
1760                         startTabConversion();
1761                 // if (isOverviewRulerVisible())
1762                 // showOverviewRuler();
1763                 //
1764                 // Preferences preferences =
1765                 // PHPeclipsePlugin.getDefault().getPluginPreferences();
1766                 // preferences.addPropertyChangeListener(fPropertyChangeListener);
1767                 IPreferenceStore preferenceStore = getPreferenceStore();
1768                 boolean closeBracketsPHP = preferenceStore
1769                                 .getBoolean(CLOSE_BRACKETS_PHP);
1770                 boolean closeStringsPHPDQ = preferenceStore
1771                                 .getBoolean(CLOSE_STRINGS_DQ_PHP);
1772                 boolean closeStringsPHPSQ = preferenceStore
1773                                 .getBoolean(CLOSE_STRINGS_SQ_PHP);
1774                 fBracketInserter.setCloseBracketsPHPEnabled(closeBracketsPHP);
1775                 fBracketInserter.setCloseStringsPHPDQEnabled(closeStringsPHPDQ);
1776                 fBracketInserter.setCloseStringsPHPSQEnabled(closeStringsPHPSQ);
1777                 ISourceViewer sourceViewer = getSourceViewer();
1778                 if (sourceViewer instanceof ITextViewerExtension)
1779                         ((ITextViewerExtension) sourceViewer)
1780                                         .prependVerifyKeyListener(fBracketInserter);
1781         }
1782
1783         private static char getPeerCharacter(char character) {
1784                 switch (character) {
1785                 case '(':
1786                         return ')';
1787                 case ')':
1788                         return '(';
1789                 case '[':
1790                         return ']';
1791                 case ']':
1792                         return '[';
1793                 case '"':
1794                         return character;
1795                 case '\'':
1796                         return character;
1797                 case '{':
1798                         return '}';
1799                 default:
1800                         throw new IllegalArgumentException();
1801                 }
1802         }
1803
1804         // private void startBracketHighlighting() {
1805         // if (fBracketPainter == null) {
1806         // ISourceViewer sourceViewer = getSourceViewer();
1807         // fBracketPainter = new BracketPainter(sourceViewer);
1808         // fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1809         // // fPaintManager.addPainter(fBracketPainter);
1810         // }
1811         // }
1812         //
1813         // private void stopBracketHighlighting() {
1814         // if (fBracketPainter != null) {
1815         // // fPaintManager.removePainter(fBracketPainter);
1816         // fBracketPainter.deactivate(true);
1817         // fBracketPainter.dispose();
1818         // fBracketPainter = null;
1819         // }
1820         // }
1821
1822         // private boolean isBracketHighlightingEnabled() {
1823         // IPreferenceStore store = getPreferenceStore();
1824         // return store.getBoolean(MATCHING_BRACKETS);
1825         // }
1826
1827         // private void startLineHighlighting() {
1828         // if (fLinePainter == null) {
1829         // ISourceViewer sourceViewer = getSourceViewer();
1830         // fLinePainter = new LinePainter(sourceViewer);
1831         // fLinePainter.setHighlightColor(getColor(CURRENT_LINE_COLOR));
1832         // // fPaintManager.addPainter(fLinePainter);
1833         // }
1834         // }
1835
1836         // private void stopLineHighlighting() {
1837         // if (fLinePainter != null) {
1838         // // fPaintManager.removePainter(fLinePainter);
1839         // fLinePainter.deactivate(true);
1840         // fLinePainter.dispose();
1841         // fLinePainter = null;
1842         // }
1843         // }
1844
1845         // private boolean isLineHighlightingEnabled() {
1846         // IPreferenceStore store = getPreferenceStore();
1847         // return store.getBoolean(CURRENT_LINE);
1848         // }
1849
1850         // private void showPrintMargin() {
1851         // if (fPrintMarginPainter == null) {
1852         // fPrintMarginPainter = new PrintMarginPainter(getSourceViewer());
1853         // fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
1854         // fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
1855         // // fPaintManager.addPainter(fPrintMarginPainter);
1856         // }
1857         // }
1858
1859         // private void hidePrintMargin() {
1860         // if (fPrintMarginPainter != null) {
1861         // // fPaintManager.removePainter(fPrintMarginPainter);
1862         // fPrintMarginPainter.deactivate(true);
1863         // fPrintMarginPainter.dispose();
1864         // fPrintMarginPainter = null;
1865         // }
1866         // }
1867
1868         // private boolean isPrintMarginVisible() {
1869         // IPreferenceStore store = getPreferenceStore();
1870         // return store.getBoolean(PRINT_MARGIN);
1871         // }
1872
1873         private int getTabSize() {
1874                 Preferences preferences = PHPeclipsePlugin.getDefault()
1875                                 .getPluginPreferences();
1876                 return preferences.getInt(CODE_FORMATTER_TAB_SIZE);
1877         }
1878
1879         private boolean isTabConversionEnabled() {
1880                 IPreferenceStore store = getPreferenceStore();
1881                 return store.getBoolean(SPACES_FOR_TABS);
1882         }
1883
1884         private Color getColor(String key) {
1885                 RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
1886                 return getColor(rgb);
1887         }
1888
1889         private Color getColor(RGB rgb) {
1890                 JavaTextTools textTools = PHPeclipsePlugin.getDefault()
1891                                 .getJavaTextTools();
1892                 return textTools.getColorManager().getColor(rgb);
1893         }
1894
1895         // private Color getColor(AnnotationType annotationType) {
1896         // AnnotationInfo info = (AnnotationInfo)
1897         // ANNOTATION_MAP.get(annotationType);
1898         // if (info != null)
1899         // return getColor(info.fColorPreference);
1900         // return null;
1901         // }
1902         public void dispose() {
1903                 ISourceViewer sourceViewer = getSourceViewer();
1904                 if (sourceViewer instanceof ITextViewerExtension)
1905                         ((ITextViewerExtension) sourceViewer)
1906                                         .removeVerifyKeyListener(fBracketInserter);
1907                 // if (fPropertyChangeListener != null) {
1908                 // Preferences preferences =
1909                 // PHPeclipsePlugin.getDefault().getPluginPreferences();
1910                 // preferences.removePropertyChangeListener(fPropertyChangeListener);
1911                 // fPropertyChangeListener = null;
1912                 // }
1913                 if (fJavaEditorErrorTickUpdater != null) {
1914                         fJavaEditorErrorTickUpdater.dispose();
1915                         fJavaEditorErrorTickUpdater = null;
1916                 }
1917                 // if (fSelectionHistory != null)
1918                 // fSelectionHistory.dispose();
1919                 // if (fPaintManager != null) {
1920                 // fPaintManager.dispose();
1921                 // fPaintManager = null;
1922                 // }
1923                 if (fActionGroups != null) {
1924                         fActionGroups.dispose();
1925                         fActionGroups = null;
1926                 }
1927                 super.dispose();
1928         }
1929
1930         // protected AnnotationType getAnnotationType(String preferenceKey) {
1931         // Iterator e = ANNOTATION_MAP.keySet().iterator();
1932         // while (e.hasNext()) {
1933         // AnnotationType type = (AnnotationType) e.next();
1934         // AnnotationInfo info = (AnnotationInfo) ANNOTATION_MAP.get(type);
1935         // if (info != null) {
1936         // if (preferenceKey.equals(info.fColorPreference)
1937         // || preferenceKey.equals(info.fEditorPreference)
1938         // || preferenceKey.equals(info.fOverviewRulerPreference))
1939         // return type;
1940         // }
1941         // }
1942         // return null;
1943         // }
1944         /*
1945          * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
1946          */
1947         protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1948                 try {
1949                         AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1950                         if (asv != null) {
1951                                 String p = event.getProperty();
1952                                 if (CLOSE_BRACKETS_PHP.equals(p)) {
1953                                         fBracketInserter
1954                                                         .setCloseBracketsPHPEnabled(getPreferenceStore()
1955                                                                         .getBoolean(p));
1956                                         return;
1957                                 }
1958                                 if (CLOSE_STRINGS_DQ_PHP.equals(p)) {
1959                                         fBracketInserter
1960                                                         .setCloseStringsPHPDQEnabled(getPreferenceStore()
1961                                                                         .getBoolean(p));
1962                                         return;
1963                                 }
1964                                 if (CLOSE_STRINGS_SQ_PHP.equals(p)) {
1965                                         fBracketInserter
1966                                                         .setCloseStringsPHPSQEnabled(getPreferenceStore()
1967                                                                         .getBoolean(p));
1968                                         return;
1969                                 }
1970                                 if (SPACES_FOR_TABS.equals(p)) {
1971                                         if (isTabConversionEnabled())
1972                                                 startTabConversion();
1973                                         else
1974                                                 stopTabConversion();
1975                                         return;
1976                                 }
1977                                 // if (MATCHING_BRACKETS.equals(p)) {
1978                                 // if (isBracketHighlightingEnabled())
1979                                 // startBracketHighlighting();
1980                                 // else
1981                                 // stopBracketHighlighting();
1982                                 // return;
1983                                 // }
1984                                 // if (MATCHING_BRACKETS_COLOR.equals(p)) {
1985                                 // if (fBracketPainter != null)
1986                                 // fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1987                                 // return;
1988                                 // }
1989                                 // if (CURRENT_LINE.equals(p)) {
1990                                 // if (isLineHighlightingEnabled())
1991                                 // startLineHighlighting();
1992                                 // else
1993                                 // stopLineHighlighting();
1994                                 // return;
1995                                 // }
1996                                 // if (CURRENT_LINE_COLOR.equals(p)) {
1997                                 // if (fLinePainter != null) {
1998                                 // stopLineHighlighting();
1999                                 // startLineHighlighting();
2000                                 // }
2001                                 // return;
2002                                 // }
2003                                 // if (PRINT_MARGIN.equals(p)) {
2004                                 // if (isPrintMarginVisible())
2005                                 // showPrintMargin();
2006                                 // else
2007                                 // hidePrintMargin();
2008                                 // return;
2009                                 // }
2010                                 // if (PRINT_MARGIN_COLOR.equals(p)) {
2011                                 // if (fPrintMarginPainter != null)
2012                                 // fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
2013                                 // return;
2014                                 // }
2015                                 // if (PRINT_MARGIN_COLUMN.equals(p)) {
2016                                 // if (fPrintMarginPainter != null)
2017                                 // fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
2018                                 // return;
2019                                 // }
2020                                 // if (OVERVIEW_RULER.equals(p)) {
2021                                 // if (isOverviewRulerVisible())
2022                                 // showOverviewRuler();
2023                                 // else
2024                                 // hideOverviewRuler();
2025                                 // return;
2026                                 // }
2027                                 // AnnotationType type = getAnnotationType(p);
2028                                 // if (type != null) {
2029                                 //
2030                                 // AnnotationInfo info = (AnnotationInfo)
2031                                 // ANNOTATION_MAP.get(type);
2032                                 // if (info.fColorPreference.equals(p)) {
2033                                 // Color color = getColor(type);
2034                                 // if (fProblemPainter != null) {
2035                                 // fProblemPainter.setColor(type, color);
2036                                 // fProblemPainter.paint(IPainter.CONFIGURATION);
2037                                 // }
2038                                 // setColorInOverviewRuler(type, color);
2039                                 // return;
2040                                 // }
2041                                 //
2042                                 // if (info.fEditorPreference.equals(p)) {
2043                                 // if (isAnnotationIndicationEnabled(type))
2044                                 // startAnnotationIndication(type);
2045                                 // else
2046                                 // stopAnnotationIndication(type);
2047                                 // return;
2048                                 // }
2049                                 //
2050                                 // if (info.fOverviewRulerPreference.equals(p)) {
2051                                 // if (isAnnotationIndicationInOverviewRulerEnabled(type))
2052                                 // showAnnotationIndicationInOverviewRuler(type, true);
2053                                 // else
2054                                 // showAnnotationIndicationInOverviewRuler(type, false);
2055                                 // return;
2056                                 // }
2057                                 // }
2058                                 IContentAssistant c = asv.getContentAssistant();
2059                                 if (c instanceof ContentAssistant)
2060                                         ContentAssistPreference.changeConfiguration(
2061                                                         (ContentAssistant) c, getPreferenceStore(), event);
2062                         }
2063                 } finally {
2064                         super.handlePreferenceStoreChanged(event);
2065                 }
2066         }
2067
2068         /*
2069          * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
2070          */
2071         protected void handlePreferencePropertyChanged(
2072                         org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
2073                 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2074                 if (asv != null) {
2075                         String p = event.getProperty();
2076                         if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
2077                                 asv.updateIndentationPrefixes();
2078                                 if (fTabConverter != null)
2079                                         fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2080                         }
2081                 }
2082                 super.handlePreferencePropertyChanged(event);
2083         }
2084
2085         /**
2086          * Handles a property change event describing a change of the php core's
2087          * preferences and updates the preference related editor properties.
2088          * 
2089          * @param event
2090          *            the property change event
2091          */
2092         // protected void
2093         // handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent
2094         // event) {
2095         // AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2096         // if (asv != null) {
2097         // String p = event.getProperty();
2098         // if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
2099         // asv.updateIndentationPrefixes();
2100         // if (fTabConverter != null)
2101         // fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2102         // }
2103         // }
2104         // }
2105         /*
2106          * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#createJavaSourceViewer(org.eclipse.swt.widgets.Composite,
2107          *      org.eclipse.jface.text.source.IVerticalRuler,
2108          *      org.eclipse.jface.text.source.IOverviewRuler, boolean, int)
2109          */
2110         protected ISourceViewer createJavaSourceViewer(Composite parent,
2111                         IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
2112                         boolean isOverviewRulerVisible, int styles, IPreferenceStore store) {
2113                 return new AdaptedSourceViewer(parent, verticalRuler, overviewRuler,
2114                                 isOverviewRulerVisible, styles, store);
2115         }
2116
2117         // protected ISourceViewer createJavaSourceViewer(Composite parent,
2118         // IVerticalRuler ruler, int styles) {
2119         // return new AdaptedSourceViewer(parent, ruler, styles);
2120         // }
2121         private boolean isValidSelection(int offset, int length) {
2122                 IDocumentProvider provider = getDocumentProvider();
2123                 if (provider != null) {
2124                         IDocument document = provider.getDocument(getEditorInput());
2125                         if (document != null) {
2126                                 int end = offset + length;
2127                                 int documentLength = document.getLength();
2128                                 return 0 <= offset && offset <= documentLength && 0 <= end
2129                                                 && end <= documentLength;
2130                         }
2131                 }
2132                 return false;
2133         }
2134
2135         /*
2136          * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#getInputElement()
2137          */
2138         protected IJavaElement getInputJavaElement() {
2139                 return PHPeclipsePlugin.getDefault().getWorkingCopyManager()
2140                                 .getWorkingCopy(getEditorInput());
2141         }
2142
2143         /*
2144          * @see AbstractTextEditor#editorContextMenuAboutToShow(IMenuManager)
2145          */
2146         public void editorContextMenuAboutToShow(IMenuManager menu) {
2147                 super.editorContextMenuAboutToShow(menu);
2148                 ActionContext context = new ActionContext(getSelectionProvider()
2149                                 .getSelection());
2150                 fContextMenuGroup.setContext(context);
2151                 fContextMenuGroup.fillContextMenu(menu);
2152                 fContextMenuGroup.setContext(null);
2153         }
2154
2155         /*
2156          * @see JavaEditor#setOutlinePageInput(JavaOutlinePage, IEditorInput)
2157          */
2158         protected void setOutlinePageInput(JavaOutlinePage page, IEditorInput input) {
2159                 if (page != null) {
2160                         IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
2161                                         .getWorkingCopyManager();
2162                         page.setInput(manager.getWorkingCopy(input));
2163                 }
2164         }
2165
2166         /*
2167          * @see AbstractTextEditor#performSaveOperation(WorkspaceModifyOperation,
2168          *      IProgressMonitor)
2169          */
2170         // protected void performSaveOperation(WorkspaceModifyOperation operation,
2171         // IProgressMonitor progressMonitor) {
2172         // IDocumentProvider p = getDocumentProvider();
2173         // if (p instanceof PHPDocumentProvider) {
2174         // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2175         // cp.setSavePolicy(fSavePolicy);
2176         // }
2177         //
2178         // try {
2179         // super.performSaveOperation(operation, progressMonitor);
2180         // } finally {
2181         // if (p instanceof PHPDocumentProvider) {
2182         // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2183         // cp.setSavePolicy(null);
2184         // }
2185         // }
2186         // }
2187         /*
2188          * @see AbstractTextEditor#doSave(IProgressMonitor)
2189          */
2190         public void doSave(IProgressMonitor progressMonitor) {
2191
2192                 IDocumentProvider p = getDocumentProvider();
2193                 if (p == null) {
2194                         // editor has been closed
2195                         return;
2196                 }
2197
2198                 if (p.isDeleted(getEditorInput())) {
2199
2200                         if (isSaveAsAllowed()) {
2201
2202                                 /*
2203                                  * 1GEUSSR: ITPUI:ALL - User should never loose changes made in
2204                                  * the editors. Changed Behavior to make sure that if called
2205                                  * inside a regular save (because of deletion of input element)
2206                                  * there is a way to report back to the caller.
2207                                  */
2208                                 performSaveAs(progressMonitor);
2209
2210                         } else {
2211
2212                                 /*
2213                                  * 1GF5YOX: ITPJUI:ALL - Save of delete file claims it's still
2214                                  * there Missing resources.
2215                                  */
2216                                 Shell shell = getSite().getShell();
2217                                 MessageDialog
2218                                                 .openError(
2219                                                                 shell,
2220                                                                 PHPEditorMessages
2221                                                                                 .getString("PHPUnitEditor.error.saving.title1"), PHPEditorMessages.getString("PHPUnitEditor.error.saving.message1")); //$NON-NLS-1$ //$NON-NLS-2$
2222                         }
2223
2224                 } else {
2225                         if (getPreferenceStore().getBoolean(
2226                                         PreferenceConstants.EDITOR_P_RTRIM_ON_SAVE)) {
2227                                 RTrimAction trimAction = new RTrimAction();
2228                                 trimAction.setActiveEditor(null, getSite().getPage()
2229                                                 .getActiveEditor());
2230                                 trimAction.run(null);
2231                         }
2232
2233                         setStatusLineErrorMessage(null);
2234
2235                         updateState(getEditorInput());
2236                         validateState(getEditorInput());
2237
2238                         IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
2239                                         .getWorkingCopyManager();
2240                         ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
2241
2242                         if (unit != null) {
2243                                 synchronized (unit) {
2244                                         performSave(false, progressMonitor);
2245                                 }
2246                         } else
2247                                 performSave(false, progressMonitor);
2248                 }
2249         }
2250
2251         public boolean isSaveAsAllowed() {
2252                 return true;
2253         }
2254
2255         /**
2256          * The compilation unit editor implementation of this
2257          * <code>AbstractTextEditor</code> method asks the user for the workspace
2258          * path of a file resource and saves the document there. See
2259          * http://dev.eclipse.org/bugs/show_bug.cgi?id=6295
2260          * 
2261          * @param progressMonitor
2262          *            the progress monitor
2263          */
2264         protected void performSaveAs(IProgressMonitor progressMonitor) {
2265
2266                 Shell shell = getSite().getShell();
2267                 IEditorInput input = getEditorInput();
2268
2269                 SaveAsDialog dialog = new SaveAsDialog(shell);
2270
2271                 IFile original = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input)
2272                                 .getFile()
2273                                 : null;
2274                 if (original != null)
2275                         dialog.setOriginalFile(original);
2276
2277                 dialog.create();
2278
2279                 IDocumentProvider provider = getDocumentProvider();
2280                 if (provider == null) {
2281                         // editor has been programmatically closed while the dialog was open
2282                         return;
2283                 }
2284
2285                 if (provider.isDeleted(input) && original != null) {
2286                         String message = PHPEditorMessages
2287                                         .getFormattedString(
2288                                                         "CompilationUnitEditor.warning.save.delete", new Object[] { original.getName() }); //$NON-NLS-1$
2289                         dialog.setErrorMessage(null);
2290                         dialog.setMessage(message, IMessageProvider.WARNING);
2291                 }
2292
2293                 if (dialog.open() == Window.CANCEL) {
2294                         if (progressMonitor != null)
2295                                 progressMonitor.setCanceled(true);
2296                         return;
2297                 }
2298
2299                 IPath filePath = dialog.getResult();
2300                 if (filePath == null) {
2301                         if (progressMonitor != null)
2302                                 progressMonitor.setCanceled(true);
2303                         return;
2304                 }
2305
2306                 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
2307                 IFile file = workspaceRoot.getFile(filePath);
2308                 final IEditorInput newInput = new FileEditorInput(file);
2309
2310                 boolean success = false;
2311                 try {
2312
2313                         provider.aboutToChange(newInput);
2314                         getDocumentProvider().saveDocument(progressMonitor, newInput,
2315                                         getDocumentProvider().getDocument(getEditorInput()), true);
2316                         success = true;
2317
2318                 } catch (CoreException x) {
2319                         IStatus status = x.getStatus();
2320                         if (status == null || status.getSeverity() != IStatus.CANCEL)
2321                                 ErrorDialog
2322                                                 .openError(
2323                                                                 shell,
2324                                                                 PHPEditorMessages
2325                                                                                 .getString("CompilationUnitEditor.error.saving.title2"), PHPEditorMessages.getString("CompilationUnitEditor.error.saving.message2"), x.getStatus()); //$NON-NLS-1$ //$NON-NLS-2$
2326                 } finally {
2327                         provider.changed(newInput);
2328                         if (success)
2329                                 setInput(newInput);
2330                 }
2331
2332                 if (progressMonitor != null)
2333                         progressMonitor.setCanceled(!success);
2334         }
2335
2336         /*
2337          * @see AbstractTextEditor#doSetInput(IEditorInput)
2338          */
2339         protected void doSetInput(IEditorInput input) throws CoreException {
2340                 super.doSetInput(input);
2341                 configureTabConverter();
2342                 configureToggleCommentAction();
2343         }
2344
2345         // /*
2346         // * @see
2347         // net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#installOverrideIndicator(boolean)
2348         // * @since 3.0
2349         // */
2350         // protected void installOverrideIndicator(boolean waitForReconcilation) {
2351         // IAnnotationModel model=
2352         // getDocumentProvider().getAnnotationModel(getEditorInput());
2353         // if (!waitForReconcilation)
2354         // super.installOverrideIndicator(false);
2355         // else {
2356         // uninstallOverrideIndicator();
2357         // IJavaElement inputElement= getInputJavaElement();
2358         // if (model == null || inputElement == null)
2359         // return;
2360         //
2361         // fOverrideIndicatorManager= new OverrideIndicatorManager(model,
2362         // inputElement, null);
2363         // addReconcileListener(fOverrideIndicatorManager);
2364         // }
2365         // }
2366         //
2367         // /*
2368         // * @see
2369         // net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#uninstallOverrideIndicator()
2370         // * @since 3.0
2371         // */
2372         // protected void uninstallOverrideIndicator() {
2373         // if (fOverrideIndicatorManager != null)
2374         // removeReconcileListener(fOverrideIndicatorManager);
2375         // super.uninstallOverrideIndicator();
2376         // }
2377
2378         /**
2379          * Configures the toggle comment action
2380          * 
2381          * @since 3.0
2382          */
2383         private void configureToggleCommentAction() {
2384                 IAction action = getAction("ToggleComment"); //$NON-NLS-1$
2385                 if (action instanceof ToggleCommentAction) {
2386                         ISourceViewer sourceViewer = getSourceViewer();
2387                         SourceViewerConfiguration configuration = getSourceViewerConfiguration();
2388                         ((ToggleCommentAction) action).configure(sourceViewer,
2389                                         configuration);
2390                 }
2391         }
2392
2393         // private void configureTabConverter() {
2394         // if (fTabConverter != null) {
2395         // IDocumentProvider provider = getDocumentProvider();
2396         // if (provider instanceof PHPDocumentProvider) {
2397         // PHPDocumentProvider cup = (PHPDocumentProvider) provider;
2398         // fTabConverter.setLineTracker(cup.createLineTracker(getEditorInput()));
2399         // }
2400         // }
2401         // }
2402         private void configureTabConverter() {
2403                 if (fTabConverter != null) {
2404                         IDocumentProvider provider = getDocumentProvider();
2405                         if (provider instanceof ICompilationUnitDocumentProvider) {
2406                                 ICompilationUnitDocumentProvider cup = (ICompilationUnitDocumentProvider) provider;
2407                                 fTabConverter.setLineTracker(cup
2408                                                 .createLineTracker(getEditorInput()));
2409                         }
2410                 }
2411         }
2412
2413         private void startTabConversion() {
2414                 if (fTabConverter == null) {
2415                         fTabConverter = new TabConverter();
2416                         configureTabConverter();
2417                         fTabConverter.setNumberOfSpacesPerTab(getTabSize());
2418                         AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2419                         asv.addTextConverter(fTabConverter);
2420                         // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
2421                         asv.updateIndentationPrefixes();
2422                 }
2423         }
2424
2425         private void stopTabConversion() {
2426                 if (fTabConverter != null) {
2427                         AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
2428                         asv.removeTextConverter(fTabConverter);
2429                         // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
2430                         asv.updateIndentationPrefixes();
2431                         fTabConverter = null;
2432                 }
2433         }
2434
2435         /*
2436          * @see org.eclipse.ui.texteditor.AbstractTextEditor#performSave(boolean,
2437          *      org.eclipse.core.runtime.IProgressMonitor)
2438          */
2439         protected void performSave(boolean overwrite,
2440                         IProgressMonitor progressMonitor) {
2441                 // IDocumentProvider p = getDocumentProvider();
2442                 // if (p instanceof PHPDocumentProvider) {
2443                 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2444                 // cp.setSavePolicy(fSavePolicy);
2445                 // }
2446                 // try {
2447                 // super.performSave(overwrite, progressMonitor);
2448                 // } finally {
2449                 // if (p instanceof PHPDocumentProvider) {
2450                 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
2451                 // cp.setSavePolicy(null);
2452                 // }
2453                 // }
2454
2455                 IDocumentProvider p = getDocumentProvider();
2456                 if (p instanceof ICompilationUnitDocumentProvider) {
2457                         ICompilationUnitDocumentProvider cp = (ICompilationUnitDocumentProvider) p;
2458                         cp.setSavePolicy(fSavePolicy);
2459                 }
2460                 try {
2461                         super.performSave(overwrite, progressMonitor);
2462                 } finally {
2463                         if (p instanceof ICompilationUnitDocumentProvider) {
2464                                 ICompilationUnitDocumentProvider cp = (ICompilationUnitDocumentProvider) p;
2465                                 cp.setSavePolicy(null);
2466                         }
2467                 }
2468         }
2469
2470         /*
2471          * @see AbstractTextEditor#doSaveAs
2472          */
2473         public void doSaveAs() {
2474                 if (askIfNonWorkbenchEncodingIsOk()) {
2475                         super.doSaveAs();
2476                 }
2477         }
2478
2479         /**
2480          * Asks the user if it is ok to store in non-workbench encoding.
2481          * 
2482          * @return <true>if the user wants to continue
2483          */
2484         private boolean askIfNonWorkbenchEncodingIsOk() {
2485                 IDocumentProvider provider = getDocumentProvider();
2486                 if (provider instanceof IStorageDocumentProvider) {
2487                         IEditorInput input = getEditorInput();
2488                         IStorageDocumentProvider storageProvider = (IStorageDocumentProvider) provider;
2489                         String encoding = storageProvider.getEncoding(input);
2490                         String defaultEncoding = storageProvider.getDefaultEncoding();
2491                         if (encoding != null && !encoding.equals(defaultEncoding)) {
2492                                 Shell shell = getSite().getShell();
2493                                 String title = PHPEditorMessages
2494                                                 .getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.title"); //$NON-NLS-1$
2495                                 String msg;
2496                                 if (input != null)
2497                                         msg = MessageFormat
2498                                                         .format(
2499                                                                         PHPEditorMessages
2500                                                                                         .getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message1"),
2501                                                                         new String[] { input.getName(), encoding }); //$NON-NLS-1$
2502                                 else
2503                                         msg = MessageFormat
2504                                                         .format(
2505                                                                         PHPEditorMessages
2506                                                                                         .getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message2"),
2507                                                                         new String[] { encoding }); //$NON-NLS-1$
2508                                 return MessageDialog.openQuestion(shell, title, msg);
2509                         }
2510                 }
2511                 return true;
2512         }
2513
2514         /*
2515          * @see net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener#aboutToBeReconciled()
2516          * @since 3.0
2517          */
2518         public void aboutToBeReconciled() {
2519
2520                 // Notify AST provider
2521                 // PHPeclipsePlugin.getDefault().getASTProvider().aboutToBeReconciled(getInputJavaElement());
2522
2523                 // Notify listeners
2524                 Object[] listeners = fReconcilingListeners.getListeners();
2525                 for (int i = 0, length = listeners.length; i < length; ++i)
2526                         ((IJavaReconcilingListener) listeners[i]).aboutToBeReconciled();
2527         }
2528
2529         /*
2530          * @see net.sourceforge.phpdt.internal.ui.text.java.IJavaReconcilingListener#reconciled(CompilationUnit,
2531          *      boolean, IProgressMonitor)
2532          * @since 3.0
2533          */
2534         public void reconciled(CompilationUnit ast, boolean forced,
2535                         IProgressMonitor progressMonitor) {
2536
2537                 // Always notify AST provider
2538                 // PHPeclipsePlugin.getDefault().getASTProvider().reconciled(ast,
2539                 // getInputJavaElement());
2540
2541                 // Notify listeners
2542                 // Object[] listeners = fReconcilingListeners.getListeners();
2543                 // for (int i = 0, length= listeners.length; i < length; ++i)
2544                 // ((IJavaReconcilingListener)listeners[i]).reconciled(ast, forced,
2545                 // progressMonitor);
2546
2547                 // Update Java Outline page selection
2548                 if (!forced && !progressMonitor.isCanceled()) {
2549                         Shell shell = getSite().getShell();
2550                         if (shell != null && !shell.isDisposed()) {
2551                                 shell.getDisplay().asyncExec(new Runnable() {
2552                                         public void run() {
2553                                                 selectionChanged();
2554                                         }
2555                                 });
2556                         }
2557                 }
2558         }
2559
2560         /**
2561          * Returns the updated java element for the old java element.
2562          */
2563         private IJavaElement findElement(IJavaElement element) {
2564                 if (element == null)
2565                         return null;
2566                 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
2567                                 .getWorkingCopyManager();
2568                 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
2569                 if (unit != null) {
2570                         try {
2571                                 synchronized (unit) {
2572                                         unit.reconcile();
2573                                 }
2574                                 IJavaElement[] findings = unit.findElements(element);
2575                                 if (findings != null && findings.length > 0)
2576                                         return findings[0];
2577                         } catch (JavaModelException x) {
2578                                 PHPeclipsePlugin.log(x.getStatus());
2579                                 // nothing found, be tolerant and go on
2580                         }
2581                 }
2582                 return null;
2583         }
2584
2585         /**
2586          * Returns the offset of the given Java element.
2587          */
2588         private int getOffset(IJavaElement element) {
2589                 if (element instanceof ISourceReference) {
2590                         ISourceReference sr = (ISourceReference) element;
2591                         try {
2592                                 ISourceRange srcRange = sr.getSourceRange();
2593                                 if (srcRange != null)
2594                                         return srcRange.getOffset();
2595                         } catch (JavaModelException e) {
2596                         }
2597                 }
2598                 return -1;
2599         }
2600
2601         /*
2602          * @see AbstractTextEditor#restoreSelection()
2603          */
2604         // protected void restoreSelection() {
2605         // try {
2606         // if (getSourceViewer() == null || fRememberedSelection == null)
2607         // return;
2608         // IJavaElement newElement = findElement(fRememberedElement);
2609         // int newOffset = getOffset(newElement);
2610         // int delta = (newOffset > -1 && fRememberedElementOffset > -1) ? newOffset
2611         // - fRememberedElementOffset : 0;
2612         // if (isValidSelection(delta + fRememberedSelection.getOffset(),
2613         // fRememberedSelection.getLength()))
2614         // selectAndReveal(delta + fRememberedSelection.getOffset(),
2615         // fRememberedSelection.getLength());
2616         // } finally {
2617         // fRememberedSelection = null;
2618         // fRememberedElement = null;
2619         // fRememberedElementOffset = -1;
2620         // }
2621         // }
2622         /**
2623          * Tells whether this is the active editor in the active page.
2624          * 
2625          * @return <code>true</code> if this is the active editor in the active
2626          *         page
2627          * @see IWorkbenchPage#getActiveEditor();
2628          */
2629         protected final boolean isActiveEditor() {
2630                 IWorkbenchWindow window = getSite().getWorkbenchWindow();
2631                 IWorkbenchPage page = window.getActivePage();
2632                 if (page == null)
2633                         return false;
2634                 IEditorPart activeEditor = page.getActiveEditor();
2635                 return activeEditor != null && activeEditor.equals(this);
2636         }
2637
2638         /**
2639          * Adds the given listener. Has no effect if an identical listener was not
2640          * already registered.
2641          * 
2642          * @param listener
2643          *            The reconcile listener to be added
2644          * @since 3.0
2645          */
2646         final void addReconcileListener(IJavaReconcilingListener listener) {
2647                 synchronized (fReconcilingListeners) {
2648                         fReconcilingListeners.add(listener);
2649                 }
2650         }
2651
2652         /**
2653          * Removes the given listener. Has no effect if an identical listener was
2654          * not already registered.
2655          * 
2656          * @param listener
2657          *            the reconcile listener to be removed
2658          * @since 3.0
2659          */
2660         final void removeReconcileListener(IJavaReconcilingListener listener) {
2661                 synchronized (fReconcilingListeners) {
2662                         fReconcilingListeners.remove(listener);
2663                 }
2664         }
2665
2666         protected void updateStateDependentActions() {
2667                 super.updateStateDependentActions();
2668                 fGenerateActionGroup.editorStateChanged();
2669         }
2670
2671         /*
2672          * @see AbstractTextEditor#rememberSelection()
2673          */
2674         protected void rememberSelection() {
2675                 fRememberedSelection.remember();
2676         }
2677
2678         /*
2679          * @see AbstractTextEditor#restoreSelection()
2680          */
2681         protected void restoreSelection() {
2682                 fRememberedSelection.restore();
2683         }
2684
2685         /*
2686          * @see AbstractTextEditor#canHandleMove(IEditorInput, IEditorInput)
2687          */
2688         protected boolean canHandleMove(IEditorInput originalElement,
2689                         IEditorInput movedElement) {
2690
2691                 String oldExtension = ""; //$NON-NLS-1$
2692                 if (originalElement instanceof IFileEditorInput) {
2693                         IFile file = ((IFileEditorInput) originalElement).getFile();
2694                         if (file != null) {
2695                                 String ext = file.getFileExtension();
2696                                 if (ext != null)
2697                                         oldExtension = ext;
2698                         }
2699                 }
2700
2701                 String newExtension = ""; //$NON-NLS-1$
2702                 if (movedElement instanceof IFileEditorInput) {
2703                         IFile file = ((IFileEditorInput) movedElement).getFile();
2704                         if (file != null)
2705                                 newExtension = file.getFileExtension();
2706                 }
2707
2708                 return oldExtension.equals(newExtension);
2709         }
2710
2711         /*
2712          * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isPrefQuickDiffAlwaysOn()
2713          */
2714         protected boolean isPrefQuickDiffAlwaysOn() {
2715                 // reestablishes the behaviour from AbstractDecoratedTextEditor which
2716                 // was hacked by JavaEditor
2717                 // to disable the change bar for the class file (attached source) java
2718                 // editor.
2719                 IPreferenceStore store = getPreferenceStore();
2720                 return store
2721                                 .getBoolean(AbstractDecoratedTextEditorPreferenceConstants.QUICK_DIFF_ALWAYS_ON);
2722         }
2723
2724         /*
2725          * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor#getAdapter(java.lang.Class)
2726          */
2727         public Object getAdapter(Class required) {
2728                 if (SmartBackspaceManager.class.equals(required)) {
2729                         if (getSourceViewer() instanceof JavaSourceViewer) {
2730                                 return ((JavaSourceViewer) getSourceViewer())
2731                                                 .getBackspaceManager();
2732                         }
2733                 }
2734
2735                 return super.getAdapter(required);
2736         }
2737
2738         /**
2739          * Returns the mutex for the reconciler. See
2740          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63898 for a description of
2741          * the problem.
2742          * <p>
2743          * TODO remove once the underlying problem is solved.
2744          * </p>
2745          * 
2746          * @return the lock reconcilers may use to synchronize on
2747          */
2748         public Object getReconcilerLock() {
2749                 return fReconcilerLock;
2750         }
2751
2752         /*
2753          * (non-Javadoc)
2754          * 
2755          * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
2756          */
2757         protected void editorSaved() {
2758                 super.editorSaved();
2759                 ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();
2760                 if (a != null) {
2761                         //a.refresh(ShowExternalPreviewAction.PHP_TYPE);
2762                         a.doRun(ShowExternalPreviewAction.PHP_TYPE);
2763                 }
2764         }
2765 }