Enable class members visibility icon in variable view.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPEditor.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
3 /**********************************************************************
4  Copyright (c) 2000, 2002 IBM Corp. and others.
5  All rights reserved. This program and the accompanying materials
6  are made available under the terms of the Common Public License v1.0
7  which accompanies this distribution, and is available at
8  http://www.eclipse.org/legal/cpl-v10.html
9
10  Contributors:
11  IBM Corporation - Initial implementation
12  www.phpeclipse.de
13  **********************************************************************/
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16 import java.text.BreakIterator;
17 import java.text.CharacterIterator;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.ResourceBundle;
24 import java.util.StringTokenizer;
25
26 import net.sourceforge.phpdt.core.ICompilationUnit;
27 import net.sourceforge.phpdt.core.IImportContainer;
28 import net.sourceforge.phpdt.core.IImportDeclaration;
29 import net.sourceforge.phpdt.core.IJavaElement;
30 import net.sourceforge.phpdt.core.IJavaProject;
31 import net.sourceforge.phpdt.core.IMember;
32 import net.sourceforge.phpdt.core.ISourceRange;
33 import net.sourceforge.phpdt.core.ISourceReference;
34 import net.sourceforge.phpdt.core.JavaCore;
35 import net.sourceforge.phpdt.core.JavaModelException;
36 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
37 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
38 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
39 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
40 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
41 import net.sourceforge.phpdt.internal.ui.actions.FoldingActionGroup;
42 import net.sourceforge.phpdt.internal.ui.actions.SelectionConverter;
43 import net.sourceforge.phpdt.internal.ui.text.CustomSourceInformationControl;
44 import net.sourceforge.phpdt.internal.ui.text.DocumentCharacterIterator;
45 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
46 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
47 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
48 import net.sourceforge.phpdt.internal.ui.text.JavaWordIterator;
49 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
50 import net.sourceforge.phpdt.internal.ui.text.PreferencesAdapter;
51 import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaExpandHover;
52 import net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST;
53 import net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider;
54 import net.sourceforge.phpdt.internal.ui.viewsupport.SelectionListenerWithASTManager;
55 import net.sourceforge.phpdt.ui.IContextMenuConstants;
56 import net.sourceforge.phpdt.ui.JavaUI;
57 import net.sourceforge.phpdt.ui.PreferenceConstants;
58 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
59 import net.sourceforge.phpdt.ui.actions.OpenEditorActionGroup;
60 import net.sourceforge.phpdt.ui.text.JavaTextTools;
61 import net.sourceforge.phpdt.ui.text.PHPSourceViewerConfiguration;
62 import net.sourceforge.phpdt.ui.text.folding.IJavaFoldingStructureProvider;
63 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
64 import net.sourceforge.phpeclipse.ui.editor.BrowserUtil;
65 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
66
67 import org.eclipse.core.resources.IFile;
68 import org.eclipse.core.resources.IMarker;
69 import org.eclipse.core.resources.IResource;
70 import org.eclipse.core.resources.ResourcesPlugin;
71 import org.eclipse.core.runtime.CoreException;
72 import org.eclipse.core.runtime.IProgressMonitor;
73 import org.eclipse.core.runtime.IStatus;
74 import org.eclipse.core.runtime.NullProgressMonitor;
75 import org.eclipse.core.runtime.Path;
76 import org.eclipse.core.runtime.Preferences;
77 import org.eclipse.core.runtime.Status;
78 import org.eclipse.core.runtime.jobs.Job;
79 import org.eclipse.jface.action.Action;
80 import org.eclipse.jface.action.GroupMarker;
81 import org.eclipse.jface.action.IAction;
82 import org.eclipse.jface.action.MenuManager;
83 import org.eclipse.jface.action.Separator;
84 import org.eclipse.jface.preference.IPreferenceStore;
85 import org.eclipse.jface.preference.PreferenceConverter;
86 import org.eclipse.jface.text.BadLocationException;
87 import org.eclipse.jface.text.DefaultInformationControl;
88 import org.eclipse.jface.text.DocumentEvent;
89 import org.eclipse.jface.text.IDocument;
90 import org.eclipse.jface.text.IDocumentExtension4;
91 import org.eclipse.jface.text.IDocumentListener;
92 import org.eclipse.jface.text.IInformationControl;
93 import org.eclipse.jface.text.IInformationControlCreator;
94 import org.eclipse.jface.text.IRegion;
95 import org.eclipse.jface.text.ISelectionValidator;
96 import org.eclipse.jface.text.ISynchronizable;
97 import org.eclipse.jface.text.ITextHover;
98 import org.eclipse.jface.text.ITextInputListener;
99 import org.eclipse.jface.text.ITextPresentationListener;
100 import org.eclipse.jface.text.ITextSelection;
101 import org.eclipse.jface.text.ITextViewer;
102 import org.eclipse.jface.text.ITextViewerExtension2;
103 import org.eclipse.jface.text.ITextViewerExtension4;
104 import org.eclipse.jface.text.ITextViewerExtension5;
105 import org.eclipse.jface.text.ITypedRegion;
106 import org.eclipse.jface.text.Position;
107 import org.eclipse.jface.text.Region;
108 import org.eclipse.jface.text.TextPresentation;
109 import org.eclipse.jface.text.TextSelection;
110 import org.eclipse.jface.text.TextUtilities;
111 import org.eclipse.jface.text.information.IInformationProvider;
112 import org.eclipse.jface.text.information.InformationPresenter;
113 import org.eclipse.jface.text.link.LinkedModeModel;
114 import org.eclipse.jface.text.reconciler.IReconciler;
115 import org.eclipse.jface.text.source.Annotation;
116 import org.eclipse.jface.text.source.AnnotationRulerColumn;
117 import org.eclipse.jface.text.source.CompositeRuler;
118 import org.eclipse.jface.text.source.IAnnotationModel;
119 import org.eclipse.jface.text.source.IAnnotationModelExtension;
120 import org.eclipse.jface.text.source.IOverviewRuler;
121 import org.eclipse.jface.text.source.ISourceViewer;
122 import org.eclipse.jface.text.source.ISourceViewerExtension2;
123 import org.eclipse.jface.text.source.IVerticalRuler;
124 import org.eclipse.jface.text.source.IVerticalRulerColumn;
125 import org.eclipse.jface.text.source.OverviewRuler;
126 import org.eclipse.jface.text.source.SourceViewerConfiguration;
127 import org.eclipse.jface.text.source.projection.ProjectionSupport;
128 import org.eclipse.jface.text.source.projection.ProjectionViewer;
129 import org.eclipse.jface.util.IPropertyChangeListener;
130 import org.eclipse.jface.util.ListenerList;
131 import org.eclipse.jface.util.PropertyChangeEvent;
132 import org.eclipse.jface.viewers.DoubleClickEvent;
133 import org.eclipse.jface.viewers.IDoubleClickListener;
134 import org.eclipse.jface.viewers.IPostSelectionProvider;
135 import org.eclipse.jface.viewers.ISelection;
136 import org.eclipse.jface.viewers.ISelectionChangedListener;
137 import org.eclipse.jface.viewers.ISelectionProvider;
138 import org.eclipse.jface.viewers.IStructuredSelection;
139 import org.eclipse.jface.viewers.SelectionChangedEvent;
140 import org.eclipse.jface.viewers.StructuredSelection;
141 import org.eclipse.swt.SWT;
142 import org.eclipse.swt.custom.BidiSegmentEvent;
143 import org.eclipse.swt.custom.BidiSegmentListener;
144 import org.eclipse.swt.custom.ST;
145 import org.eclipse.swt.custom.StyleRange;
146 import org.eclipse.swt.custom.StyledText;
147 import org.eclipse.swt.events.FocusEvent;
148 import org.eclipse.swt.events.FocusListener;
149 import org.eclipse.swt.events.KeyEvent;
150 import org.eclipse.swt.events.KeyListener;
151 import org.eclipse.swt.events.MouseEvent;
152 import org.eclipse.swt.events.MouseListener;
153 import org.eclipse.swt.events.MouseMoveListener;
154 import org.eclipse.swt.events.PaintEvent;
155 import org.eclipse.swt.events.PaintListener;
156 import org.eclipse.swt.graphics.Color;
157 import org.eclipse.swt.graphics.Cursor;
158 import org.eclipse.swt.graphics.GC;
159 import org.eclipse.swt.graphics.Image;
160 import org.eclipse.swt.graphics.Point;
161 import org.eclipse.swt.graphics.RGB;
162 import org.eclipse.swt.widgets.Composite;
163 import org.eclipse.swt.widgets.Control;
164 import org.eclipse.swt.widgets.Display;
165 import org.eclipse.swt.widgets.Shell;
166 import org.eclipse.ui.IEditorInput;
167 import org.eclipse.ui.IEditorPart;
168 import org.eclipse.ui.IPageLayout;
169 import org.eclipse.ui.IPartService;
170 import org.eclipse.ui.ISelectionListener;
171 import org.eclipse.ui.IViewPart;
172 import org.eclipse.ui.IWindowListener;
173 import org.eclipse.ui.IWorkbenchPage;
174 import org.eclipse.ui.IWorkbenchPart;
175 import org.eclipse.ui.IWorkbenchWindow;
176 import org.eclipse.ui.PlatformUI;
177 import org.eclipse.ui.actions.ActionContext;
178 import org.eclipse.ui.actions.ActionGroup;
179 import org.eclipse.ui.editors.text.DefaultEncodingSupport;
180 import org.eclipse.ui.editors.text.EditorsUI;
181 import org.eclipse.ui.editors.text.IEncodingSupport;
182 import org.eclipse.ui.ide.FileStoreEditorInput;
183 import org.eclipse.ui.part.FileEditorInput;
184 import org.eclipse.ui.part.IShowInSource;
185 import org.eclipse.ui.part.IShowInTargetList;
186 import org.eclipse.ui.part.ShowInContext;
187 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
188 import org.eclipse.ui.texteditor.AnnotationPreference;
189 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
190 import org.eclipse.ui.texteditor.IDocumentProvider;
191 import org.eclipse.ui.texteditor.IEditorStatusLine;
192 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
193 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
194 import org.eclipse.ui.texteditor.IUpdate;
195 import org.eclipse.ui.texteditor.MarkerAnnotation;
196 import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
197 import org.eclipse.ui.texteditor.TextEditorAction;
198 import org.eclipse.ui.texteditor.TextNavigationAction;
199 import org.eclipse.ui.texteditor.TextOperationAction;
200 import org.eclipse.ui.views.contentoutline.ContentOutline;
201 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
202 import org.eclipse.ui.views.tasklist.TaskList;
203
204 /**
205  * PHP specific text editor.
206  */
207 public abstract class PHPEditor extends AbstractDecoratedTextEditor implements
208                 IViewPartInputProvider, IShowInTargetList, IShowInSource {
209         // extends StatusTextEditor implements IViewPartInputProvider { // extends
210         // TextEditor {
211
212         /**
213          * Internal implementation class for a change listener.
214          * 
215          * @since 3.0
216          */
217         protected abstract class AbstractSelectionChangedListener implements
218                         ISelectionChangedListener {
219
220                 /**
221                  * Installs this selection changed listener with the given selection
222                  * provider. If the selection provider is a post selection provider,
223                  * post selection changed events are the preferred choice, otherwise
224                  * normal selection changed events are requested.
225                  * 
226                  * @param selectionProvider
227                  */
228                 public void install(ISelectionProvider selectionProvider) {
229                         if (selectionProvider == null)
230                                 return;
231
232                         if (selectionProvider instanceof IPostSelectionProvider) {
233                                 IPostSelectionProvider provider = (IPostSelectionProvider) selectionProvider;
234                                 provider.addPostSelectionChangedListener(this);
235                         } else {
236                                 selectionProvider.addSelectionChangedListener(this);
237                         }
238                 }
239
240                 /**
241                  * Removes this selection changed listener from the given selection
242                  * provider.
243                  * 
244                  * @param selectionProvider
245                  *            the selection provider
246                  */
247                 public void uninstall(ISelectionProvider selectionProvider) {
248                         if (selectionProvider == null)
249                                 return;
250
251                         if (selectionProvider instanceof IPostSelectionProvider) {
252                                 IPostSelectionProvider provider = (IPostSelectionProvider) selectionProvider;
253                                 provider.removePostSelectionChangedListener(this);
254                         } else {
255                                 selectionProvider.removeSelectionChangedListener(this);
256                         }
257                 }
258         }
259
260         /**
261          * Updates the Java outline page selection and this editor's range
262          * indicator.
263          * 
264          * @since 3.0
265          */
266         private class EditorSelectionChangedListener extends
267                         AbstractSelectionChangedListener {
268
269                 /*
270                  * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
271                  */
272                 public void selectionChanged(SelectionChangedEvent event) {
273                         // XXX: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=56161
274                         PHPEditor.this.selectionChanged();
275                 }
276         }
277
278         /**
279          * "Smart" runnable for updating the outline page's selection.
280          */
281         // class OutlinePageSelectionUpdater implements Runnable {
282         //
283         // /** Has the runnable already been posted? */
284         // private boolean fPosted = false;
285         //
286         // public OutlinePageSelectionUpdater() {
287         // }
288         //
289         // /*
290         // * @see Runnable#run()
291         // */
292         // public void run() {
293         // synchronizeOutlinePageSelection();
294         // fPosted = false;
295         // }
296         //
297         // /**
298         // * Posts this runnable into the event queue.
299         // */
300         // public void post() {
301         // if (fPosted)
302         // return;
303         //
304         // Shell shell = getSite().getShell();
305         // if (shell != null & !shell.isDisposed()) {
306         // fPosted = true;
307         // shell.getDisplay().asyncExec(this);
308         // }
309         // }
310         // };
311         class SelectionChangedListener implements ISelectionChangedListener {
312                 public void selectionChanged(SelectionChangedEvent event) {
313                         doSelectionChanged(event);
314                 }
315         };
316
317         /**
318          * Adapts an options {@link java.util.Map}to
319          * {@link org.eclipse.jface.preference.IPreferenceStore}.
320          * <p>
321          * This preference store is read-only i.e. write access throws an
322          * {@link java.lang.UnsupportedOperationException}.
323          * </p>
324          * 
325          * @since 3.0
326          */
327         private static class OptionsAdapter implements IPreferenceStore {
328
329                 /**
330                  * A property change event filter.
331                  */
332                 public interface IPropertyChangeEventFilter {
333
334                         /**
335                          * Should the given event be filtered?
336                          * 
337                          * @param event
338                          *            The property change event.
339                          * @return <code>true</code> iff the given event should be
340                          *         filtered.
341                          */
342                         public boolean isFiltered(PropertyChangeEvent event);
343
344                 }
345
346                 /**
347                  * Property change listener. Listens for events in the options Map and
348                  * fires a {@link org.eclipse.jface.util.PropertyChangeEvent}on this
349                  * adapter with arguments from the received event.
350                  */
351                 private class PropertyChangeListener implements IPropertyChangeListener {
352
353                         /**
354                          * {@inheritDoc}
355                          */
356                         public void propertyChange(PropertyChangeEvent event) {
357                                 if (getFilter().isFiltered(event))
358                                         return;
359
360                                 if (event.getNewValue() == null)
361                                         fOptions.remove(event.getProperty());
362                                 else
363                                         fOptions.put(event.getProperty(), event.getNewValue());
364
365                                 firePropertyChangeEvent(event.getProperty(), event
366                                                 .getOldValue(), event.getNewValue());
367                         }
368                 }
369
370                 /** Listeners on this adapter */
371                 private ListenerList fListeners = new ListenerList();
372
373                 /** Listener on the adapted options Map */
374                 private IPropertyChangeListener fListener = new PropertyChangeListener();
375
376                 /** Adapted options Map */
377                 private Map fOptions;
378
379                 /** Preference store through which events are received. */
380                 private IPreferenceStore fMockupPreferenceStore;
381
382                 /** Property event filter. */
383                 private IPropertyChangeEventFilter fFilter;
384
385                 /**
386                  * Initialize with the given options.
387                  * 
388                  * @param options
389                  *            The options to wrap
390                  * @param mockupPreferenceStore
391                  *            the mock-up preference store
392                  * @param filter
393                  *            the property change filter
394                  */
395                 public OptionsAdapter(Map options,
396                                 IPreferenceStore mockupPreferenceStore,
397                                 IPropertyChangeEventFilter filter) {
398                         fMockupPreferenceStore = mockupPreferenceStore;
399                         fOptions = options;
400                         setFilter(filter);
401                 }
402
403                 /**
404                  * {@inheritDoc}
405                  */
406                 public void addPropertyChangeListener(IPropertyChangeListener listener) {
407                         if (fListeners.size() == 0)
408                                 fMockupPreferenceStore.addPropertyChangeListener(fListener);
409                         fListeners.add(listener);
410                 }
411
412                 /**
413                  * {@inheritDoc}
414                  */
415                 public void removePropertyChangeListener(
416                                 IPropertyChangeListener listener) {
417                         fListeners.remove(listener);
418                         if (fListeners.size() == 0)
419                                 fMockupPreferenceStore.removePropertyChangeListener(fListener);
420                 }
421
422                 /**
423                  * {@inheritDoc}
424                  */
425                 public boolean contains(String name) {
426                         return fOptions.containsKey(name);
427                 }
428
429                 /**
430                  * {@inheritDoc}
431                  */
432                 public void firePropertyChangeEvent(String name, Object oldValue,
433                                 Object newValue) {
434                         PropertyChangeEvent event = new PropertyChangeEvent(this, name,
435                                         oldValue, newValue);
436                         Object[] listeners = fListeners.getListeners();
437                         for (int i = 0; i < listeners.length; i++)
438                                 ((IPropertyChangeListener) listeners[i]).propertyChange(event);
439                 }
440
441                 /**
442                  * {@inheritDoc}
443                  */
444                 public boolean getBoolean(String name) {
445                         boolean value = BOOLEAN_DEFAULT_DEFAULT;
446                         String s = (String) fOptions.get(name);
447                         if (s != null)
448                                 value = s.equals(TRUE);
449                         return value;
450                 }
451
452                 /**
453                  * {@inheritDoc}
454                  */
455                 public boolean getDefaultBoolean(String name) {
456                         return BOOLEAN_DEFAULT_DEFAULT;
457                 }
458
459                 /**
460                  * {@inheritDoc}
461                  */
462                 public double getDefaultDouble(String name) {
463                         return DOUBLE_DEFAULT_DEFAULT;
464                 }
465
466                 /**
467                  * {@inheritDoc}
468                  */
469                 public float getDefaultFloat(String name) {
470                         return FLOAT_DEFAULT_DEFAULT;
471                 }
472
473                 /**
474                  * {@inheritDoc}
475                  */
476                 public int getDefaultInt(String name) {
477                         return INT_DEFAULT_DEFAULT;
478                 }
479
480                 /**
481                  * {@inheritDoc}
482                  */
483                 public long getDefaultLong(String name) {
484                         return LONG_DEFAULT_DEFAULT;
485                 }
486
487                 /**
488                  * {@inheritDoc}
489                  */
490                 public String getDefaultString(String name) {
491                         return STRING_DEFAULT_DEFAULT;
492                 }
493
494                 /**
495                  * {@inheritDoc}
496                  */
497                 public double getDouble(String name) {
498                         double value = DOUBLE_DEFAULT_DEFAULT;
499                         String s = (String) fOptions.get(name);
500                         if (s != null) {
501                                 try {
502                                         value = new Double(s).doubleValue();
503                                 } catch (NumberFormatException e) {
504                                 }
505                         }
506                         return value;
507                 }
508
509                 /**
510                  * {@inheritDoc}
511                  */
512                 public float getFloat(String name) {
513                         float value = FLOAT_DEFAULT_DEFAULT;
514                         String s = (String) fOptions.get(name);
515                         if (s != null) {
516                                 try {
517                                         value = new Float(s).floatValue();
518                                 } catch (NumberFormatException e) {
519                                 }
520                         }
521                         return value;
522                 }
523
524                 /**
525                  * {@inheritDoc}
526                  */
527                 public int getInt(String name) {
528                         int value = INT_DEFAULT_DEFAULT;
529                         String s = (String) fOptions.get(name);
530                         if (s != null) {
531                                 try {
532                                         value = new Integer(s).intValue();
533                                 } catch (NumberFormatException e) {
534                                 }
535                         }
536                         return value;
537                 }
538
539                 /**
540                  * {@inheritDoc}
541                  */
542                 public long getLong(String name) {
543                         long value = LONG_DEFAULT_DEFAULT;
544                         String s = (String) fOptions.get(name);
545                         if (s != null) {
546                                 try {
547                                         value = new Long(s).longValue();
548                                 } catch (NumberFormatException e) {
549                                 }
550                         }
551                         return value;
552                 }
553
554                 /**
555                  * {@inheritDoc}
556                  */
557                 public String getString(String name) {
558                         String value = (String) fOptions.get(name);
559                         if (value == null)
560                                 value = STRING_DEFAULT_DEFAULT;
561                         return value;
562                 }
563
564                 /**
565                  * {@inheritDoc}
566                  */
567                 public boolean isDefault(String name) {
568                         return false;
569                 }
570
571                 /**
572                  * {@inheritDoc}
573                  */
574                 public boolean needsSaving() {
575                         return !fOptions.isEmpty();
576                 }
577
578                 /**
579                  * {@inheritDoc}
580                  */
581                 public void putValue(String name, String value) {
582                         throw new UnsupportedOperationException();
583                 }
584
585                 /**
586                  * {@inheritDoc}
587                  */
588                 public void setDefault(String name, double value) {
589                         throw new UnsupportedOperationException();
590                 }
591
592                 /**
593                  * {@inheritDoc}
594                  */
595                 public void setDefault(String name, float value) {
596                         throw new UnsupportedOperationException();
597                 }
598
599                 /**
600                  * {@inheritDoc}
601                  */
602                 public void setDefault(String name, int value) {
603                         throw new UnsupportedOperationException();
604                 }
605
606                 /**
607                  * {@inheritDoc}
608                  */
609                 public void setDefault(String name, long value) {
610                         throw new UnsupportedOperationException();
611                 }
612
613                 /**
614                  * {@inheritDoc}
615                  */
616                 public void setDefault(String name, String defaultObject) {
617                         throw new UnsupportedOperationException();
618                 }
619
620                 /**
621                  * {@inheritDoc}
622                  */
623                 public void setDefault(String name, boolean value) {
624                         throw new UnsupportedOperationException();
625                 }
626
627                 /**
628                  * {@inheritDoc}
629                  */
630                 public void setToDefault(String name) {
631                         throw new UnsupportedOperationException();
632                 }
633
634                 /**
635                  * {@inheritDoc}
636                  */
637                 public void setValue(String name, double value) {
638                         throw new UnsupportedOperationException();
639                 }
640
641                 /**
642                  * {@inheritDoc}
643                  */
644                 public void setValue(String name, float value) {
645                         throw new UnsupportedOperationException();
646                 }
647
648                 /**
649                  * {@inheritDoc}
650                  */
651                 public void setValue(String name, int value) {
652                         throw new UnsupportedOperationException();
653                 }
654
655                 /**
656                  * {@inheritDoc}
657                  */
658                 public void setValue(String name, long value) {
659                         throw new UnsupportedOperationException();
660                 }
661
662                 /**
663                  * {@inheritDoc}
664                  */
665                 public void setValue(String name, String value) {
666                         throw new UnsupportedOperationException();
667                 }
668
669                 /**
670                  * {@inheritDoc}
671                  */
672                 public void setValue(String name, boolean value) {
673                         throw new UnsupportedOperationException();
674                 }
675
676                 /**
677                  * Returns the adapted options Map.
678                  * 
679                  * @return Returns the adapted options Map.
680                  */
681                 public Map getOptions() {
682                         return fOptions;
683                 }
684
685                 /**
686                  * Returns the mock-up preference store, events are received through
687                  * this preference store.
688                  * 
689                  * @return Returns the mock-up preference store.
690                  */
691                 public IPreferenceStore getMockupPreferenceStore() {
692                         return fMockupPreferenceStore;
693                 }
694
695                 /**
696                  * Set the event filter to the given filter.
697                  * 
698                  * @param filter
699                  *            The new filter.
700                  */
701                 public void setFilter(IPropertyChangeEventFilter filter) {
702                         fFilter = filter;
703                 }
704
705                 /**
706                  * Returns the event filter.
707                  * 
708                  * @return The event filter.
709                  */
710                 public IPropertyChangeEventFilter getFilter() {
711                         return fFilter;
712                 }
713         }
714
715         /*
716          * Link mode.
717          */
718         // class MouseClickListener implements KeyListener, MouseListener,
719         // MouseMoveListener, FocusListener, PaintListener,
720         // IPropertyChangeListener, IDocumentListener, ITextInputListener {
721         //
722         // /** The session is active. */
723         // private boolean fActive;
724         //
725         // /** The currently active style range. */
726         // private IRegion fActiveRegion;
727         //
728         // /** The currently active style range as position. */
729         // private Position fRememberedPosition;
730         //
731         // /** The hand cursor. */
732         // private Cursor fCursor;
733         //
734         // /** The link color. */
735         // private Color fColor;
736         //
737         // /** The key modifier mask. */
738         // private int fKeyModifierMask;
739         //
740         // public void deactivate() {
741         // deactivate(false);
742         // }
743         //
744         // public void deactivate(boolean redrawAll) {
745         // if (!fActive)
746         // return;
747         //
748         // repairRepresentation(redrawAll);
749         // fActive = false;
750         // }
751         //
752         // public void install() {
753         //
754         // ISourceViewer sourceViewer = getSourceViewer();
755         // if (sourceViewer == null)
756         // return;
757         //
758         // StyledText text = sourceViewer.getTextWidget();
759         // if (text == null || text.isDisposed())
760         // return;
761         //
762         // updateColor(sourceViewer);
763         //
764         // sourceViewer.addTextInputListener(this);
765         //
766         // IDocument document = sourceViewer.getDocument();
767         // if (document != null)
768         // document.addDocumentListener(this);
769         //
770         // text.addKeyListener(this);
771         // text.addMouseListener(this);
772         // text.addMouseMoveListener(this);
773         // text.addFocusListener(this);
774         // text.addPaintListener(this);
775         //
776         // updateKeyModifierMask();
777         //
778         // IPreferenceStore preferenceStore = getPreferenceStore();
779         // preferenceStore.addPropertyChangeListener(this);
780         // }
781         //
782         // private void updateKeyModifierMask() {
783         // String modifiers =
784         // getPreferenceStore().getString(BROWSER_LIKE_LINKS_KEY_MODIFIER);
785         // fKeyModifierMask = computeStateMask(modifiers);
786         // if (fKeyModifierMask == -1) {
787         // // Fallback to stored state mask
788         // fKeyModifierMask =
789         // getPreferenceStore().getInt(BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK);
790         // }
791         // ;
792         // }
793         //
794         // private int computeStateMask(String modifiers) {
795         // if (modifiers == null)
796         // return -1;
797         //
798         // if (modifiers.length() == 0)
799         // return SWT.NONE;
800         //
801         // int stateMask = 0;
802         // StringTokenizer modifierTokenizer = new StringTokenizer(modifiers,
803         // ",;.:+-*
804         // "); //$NON-NLS-1$
805         // while (modifierTokenizer.hasMoreTokens()) {
806         // int modifier =
807         // EditorUtility.findLocalizedModifier(modifierTokenizer.nextToken());
808         // if (modifier == 0 || (stateMask & modifier) == modifier)
809         // return -1;
810         // stateMask = stateMask | modifier;
811         // }
812         // return stateMask;
813         // }
814         //
815         // public void uninstall() {
816         //
817         // if (fColor != null) {
818         // fColor.dispose();
819         // fColor = null;
820         // }
821         //
822         // if (fCursor != null) {
823         // fCursor.dispose();
824         // fCursor = null;
825         // }
826         //
827         // ISourceViewer sourceViewer = getSourceViewer();
828         // if (sourceViewer == null)
829         // return;
830         //
831         // sourceViewer.removeTextInputListener(this);
832         //
833         // IDocument document = sourceViewer.getDocument();
834         // if (document != null)
835         // document.removeDocumentListener(this);
836         //
837         // IPreferenceStore preferenceStore = getPreferenceStore();
838         // if (preferenceStore != null)
839         // preferenceStore.removePropertyChangeListener(this);
840         //
841         // StyledText text = sourceViewer.getTextWidget();
842         // if (text == null || text.isDisposed())
843         // return;
844         //
845         // text.removeKeyListener(this);
846         // text.removeMouseListener(this);
847         // text.removeMouseMoveListener(this);
848         // text.removeFocusListener(this);
849         // text.removePaintListener(this);
850         // }
851         //
852         // /*
853         // * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
854         // */
855         // public void propertyChange(PropertyChangeEvent event) {
856         // if (event.getProperty().equals(PHPEditor.LINK_COLOR)) {
857         // ISourceViewer viewer = getSourceViewer();
858         // if (viewer != null)
859         // updateColor(viewer);
860         // } else if (event.getProperty().equals(BROWSER_LIKE_LINKS_KEY_MODIFIER)) {
861         // updateKeyModifierMask();
862         // }
863         // }
864         //
865         // private void updateColor(ISourceViewer viewer) {
866         // if (fColor != null)
867         // fColor.dispose();
868         //
869         // StyledText text = viewer.getTextWidget();
870         // if (text == null || text.isDisposed())
871         // return;
872         //
873         // Display display = text.getDisplay();
874         // fColor = createColor(getPreferenceStore(), PHPEditor.LINK_COLOR,
875         // display);
876         // }
877         //
878         // /**
879         // * Creates a color from the information stored in the given preference
880         // store. Returns <code>null</code> if there is no such
881         // * information available.
882         // */
883         // private Color createColor(IPreferenceStore store, String key, Display
884         // display) {
885         //
886         // RGB rgb = null;
887         //
888         // if (store.contains(key)) {
889         //
890         // if (store.isDefault(key))
891         // rgb = PreferenceConverter.getDefaultColor(store, key);
892         // else
893         // rgb = PreferenceConverter.getColor(store, key);
894         //
895         // if (rgb != null)
896         // return new Color(display, rgb);
897         // }
898         //
899         // return null;
900         // }
901         //
902         // private void repairRepresentation() {
903         // repairRepresentation(false);
904         // }
905         //
906         // private void repairRepresentation(boolean redrawAll) {
907         //
908         // if (fActiveRegion == null)
909         // return;
910         //
911         // ISourceViewer viewer = getSourceViewer();
912         // if (viewer != null) {
913         // resetCursor(viewer);
914         //
915         // int offset = fActiveRegion.getOffset();
916         // int length = fActiveRegion.getLength();
917         //
918         // // remove style
919         // if (!redrawAll && viewer instanceof ITextViewerExtension2)
920         // ((ITextViewerExtension2) viewer).invalidateTextPresentation(offset,
921         // length);
922         // else
923         // viewer.invalidateTextPresentation();
924         //
925         // // remove underline
926         // if (viewer instanceof ITextViewerExtension3) {
927         // ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
928         // offset = extension.modelOffset2WidgetOffset(offset);
929         // } else {
930         // offset -= viewer.getVisibleRegion().getOffset();
931         // }
932         //
933         // StyledText text = viewer.getTextWidget();
934         // try {
935         // text.redrawRange(offset, length, true);
936         // } catch (IllegalArgumentException x) {
937         // PHPeclipsePlugin.log(x);
938         // }
939         // }
940         //
941         // fActiveRegion = null;
942         // }
943         //
944         // // will eventually be replaced by a method provided by jdt.core
945         // private IRegion selectWord(IDocument document, int anchor) {
946         //
947         // try {
948         // int offset = anchor;
949         // char c;
950         //
951         // while (offset >= 0) {
952         // c = document.getChar(offset);
953         // if (!Scanner.isPHPIdentifierPart(c))
954         // break;
955         // --offset;
956         // }
957         //
958         // int start = offset;
959         //
960         // offset = anchor;
961         // int length = document.getLength();
962         //
963         // while (offset < length) {
964         // c = document.getChar(offset);
965         // if (!Scanner.isPHPIdentifierPart(c))
966         // break;
967         // ++offset;
968         // }
969         //
970         // int end = offset;
971         //
972         // if (start == end)
973         // return new Region(start, 0);
974         // else
975         // return new Region(start + 1, end - start - 1);
976         //
977         // } catch (BadLocationException x) {
978         // return null;
979         // }
980         // }
981         //
982         // IRegion getCurrentTextRegion(ISourceViewer viewer) {
983         //
984         // int offset = getCurrentTextOffset(viewer);
985         // if (offset == -1)
986         // return null;
987         //
988         // return null;
989         // // IJavaElement input= SelectionConverter.getInput(PHPEditor.this);
990         // // if (input == null)
991         // // return null;
992         // //
993         // // try {
994         // //
995         // // IJavaElement[] elements= null;
996         // // synchronized (input) {
997         // // elements= ((ICodeAssist) input).codeSelect(offset, 0);
998         // // }
999         // //
1000         // // if (elements == null || elements.length == 0)
1001         // // return null;
1002         // //
1003         // // return selectWord(viewer.getDocument(), offset);
1004         // //
1005         // // } catch (JavaModelException e) {
1006         // // return null;
1007         // // }
1008         // }
1009         //
1010         // private int getCurrentTextOffset(ISourceViewer viewer) {
1011         //
1012         // try {
1013         // StyledText text = viewer.getTextWidget();
1014         // if (text == null || text.isDisposed())
1015         // return -1;
1016         //
1017         // Display display = text.getDisplay();
1018         // Point absolutePosition = display.getCursorLocation();
1019         // Point relativePosition = text.toControl(absolutePosition);
1020         //
1021         // int widgetOffset = text.getOffsetAtLocation(relativePosition);
1022         // if (viewer instanceof ITextViewerExtension3) {
1023         // ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
1024         // return extension.widgetOffset2ModelOffset(widgetOffset);
1025         // } else {
1026         // return widgetOffset + viewer.getVisibleRegion().getOffset();
1027         // }
1028         //
1029         // } catch (IllegalArgumentException e) {
1030         // return -1;
1031         // }
1032         // }
1033         //
1034         // private void highlightRegion(ISourceViewer viewer, IRegion region) {
1035         //
1036         // if (region.equals(fActiveRegion))
1037         // return;
1038         //
1039         // repairRepresentation();
1040         //
1041         // StyledText text = viewer.getTextWidget();
1042         // if (text == null || text.isDisposed())
1043         // return;
1044         //
1045         // // highlight region
1046         // int offset = 0;
1047         // int length = 0;
1048         //
1049         // if (viewer instanceof ITextViewerExtension3) {
1050         // ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
1051         // IRegion widgetRange = extension.modelRange2WidgetRange(region);
1052         // if (widgetRange == null)
1053         // return;
1054         //
1055         // offset = widgetRange.getOffset();
1056         // length = widgetRange.getLength();
1057         //
1058         // } else {
1059         // offset = region.getOffset() - viewer.getVisibleRegion().getOffset();
1060         // length = region.getLength();
1061         // }
1062         //
1063         // StyleRange oldStyleRange = text.getStyleRangeAtOffset(offset);
1064         // Color foregroundColor = fColor;
1065         // Color backgroundColor = oldStyleRange == null ? text.getBackground() :
1066         // oldStyleRange.background;
1067         // StyleRange styleRange = new StyleRange(offset, length, foregroundColor,
1068         // backgroundColor);
1069         // text.setStyleRange(styleRange);
1070         //
1071         // // underline
1072         // text.redrawRange(offset, length, true);
1073         //
1074         // fActiveRegion = region;
1075         // }
1076         //
1077         // private void activateCursor(ISourceViewer viewer) {
1078         // StyledText text = viewer.getTextWidget();
1079         // if (text == null || text.isDisposed())
1080         // return;
1081         // Display display = text.getDisplay();
1082         // if (fCursor == null)
1083         // fCursor = new Cursor(display, SWT.CURSOR_HAND);
1084         // text.setCursor(fCursor);
1085         // }
1086         //
1087         // private void resetCursor(ISourceViewer viewer) {
1088         // StyledText text = viewer.getTextWidget();
1089         // if (text != null && !text.isDisposed())
1090         // text.setCursor(null);
1091         //
1092         // if (fCursor != null) {
1093         // fCursor.dispose();
1094         // fCursor = null;
1095         // }
1096         // }
1097         //
1098         // /*
1099         // * @see
1100         // org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
1101         // */
1102         // public void keyPressed(KeyEvent event) {
1103         //
1104         // if (fActive) {
1105         // deactivate();
1106         // return;
1107         // }
1108         //
1109         // if (event.keyCode != fKeyModifierMask) {
1110         // deactivate();
1111         // return;
1112         // }
1113         //
1114         // fActive = true;
1115         //
1116         // // removed for #25871
1117         // //
1118         // // ISourceViewer viewer= getSourceViewer();
1119         // // if (viewer == null)
1120         // // return;
1121         // //
1122         // // IRegion region= getCurrentTextRegion(viewer);
1123         // // if (region == null)
1124         // // return;
1125         // //
1126         // // highlightRegion(viewer, region);
1127         // // activateCursor(viewer);
1128         // }
1129         //
1130         // /*
1131         // * @see
1132         // org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
1133         // */
1134         // public void keyReleased(KeyEvent event) {
1135         //
1136         // if (!fActive)
1137         // return;
1138         //
1139         // deactivate();
1140         // }
1141         //
1142         // /*
1143         // * @see
1144         // org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
1145         // */
1146         // public void mouseDoubleClick(MouseEvent e) {
1147         // }
1148         //
1149         // /*
1150         // * @see
1151         // org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
1152         // */
1153         // public void mouseDown(MouseEvent event) {
1154         //
1155         // if (!fActive)
1156         // return;
1157         //
1158         // if (event.stateMask != fKeyModifierMask) {
1159         // deactivate();
1160         // return;
1161         // }
1162         //
1163         // if (event.button != 1) {
1164         // deactivate();
1165         // return;
1166         // }
1167         // }
1168         //
1169         // /*
1170         // * @see
1171         // org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
1172         // */
1173         // public void mouseUp(MouseEvent e) {
1174         //
1175         // if (!fActive)
1176         // return;
1177         //
1178         // if (e.button != 1) {
1179         // deactivate();
1180         // return;
1181         // }
1182         //
1183         // boolean wasActive = fCursor != null;
1184         //
1185         // deactivate();
1186         //
1187         // if (wasActive) {
1188         // IAction action = getAction("OpenEditor"); //$NON-NLS-1$
1189         // if (action != null)
1190         // action.run();
1191         // }
1192         // }
1193         //
1194         // /*
1195         // * @see
1196         // org.eclipse.swt.events.MouseMoveListener#mouseMove(org.eclipse.swt.events.MouseEvent)
1197         // */
1198         // public void mouseMove(MouseEvent event) {
1199         //
1200         // if (event.widget instanceof Control && !((Control)
1201         // event.widget).isFocusControl()) {
1202         // deactivate();
1203         // return;
1204         // }
1205         //
1206         // if (!fActive) {
1207         // if (event.stateMask != fKeyModifierMask)
1208         // return;
1209         // // modifier was already pressed
1210         // fActive = true;
1211         // }
1212         //
1213         // ISourceViewer viewer = getSourceViewer();
1214         // if (viewer == null) {
1215         // deactivate();
1216         // return;
1217         // }
1218         //
1219         // StyledText text = viewer.getTextWidget();
1220         // if (text == null || text.isDisposed()) {
1221         // deactivate();
1222         // return;
1223         // }
1224         //
1225         // if ((event.stateMask & SWT.BUTTON1) != 0 && text.getSelectionCount() !=
1226         // 0)
1227         // {
1228         // deactivate();
1229         // return;
1230         // }
1231         //
1232         // IRegion region = getCurrentTextRegion(viewer);
1233         // if (region == null || region.getLength() == 0) {
1234         // repairRepresentation();
1235         // return;
1236         // }
1237         //
1238         // highlightRegion(viewer, region);
1239         // activateCursor(viewer);
1240         // }
1241         //
1242         // /*
1243         // * @see
1244         // org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
1245         // */
1246         // public void focusGained(FocusEvent e) {
1247         // }
1248         //
1249         // /*
1250         // * @see
1251         // org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
1252         // */
1253         // public void focusLost(FocusEvent event) {
1254         // deactivate();
1255         // }
1256         //
1257         // /*
1258         // * @see
1259         // org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
1260         // */
1261         // public void documentAboutToBeChanged(DocumentEvent event) {
1262         // if (fActive && fActiveRegion != null) {
1263         // fRememberedPosition = new Position(fActiveRegion.getOffset(),
1264         // fActiveRegion.getLength());
1265         // try {
1266         // event.getDocument().addPosition(fRememberedPosition);
1267         // } catch (BadLocationException x) {
1268         // fRememberedPosition = null;
1269         // }
1270         // }
1271         // }
1272         //
1273         // /*
1274         // * @see
1275         // org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
1276         // */
1277         // public void documentChanged(DocumentEvent event) {
1278         // if (fRememberedPosition != null && !fRememberedPosition.isDeleted()) {
1279         // event.getDocument().removePosition(fRememberedPosition);
1280         // fActiveRegion = new Region(fRememberedPosition.getOffset(),
1281         // fRememberedPosition.getLength());
1282         // }
1283         // fRememberedPosition = null;
1284         //
1285         // ISourceViewer viewer = getSourceViewer();
1286         // if (viewer != null) {
1287         // StyledText widget = viewer.getTextWidget();
1288         // if (widget != null && !widget.isDisposed()) {
1289         // widget.getDisplay().asyncExec(new Runnable() {
1290         // public void run() {
1291         // deactivate();
1292         // }
1293         // });
1294         // }
1295         // }
1296         // }
1297         //
1298         // /*
1299         // * @see
1300         // org.eclipse.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(org.eclipse.jface.text.IDocument,
1301         // * org.eclipse.jface.text.IDocument)
1302         // */
1303         // public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument
1304         // newInput) {
1305         // if (oldInput == null)
1306         // return;
1307         // deactivate();
1308         // oldInput.removeDocumentListener(this);
1309         // }
1310         //
1311         // /*
1312         // * @see
1313         // org.eclipse.jface.text.ITextInputListener#inputDocumentChanged(org.eclipse.jface.text.IDocument,
1314         // * org.eclipse.jface.text.IDocument)
1315         // */
1316         // public void inputDocumentChanged(IDocument oldInput, IDocument newInput)
1317         // {
1318         // if (newInput == null)
1319         // return;
1320         // newInput.addDocumentListener(this);
1321         // }
1322         //
1323         // /*
1324         // * @see PaintListener#paintControl(PaintEvent)
1325         // */
1326         // public void paintControl(PaintEvent event) {
1327         // if (fActiveRegion == null)
1328         // return;
1329         //
1330         // ISourceViewer viewer = getSourceViewer();
1331         // if (viewer == null)
1332         // return;
1333         //
1334         // StyledText text = viewer.getTextWidget();
1335         // if (text == null || text.isDisposed())
1336         // return;
1337         //
1338         // int offset = 0;
1339         // int length = 0;
1340         //
1341         // if (viewer instanceof ITextViewerExtension3) {
1342         //
1343         // ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
1344         // IRegion widgetRange = extension.modelRange2WidgetRange(new Region(offset,
1345         // length));
1346         // if (widgetRange == null)
1347         // return;
1348         //
1349         // offset = widgetRange.getOffset();
1350         // length = widgetRange.getLength();
1351         //
1352         // } else {
1353         //
1354         // IRegion region = viewer.getVisibleRegion();
1355         // if (!includes(region, fActiveRegion))
1356         // return;
1357         //
1358         // offset = fActiveRegion.getOffset() - region.getOffset();
1359         // length = fActiveRegion.getLength();
1360         // }
1361         //
1362         // // support for bidi
1363         // Point minLocation = getMinimumLocation(text, offset, length);
1364         // Point maxLocation = getMaximumLocation(text, offset, length);
1365         //
1366         // int x1 = minLocation.x;
1367         // int x2 = minLocation.x + maxLocation.x - minLocation.x - 1;
1368         // int y = minLocation.y + text.getLineHeight() - 1;
1369         //
1370         // GC gc = event.gc;
1371         // if (fColor != null && !fColor.isDisposed())
1372         // gc.setForeground(fColor);
1373         // gc.drawLine(x1, y, x2, y);
1374         // }
1375         //
1376         // private boolean includes(IRegion region, IRegion position) {
1377         // return position.getOffset() >= region.getOffset()
1378         // && position.getOffset() + position.getLength() <= region.getOffset() +
1379         // region.getLength();
1380         // }
1381         //
1382         // private Point getMinimumLocation(StyledText text, int offset, int length)
1383         // {
1384         // Point minLocation = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
1385         //
1386         // for (int i = 0; i <= length; i++) {
1387         // Point location = text.getLocationAtOffset(offset + i);
1388         //
1389         // if (location.x < minLocation.x)
1390         // minLocation.x = location.x;
1391         // if (location.y < minLocation.y)
1392         // minLocation.y = location.y;
1393         // }
1394         //
1395         // return minLocation;
1396         // }
1397         //
1398         // private Point getMaximumLocation(StyledText text, int offset, int length)
1399         // {
1400         // Point maxLocation = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
1401         //
1402         // for (int i = 0; i <= length; i++) {
1403         // Point location = text.getLocationAtOffset(offset + i);
1404         //
1405         // if (location.x > maxLocation.x)
1406         // maxLocation.x = location.x;
1407         // if (location.y > maxLocation.y)
1408         // maxLocation.y = location.y;
1409         // }
1410         //
1411         // return maxLocation;
1412         // }
1413         // };
1414         /*
1415          * Link mode.
1416          */
1417         class MouseClickListener implements KeyListener, MouseListener,
1418                         MouseMoveListener, FocusListener, PaintListener,
1419                         IPropertyChangeListener, IDocumentListener, ITextInputListener,
1420                         ITextPresentationListener {
1421
1422                 /** The session is active. */
1423                 private boolean fActive;
1424
1425                 /** The currently active style range. */
1426                 private IRegion fActiveRegion;
1427
1428                 /** The currently active style range as position. */
1429                 private Position fRememberedPosition;
1430
1431                 /** The hand cursor. */
1432                 private Cursor fCursor;
1433
1434                 /** The link color. */
1435                 private Color fColor;
1436
1437                 /** The key modifier mask. */
1438                 private int fKeyModifierMask;
1439
1440                 public void deactivate() {
1441                         deactivate(false);
1442                 }
1443
1444                 public void deactivate(boolean redrawAll) {
1445                         if (!fActive)
1446                                 return;
1447
1448                         repairRepresentation(redrawAll);
1449                         fActive = false;
1450                 }
1451
1452                 public void install() {
1453                         ISourceViewer sourceViewer = getSourceViewer();
1454                         if (sourceViewer == null)
1455                                 return;
1456
1457                         StyledText text = sourceViewer.getTextWidget();
1458                         if (text == null || text.isDisposed())
1459                                 return;
1460
1461                         updateColor(sourceViewer);
1462
1463                         sourceViewer.addTextInputListener(this);
1464
1465                         IDocument document = sourceViewer.getDocument();
1466                         if (document != null)
1467                                 document.addDocumentListener(this);
1468
1469                         text.addKeyListener(this);
1470                         text.addMouseListener(this);
1471                         text.addMouseMoveListener(this);
1472                         text.addFocusListener(this);
1473                         text.addPaintListener(this);
1474
1475                         ((ITextViewerExtension4) sourceViewer)
1476                                         .addTextPresentationListener(this);
1477
1478                         updateKeyModifierMask();
1479
1480                         IPreferenceStore preferenceStore = getPreferenceStore();
1481                         preferenceStore.addPropertyChangeListener(this);
1482                 }
1483
1484                 private void updateKeyModifierMask() {
1485                         String modifiers = getPreferenceStore().getString(
1486                                         BROWSER_LIKE_LINKS_KEY_MODIFIER);
1487                         fKeyModifierMask = computeStateMask(modifiers);
1488                         if (fKeyModifierMask == -1) {
1489                                 // Fall back to stored state mask
1490                                 fKeyModifierMask = getPreferenceStore().getInt(
1491                                                 BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK);
1492                         }
1493                 }
1494
1495                 private int computeStateMask(String modifiers) {
1496                         if (modifiers == null)
1497                                 return -1;
1498
1499                         if (modifiers.length() == 0)
1500                                 return SWT.NONE;
1501
1502                         int stateMask = 0;
1503                         StringTokenizer modifierTokenizer = new StringTokenizer(modifiers,
1504                                         ",;.:+-* "); //$NON-NLS-1$
1505                         while (modifierTokenizer.hasMoreTokens()) {
1506                                 int modifier = EditorUtility
1507                                                 .findLocalizedModifier(modifierTokenizer.nextToken());
1508                                 if (modifier == 0 || (stateMask & modifier) == modifier)
1509                                         return -1;
1510                                 stateMask = stateMask | modifier;
1511                         }
1512                         return stateMask;
1513                 }
1514
1515                 public void uninstall() {
1516
1517                         if (fColor != null) {
1518                                 fColor.dispose();
1519                                 fColor = null;
1520                         }
1521
1522                         if (fCursor != null) {
1523                                 fCursor.dispose();
1524                                 fCursor = null;
1525                         }
1526
1527                         ISourceViewer sourceViewer = getSourceViewer();
1528                         if (sourceViewer != null)
1529                                 sourceViewer.removeTextInputListener(this);
1530
1531                         IDocumentProvider documentProvider = getDocumentProvider();
1532                         if (documentProvider != null) {
1533                                 IDocument document = documentProvider
1534                                                 .getDocument(getEditorInput());
1535                                 if (document != null)
1536                                         document.removeDocumentListener(this);
1537                         }
1538
1539                         IPreferenceStore preferenceStore = getPreferenceStore();
1540                         if (preferenceStore != null)
1541                                 preferenceStore.removePropertyChangeListener(this);
1542
1543                         if (sourceViewer == null)
1544                                 return;
1545
1546                         StyledText text = sourceViewer.getTextWidget();
1547                         if (text == null || text.isDisposed())
1548                                 return;
1549
1550                         text.removeKeyListener(this);
1551                         text.removeMouseListener(this);
1552                         text.removeMouseMoveListener(this);
1553                         text.removeFocusListener(this);
1554                         text.removePaintListener(this);
1555
1556                         ((ITextViewerExtension4) sourceViewer)
1557                                         .removeTextPresentationListener(this);
1558                 }
1559
1560                 /*
1561                  * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
1562                  */
1563                 public void propertyChange(PropertyChangeEvent event) {
1564                         if (event.getProperty().equals(PHPEditor.LINK_COLOR)) {
1565                                 ISourceViewer viewer = getSourceViewer();
1566                                 if (viewer != null)
1567                                         updateColor(viewer);
1568                         } else if (event.getProperty().equals(
1569                                         BROWSER_LIKE_LINKS_KEY_MODIFIER)) {
1570                                 updateKeyModifierMask();
1571                         }
1572                 }
1573
1574                 private void updateColor(ISourceViewer viewer) {
1575                         if (fColor != null)
1576                                 fColor.dispose();
1577
1578                         StyledText text = viewer.getTextWidget();
1579                         if (text == null || text.isDisposed())
1580                                 return;
1581
1582                         Display display = text.getDisplay();
1583                         fColor = createColor(getPreferenceStore(), PHPEditor.LINK_COLOR,
1584                                         display);
1585                 }
1586
1587                 /**
1588                  * Creates a color from the information stored in the given preference
1589                  * store.
1590                  * 
1591                  * @param store
1592                  *            the preference store
1593                  * @param key
1594                  *            the key
1595                  * @param display
1596                  *            the display
1597                  * @return the color or <code>null</code> if there is no such
1598                  *         information available
1599                  */
1600                 private Color createColor(IPreferenceStore store, String key,
1601                                 Display display) {
1602
1603                         RGB rgb = null;
1604
1605                         if (store.contains(key)) {
1606
1607                                 if (store.isDefault(key))
1608                                         rgb = PreferenceConverter.getDefaultColor(store, key);
1609                                 else
1610                                         rgb = PreferenceConverter.getColor(store, key);
1611
1612                                 if (rgb != null)
1613                                         return new Color(display, rgb);
1614                         }
1615
1616                         return null;
1617                 }
1618
1619                 private void repairRepresentation() {
1620                         repairRepresentation(false);
1621                 }
1622
1623                 private void repairRepresentation(boolean redrawAll) {
1624
1625                         if (fActiveRegion == null)
1626                                 return;
1627
1628                         int offset = fActiveRegion.getOffset();
1629                         int length = fActiveRegion.getLength();
1630                         fActiveRegion = null;
1631
1632                         ISourceViewer viewer = getSourceViewer();
1633                         if (viewer != null) {
1634
1635                                 resetCursor(viewer);
1636
1637                                 // Invalidate ==> remove applied text presentation
1638                                 if (!redrawAll && viewer instanceof ITextViewerExtension2)
1639                                         ((ITextViewerExtension2) viewer)
1640                                                         .invalidateTextPresentation(offset, length);
1641                                 else
1642                                         viewer.invalidateTextPresentation();
1643
1644                                 // Remove underline
1645                                 if (viewer instanceof ITextViewerExtension5) {
1646                                         ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
1647                                         offset = extension.modelOffset2WidgetOffset(offset);
1648                                 } else {
1649                                         offset -= viewer.getVisibleRegion().getOffset();
1650                                 }
1651                                 try {
1652                                         StyledText text = viewer.getTextWidget();
1653
1654                                         text.redrawRange(offset, length, false);
1655                                 } catch (IllegalArgumentException x) {
1656                                         // JavaPlugin.log(x);
1657                                 }
1658                         }
1659                 }
1660
1661                 // will eventually be replaced by a method provided by jdt.core
1662                 private IRegion selectWord(IDocument document, int anchor) {
1663
1664                         try {
1665                                 int offset = anchor;
1666                                 char c;
1667
1668                                 while (offset >= 0) {
1669                                         c = document.getChar(offset);
1670                                         if (!Scanner.isPHPIdentifierPart(c) && c != '$')
1671                                                 break;
1672                                         --offset;
1673                                 }
1674
1675                                 int start = offset;
1676
1677                                 offset = anchor;
1678                                 int length = document.getLength();
1679
1680                                 while (offset < length) {
1681                                         c = document.getChar(offset);
1682                                         if (!Scanner.isPHPIdentifierPart(c) && c != '$')
1683                                                 break;
1684                                         ++offset;
1685                                 }
1686
1687                                 int end = offset;
1688
1689                                 if (start == end)
1690                                         return new Region(start, 0);
1691                                 else
1692                                         return new Region(start + 1, end - start - 1);
1693
1694                         } catch (BadLocationException x) {
1695                                 return null;
1696                         }
1697                 }
1698
1699                 IRegion getCurrentTextRegion(ISourceViewer viewer) {
1700
1701                         int offset = getCurrentTextOffset(viewer);
1702                         if (offset == -1)
1703                                 return null;
1704
1705                         IJavaElement input = SelectionConverter.getInput(PHPEditor.this);
1706                         if (input == null)
1707                                 return null;
1708
1709                         // try {
1710
1711                         // IJavaElement[] elements= null;
1712                         // synchronized (input) {
1713                         // elements= ((ICodeAssist) input).codeSelect(offset, 0);
1714                         // }
1715                         //
1716                         // if (elements == null || elements.length == 0)
1717                         // return null;
1718
1719                         return selectWord(viewer.getDocument(), offset);
1720
1721                         // } catch (JavaModelException e) {
1722                         // return null;
1723                         // }
1724                 }
1725
1726                 private int getCurrentTextOffset(ISourceViewer viewer) {
1727
1728                         try {
1729                                 StyledText text = viewer.getTextWidget();
1730                                 if (text == null || text.isDisposed())
1731                                         return -1;
1732
1733                                 Display display = text.getDisplay();
1734                                 Point absolutePosition = display.getCursorLocation();
1735                                 Point relativePosition = text.toControl(absolutePosition);
1736
1737                                 int widgetOffset = text.getOffsetAtLocation(relativePosition);
1738                                 if (viewer instanceof ITextViewerExtension5) {
1739                                         ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
1740                                         return extension.widgetOffset2ModelOffset(widgetOffset);
1741                                 } else {
1742                                         return widgetOffset + viewer.getVisibleRegion().getOffset();
1743                                 }
1744
1745                         } catch (IllegalArgumentException e) {
1746                                 return -1;
1747                         }
1748                 }
1749
1750                 public void applyTextPresentation(TextPresentation textPresentation) {
1751                         if (fActiveRegion == null)
1752                                 return;
1753                         IRegion region = textPresentation.getExtent();
1754                         if (fActiveRegion.getOffset() + fActiveRegion.getLength() >= region
1755                                         .getOffset()
1756                                         && region.getOffset() + region.getLength() > fActiveRegion
1757                                                         .getOffset())
1758                                 textPresentation.mergeStyleRange(new StyleRange(fActiveRegion
1759                                                 .getOffset(), fActiveRegion.getLength(), fColor, null));
1760                 }
1761
1762                 private void highlightRegion(ISourceViewer viewer, IRegion region) {
1763
1764                         if (region.equals(fActiveRegion))
1765                                 return;
1766
1767                         repairRepresentation();
1768
1769                         StyledText text = viewer.getTextWidget();
1770                         if (text == null || text.isDisposed())
1771                                 return;
1772
1773                         // Underline
1774                         int offset = 0;
1775                         int length = 0;
1776                         if (viewer instanceof ITextViewerExtension5) {
1777                                 ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
1778                                 IRegion widgetRange = extension.modelRange2WidgetRange(region);
1779                                 if (widgetRange == null)
1780                                         return;
1781
1782                                 offset = widgetRange.getOffset();
1783                                 length = widgetRange.getLength();
1784
1785                         } else {
1786                                 offset = region.getOffset()
1787                                                 - viewer.getVisibleRegion().getOffset();
1788                                 length = region.getLength();
1789                         }
1790                         text.redrawRange(offset, length, false);
1791
1792                         // Invalidate region ==> apply text presentation
1793                         fActiveRegion = region;
1794                         if (viewer instanceof ITextViewerExtension2)
1795                                 ((ITextViewerExtension2) viewer).invalidateTextPresentation(
1796                                                 region.getOffset(), region.getLength());
1797                         else
1798                                 viewer.invalidateTextPresentation();
1799                 }
1800
1801                 private void activateCursor(ISourceViewer viewer) {
1802                         StyledText text = viewer.getTextWidget();
1803                         if (text == null || text.isDisposed())
1804                                 return;
1805                         Display display = text.getDisplay();
1806                         if (fCursor == null)
1807                                 fCursor = new Cursor(display, SWT.CURSOR_HAND);
1808                         text.setCursor(fCursor);
1809                 }
1810
1811                 private void resetCursor(ISourceViewer viewer) {
1812                         StyledText text = viewer.getTextWidget();
1813                         if (text != null && !text.isDisposed())
1814                                 text.setCursor(null);
1815
1816                         if (fCursor != null) {
1817                                 fCursor.dispose();
1818                                 fCursor = null;
1819                         }
1820                 }
1821
1822                 /*
1823                  * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
1824                  */
1825                 public void keyPressed(KeyEvent event) {
1826
1827                         if (fActive) {
1828                                 deactivate();
1829                                 return;
1830                         }
1831
1832                         if (event.keyCode != fKeyModifierMask) {
1833                                 deactivate();
1834                                 return;
1835                         }
1836
1837                         fActive = true;
1838
1839                         // removed for #25871
1840                         //
1841                         // ISourceViewer viewer= getSourceViewer();
1842                         // if (viewer == null)
1843                         // return;
1844                         //
1845                         // IRegion region= getCurrentTextRegion(viewer);
1846                         // if (region == null)
1847                         // return;
1848                         //
1849                         // highlightRegion(viewer, region);
1850                         // activateCursor(viewer);
1851                 }
1852
1853                 /*
1854                  * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
1855                  */
1856                 public void keyReleased(KeyEvent event) {
1857
1858                         if (!fActive)
1859                                 return;
1860
1861                         deactivate();
1862                 }
1863
1864                 /*
1865                  * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
1866                  */
1867                 public void mouseDoubleClick(MouseEvent e) {
1868                 }
1869
1870                 /*
1871                  * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
1872                  */
1873                 public void mouseDown(MouseEvent event) {
1874
1875                         if (!fActive)
1876                                 return;
1877
1878                         if (event.stateMask != fKeyModifierMask) {
1879                                 deactivate();
1880                                 return;
1881                         }
1882
1883                         if (event.button != 1) {
1884                                 deactivate();
1885                                 return;
1886                         }
1887                 }
1888
1889                 /*
1890                  * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
1891                  */
1892                 public void mouseUp(MouseEvent e) {
1893
1894                         if (!fActive)
1895                                 return;
1896
1897                         if (e.button != 1) {
1898                                 deactivate();
1899                                 return;
1900                         }
1901
1902                         boolean wasActive = fCursor != null;
1903
1904                         deactivate();
1905
1906                         if (wasActive) {
1907                                 IAction action = getAction("OpenEditor"); //$NON-NLS-1$
1908                                 if (action != null)
1909                                         action.run();
1910                         }
1911                 }
1912
1913                 /*
1914                  * @see org.eclipse.swt.events.MouseMoveListener#mouseMove(org.eclipse.swt.events.MouseEvent)
1915                  */
1916                 public void mouseMove(MouseEvent event) {
1917
1918                         if (event.widget instanceof Control
1919                                         && !((Control) event.widget).isFocusControl()) {
1920                                 deactivate();
1921                                 return;
1922                         }
1923
1924                         if (!fActive) {
1925                                 if (event.stateMask != fKeyModifierMask)
1926                                         return;
1927                                 // modifier was already pressed
1928                                 fActive = true;
1929                         }
1930
1931                         ISourceViewer viewer = getSourceViewer();
1932                         if (viewer == null) {
1933                                 deactivate();
1934                                 return;
1935                         }
1936
1937                         StyledText text = viewer.getTextWidget();
1938                         if (text == null || text.isDisposed()) {
1939                                 deactivate();
1940                                 return;
1941                         }
1942
1943                         if ((event.stateMask & SWT.BUTTON1) != 0
1944                                         && text.getSelectionCount() != 0) {
1945                                 deactivate();
1946                                 return;
1947                         }
1948
1949                         IRegion region = getCurrentTextRegion(viewer);
1950                         if (region == null || region.getLength() == 0) {
1951                                 repairRepresentation();
1952                                 return;
1953                         }
1954
1955                         highlightRegion(viewer, region);
1956                         activateCursor(viewer);
1957                 }
1958
1959                 /*
1960                  * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
1961                  */
1962                 public void focusGained(FocusEvent e) {
1963                 }
1964
1965                 /*
1966                  * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
1967                  */
1968                 public void focusLost(FocusEvent event) {
1969                         deactivate();
1970                 }
1971
1972                 /*
1973                  * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
1974                  */
1975                 public void documentAboutToBeChanged(DocumentEvent event) {
1976                         if (fActive && fActiveRegion != null) {
1977                                 fRememberedPosition = new Position(fActiveRegion.getOffset(),
1978                                                 fActiveRegion.getLength());
1979                                 try {
1980                                         event.getDocument().addPosition(fRememberedPosition);
1981                                 } catch (BadLocationException x) {
1982                                         fRememberedPosition = null;
1983                                 }
1984                         }
1985                 }
1986
1987                 /*
1988                  * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
1989                  */
1990                 public void documentChanged(DocumentEvent event) {
1991                         if (fRememberedPosition != null) {
1992                                 if (!fRememberedPosition.isDeleted()) {
1993
1994                                         event.getDocument().removePosition(fRememberedPosition);
1995                                         fActiveRegion = new Region(fRememberedPosition.getOffset(),
1996                                                         fRememberedPosition.getLength());
1997                                         fRememberedPosition = null;
1998
1999                                         ISourceViewer viewer = getSourceViewer();
2000                                         if (viewer != null) {
2001                                                 StyledText widget = viewer.getTextWidget();
2002                                                 if (widget != null && !widget.isDisposed()) {
2003                                                         widget.getDisplay().asyncExec(new Runnable() {
2004                                                                 public void run() {
2005                                                                         deactivate();
2006                                                                 }
2007                                                         });
2008                                                 }
2009                                         }
2010
2011                                 } else {
2012                                         fActiveRegion = null;
2013                                         fRememberedPosition = null;
2014                                         deactivate();
2015                                 }
2016                         }
2017                 }
2018
2019                 /*
2020                  * @see org.eclipse.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(org.eclipse.jface.text.IDocument,
2021                  *      org.eclipse.jface.text.IDocument)
2022                  */
2023                 public void inputDocumentAboutToBeChanged(IDocument oldInput,
2024                                 IDocument newInput) {
2025                         if (oldInput == null)
2026                                 return;
2027                         deactivate();
2028                         oldInput.removeDocumentListener(this);
2029                 }
2030
2031                 /*
2032                  * @see org.eclipse.jface.text.ITextInputListener#inputDocumentChanged(org.eclipse.jface.text.IDocument,
2033                  *      org.eclipse.jface.text.IDocument)
2034                  */
2035                 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
2036                         if (newInput == null)
2037                                 return;
2038                         newInput.addDocumentListener(this);
2039                 }
2040
2041                 /*
2042                  * @see PaintListener#paintControl(PaintEvent)
2043                  */
2044                 public void paintControl(PaintEvent event) {
2045                         if (fActiveRegion == null)
2046                                 return;
2047
2048                         ISourceViewer viewer = getSourceViewer();
2049                         if (viewer == null)
2050                                 return;
2051
2052                         StyledText text = viewer.getTextWidget();
2053                         if (text == null || text.isDisposed())
2054                                 return;
2055
2056                         int offset = 0;
2057                         int length = 0;
2058
2059                         if (viewer instanceof ITextViewerExtension5) {
2060
2061                                 ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
2062                                 IRegion widgetRange = extension
2063                                                 .modelRange2WidgetRange(fActiveRegion);
2064                                 if (widgetRange == null)
2065                                         return;
2066
2067                                 offset = widgetRange.getOffset();
2068                                 length = widgetRange.getLength();
2069
2070                         } else {
2071
2072                                 IRegion region = viewer.getVisibleRegion();
2073                                 if (!includes(region, fActiveRegion))
2074                                         return;
2075
2076                                 offset = fActiveRegion.getOffset() - region.getOffset();
2077                                 length = fActiveRegion.getLength();
2078                         }
2079
2080                         // support for bidi
2081                         Point minLocation = getMinimumLocation(text, offset, length);
2082                         Point maxLocation = getMaximumLocation(text, offset, length);
2083
2084                         int x1 = minLocation.x;
2085                         int x2 = minLocation.x + maxLocation.x - minLocation.x - 1;
2086                         int y = minLocation.y + text.getLineHeight() - 1;
2087
2088                         GC gc = event.gc;
2089                         if (fColor != null && !fColor.isDisposed())
2090                                 gc.setForeground(fColor);
2091                         gc.drawLine(x1, y, x2, y);
2092                 }
2093
2094                 private boolean includes(IRegion region, IRegion position) {
2095                         return position.getOffset() >= region.getOffset()
2096                                         && position.getOffset() + position.getLength() <= region
2097                                                         .getOffset()
2098                                                         + region.getLength();
2099                 }
2100
2101                 private Point getMinimumLocation(StyledText text, int offset, int length) {
2102                         Point minLocation = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
2103
2104                         for (int i = 0; i <= length; i++) {
2105                                 Point location = text.getLocationAtOffset(offset + i);
2106
2107                                 if (location.x < minLocation.x)
2108                                         minLocation.x = location.x;
2109                                 if (location.y < minLocation.y)
2110                                         minLocation.y = location.y;
2111                         }
2112
2113                         return minLocation;
2114                 }
2115
2116                 private Point getMaximumLocation(StyledText text, int offset, int length) {
2117                         Point maxLocation = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
2118
2119                         for (int i = 0; i <= length; i++) {
2120                                 Point location = text.getLocationAtOffset(offset + i);
2121
2122                                 if (location.x > maxLocation.x)
2123                                         maxLocation.x = location.x;
2124                                 if (location.y > maxLocation.y)
2125                                         maxLocation.y = location.y;
2126                         }
2127
2128                         return maxLocation;
2129                 }
2130         }
2131
2132         /**
2133          * This action dispatches into two behaviours: If there is no current text
2134          * hover, the javadoc is displayed using information presenter. If there is
2135          * a current text hover, it is converted into a information presenter in
2136          * order to make it sticky.
2137          */
2138         class InformationDispatchAction extends TextEditorAction {
2139
2140                 /** The wrapped text operation action. */
2141                 private final TextOperationAction fTextOperationAction;
2142
2143                 /**
2144                  * Creates a dispatch action.
2145                  */
2146                 public InformationDispatchAction(ResourceBundle resourceBundle,
2147                                 String prefix, final TextOperationAction textOperationAction) {
2148                         super(resourceBundle, prefix, PHPEditor.this);
2149                         if (textOperationAction == null)
2150                                 throw new IllegalArgumentException();
2151                         fTextOperationAction = textOperationAction;
2152                 }
2153
2154                 /*
2155                  * @see org.eclipse.jface.action.IAction#run()
2156                  */
2157                 public void run() {
2158
2159                         ISourceViewer sourceViewer = getSourceViewer();
2160                         if (sourceViewer == null) {
2161                                 fTextOperationAction.run();
2162                                 return;
2163                         }
2164
2165                         if (!(sourceViewer instanceof ITextViewerExtension2)) {
2166                                 fTextOperationAction.run();
2167                                 return;
2168                         }
2169
2170                         ITextViewerExtension2 textViewerExtension2 = (ITextViewerExtension2) sourceViewer;
2171
2172                         // does a text hover exist?
2173                         ITextHover textHover = textViewerExtension2.getCurrentTextHover();
2174                         if (textHover == null) {
2175                                 fTextOperationAction.run();
2176                                 return;
2177                         }
2178
2179                         Point hoverEventLocation = textViewerExtension2
2180                                         .getHoverEventLocation();
2181                         int offset = computeOffsetAtLocation(sourceViewer,
2182                                         hoverEventLocation.x, hoverEventLocation.y);
2183                         if (offset == -1) {
2184                                 fTextOperationAction.run();
2185                                 return;
2186                         }
2187
2188                         try {
2189                                 // get the text hover content
2190                                 IDocument document = sourceViewer.getDocument();
2191                                 String contentType = document.getContentType(offset);
2192
2193                                 final IRegion hoverRegion = textHover.getHoverRegion(
2194                                                 sourceViewer, offset);
2195                                 if (hoverRegion == null)
2196                                         return;
2197
2198                                 final String hoverInfo = textHover.getHoverInfo(sourceViewer,
2199                                                 hoverRegion);
2200
2201                                 // with information provider
2202                                 IInformationProvider informationProvider = new IInformationProvider() {
2203                                         /*
2204                                          * @see org.eclipse.jface.text.information.IInformationProvider#getSubject(org.eclipse.jface.text.ITextViewer,
2205                                          *      int)
2206                                          */
2207                                         public IRegion getSubject(ITextViewer textViewer, int offset) {
2208                                                 return hoverRegion;
2209                                         }
2210
2211                                         /*
2212                                          * @see org.eclipse.jface.text.information.IInformationProvider#getInformation(org.eclipse.jface.text.ITextViewer,
2213                                          *      org.eclipse.jface.text.IRegion)
2214                                          */
2215                                         public String getInformation(ITextViewer textViewer,
2216                                                         IRegion subject) {
2217                                                 return hoverInfo;
2218                                         }
2219                                 };
2220
2221                                 fInformationPresenter.setOffset(offset);
2222                                 fInformationPresenter.setInformationProvider(
2223                                                 informationProvider, contentType);
2224                                 fInformationPresenter.showInformation();
2225
2226                         } catch (BadLocationException e) {
2227                         }
2228                 }
2229
2230                 // modified version from TextViewer
2231                 private int computeOffsetAtLocation(ITextViewer textViewer, int x, int y) {
2232
2233                         StyledText styledText = textViewer.getTextWidget();
2234                         IDocument document = textViewer.getDocument();
2235
2236                         if (document == null)
2237                                 return -1;
2238
2239                         try {
2240                                 int widgetLocation = styledText.getOffsetAtLocation(new Point(
2241                                                 x, y));
2242                                 if (textViewer instanceof ITextViewerExtension5) {
2243                                         ITextViewerExtension5 extension = (ITextViewerExtension5) textViewer;
2244                                         return extension.widgetOffset2ModelOffset(widgetLocation);
2245                                 } else {
2246                                         IRegion visibleRegion = textViewer.getVisibleRegion();
2247                                         return widgetLocation + visibleRegion.getOffset();
2248                                 }
2249                         } catch (IllegalArgumentException e) {
2250                                 return -1;
2251                         }
2252
2253                 }
2254         };
2255
2256         /**
2257          * This action implements smart home.
2258          * 
2259          * Instead of going to the start of a line it does the following: - if smart
2260          * home/end is enabled and the caret is after the line's first
2261          * non-whitespace then the caret is moved directly before it, taking JavaDoc
2262          * and multi-line comments into account. - if the caret is before the line's
2263          * first non-whitespace the caret is moved to the beginning of the line - if
2264          * the caret is at the beginning of the line see first case.
2265          * 
2266          * @since 3.0
2267          */
2268         protected class SmartLineStartAction extends LineStartAction {
2269
2270                 /**
2271                  * Creates a new smart line start action
2272                  * 
2273                  * @param textWidget
2274                  *            the styled text widget
2275                  * @param doSelect
2276                  *            a boolean flag which tells if the text up to the beginning
2277                  *            of the line should be selected
2278                  */
2279                 public SmartLineStartAction(final StyledText textWidget,
2280                                 final boolean doSelect) {
2281                         super(textWidget, doSelect);
2282                 }
2283
2284                 /*
2285                  * @see org.eclipse.ui.texteditor.AbstractTextEditor.LineStartAction#getLineStartPosition(java.lang.String,
2286                  *      int, java.lang.String)
2287                  */
2288                 protected int getLineStartPosition(final IDocument document,
2289                                 final String line, final int length, final int offset) {
2290
2291                         String type = IDocument.DEFAULT_CONTENT_TYPE;
2292                         try {
2293                                 type = TextUtilities.getContentType(document,
2294                                                 IPHPPartitions.PHP_PARTITIONING, offset, true);
2295                         } catch (BadLocationException exception) {
2296                                 // Should not happen
2297                         }
2298
2299                         int index = super.getLineStartPosition(document, line, length,
2300                                         offset);
2301                         if (type.equals(IPHPPartitions.PHP_PHPDOC_COMMENT)
2302                                         || type.equals(IPHPPartitions.PHP_MULTILINE_COMMENT)) {
2303                                 if (index < length - 1 && line.charAt(index) == '*'
2304                                                 && line.charAt(index + 1) != '/') {
2305                                         do {
2306                                                 ++index;
2307                                         } while (index < length
2308                                                         && Character.isWhitespace(line.charAt(index)));
2309                                 }
2310                         } else {
2311                                 if (index < length - 1 && line.charAt(index) == '/'
2312                                                 && line.charAt(index + 1) == '/') {
2313                                         index++;
2314                                         do {
2315                                                 ++index;
2316                                         } while (index < length
2317                                                         && Character.isWhitespace(line.charAt(index)));
2318                                 }
2319                         }
2320                         return index;
2321                 }
2322         }
2323
2324         /**
2325          * Text navigation action to navigate to the next sub-word.
2326          * 
2327          * @since 3.0
2328          */
2329         protected abstract class NextSubWordAction extends TextNavigationAction {
2330
2331                 protected JavaWordIterator fIterator = new JavaWordIterator();
2332
2333                 /**
2334                  * Creates a new next sub-word action.
2335                  * 
2336                  * @param code
2337                  *            Action code for the default operation. Must be an action
2338                  *            code from
2339                  * @see org.eclipse.swt.custom.ST.
2340                  */
2341                 protected NextSubWordAction(int code) {
2342                         super(getSourceViewer().getTextWidget(), code);
2343                 }
2344
2345                 /*
2346                  * @see org.eclipse.jface.action.IAction#run()
2347                  */
2348                 public void run() {
2349                         // Check whether we are in a java code partition and the preference
2350                         // is
2351                         // enabled
2352                         final IPreferenceStore store = getPreferenceStore();
2353                         if (!store
2354                                         .getBoolean(PreferenceConstants.EDITOR_SUB_WORD_NAVIGATION)) {
2355                                 super.run();
2356                                 return;
2357                         }
2358
2359                         final ISourceViewer viewer = getSourceViewer();
2360                         final IDocument document = viewer.getDocument();
2361                         fIterator
2362                                         .setText((CharacterIterator) new DocumentCharacterIterator(
2363                                                         document));
2364                         int position = widgetOffset2ModelOffset(viewer, viewer
2365                                         .getTextWidget().getCaretOffset());
2366                         if (position == -1)
2367                                 return;
2368
2369                         int next = findNextPosition(position);
2370                         if (next != BreakIterator.DONE) {
2371                                 setCaretPosition(next);
2372                                 getTextWidget().showSelection();
2373                                 fireSelectionChanged();
2374                         }
2375
2376                 }
2377                 
2378                 /**
2379                  * Finds the next position after the given position.
2380                  * 
2381                  * @param position
2382                  *            the current position
2383                  * @return the next position
2384                  */
2385                 protected int findNextPosition(int position) {
2386                         ISourceViewer viewer = getSourceViewer();
2387                         int widget = -1;
2388                         while (position != BreakIterator.DONE && widget == -1) { // TODO:
2389                                 // optimize
2390                                 position = fIterator.following(position);
2391                                 if (position != BreakIterator.DONE)
2392                                         widget = modelOffset2WidgetOffset(viewer, position);
2393                         }
2394                         return position;
2395                 }
2396
2397                 /**
2398                  * Sets the caret position to the sub-word boundary given with
2399                  * <code>position</code>.
2400                  * 
2401                  * @param position
2402                  *            Position where the action should move the caret
2403                  */
2404                 protected abstract void setCaretPosition(int position);
2405         }
2406
2407         /**
2408          * Text navigation action to navigate to the next sub-word.
2409          * 
2410          * @since 3.0
2411          */
2412         protected class NavigateNextSubWordAction extends NextSubWordAction {
2413
2414                 /**
2415                  * Creates a new navigate next sub-word action.
2416                  */
2417                 public NavigateNextSubWordAction() {
2418                         super(ST.WORD_NEXT);
2419                 }
2420
2421                 /*
2422                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor.NextSubWordAction#setCaretPosition(int)
2423                  */
2424                 protected void setCaretPosition(final int position) {
2425                         getTextWidget().setCaretOffset(
2426                                         modelOffset2WidgetOffset(getSourceViewer(), position));
2427                 }
2428         }
2429
2430         /**
2431          * Text operation action to delete the next sub-word.
2432          * 
2433          * @since 3.0
2434          */
2435         protected class DeleteNextSubWordAction extends NextSubWordAction implements
2436                         IUpdate {
2437
2438                 /**
2439                  * Creates a new delete next sub-word action.
2440                  */
2441                 public DeleteNextSubWordAction() {
2442                         super(ST.DELETE_WORD_NEXT);
2443                 }
2444
2445                 /*
2446                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor.NextSubWordAction#setCaretPosition(int)
2447                  */
2448                 protected void setCaretPosition(final int position) {
2449                         if (!validateEditorInputState())
2450                                 return;
2451
2452                         final ISourceViewer viewer = getSourceViewer();
2453                         final int caret = widgetOffset2ModelOffset(viewer, viewer
2454                                         .getTextWidget().getCaretOffset());
2455
2456                         try {
2457                                 viewer.getDocument().replace(caret, position - caret, ""); //$NON-NLS-1$
2458                         } catch (BadLocationException exception) {
2459                                 // Should not happen
2460                         }
2461                 }
2462
2463                 /*
2464                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor.NextSubWordAction#findNextPosition(int)
2465                  */
2466                 protected int findNextPosition(int position) {
2467                         return fIterator.following(position);
2468                 }
2469
2470                 /*
2471                  * @see org.eclipse.ui.texteditor.IUpdate#update()
2472                  */
2473                 public void update() {
2474                         setEnabled(isEditorInputModifiable());
2475                 }
2476         }
2477
2478         /**
2479          * Text operation action to select the next sub-word.
2480          * 
2481          * @since 3.0
2482          */
2483         protected class SelectNextSubWordAction extends NextSubWordAction {
2484
2485                 /**
2486                  * Creates a new select next sub-word action.
2487                  */
2488                 public SelectNextSubWordAction() {
2489                         super(ST.SELECT_WORD_NEXT);
2490                 }
2491
2492                 /*
2493                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor.NextSubWordAction#setCaretPosition(int)
2494                  */
2495                 protected void setCaretPosition(final int position) {
2496                         final ISourceViewer viewer = getSourceViewer();
2497
2498                         final StyledText text = viewer.getTextWidget();
2499                         if (text != null && !text.isDisposed()) {
2500
2501                                 final Point selection = text.getSelection();
2502                                 final int caret = text.getCaretOffset();
2503                                 final int offset = modelOffset2WidgetOffset(viewer, position);
2504
2505                                 if (caret == selection.x)
2506                                         text.setSelectionRange(selection.y, offset - selection.y);
2507                                 else
2508                                         text.setSelectionRange(selection.x, offset - selection.x);
2509                         }
2510                 }
2511         }
2512
2513         /**
2514          * Text navigation action to navigate to the previous sub-word.
2515          * 
2516          * @since 3.0
2517          */
2518         protected abstract class PreviousSubWordAction extends TextNavigationAction {
2519
2520                 protected JavaWordIterator fIterator = new JavaWordIterator();
2521
2522                 /**
2523                  * Creates a new previous sub-word action.
2524                  * 
2525                  * @param code
2526                  *            Action code for the default operation. Must be an action
2527                  *            code from
2528                  * @see org.eclipse.swt.custom.ST.
2529                  */
2530                 protected PreviousSubWordAction(final int code) {
2531                         super(getSourceViewer().getTextWidget(), code);
2532                 }
2533
2534                 /*
2535                  * @see org.eclipse.jface.action.IAction#run()
2536                  */
2537                 public void run() {
2538                         // Check whether we are in a java code partition and the preference
2539                         // is
2540                         // enabled
2541                         final IPreferenceStore store = getPreferenceStore();
2542                         if (!store
2543                                         .getBoolean(PreferenceConstants.EDITOR_SUB_WORD_NAVIGATION)) {
2544                                 super.run();
2545                                 return;
2546                         }
2547
2548                         final ISourceViewer viewer = getSourceViewer();
2549                         final IDocument document = viewer.getDocument();
2550                         fIterator
2551                                         .setText((CharacterIterator) new DocumentCharacterIterator(
2552                                                         document));
2553                         int position = widgetOffset2ModelOffset(viewer, viewer
2554                                         .getTextWidget().getCaretOffset());
2555                         if (position == -1)
2556                                 return;
2557
2558                         int previous = findPreviousPosition(position);
2559                         if (previous != BreakIterator.DONE) {
2560                                 setCaretPosition(previous);
2561                                 getTextWidget().showSelection();
2562                                 fireSelectionChanged();
2563                         }
2564
2565                 }
2566
2567                 /**
2568                  * Finds the previous position before the given position.
2569                  * 
2570                  * @param position
2571                  *            the current position
2572                  * @return the previous position
2573                  */
2574                 protected int findPreviousPosition(int position) {
2575                         ISourceViewer viewer = getSourceViewer();
2576                         int widget = -1;
2577                         while (position != BreakIterator.DONE && widget == -1) { // TODO:
2578                                 // optimize
2579                                 position = fIterator.preceding(position);
2580                                 if (position != BreakIterator.DONE)
2581                                         widget = modelOffset2WidgetOffset(viewer, position);
2582                         }
2583                         return position;
2584                 }
2585
2586                 /**
2587                  * Sets the caret position to the sub-word boundary given with
2588                  * <code>position</code>.
2589                  * 
2590                  * @param position
2591                  *            Position where the action should move the caret
2592                  */
2593                 protected abstract void setCaretPosition(int position);
2594         }
2595
2596         /**
2597          * Text navigation action to navigate to the previous sub-word.
2598          * 
2599          * @since 3.0
2600          */
2601         protected class NavigatePreviousSubWordAction extends PreviousSubWordAction {
2602
2603                 /**
2604                  * Creates a new navigate previous sub-word action.
2605                  */
2606                 public NavigatePreviousSubWordAction() {
2607                         super(ST.WORD_PREVIOUS);
2608                 }
2609
2610                 /*
2611                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor.PreviousSubWordAction#setCaretPosition(int)
2612                  */
2613                 protected void setCaretPosition(final int position) {
2614                         getTextWidget().setCaretOffset(
2615                                         modelOffset2WidgetOffset(getSourceViewer(), position));
2616                 }
2617         }
2618
2619         /**
2620          * Text operation action to delete the previous sub-word.
2621          * 
2622          * @since 3.0
2623          */
2624         protected class DeletePreviousSubWordAction extends PreviousSubWordAction
2625                         implements IUpdate {
2626
2627                 /**
2628                  * Creates a new delete previous sub-word action.
2629                  */
2630                 public DeletePreviousSubWordAction() {
2631                         super(ST.DELETE_WORD_PREVIOUS);
2632                 }
2633
2634                 /*
2635                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor.PreviousSubWordAction#setCaretPosition(int)
2636                  */
2637                 protected void setCaretPosition(final int position) {
2638                         if (!validateEditorInputState())
2639                                 return;
2640
2641                         final ISourceViewer viewer = getSourceViewer();
2642                         final int caret = widgetOffset2ModelOffset(viewer, viewer
2643                                         .getTextWidget().getCaretOffset());
2644
2645                         try {
2646                                 viewer.getDocument().replace(position, caret - position, ""); //$NON-NLS-1$
2647                         } catch (BadLocationException exception) {
2648                                 // Should not happen
2649                         }
2650                 }
2651
2652                 /*
2653                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor.PreviousSubWordAction#findPreviousPosition(int)
2654                  */
2655                 protected int findPreviousPosition(int position) {
2656                         return fIterator.preceding(position);
2657                 }
2658
2659                 /*
2660                  * @see org.eclipse.ui.texteditor.IUpdate#update()
2661                  */
2662                 public void update() {
2663                         setEnabled(isEditorInputModifiable());
2664                 }
2665         }
2666
2667         /**
2668          * Text operation action to select the previous sub-word.
2669          * 
2670          * @since 3.0
2671          */
2672         protected class SelectPreviousSubWordAction extends PreviousSubWordAction {
2673
2674                 /**
2675                  * Creates a new select previous sub-word action.
2676                  */
2677                 public SelectPreviousSubWordAction() {
2678                         super(ST.SELECT_WORD_PREVIOUS);
2679                 }
2680
2681                 /*
2682                  * @see net.sourceforge.phpdt.internal.ui.javaeditor.JavaEditor.PreviousSubWordAction#setCaretPosition(int)
2683                  */
2684                 protected void setCaretPosition(final int position) {
2685                         final ISourceViewer viewer = getSourceViewer();
2686
2687                         final StyledText text = viewer.getTextWidget();
2688                         if (text != null && !text.isDisposed()) {
2689
2690                                 final Point selection = text.getSelection();
2691                                 final int caret = text.getCaretOffset();
2692                                 final int offset = modelOffset2WidgetOffset(viewer, position);
2693
2694                                 if (caret == selection.x)
2695                                         text.setSelectionRange(selection.y, offset - selection.y);
2696                                 else
2697                                         text.setSelectionRange(selection.x, offset - selection.x);
2698                         }
2699                 }
2700         }
2701
2702         // static protected class AnnotationAccess implements IAnnotationAccess {
2703         // /*
2704         // * @see
2705         // org.eclipse.jface.text.source.IAnnotationAccess#getType(org.eclipse.jface.text.source.Annotation)
2706         // */
2707         // public Object getType(Annotation annotation) {
2708         // if (annotation instanceof IJavaAnnotation) {
2709         // IJavaAnnotation javaAnnotation = (IJavaAnnotation) annotation;
2710         // // if (javaAnnotation.isRelevant())
2711         // // return javaAnnotation.getAnnotationType();
2712         // }
2713         // return null;
2714         // }
2715         //
2716         // /*
2717         // * @see
2718         // org.eclipse.jface.text.source.IAnnotationAccess#isMultiLine(org.eclipse.jface.text.source.Annotation)
2719         // */
2720         // public boolean isMultiLine(Annotation annotation) {
2721         // return true;
2722         // }
2723         //
2724         // /*
2725         // * @see
2726         // org.eclipse.jface.text.source.IAnnotationAccess#isTemporary(org.eclipse.jface.text.source.Annotation)
2727         // */
2728         // public boolean isTemporary(Annotation annotation) {
2729         // if (annotation instanceof IJavaAnnotation) {
2730         // IJavaAnnotation javaAnnotation = (IJavaAnnotation) annotation;
2731         // if (javaAnnotation.isRelevant())
2732         // return javaAnnotation.isTemporary();
2733         // }
2734         // return false;
2735         // }
2736         // };
2737
2738         private class PropertyChangeListener implements
2739                         org.eclipse.core.runtime.Preferences.IPropertyChangeListener {
2740                 /*
2741                  * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
2742                  */
2743                 public void propertyChange(
2744                                 org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
2745                         handlePreferencePropertyChanged(event);
2746                 }
2747         };
2748
2749         /**
2750          * Finds and marks occurrence annotations.
2751          * 
2752          * @since 3.0
2753          */
2754         class OccurrencesFinderJob extends Job {
2755
2756                 private IDocument fDocument;
2757
2758                 private ISelection fSelection;
2759
2760                 private ISelectionValidator fPostSelectionValidator;
2761
2762                 private boolean fCanceled = false;
2763
2764                 private IProgressMonitor fProgressMonitor;
2765
2766                 private Position[] fPositions;
2767
2768                 public OccurrencesFinderJob(IDocument document, Position[] positions,
2769                                 ISelection selection) {
2770                         super(PHPEditorMessages.JavaEditor_markOccurrences_job_name);
2771                         fDocument = document;
2772                         fSelection = selection;
2773                         fPositions = positions;
2774
2775                         if (getSelectionProvider() instanceof ISelectionValidator)
2776                                 fPostSelectionValidator = (ISelectionValidator) getSelectionProvider();
2777                 }
2778
2779                 // cannot use cancel() because it is declared final
2780                 void doCancel() {
2781                         fCanceled = true;
2782                         cancel();
2783                 }
2784
2785                 private boolean isCanceled() {
2786                         return fCanceled
2787                                         || fProgressMonitor.isCanceled()
2788                                         || fPostSelectionValidator != null
2789                                         && !(fPostSelectionValidator.isValid(fSelection) || fForcedMarkOccurrencesSelection == fSelection)
2790                                         || LinkedModeModel.hasInstalledModel(fDocument);
2791                 }
2792
2793                 /*
2794                  * @see Job#run(org.eclipse.core.runtime.IProgressMonitor)
2795                  */
2796                 public IStatus run(IProgressMonitor progressMonitor) {
2797
2798                         fProgressMonitor = progressMonitor;
2799
2800                         if (isCanceled())
2801                                 return Status.CANCEL_STATUS;
2802
2803                         ITextViewer textViewer = getViewer();
2804                         if (textViewer == null)
2805                                 return Status.CANCEL_STATUS;
2806
2807                         IDocument document = textViewer.getDocument();
2808                         if (document == null)
2809                                 return Status.CANCEL_STATUS;
2810
2811                         IDocumentProvider documentProvider = getDocumentProvider();
2812                         if (documentProvider == null)
2813                                 return Status.CANCEL_STATUS;
2814
2815                         IAnnotationModel annotationModel = documentProvider
2816                                         .getAnnotationModel(getEditorInput());
2817                         if (annotationModel == null)
2818                                 return Status.CANCEL_STATUS;
2819
2820                         // Add occurrence annotations
2821                         int length = fPositions.length;
2822                         Map annotationMap = new HashMap(length);
2823                         for (int i = 0; i < length; i++) {
2824
2825                                 if (isCanceled())
2826                                         return Status.CANCEL_STATUS;
2827
2828                                 String message;
2829                                 Position position = fPositions[i];
2830
2831                                 // Create & add annotation
2832                                 try {
2833                                         message = document.get(position.offset, position.length);
2834                                 } catch (BadLocationException ex) {
2835                                         // Skip this match
2836                                         continue;
2837                                 }
2838                                 annotationMap
2839                                                 .put(
2840                                                                 new Annotation(
2841                                                                                 "net.sourceforge.phpdt.ui.occurrences", false, message), //$NON-NLS-1$
2842                                                                 position);
2843                         }
2844
2845                         if (isCanceled())
2846                                 return Status.CANCEL_STATUS;
2847
2848                         synchronized (getLockObject(annotationModel)) {
2849                                 if (annotationModel instanceof IAnnotationModelExtension) {
2850                                         ((IAnnotationModelExtension) annotationModel)
2851                                                         .replaceAnnotations(fOccurrenceAnnotations,
2852                                                                         annotationMap);
2853                                 } else {
2854                                         removeOccurrenceAnnotations();
2855                                         Iterator iter = annotationMap.entrySet().iterator();
2856                                         while (iter.hasNext()) {
2857                                                 Map.Entry mapEntry = (Map.Entry) iter.next();
2858                                                 annotationModel.addAnnotation((Annotation) mapEntry
2859                                                                 .getKey(), (Position) mapEntry.getValue());
2860                                         }
2861                                 }
2862                                 fOccurrenceAnnotations = (Annotation[]) annotationMap.keySet()
2863                                                 .toArray(new Annotation[annotationMap.keySet().size()]);
2864                         }
2865
2866                         return Status.OK_STATUS;
2867                 }
2868         }
2869
2870         /**
2871          * Cancels the occurrences finder job upon document changes.
2872          * 
2873          * @since 3.0
2874          */
2875         class OccurrencesFinderJobCanceler implements IDocumentListener,
2876                         ITextInputListener {
2877
2878                 public void install() {
2879                         ISourceViewer sourceViewer = getSourceViewer();
2880                         if (sourceViewer == null)
2881                                 return;
2882
2883                         StyledText text = sourceViewer.getTextWidget();
2884                         if (text == null || text.isDisposed())
2885                                 return;
2886
2887                         sourceViewer.addTextInputListener(this);
2888
2889                         IDocument document = sourceViewer.getDocument();
2890                         if (document != null)
2891                                 document.addDocumentListener(this);
2892                 }
2893
2894                 public void uninstall() {
2895                         ISourceViewer sourceViewer = getSourceViewer();
2896                         if (sourceViewer != null)
2897                                 sourceViewer.removeTextInputListener(this);
2898
2899                         IDocumentProvider documentProvider = getDocumentProvider();
2900                         if (documentProvider != null) {
2901                                 IDocument document = documentProvider
2902                                                 .getDocument(getEditorInput());
2903                                 if (document != null)
2904                                         document.removeDocumentListener(this);
2905                         }
2906                 }
2907
2908                 /*
2909                  * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
2910                  */
2911                 public void documentAboutToBeChanged(DocumentEvent event) {
2912                         if (fOccurrencesFinderJob != null)
2913                                 fOccurrencesFinderJob.doCancel();
2914                 }
2915
2916                 /*
2917                  * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
2918                  */
2919                 public void documentChanged(DocumentEvent event) {
2920                 }
2921
2922                 /*
2923                  * @see org.eclipse.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(org.eclipse.jface.text.IDocument,
2924                  *      org.eclipse.jface.text.IDocument)
2925                  */
2926                 public void inputDocumentAboutToBeChanged(IDocument oldInput,
2927                                 IDocument newInput) {
2928                         if (oldInput == null)
2929                                 return;
2930
2931                         oldInput.removeDocumentListener(this);
2932                 }
2933
2934                 /*
2935                  * @see org.eclipse.jface.text.ITextInputListener#inputDocumentChanged(org.eclipse.jface.text.IDocument,
2936                  *      org.eclipse.jface.text.IDocument)
2937                  */
2938                 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
2939                         if (newInput == null)
2940                                 return;
2941                         newInput.addDocumentListener(this);
2942                 }
2943         }
2944
2945         /**
2946          * Internal activation listener.
2947          * 
2948          * @since 3.0
2949          */
2950         private class ActivationListener implements IWindowListener {
2951
2952                 /*
2953                  * @see org.eclipse.ui.IWindowListener#windowActivated(org.eclipse.ui.IWorkbenchWindow)
2954                  * @since 3.1
2955                  */
2956                 public void windowActivated(IWorkbenchWindow window) {
2957                         if (window == getEditorSite().getWorkbenchWindow()
2958                                         && fMarkOccurrenceAnnotations && isActivePart()) {
2959                                 fForcedMarkOccurrencesSelection = getSelectionProvider()
2960                                                 .getSelection();
2961                                 SelectionListenerWithASTManager
2962                                                 .getDefault()
2963                                                 .forceSelectionChange(
2964                                                                 PHPEditor.this,
2965                                                                 (ITextSelection) fForcedMarkOccurrencesSelection);
2966                         }
2967                 }
2968
2969                 /*
2970                  * @see org.eclipse.ui.IWindowListener#windowDeactivated(org.eclipse.ui.IWorkbenchWindow)
2971                  * @since 3.1
2972                  */
2973                 public void windowDeactivated(IWorkbenchWindow window) {
2974                         if (window == getEditorSite().getWorkbenchWindow()
2975                                         && fMarkOccurrenceAnnotations && isActivePart())
2976                                 removeOccurrenceAnnotations();
2977                 }
2978
2979                 /*
2980                  * @see org.eclipse.ui.IWindowListener#windowClosed(org.eclipse.ui.IWorkbenchWindow)
2981                  * @since 3.1
2982                  */
2983                 public void windowClosed(IWorkbenchWindow window) {
2984                 }
2985
2986                 /*
2987                  * @see org.eclipse.ui.IWindowListener#windowOpened(org.eclipse.ui.IWorkbenchWindow)
2988                  * @since 3.1
2989                  */
2990                 public void windowOpened(IWorkbenchWindow window) {
2991                 }
2992         }
2993
2994         /**
2995          * Updates the selection in the editor's widget with the selection of the
2996          * outline page.
2997          */
2998         class OutlineSelectionChangedListener extends
2999                         AbstractSelectionChangedListener {
3000                 public void selectionChanged(SelectionChangedEvent event) {
3001                         doSelectionChanged(event);
3002                 }
3003         }
3004
3005         /**
3006          * The internal shell activation listener for updating occurrences.
3007          * 
3008          * @since 3.0
3009          */
3010         private ActivationListener fActivationListener = new ActivationListener();
3011
3012         private ISelectionListenerWithAST fPostSelectionListenerWithAST;
3013
3014         private OccurrencesFinderJob fOccurrencesFinderJob;
3015
3016         /** The occurrences finder job canceler */
3017         private OccurrencesFinderJobCanceler fOccurrencesFinderJobCanceler;
3018
3019         /**
3020          * Holds the current occurrence annotations.
3021          * 
3022          * @since 3.0
3023          */
3024         private Annotation[] fOccurrenceAnnotations = null;
3025
3026         /**
3027          * Tells whether all occurrences of the element at the current caret
3028          * location are automatically marked in this editor.
3029          * 
3030          * @since 3.0
3031          */
3032         private boolean fMarkOccurrenceAnnotations;
3033
3034         /**
3035          * The selection used when forcing occurrence marking through code.
3036          * 
3037          * @since 3.0
3038          */
3039         private ISelection fForcedMarkOccurrencesSelection;
3040
3041         /**
3042          * The document modification stamp at the time when the last occurrence
3043          * marking took place.
3044          * 
3045          * @since 3.1
3046          */
3047         private long fMarkOccurrenceModificationStamp = IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP;
3048
3049         /**
3050          * The region of the word under the caret used to when computing the current
3051          * occurrence markings.
3052          * 
3053          * @since 3.1
3054          */
3055         private IRegion fMarkOccurrenceTargetRegion;
3056
3057         /**
3058          * Tells whether the occurrence annotations are sticky i.e. whether they
3059          * stay even if there's no valid Java element at the current caret position.
3060          * Only valid if {@link #fMarkOccurrenceAnnotations} is <code>true</code>.
3061          * 
3062          * @since 3.0
3063          */
3064         private boolean fStickyOccurrenceAnnotations;
3065
3066         /** Preference key for the link color */
3067         private final static String LINK_COLOR = PreferenceConstants.EDITOR_LINK_COLOR;
3068
3069         /** Preference key for compiler task tags */
3070         private final static String COMPILER_TASK_TAGS = JavaCore.COMPILER_TASK_TAGS;
3071
3072         // protected PHPActionGroup fActionGroups;
3073         // /** The outline page */
3074         // private AbstractContentOutlinePage fOutlinePage;
3075         /** The outline page */
3076         protected JavaOutlinePage fOutlinePage;
3077
3078         /** Outliner context menu Id */
3079         protected String fOutlinerContextMenuId;
3080
3081         /**
3082          * Indicates whether this editor should react on outline page selection
3083          * changes
3084          */
3085         private int fIgnoreOutlinePageSelection;
3086
3087         /** The outline page selection updater */
3088         // private OutlinePageSelectionUpdater fUpdater;
3089         // protected PHPSyntaxParserThread fValidationThread = null;
3090         // private IPreferenceStore fPHPPrefStore;
3091         /** The selection changed listener */
3092         // protected ISelectionChangedListener fSelectionChangedListener = new
3093         // SelectionChangedListener();
3094         /**
3095          * The editor selection changed listener.
3096          * 
3097          * @since 3.0
3098          */
3099         private EditorSelectionChangedListener fEditorSelectionChangedListener;
3100
3101         /** The selection changed listener */
3102         protected AbstractSelectionChangedListener fOutlineSelectionChangedListener = new OutlineSelectionChangedListener();
3103
3104         /** The editor's bracket matcher */
3105         private PHPPairMatcher fBracketMatcher = new PHPPairMatcher(BRACKETS);
3106
3107         /** The line number ruler column */
3108         // private LineNumberRulerColumn fLineNumberRulerColumn;
3109         /** This editor's encoding support */
3110         private DefaultEncodingSupport fEncodingSupport;
3111
3112         /** The mouse listener */
3113         private MouseClickListener fMouseListener;
3114
3115         /**
3116          * Indicates whether this editor is about to update any annotation views.
3117          * 
3118          * @since 3.0
3119          */
3120         private boolean fIsUpdatingAnnotationViews = false;
3121
3122         /**
3123          * The marker that served as last target for a goto marker request.
3124          * 
3125          * @since 3.0
3126          */
3127         private IMarker fLastMarkerTarget = null;
3128
3129         protected CompositeActionGroup fActionGroups;
3130
3131         protected CompositeActionGroup fContextMenuGroup;
3132
3133         /**
3134          * This editor's projection support
3135          * 
3136          * @since 3.0
3137          */
3138         private ProjectionSupport fProjectionSupport;
3139
3140         /**
3141          * This editor's projection model updater
3142          * 
3143          * @since 3.0
3144          */
3145         private IJavaFoldingStructureProvider fProjectionModelUpdater;
3146
3147         /**
3148          * The override and implements indicator manager for this editor.
3149          * 
3150          * @since 3.0
3151          */
3152         // protected OverrideIndicatorManager fOverrideIndicatorManager;
3153         /**
3154          * The action group for folding.
3155          * 
3156          * @since 3.0
3157          */
3158         private FoldingActionGroup fFoldingGroup;
3159
3160         /** The information presenter. */
3161         private InformationPresenter fInformationPresenter;
3162
3163         /** The annotation access */
3164         // protected IAnnotationAccess fAnnotationAccess = new AnnotationAccess();
3165         /** The overview ruler */
3166         protected OverviewRuler isOverviewRulerVisible;
3167
3168         /** The source viewer decoration support */
3169         // protected SourceViewerDecorationSupport fSourceViewerDecorationSupport;
3170         /** The overview ruler */
3171         // protected OverviewRuler fOverviewRuler;
3172         /** The preference property change listener for java core. */
3173         private org.eclipse.core.runtime.Preferences.IPropertyChangeListener fPropertyChangeListener = new PropertyChangeListener();
3174
3175         /**
3176          * Returns the most narrow java element including the given offset
3177          * 
3178          * @param offset
3179          *            the offset inside of the requested element
3180          */
3181         abstract protected IJavaElement getElementAt(int offset);
3182
3183         /**
3184          * Returns the java element of this editor's input corresponding to the
3185          * given IJavaElement
3186          */
3187         abstract protected IJavaElement getCorrespondingElement(IJavaElement element);
3188
3189         /**
3190          * Sets the input of the editor's outline page.
3191          */
3192         abstract protected void setOutlinePageInput(JavaOutlinePage page,
3193                         IEditorInput input);
3194
3195         /**
3196          * Default constructor.
3197          */
3198         public PHPEditor() {
3199                 super();                
3200         }
3201
3202         /*
3203          * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeKeyBindingScopes()
3204          */
3205         protected void initializeKeyBindingScopes() {
3206                 setKeyBindingScopes(new String[] { "net.sourceforge.phpdt.ui.phpEditorScope" }); //$NON-NLS-1$
3207         }
3208
3209         /*
3210          * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeEditor()
3211          */
3212         protected void initializeEditor() {
3213                 // jsurfer old code
3214                 // JavaTextTools textTools =
3215                 // PHPeclipsePlugin.getDefault().getJavaTextTools();
3216                 // setSourceViewerConfiguration(new
3217                 // PHPSourceViewerConfiguration(textTools,
3218                 // this, IPHPPartitions.PHP_PARTITIONING)); //,
3219                 // IJavaPartitions.JAVA_PARTITIONING));
3220                 IPreferenceStore store = createCombinedPreferenceStore(null);
3221                 setPreferenceStore(store);
3222                 JavaTextTools textTools = PHPeclipsePlugin.getDefault()
3223                                 .getJavaTextTools();
3224                 setSourceViewerConfiguration(new PHPSourceViewerConfiguration(textTools
3225                                 .getColorManager(), store, this,
3226                                 IPHPPartitions.PHP_PARTITIONING));
3227                 
3228                 // TODO changed in 3.x ?
3229                 // setRangeIndicator(new DefaultRangeIndicator());
3230                 // if
3231                 // (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE))
3232                 // fUpdater = new OutlinePageSelectionUpdater();
3233                 // jsurfer end
3234
3235                 // IPreferenceStore store= createCombinedPreferenceStore(null);
3236                 // setPreferenceStore(store);
3237                 // JavaTextTools textTools=
3238                 // PHPeclipsePlugin.getDefault().getJavaTextTools();
3239                 // setSourceViewerConfiguration(new
3240                 // JavaSourceViewerConfiguration(textTools.getColorManager(), store,
3241                 // this, IJavaPartitions.JAVA_PARTITIONING));
3242                 fMarkOccurrenceAnnotations = store
3243                                 .getBoolean(PreferenceConstants.EDITOR_MARK_OCCURRENCES);
3244                 fStickyOccurrenceAnnotations = store
3245                                 .getBoolean(PreferenceConstants.EDITOR_STICKY_OCCURRENCES);
3246                 // fMarkTypeOccurrences=
3247                 // store.getBoolean(PreferenceConstants.EDITOR_MARK_TYPE_OCCURRENCES);
3248                 // fMarkMethodOccurrences=
3249                 // store.getBoolean(PreferenceConstants.EDITOR_MARK_METHOD_OCCURRENCES);
3250                 // fMarkConstantOccurrences=
3251                 // store.getBoolean(PreferenceConstants.EDITOR_MARK_CONSTANT_OCCURRENCES);
3252                 // fMarkFieldOccurrences=
3253                 // store.getBoolean(PreferenceConstants.EDITOR_MARK_FIELD_OCCURRENCES);
3254                 // fMarkLocalVariableypeOccurrences=
3255                 // store.getBoolean(PreferenceConstants.EDITOR_MARK_LOCAL_VARIABLE_OCCURRENCES);
3256                 // fMarkExceptions=
3257                 // store.getBoolean(PreferenceConstants.EDITOR_MARK_EXCEPTION_OCCURRENCES);
3258                 // fMarkImplementors=
3259                 // store.getBoolean(PreferenceConstants.EDITOR_MARK_IMPLEMENTORS);
3260                 // fMarkMethodExitPoints=
3261                 // store.getBoolean(PreferenceConstants.EDITOR_MARK_METHOD_EXIT_POINTS);
3262
3263         }
3264
3265         /*
3266          * @see org.eclipse.ui.texteditor.AbstractTextEditor#updatePropertyDependentActions()
3267          */
3268         protected void updatePropertyDependentActions() {
3269                 super.updatePropertyDependentActions();
3270                 if (fEncodingSupport != null)
3271                         fEncodingSupport.reset();
3272         }
3273
3274         /*
3275          * Update the hovering behavior depending on the preferences.
3276          */
3277         private void updateHoverBehavior() {
3278                 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
3279                 String[] types = configuration
3280                                 .getConfiguredContentTypes(getSourceViewer());
3281
3282                 for (int i = 0; i < types.length; i++) {
3283
3284                         String t = types[i];
3285
3286                         int[] stateMasks = configuration.getConfiguredTextHoverStateMasks(
3287                                         getSourceViewer(), t);
3288
3289                         ISourceViewer sourceViewer = getSourceViewer();
3290                         if (sourceViewer instanceof ITextViewerExtension2) {
3291                                 if (stateMasks != null) {
3292                                         for (int j = 0; j < stateMasks.length; j++) {
3293                                                 int stateMask = stateMasks[j];
3294                                                 ITextHover textHover = configuration.getTextHover(
3295                                                                 sourceViewer, t, stateMask);
3296                                                 ((ITextViewerExtension2) sourceViewer).setTextHover(
3297                                                                 textHover, t, stateMask);
3298                                         }
3299                                 } else {
3300                                         ITextHover textHover = configuration.getTextHover(
3301                                                         sourceViewer, t);
3302                                         ((ITextViewerExtension2) sourceViewer).setTextHover(
3303                                                         textHover, t,
3304                                                         ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
3305                                 }
3306                         } else
3307                                 sourceViewer.setTextHover(configuration.getTextHover(
3308                                                 sourceViewer, t), t);
3309                 }
3310         }
3311
3312         public void updatedTitleImage(Image image) {
3313                 setTitleImage(image);
3314         }
3315
3316         /*
3317          * @see net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
3318          */
3319         public Object getViewPartInput() {
3320                 return getEditorInput().getAdapter(IResource.class);
3321         }
3322
3323         /*
3324          * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSetSelection(ISelection)
3325          */
3326         protected void doSetSelection(ISelection selection) {
3327                 super.doSetSelection(selection);
3328                 synchronizeOutlinePageSelection();
3329         }
3330
3331         boolean isFoldingEnabled() {
3332                 return PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(
3333                                 PreferenceConstants.EDITOR_FOLDING_ENABLED);
3334         }
3335
3336         /*
3337          * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.
3338          *      widgets.Composite)
3339          */
3340         public void createPartControl(Composite parent) {
3341                 super.createPartControl(parent);
3342
3343                 // fSourceViewerDecorationSupport.install(getPreferenceStore());
3344
3345                 ProjectionViewer projectionViewer = (ProjectionViewer) getSourceViewer();
3346
3347                 fProjectionSupport = new ProjectionSupport(projectionViewer,
3348                                 getAnnotationAccess(), getSharedColors());
3349                 fProjectionSupport
3350                                 .addSummarizableAnnotationType("org.eclipse.ui.workbench.texteditor.error"); //$NON-NLS-1$
3351                 fProjectionSupport
3352                                 .addSummarizableAnnotationType("org.eclipse.ui.workbench.texteditor.warning"); //$NON-NLS-1$
3353                 fProjectionSupport
3354                                 .setHoverControlCreator(new IInformationControlCreator() {
3355                                         public IInformationControl createInformationControl(
3356                                                         Shell shell) {
3357                                                 return new CustomSourceInformationControl(shell,
3358                                                                 IDocument.DEFAULT_CONTENT_TYPE);
3359                                         }
3360                                 });
3361                 fProjectionSupport.install();
3362
3363                 fProjectionModelUpdater = PHPeclipsePlugin.getDefault()
3364                                 .getFoldingStructureProviderRegistry()
3365                                 .getCurrentFoldingProvider();
3366                 if (fProjectionModelUpdater != null)
3367                         fProjectionModelUpdater.install(this, projectionViewer);
3368
3369                 if (isFoldingEnabled())
3370                         projectionViewer.doOperation(ProjectionViewer.TOGGLE);
3371                 Preferences preferences = PHPeclipsePlugin.getDefault()
3372                                 .getPluginPreferences();
3373                 preferences.addPropertyChangeListener(fPropertyChangeListener);
3374                 IInformationControlCreator informationControlCreator = new IInformationControlCreator() {
3375                         public IInformationControl createInformationControl(Shell parent) {
3376                                 boolean cutDown = false;
3377                                 int style = cutDown ? SWT.NONE : (SWT.V_SCROLL | SWT.H_SCROLL);
3378                                 return new DefaultInformationControl(parent, SWT.RESIZE, style,
3379                                                 new HTMLTextPresenter(cutDown));
3380                         }
3381                 };
3382
3383                 fInformationPresenter = new InformationPresenter(
3384                                 informationControlCreator);
3385                 fInformationPresenter.setSizeConstraints(60, 10, true, true);
3386                 fInformationPresenter.install(getSourceViewer());
3387
3388                 fEditorSelectionChangedListener = new EditorSelectionChangedListener();
3389                 fEditorSelectionChangedListener.install(getSelectionProvider());
3390
3391                 if (isBrowserLikeLinks())
3392                         enableBrowserLikeLinks();
3393
3394                 if (PreferenceConstants.getPreferenceStore().getBoolean(
3395                                 PreferenceConstants.EDITOR_DISABLE_OVERWRITE_MODE))
3396                         enableOverwriteMode(false);
3397
3398                 if (fMarkOccurrenceAnnotations)
3399                         installOccurrencesFinder();
3400
3401                 PlatformUI.getWorkbench().addWindowListener(fActivationListener);
3402                 
3403                 /*
3404                  * start of EDITOR_SAVE_ON_BLUR
3405                  * ed_mann
3406                  */
3407                 final PHPEditor editor = this;
3408                 FocusListener focusListener = new FocusListener() {
3409                         
3410                       public void focusGained(FocusEvent e) {
3411                           return;
3412                         }
3413
3414                         public void focusLost(FocusEvent e) {
3415                                 //viewer.get
3416                                 if(editor.isDirty() && PHPeclipsePlugin.getDefault().getPreferenceStore()
3417                                                         .getBoolean(PreferenceConstants.EDITOR_SAVE_ON_BLUR)){
3418                                         editor.doSave(null);
3419                                 }
3420                         }
3421                       };
3422                   projectionViewer.getTextWidget().addFocusListener(focusListener);
3423                         /*
3424                          * end of EDITOR_SAVE_ON_BLUR
3425                          * ed_mann
3426                          */
3427                   
3428                 setWordWrap();
3429         }
3430
3431         private void setWordWrap() {
3432                 if (getSourceViewer() != null) {
3433                         getSourceViewer().getTextWidget().setWordWrap(
3434                                         PHPeclipsePlugin.getDefault().getPreferenceStore()
3435                                                         .getBoolean(PreferenceConstants.EDITOR_WRAP_WORDS));
3436                 }
3437         }
3438
3439         protected void configureSourceViewerDecorationSupport(
3440                         SourceViewerDecorationSupport support) {
3441
3442                 support.setCharacterPairMatcher(fBracketMatcher);
3443                 support.setMatchingCharacterPainterPreferenceKeys(MATCHING_BRACKETS,
3444                                 MATCHING_BRACKETS_COLOR);
3445
3446                 super.configureSourceViewerDecorationSupport(support);
3447         }
3448
3449         /*
3450          * @see org.eclipse.ui.texteditor.AbstractTextEditor#gotoMarker(org.eclipse.core.resources.IMarker)
3451          */
3452         public void gotoMarker(IMarker marker) {
3453                 fLastMarkerTarget = marker;
3454                 if (!fIsUpdatingAnnotationViews) {
3455                         super.gotoMarker(marker);
3456                 }
3457         }
3458
3459         /**
3460          * Jumps to the next enabled annotation according to the given direction. An
3461          * annotation type is enabled if it is configured to be in the Next/Previous
3462          * tool bar drop down menu and if it is checked.
3463          * 
3464          * @param forward
3465          *            <code>true</code> if search direction is forward,
3466          *            <code>false</code> if backward
3467          */
3468         public Annotation gotoAnnotation(boolean forward) {
3469                 ITextSelection selection = (ITextSelection) getSelectionProvider()
3470                                 .getSelection();
3471                 Position position = new Position(0, 0);
3472                 Annotation annotation = null;
3473                 if (false /* delayed - see bug 18316 */) {
3474                         annotation = getNextAnnotation(selection.getOffset(), selection
3475                                         .getLength(), forward, position);
3476                         selectAndReveal(position.getOffset(), position.getLength());
3477                 } else /* no delay - see bug 18316 */{
3478                         annotation = getNextAnnotation(selection.getOffset(), selection
3479                                         .getLength(), forward, position);
3480                         setStatusLineErrorMessage(null);
3481                         setStatusLineMessage(null);
3482                         if (annotation != null) {
3483                                 updateAnnotationViews(annotation);
3484                                 selectAndReveal(position.getOffset(), position.getLength());
3485                                 setStatusLineMessage(annotation.getText());
3486                         }
3487                 }
3488                 return annotation;
3489         }
3490
3491         /**
3492          * Returns the lock object for the given annotation model.
3493          * 
3494          * @param annotationModel
3495          *            the annotation model
3496          * @return the annotation model's lock object
3497          * @since 3.0
3498          */
3499         private Object getLockObject(IAnnotationModel annotationModel) {
3500                 if (annotationModel instanceof ISynchronizable)
3501                         return ((ISynchronizable) annotationModel).getLockObject();
3502                 else
3503                         return annotationModel;
3504         }
3505
3506         /**
3507          * Updates the annotation views that show the given annotation.
3508          * 
3509          * @param annotation
3510          *            the annotation
3511          */
3512         private void updateAnnotationViews(Annotation annotation) {
3513                 IMarker marker = null;
3514                 if (annotation instanceof MarkerAnnotation)
3515                         marker = ((MarkerAnnotation) annotation).getMarker();
3516                 else if (annotation instanceof IJavaAnnotation) {
3517                         Iterator e = ((IJavaAnnotation) annotation).getOverlaidIterator();
3518                         if (e != null) {
3519                                 while (e.hasNext()) {
3520                                         Object o = e.next();
3521                                         if (o instanceof MarkerAnnotation) {
3522                                                 marker = ((MarkerAnnotation) o).getMarker();
3523                                                 break;
3524                                         }
3525                                 }
3526                         }
3527                 }
3528
3529                 if (marker != null && !marker.equals(fLastMarkerTarget)) {
3530                         try {
3531                                 boolean isProblem = marker.isSubtypeOf(IMarker.PROBLEM);
3532                                 IWorkbenchPage page = getSite().getPage();
3533                                 IViewPart view = page
3534                                                 .findView(isProblem ? IPageLayout.ID_PROBLEM_VIEW
3535                                                                 : IPageLayout.ID_TASK_LIST); //$NON-NLS-1$  //$NON-NLS-2$
3536                                 if (view != null) {
3537                                         Method method = view
3538                                                         .getClass()
3539                                                         .getMethod(
3540                                                                         "setSelection", new Class[] { IStructuredSelection.class, boolean.class }); //$NON-NLS-1$
3541                                         method.invoke(view, new Object[] {
3542                                                         new StructuredSelection(marker), Boolean.TRUE });
3543                                 }
3544                         } catch (CoreException x) {
3545                         } catch (NoSuchMethodException x) {
3546                         } catch (IllegalAccessException x) {
3547                         } catch (InvocationTargetException x) {
3548                         }
3549                         // ignore exceptions, don't update any of the lists, just set status
3550                         // line
3551                 }
3552         }
3553
3554         /**
3555          * Returns this document's complete text.
3556          * 
3557          * @return the document's complete text
3558          */
3559         public String get() {
3560                 IDocument doc = this.getDocumentProvider().getDocument(
3561                                 this.getEditorInput());
3562                 return doc.get();
3563         }
3564
3565         /**
3566          * Sets the outliner's context menu ID.
3567          */
3568         protected void setOutlinerContextMenuId(String menuId) {
3569                 fOutlinerContextMenuId = menuId;
3570         }
3571
3572         /**
3573          * Returns the standard action group of this editor.
3574          */
3575         protected ActionGroup getActionGroup() {
3576                 return fActionGroups;
3577         }
3578
3579         // public JavaOutlinePage getfOutlinePage() {
3580         // return fOutlinePage;
3581         // }
3582
3583         /**
3584          * The <code>PHPEditor</code> implementation of this
3585          * <code>AbstractTextEditor</code> method extend the actions to add those
3586          * specific to the receiver
3587          */
3588         protected void createActions() {
3589                 super.createActions();
3590
3591                 ActionGroup oeg, ovg, jsg, sg;
3592                 fActionGroups = new CompositeActionGroup(
3593                                 new ActionGroup[] { oeg = new OpenEditorActionGroup(this),
3594                                 // sg= new ShowActionGroup(this),
3595                                 // ovg= new OpenViewActionGroup(this),
3596                                 // jsg= new JavaSearchActionGroup(this)
3597                                 });
3598                 fContextMenuGroup = new CompositeActionGroup(new ActionGroup[] { oeg });
3599                 // , ovg, sg, jsg});
3600
3601                 fFoldingGroup = new FoldingActionGroup(this, getViewer());
3602
3603                 // ResourceAction resAction = new
3604                 // TextOperationAction(PHPEditorMessages.getResourceBundle(),
3605                 // "ShowJavaDoc.", this, ISourceViewer.INFORMATION, true); //$NON-NLS-1$
3606                 // resAction = new
3607                 // InformationDispatchAction(PHPEditorMessages.getResourceBundle(),
3608                 // "ShowJavaDoc.", (TextOperationAction) resAction); //$NON-NLS-1$
3609                 // resAction.setActionDefinitionId(net.sourceforge.phpdt.ui.actions.PHPEditorActionDefinitionIds.SHOW_JAVADOC);
3610                 // setAction("ShowJavaDoc", resAction); //$NON-NLS-1$
3611
3612                 // WorkbenchHelp.setHelp(resAction,
3613                 // IJavaHelpContextIds.SHOW_JAVADOC_ACTION);
3614
3615                 Action action = new GotoMatchingBracketAction(this);
3616                 action
3617                                 .setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
3618                 setAction(GotoMatchingBracketAction.GOTO_MATCHING_BRACKET, action);
3619
3620                 // action= new
3621                 // TextOperationAction(PHPEditorMessages.getResourceBundle(),"ShowOutline.",
3622                 // this, JavaSourceViewer.SHOW_OUTLINE, true); //$NON-NLS-1$
3623                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.SHOW_OUTLINE);
3624                 // setAction(PHPEditorActionDefinitionIds.SHOW_OUTLINE, action);
3625                 // // WorkbenchHelp.setHelp(action,
3626                 // IJavaHelpContextIds.SHOW_OUTLINE_ACTION);
3627                 //
3628                 // action= new
3629                 // TextOperationAction(PHPEditorMessages.getResourceBundle(),"OpenStructure.",
3630                 // this, JavaSourceViewer.OPEN_STRUCTURE, true); //$NON-NLS-1$
3631                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.SHOW_OUTLINE.OPEN_STRUCTURE);
3632                 // setAction(PHPEditorActionDefinitionIds.SHOW_OUTLINE.OPEN_STRUCTURE,
3633                 // action);
3634                 // // WorkbenchHelp.setHelp(action,
3635                 // IJavaHelpContextIds.OPEN_STRUCTURE_ACTION);
3636                 //
3637                 // action= new
3638                 // TextOperationAction(PHPEditorMessages.getResourceBundle(),"OpenHierarchy.",
3639                 // this, JavaSourceViewer.SHOW_HIERARCHY, true); //$NON-NLS-1$
3640                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.SHOW_OUTLINE.OPEN_HIERARCHY);
3641                 // setAction(PHPEditorActionDefinitionIds.SHOW_OUTLINE.OPEN_HIERARCHY,
3642                 // action);
3643                 // // WorkbenchHelp.setHelp(action,
3644                 // IJavaHelpContextIds.OPEN_HIERARCHY_ACTION);
3645
3646                 fEncodingSupport = new DefaultEncodingSupport();
3647                 fEncodingSupport.initialize(this);
3648
3649                 // fSelectionHistory= new SelectionHistory(this);
3650                 //
3651                 // action= new StructureSelectEnclosingAction(this, fSelectionHistory);
3652                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.SELECT_ENCLOSING);
3653                 // setAction(StructureSelectionAction.ENCLOSING, action);
3654                 //
3655                 // action= new StructureSelectNextAction(this, fSelectionHistory);
3656                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.SELECT_NEXT);
3657                 // setAction(StructureSelectionAction.NEXT, action);
3658                 //
3659                 // action= new StructureSelectPreviousAction(this, fSelectionHistory);
3660                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.SELECT_PREVIOUS);
3661                 // setAction(StructureSelectionAction.PREVIOUS, action);
3662                 //
3663                 // StructureSelectHistoryAction historyAction= new
3664                 // StructureSelectHistoryAction(this, fSelectionHistory);
3665                 // historyAction.setActionDefinitionId(PHPEditorActionDefinitionIds.SELECT_LAST);
3666                 // setAction(StructureSelectionAction.HISTORY, historyAction);
3667                 // fSelectionHistory.setHistoryAction(historyAction);
3668                 //
3669                 // action= GoToNextPreviousMemberAction.newGoToNextMemberAction(this);
3670                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_NEXT_MEMBER);
3671                 // setAction(GoToNextPreviousMemberAction.NEXT_MEMBER, action);
3672                 //
3673                 // action=
3674                 // GoToNextPreviousMemberAction.newGoToPreviousMemberAction(this);
3675                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_PREVIOUS_MEMBER);
3676                 // setAction(GoToNextPreviousMemberAction.PREVIOUS_MEMBER, action);
3677                 //
3678                 // action= new QuickFormatAction();
3679                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.QUICK_FORMAT);
3680                 // setAction(IJavaEditorActionDefinitionIds.QUICK_FORMAT, action);
3681                 //
3682                 // action= new RemoveOccurrenceAnnotations(this);
3683                 // action.setActionDefinitionId(PHPEditorActionDefinitionIds.REMOVE_OCCURRENCE_ANNOTATIONS);
3684                 // setAction("RemoveOccurrenceAnnotations", action); //$NON-NLS-1$
3685
3686                 // add annotation actions
3687                 action = new JavaSelectMarkerRulerAction2(PHPEditorMessages
3688                                 .getResourceBundle(), "Editor.RulerAnnotationSelection.", this); //$NON-NLS-1$
3689                 setAction("AnnotationAction", action); //$NON-NLS-1$
3690         }
3691
3692         private void internalDoSetInput(IEditorInput input) throws CoreException {
3693                 super.doSetInput(input);
3694
3695                 if (getSourceViewer() instanceof JavaSourceViewer) {
3696                         JavaSourceViewer viewer = (JavaSourceViewer) getSourceViewer();
3697                         if (viewer.getReconciler() == null) {
3698                                 IReconciler reconciler = getSourceViewerConfiguration()
3699                                                 .getReconciler(viewer);
3700                                 if (reconciler != null) {
3701                                         reconciler.install(viewer);
3702                                         viewer.setReconciler(reconciler);
3703                                 }
3704                         }
3705                 }
3706
3707                 if (fEncodingSupport != null)
3708                         fEncodingSupport.reset();
3709
3710                 setOutlinePageInput(fOutlinePage, input);
3711
3712                 if (fProjectionModelUpdater != null)
3713                         fProjectionModelUpdater.initialize();
3714
3715                 // if (isShowingOverrideIndicators())
3716                 // installOverrideIndicator(false);
3717         }
3718
3719         /*
3720          * @see org.eclipse.ui.texteditor.AbstractTextEditor#setPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)
3721          * @since 3.0
3722          */
3723         protected void setPreferenceStore(IPreferenceStore store) {
3724                 super.setPreferenceStore(store);
3725                 if (getSourceViewerConfiguration() instanceof PHPSourceViewerConfiguration) {
3726                         JavaTextTools textTools = PHPeclipsePlugin.getDefault()
3727                                         .getJavaTextTools();
3728                         setSourceViewerConfiguration(new PHPSourceViewerConfiguration(
3729                                         textTools.getColorManager(), store, this,
3730                                         IPHPPartitions.PHP_PARTITIONING));
3731                 }
3732                 if (getSourceViewer() instanceof JavaSourceViewer)
3733                         ((JavaSourceViewer) getSourceViewer()).setPreferenceStore(store);
3734         }
3735
3736         /**
3737          * The <code>PHPEditor</code> implementation of this
3738          * <code>AbstractTextEditor</code> method performs any extra disposal
3739          * actions required by the php editor.
3740          */
3741         public void dispose() {
3742                 if (fProjectionModelUpdater != null) {
3743                         fProjectionModelUpdater.uninstall();
3744                         fProjectionModelUpdater = null;
3745                 }
3746
3747                 if (fProjectionSupport != null) {
3748                         fProjectionSupport.dispose();
3749                         fProjectionSupport = null;
3750                 }
3751                 // PHPEditorEnvironment.disconnect(this);
3752                 if (fOutlinePage != null)
3753                         fOutlinePage.setInput(null);
3754
3755                 if (fActionGroups != null)
3756                         fActionGroups.dispose();
3757
3758                 if (isBrowserLikeLinks())
3759                         disableBrowserLikeLinks();
3760
3761                 // cancel possible running computation
3762                 fMarkOccurrenceAnnotations = false;
3763                 uninstallOccurrencesFinder();
3764
3765                 uninstallOverrideIndicator();
3766
3767                 if (fActivationListener != null) {
3768                         PlatformUI.getWorkbench().removeWindowListener(fActivationListener);
3769                         fActivationListener = null;
3770                 }
3771
3772                 if (fEncodingSupport != null) {
3773                         fEncodingSupport.dispose();
3774                         fEncodingSupport = null;
3775                 }
3776
3777                 if (fPropertyChangeListener != null) {
3778                         Preferences preferences = PHPeclipsePlugin.getDefault()
3779                                         .getPluginPreferences();
3780                         preferences.removePropertyChangeListener(fPropertyChangeListener);
3781                         fPropertyChangeListener = null;
3782                 }
3783
3784                 // if (fSourceViewerDecorationSupport != null) {
3785                 // fSourceViewerDecorationSupport.dispose();
3786                 // fSourceViewerDecorationSupport = null;
3787                 // }
3788
3789                 if (fBracketMatcher != null) {
3790                         fBracketMatcher.dispose();
3791                         fBracketMatcher = null;
3792                 }
3793
3794                 if (fEditorSelectionChangedListener != null) {
3795                         fEditorSelectionChangedListener.uninstall(getSelectionProvider());
3796                         fEditorSelectionChangedListener = null;
3797                 }
3798
3799                 super.dispose();
3800         }
3801
3802         /**
3803          * The <code>PHPEditor</code> implementation of this
3804          * <code>AbstractTextEditor</code> method performs any extra revert
3805          * behavior required by the php editor.
3806          */
3807         // public void doRevertToSaved() {
3808         // super.doRevertToSaved();
3809         // if (fOutlinePage != null)
3810         // fOutlinePage.update();
3811         // }
3812         /**
3813          * The <code>PHPEditor</code> implementation of this
3814          * <code>AbstractTextEditor</code> method performs any extra save behavior
3815          * required by the php editor.
3816          */
3817         // public void doSave(IProgressMonitor monitor) {
3818         // super.doSave(monitor);
3819         // compile or not, according to the user preferences
3820         // IPreferenceStore store = getPreferenceStore();
3821         // the parse on save was changed to the eclipse "builders" concept
3822         // if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
3823         // IAction a = PHPParserAction.getInstance();
3824         // if (a != null)
3825         // a.run();
3826         // }
3827         // if (SWT.getPlatform().equals("win32")) {
3828         // IAction a = ShowExternalPreviewAction.getInstance();
3829         // if (a != null)
3830         // a.run();
3831         // }
3832         // if (fOutlinePage != null)
3833         // fOutlinePage.update();
3834         // }
3835         /**
3836          * The <code>PHPEditor</code> implementation of this
3837          * <code>AbstractTextEditor</code> method performs any extra save as
3838          * behavior required by the php editor.
3839          */
3840         // public void doSaveAs() {
3841         // super.doSaveAs();
3842         // if (fOutlinePage != null)
3843         // fOutlinePage.update();
3844         // }
3845         /*
3846          * @see StatusTextEditor#getStatusHeader(IStatus)
3847          */
3848         protected String getStatusHeader(IStatus status) {
3849                 if (fEncodingSupport != null) {
3850                         String message = fEncodingSupport.getStatusHeader(status);
3851                         if (message != null)
3852                                 return message;
3853                 }
3854                 return super.getStatusHeader(status);
3855         }
3856
3857         /*
3858          * @see StatusTextEditor#getStatusBanner(IStatus)
3859          */
3860         protected String getStatusBanner(IStatus status) {
3861                 if (fEncodingSupport != null) {
3862                         String message = fEncodingSupport.getStatusBanner(status);
3863                         if (message != null)
3864                                 return message;
3865                 }
3866                 return super.getStatusBanner(status);
3867         }
3868
3869         /*
3870          * @see StatusTextEditor#getStatusMessage(IStatus)
3871          */
3872         protected String getStatusMessage(IStatus status) {
3873                 if (fEncodingSupport != null) {
3874                         String message = fEncodingSupport.getStatusMessage(status);
3875                         if (message != null)
3876                                 return message;
3877                 }
3878                 return super.getStatusMessage(status);
3879         }
3880
3881         /**
3882          * The <code>PHPEditor</code> implementation of this
3883          * <code>AbstractTextEditor</code> method performs sets the input of the
3884          * outline page after AbstractTextEditor has set input.
3885          */
3886         // protected void doSetInput(IEditorInput input) throws CoreException {
3887         // super.doSetInput(input);
3888         // if (fEncodingSupport != null)
3889         // fEncodingSupport.reset();
3890         // setOutlinePageInput(fOutlinePage, input);
3891         // }
3892         /*
3893          * @see AbstractTextEditor#doSetInput
3894          */
3895         protected void doSetInput(IEditorInput input) throws CoreException {
3896                 ISourceViewer sourceViewer = getSourceViewer();
3897                 if (!(sourceViewer instanceof ISourceViewerExtension2)) {
3898                         setPreferenceStore(createCombinedPreferenceStore(input));
3899                         internalDoSetInput(input);
3900                         return;
3901                 }
3902
3903                 // uninstall & unregister preference store listener
3904                 if (isBrowserLikeLinks())
3905                         disableBrowserLikeLinks();
3906                 getSourceViewerDecorationSupport(sourceViewer).uninstall();
3907                 ((ISourceViewerExtension2) sourceViewer).unconfigure();
3908
3909                 setPreferenceStore(createCombinedPreferenceStore(input));
3910
3911                 // install & register preference store listener
3912                 sourceViewer.configure(getSourceViewerConfiguration());
3913                 getSourceViewerDecorationSupport(sourceViewer).install(
3914                                 getPreferenceStore());
3915                 if (isBrowserLikeLinks())
3916                         enableBrowserLikeLinks();
3917
3918                 internalDoSetInput(input);
3919         }
3920
3921         /*
3922          * @see org.phpeclipse.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
3923          */
3924         // public Object getViewPartInput() {
3925         // return getEditorInput().getAdapter(IFile.class);
3926         // }
3927         /**
3928          * The <code>PHPEditor</code> implementation of this
3929          * <code>AbstractTextEditor</code> method adds any PHPEditor specific
3930          * entries.
3931          */
3932         public void editorContextMenuAboutToShow(MenuManager menu) {
3933                 super.editorContextMenuAboutToShow(menu);
3934                 menu.appendToGroup(ITextEditorActionConstants.GROUP_UNDO,
3935                                 new Separator(IContextMenuConstants.GROUP_OPEN));
3936                 menu.insertAfter(IContextMenuConstants.GROUP_OPEN, new GroupMarker(
3937                                 IContextMenuConstants.GROUP_SHOW));
3938
3939                 ActionContext context = new ActionContext(getSelectionProvider()
3940                                 .getSelection());
3941                 fContextMenuGroup.setContext(context);
3942                 fContextMenuGroup.fillContextMenu(menu);
3943                 fContextMenuGroup.setContext(null);
3944                 // addAction(menu, ITextEditorActionConstants.GROUP_EDIT, "Format");
3945                 // //$NON-NLS-1$
3946                 //
3947                 // ActionContext context =
3948                 // new ActionContext(getSelectionProvider().getSelection());
3949                 // fContextMenuGroup.setContext(context);
3950                 // fContextMenuGroup.fillContextMenu(menu);
3951                 // fContextMenuGroup.setContext(null);
3952         }
3953
3954         /**
3955          * Creates the outline page used with this editor.
3956          */
3957         protected JavaOutlinePage createOutlinePage() {
3958                 JavaOutlinePage page = new JavaOutlinePage(fOutlinerContextMenuId, this);
3959                 fOutlineSelectionChangedListener.install(page);
3960                 setOutlinePageInput(page, getEditorInput());
3961                 return page;
3962         }
3963
3964         /**
3965          * Informs the editor that its outliner has been closed.
3966          */
3967         public void outlinePageClosed() {
3968                 if (fOutlinePage != null) {
3969                         fOutlineSelectionChangedListener.uninstall(fOutlinePage);
3970                         fOutlinePage = null;
3971                         resetHighlightRange();
3972                 }
3973         }
3974
3975         /**
3976          * Synchronizes the outliner selection with the given element position in
3977          * the editor.
3978          * 
3979          * @param element
3980          *            the java element to select
3981          */
3982         protected void synchronizeOutlinePage(ISourceReference element) {
3983                 synchronizeOutlinePage(element, true);
3984         }
3985
3986         /**
3987          * Synchronizes the outliner selection with the given element position in
3988          * the editor.
3989          * 
3990          * @param element
3991          *            the java element to select
3992          * @param checkIfOutlinePageActive
3993          *            <code>true</code> if check for active outline page needs to
3994          *            be done
3995          */
3996         protected void synchronizeOutlinePage(ISourceReference element,
3997                         boolean checkIfOutlinePageActive) {
3998                 if (fOutlinePage != null && element != null
3999                                 && !(checkIfOutlinePageActive && isJavaOutlinePageActive())) {
4000                         fOutlineSelectionChangedListener.uninstall(fOutlinePage);
4001                         fOutlinePage.select(element);
4002                         fOutlineSelectionChangedListener.install(fOutlinePage);
4003                 }
4004         }
4005
4006         /**
4007          * Synchronizes the outliner selection with the actual cursor position in
4008          * the editor.
4009          */
4010         public void synchronizeOutlinePageSelection() {
4011                 synchronizeOutlinePage(computeHighlightRangeSourceReference());
4012
4013                 // ISourceViewer sourceViewer = getSourceViewer();
4014                 // if (sourceViewer == null || fOutlinePage == null)
4015                 // return;
4016                 //
4017                 // StyledText styledText = sourceViewer.getTextWidget();
4018                 // if (styledText == null)
4019                 // return;
4020                 //
4021                 // int caret = 0;
4022                 // if (sourceViewer instanceof ITextViewerExtension3) {
4023                 // ITextViewerExtension3 extension = (ITextViewerExtension3)
4024                 // sourceViewer;
4025                 // caret =
4026                 // extension.widgetOffset2ModelOffset(styledText.getCaretOffset());
4027                 // } else {
4028                 // int offset = sourceViewer.getVisibleRegion().getOffset();
4029                 // caret = offset + styledText.getCaretOffset();
4030                 // }
4031                 //
4032                 // IJavaElement element = getElementAt(caret);
4033                 // if (element instanceof ISourceReference) {
4034                 // fOutlinePage.removeSelectionChangedListener(fSelectionChangedListener);
4035                 // fOutlinePage.select((ISourceReference) element);
4036                 // fOutlinePage.addSelectionChangedListener(fSelectionChangedListener);
4037                 // }
4038         }
4039
4040         protected void setSelection(ISourceReference reference, boolean moveCursor) {
4041
4042                 ISelection selection = getSelectionProvider().getSelection();
4043                 if (selection instanceof TextSelection) {
4044                         TextSelection textSelection = (TextSelection) selection;
4045                         if (textSelection.getOffset() != 0
4046                                         || textSelection.getLength() != 0)
4047                                 markInNavigationHistory();
4048                 }
4049
4050                 if (reference != null) {
4051
4052                         StyledText textWidget = null;
4053
4054                         ISourceViewer sourceViewer = getSourceViewer();
4055                         if (sourceViewer != null)
4056                                 textWidget = sourceViewer.getTextWidget();
4057
4058                         if (textWidget == null)
4059                                 return;
4060
4061                         try {
4062
4063                                 ISourceRange range = reference.getSourceRange();
4064                                 if (range == null)
4065                                         return;
4066
4067                                 int offset = range.getOffset();
4068                                 int length = range.getLength();
4069
4070                                 if (offset < 0 || length < 0)
4071                                         return;
4072
4073                                 textWidget.setRedraw(false);
4074
4075                                 setHighlightRange(offset, length, moveCursor);
4076
4077                                 if (!moveCursor)
4078                                         return;
4079
4080                                 offset = -1;
4081                                 length = -1;
4082
4083                                 if (reference instanceof IMember) {
4084                                         range = ((IMember) reference).getNameRange();
4085                                         if (range != null) {
4086                                                 offset = range.getOffset();
4087                                                 length = range.getLength();
4088                                         }
4089                                 }
4090                                 // else if (reference instanceof IImportDeclaration) {
4091                                 // String name= ((IImportDeclaration)
4092                                 // reference).getElementName();
4093                                 // if (name != null && name.length() > 0) {
4094                                 // String content= reference.getSource();
4095                                 // if (content != null) {
4096                                 // offset= range.getOffset() + content.indexOf(name);
4097                                 // length= name.length();
4098                                 // }
4099                                 // }
4100                                 // } else if (reference instanceof IPackageDeclaration) {
4101                                 // String name= ((IPackageDeclaration)
4102                                 // reference).getElementName();
4103                                 // if (name != null && name.length() > 0) {
4104                                 // String content= reference.getSource();
4105                                 // if (content != null) {
4106                                 // offset= range.getOffset() + content.indexOf(name);
4107                                 // length= name.length();
4108                                 // }
4109                                 // }
4110                                 // }
4111
4112                                 if (offset > -1 && length > 0) {
4113                                         sourceViewer.revealRange(offset, length);
4114                                         sourceViewer.setSelectedRange(offset, length);
4115                                 }
4116
4117                         } catch (JavaModelException x) {
4118                         } catch (IllegalArgumentException x) {
4119                         } finally {
4120                                 if (textWidget != null)
4121                                         textWidget.setRedraw(true);
4122                         }
4123
4124                 } else if (moveCursor) {
4125                         resetHighlightRange();
4126                 }
4127
4128                 markInNavigationHistory();
4129         }
4130
4131         public void setSelection(IJavaElement element) {
4132                 if (element == null || element instanceof ICompilationUnit) { // ||
4133                         // element
4134                         // instanceof
4135                         // IClassFile)
4136                         // {
4137                         /*
4138                          * If the element is an ICompilationUnit this unit is either the
4139                          * input of this editor or not being displayed. In both cases,
4140                          * nothing should happened.
4141                          * (http://dev.eclipse.org/bugs/show_bug.cgi?id=5128)
4142                          */
4143                         return;
4144                 }
4145
4146                 IJavaElement corresponding = getCorrespondingElement(element);
4147                 if (corresponding instanceof ISourceReference) {
4148                         ISourceReference reference = (ISourceReference) corresponding;
4149                         // set highlight range
4150                         setSelection(reference, true);
4151                         // set outliner selection
4152                         if (fOutlinePage != null) {
4153                                 fOutlineSelectionChangedListener.uninstall(fOutlinePage);
4154                                 fOutlinePage.select(reference);
4155                                 fOutlineSelectionChangedListener.install(fOutlinePage);
4156                         }
4157                 }
4158         }
4159
4160         public synchronized void editingScriptStarted() {
4161                 ++fIgnoreOutlinePageSelection;
4162         }
4163
4164         public synchronized void editingScriptEnded() {
4165                 --fIgnoreOutlinePageSelection;
4166         }
4167
4168         public synchronized boolean isEditingScriptRunning() {
4169                 return (fIgnoreOutlinePageSelection > 0);
4170         }
4171
4172         /**
4173          * The <code>PHPEditor</code> implementation of this
4174          * <code>AbstractTextEditor</code> method performs gets the java content
4175          * outline page if request is for a an outline page.
4176          */
4177         public Object getAdapter(Class required) {
4178
4179                 if (IContentOutlinePage.class.equals(required)) {
4180                         if (fOutlinePage == null)
4181                                 fOutlinePage = createOutlinePage();
4182                         return fOutlinePage;
4183                 }
4184
4185                 if (IEncodingSupport.class.equals(required))
4186                         return fEncodingSupport;
4187
4188                 if (required == IShowInTargetList.class) {
4189                         return new IShowInTargetList() {
4190                                 public String[] getShowInTargetIds() {
4191                                         return new String[] { JavaUI.ID_PACKAGES,
4192                                                         IPageLayout.ID_OUTLINE, IPageLayout.ID_RES_NAV };
4193                                 }
4194
4195                         };
4196                 }
4197                 if (fProjectionSupport != null) {
4198                         Object adapter = fProjectionSupport.getAdapter(getSourceViewer(),
4199                                         required);
4200                         if (adapter != null)
4201                                 return adapter;
4202                 }
4203
4204                 return super.getAdapter(required);
4205         }
4206
4207         // public Object getAdapter(Class required) {
4208         // if (IContentOutlinePage.class.equals(required)) {
4209         // if (fOutlinePage == null) {
4210         // fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
4211         // if (getEditorInput() != null)
4212         // fOutlinePage.setInput(getEditorInput());
4213         // }
4214         // return fOutlinePage;
4215         // }
4216         //
4217         // if (IEncodingSupport.class.equals(required))
4218         // return fEncodingSupport;
4219         //
4220         // return super.getAdapter(required);
4221         // }
4222
4223         protected void doSelectionChanged(SelectionChangedEvent event) {
4224                 ISourceReference reference = null;
4225
4226                 ISelection selection = event.getSelection();
4227                 Iterator iter = ((IStructuredSelection) selection).iterator();
4228                 while (iter.hasNext()) {
4229                         Object o = iter.next();
4230                         if (o instanceof ISourceReference) {
4231                                 reference = (ISourceReference) o;
4232                                 break;
4233                         }
4234                 }
4235
4236                 if (!isActivePart() && PHPeclipsePlugin.getActivePage() != null)
4237                         PHPeclipsePlugin.getActivePage().bringToTop(this);
4238
4239                 try {
4240                         editingScriptStarted();
4241                         setSelection(reference, !isActivePart());
4242                 } finally {
4243                         editingScriptEnded();
4244                 }
4245         }
4246
4247         /*
4248          * @see AbstractTextEditor#adjustHighlightRange(int, int)
4249          */
4250         protected void adjustHighlightRange(int offset, int length) {
4251
4252                 try {
4253
4254                         IJavaElement element = getElementAt(offset);
4255                         while (element instanceof ISourceReference) {
4256                                 ISourceRange range = ((ISourceReference) element)
4257                                                 .getSourceRange();
4258                                 if (offset < range.getOffset() + range.getLength()
4259                                                 && range.getOffset() < offset + length) {
4260
4261                                         ISourceViewer viewer = getSourceViewer();
4262                                         if (viewer instanceof ITextViewerExtension5) {
4263                                                 ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
4264                                                 extension.exposeModelRange(new Region(
4265                                                                 range.getOffset(), range.getLength()));
4266                                         }
4267
4268                                         setHighlightRange(range.getOffset(), range.getLength(),
4269                                                         true);
4270                                         if (fOutlinePage != null) {
4271                                                 fOutlineSelectionChangedListener
4272                                                                 .uninstall(fOutlinePage);
4273                                                 fOutlinePage.select((ISourceReference) element);
4274                                                 fOutlineSelectionChangedListener.install(fOutlinePage);
4275                                         }
4276
4277                                         return;
4278                                 }
4279                                 element = element.getParent();
4280                         }
4281
4282                 } catch (JavaModelException x) {
4283                         PHPeclipsePlugin.log(x.getStatus());
4284                 }
4285
4286                 ISourceViewer viewer = getSourceViewer();
4287                 if (viewer instanceof ITextViewerExtension5) {
4288                         ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
4289                         extension.exposeModelRange(new Region(offset, length));
4290                 } else {
4291                         resetHighlightRange();
4292                 }
4293
4294         }
4295
4296         protected boolean isActivePart() {
4297                 IWorkbenchWindow window = getSite().getWorkbenchWindow();
4298                 IPartService service = window.getPartService();
4299                 IWorkbenchPart part = service.getActivePart();
4300                 return part != null && part.equals(this);
4301         }
4302
4303         // public void openContextHelp() {
4304         // IDocument doc =
4305         // this.getDocumentProvider().getDocument(this.getEditorInput());
4306         // ITextSelection selection = (ITextSelection)
4307         // this.getSelectionProvider().getSelection();
4308         // int pos = selection.getOffset();
4309         // String word = getFunctionName(doc, pos);
4310         // openContextHelp(word);
4311         // }
4312         //
4313         // private void openContextHelp(String word) {
4314         // open(word);
4315         // }
4316         //
4317         // public static void open(String word) {
4318         // IHelp help = WorkbenchHelp.getHelpSupport();
4319         // if (help != null) {
4320         // IHelpResource helpResource = new PHPFunctionHelpResource(word);
4321         // WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
4322         // } else {
4323         // // showMessage(shell, dialogTitle, ActionMessages.getString("Open help
4324         // not available"), false); //$NON-NLS-1$
4325         // }
4326         // }
4327
4328         // private String getFunctionName(IDocument doc, int pos) {
4329         // Point word = PHPWordExtractor.findWord(doc, pos);
4330         // if (word != null) {
4331         // try {
4332         // return doc.get(word.x, word.y).replace('_', '-');
4333         // } catch (BadLocationException e) {
4334         // }
4335         // }
4336         // return "";
4337         // }
4338
4339         /*
4340          * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
4341          */
4342         protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
4343
4344                 try {
4345
4346                         ISourceViewer sourceViewer = getSourceViewer();
4347                         if (sourceViewer == null)
4348                                 return;
4349
4350                         String property = event.getProperty();
4351
4352                         if (PreferenceConstants.EDITOR_TAB_WIDTH.equals(property)) {
4353                                 Object value = event.getNewValue();
4354                                 if (value instanceof Integer) {
4355                                         sourceViewer.getTextWidget().setTabs(
4356                                                         ((Integer) value).intValue());
4357                                 } else if (value instanceof String) {
4358                                         try {
4359                                                 sourceViewer.getTextWidget().setTabs(
4360                                                                 Integer.parseInt((String) value));
4361                                         } catch (NumberFormatException e) {
4362                                                 // bug #1038071 - set default tab:
4363                                                 sourceViewer.getTextWidget().setTabs(80);
4364                                         }
4365                                 }
4366                                 return;
4367                         }
4368
4369                         // if (OVERVIEW_RULER.equals(property)) {
4370                         // if (isOverviewRulerVisible())
4371                         // showOverviewRuler();
4372                         // else
4373                         // hideOverviewRuler();
4374                         // return;
4375                         // }
4376
4377                         // if (LINE_NUMBER_RULER.equals(property)) {
4378                         // if (isLineNumberRulerVisible())
4379                         // showLineNumberRuler();
4380                         // else
4381                         // hideLineNumberRuler();
4382                         // return;
4383                         // }
4384
4385                         // if (fLineNumberRulerColumn != null
4386                         // && (LINE_NUMBER_COLOR.equals(property) ||
4387                         // PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property) ||
4388                         // PREFERENCE_COLOR_BACKGROUND.equals(property))) {
4389                         //
4390                         // initializeLineNumberRulerColumn(fLineNumberRulerColumn);
4391                         // }
4392
4393                         if (isJavaEditorHoverProperty(property))
4394                                 updateHoverBehavior();
4395
4396                         if (BROWSER_LIKE_LINKS.equals(property)) {
4397                                 if (isBrowserLikeLinks())
4398                                         enableBrowserLikeLinks();
4399                                 else
4400                                         disableBrowserLikeLinks();
4401                                 return;
4402                         }
4403
4404                         if (PreferenceConstants.EDITOR_DISABLE_OVERWRITE_MODE
4405                                         .equals(property)) {
4406                                 if (event.getNewValue() instanceof Boolean) {
4407                                         Boolean disable = (Boolean) event.getNewValue();
4408                                         enableOverwriteMode(!disable.booleanValue());
4409                                 }
4410                                 return;
4411                         }
4412
4413                         boolean newBooleanValue = false;
4414                         Object newValue = event.getNewValue();
4415                         if (newValue != null)
4416                                 newBooleanValue = Boolean.valueOf(newValue.toString())
4417                                                 .booleanValue();
4418
4419                         if (PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE
4420                                         .equals(property)) {
4421                                 if (newBooleanValue)
4422                                         selectionChanged();
4423                                 return;
4424                         }
4425
4426                         if (PreferenceConstants.EDITOR_MARK_OCCURRENCES.equals(property)) {
4427                                 if (newBooleanValue != fMarkOccurrenceAnnotations) {
4428                                         fMarkOccurrenceAnnotations = newBooleanValue;
4429                                         if (!fMarkOccurrenceAnnotations)
4430                                                 uninstallOccurrencesFinder();
4431                                         else
4432                                                 installOccurrencesFinder();
4433                                 }
4434                                 return;
4435                         }
4436
4437                         if (PreferenceConstants.EDITOR_STICKY_OCCURRENCES.equals(property)) {
4438                                 fStickyOccurrenceAnnotations = newBooleanValue;
4439                                 return;
4440                         }
4441                         // }
4442                         // }
4443                         // if
4444                         // (PreferenceConstants.EDITOR_STICKY_OCCURRENCES.equals(property))
4445                         // {
4446                         // if (event.getNewValue() instanceof Boolean) {
4447                         // boolean stickyOccurrenceAnnotations=
4448                         // ((Boolean)event.getNewValue()).booleanValue();
4449                         // if (stickyOccurrenceAnnotations != fStickyOccurrenceAnnotations)
4450                         // {
4451
4452                         ((PHPSourceViewerConfiguration) getSourceViewerConfiguration())
4453                                         .handlePropertyChangeEvent(event);
4454
4455                         // if (affectsOverrideIndicatorAnnotations(event)) {
4456                         // if (isShowingOverrideIndicators()) {
4457                         // if (fOverrideIndicatorManager == null)
4458                         // installOverrideIndicator(true);
4459                         // } else {
4460                         // if (fOverrideIndicatorManager != null)
4461                         // uninstallOverrideIndicator();
4462                         // }
4463                         // return;
4464                         // }
4465
4466                         if (PreferenceConstants.EDITOR_FOLDING_PROVIDER.equals(property)) {
4467                                 if (sourceViewer instanceof ProjectionViewer) {
4468                                         ProjectionViewer projectionViewer = (ProjectionViewer) sourceViewer;
4469                                         if (fProjectionModelUpdater != null)
4470                                                 fProjectionModelUpdater.uninstall();
4471                                         // either freshly enabled or provider changed
4472                                         fProjectionModelUpdater = PHPeclipsePlugin.getDefault()
4473                                                         .getFoldingStructureProviderRegistry()
4474                                                         .getCurrentFoldingProvider();
4475                                         if (fProjectionModelUpdater != null) {
4476                                                 fProjectionModelUpdater.install(this, projectionViewer);
4477                                         }
4478                                 }
4479                                 return;
4480                         }
4481                 } finally {
4482                         super.handlePreferenceStoreChanged(event);
4483                 }
4484         }
4485
4486         // /*
4487         // * @see
4488         // AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
4489         // */
4490         // protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
4491         //
4492         // try {
4493         //
4494         // ISourceViewer sourceViewer = getSourceViewer();
4495         // if (sourceViewer == null)
4496         // return;
4497         //
4498         // String property = event.getProperty();
4499         //
4500         // // if
4501         // (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
4502         // // Object value= event.getNewValue();
4503         // // if (value instanceof Integer) {
4504         // // sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
4505         // // } else if (value instanceof String) {
4506         // // sourceViewer.getTextWidget().setTabs(Integer.parseInt((String)
4507         // value));
4508         // // }
4509         // // return;
4510         // // }
4511         //
4512         // if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
4513         // if (isLineNumberRulerVisible())
4514         // showLineNumberRuler();
4515         // else
4516         // hideLineNumberRuler();
4517         // return;
4518         // }
4519         //
4520         // if (fLineNumberRulerColumn != null
4521         // && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
4522         // || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
4523         // || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
4524         //
4525         // initializeLineNumberRulerColumn(fLineNumberRulerColumn);
4526         // }
4527         //
4528         // } finally {
4529         // super.handlePreferenceStoreChanged(event);
4530         // }
4531         // }
4532
4533         // private boolean isJavaEditorHoverProperty(String property) {
4534         // return PreferenceConstants.EDITOR_DEFAULT_HOVER.equals(property)
4535         // || PreferenceConstants.EDITOR_NONE_HOVER.equals(property)
4536         // || PreferenceConstants.EDITOR_CTRL_HOVER.equals(property)
4537         // || PreferenceConstants.EDITOR_SHIFT_HOVER.equals(property)
4538         // || PreferenceConstants.EDITOR_CTRL_ALT_HOVER.equals(property)
4539         // || PreferenceConstants.EDITOR_CTRL_SHIFT_HOVER.equals(property)
4540         // || PreferenceConstants.EDITOR_CTRL_ALT_SHIFT_HOVER.equals(property)
4541         // || PreferenceConstants.EDITOR_ALT_SHIFT_HOVER.equals(property);
4542         // }
4543
4544         /**
4545          * Shows the line number ruler column.
4546          */
4547         // private void showLineNumberRuler() {
4548         // IVerticalRuler v = getVerticalRuler();
4549         // if (v instanceof CompositeRuler) {
4550         // CompositeRuler c = (CompositeRuler) v;
4551         // c.addDecorator(1, createLineNumberRulerColumn());
4552         // }
4553         // }
4554         private boolean isJavaEditorHoverProperty(String property) {
4555                 return PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS.equals(property);
4556         }
4557
4558         /**
4559          * Return whether the browser like links should be enabled according to the
4560          * preference store settings.
4561          * 
4562          * @return <code>true</code> if the browser like links should be enabled
4563          */
4564         private boolean isBrowserLikeLinks() {
4565                 IPreferenceStore store = getPreferenceStore();
4566                 return store.getBoolean(BROWSER_LIKE_LINKS);
4567         }
4568
4569         /**
4570          * Enables browser like links.
4571          */
4572         private void enableBrowserLikeLinks() {
4573                 if (fMouseListener == null) {
4574                         fMouseListener = new MouseClickListener();
4575                         fMouseListener.install();
4576                 }
4577         }
4578
4579         /**
4580          * Disables browser like links.
4581          */
4582         private void disableBrowserLikeLinks() {
4583                 if (fMouseListener != null) {
4584                         fMouseListener.uninstall();
4585                         fMouseListener = null;
4586                 }
4587         }
4588
4589         /**
4590          * Handles a property change event describing a change of the java core's
4591          * preferences and updates the preference related editor properties.
4592          * 
4593          * @param event
4594          *            the property change event
4595          */
4596         protected void handlePreferencePropertyChanged(
4597                         org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
4598                 if (COMPILER_TASK_TAGS.equals(event.getProperty())) {
4599                         ISourceViewer sourceViewer = getSourceViewer();
4600                         if (sourceViewer != null
4601                                         && affectsTextPresentation(new PropertyChangeEvent(event
4602                                                         .getSource(), event.getProperty(), event
4603                                                         .getOldValue(), event.getNewValue())))
4604                                 sourceViewer.invalidateTextPresentation();
4605                 }
4606                 if (PreferenceConstants.EDITOR_WRAP_WORDS.equals(event.getProperty())) {
4607                         setWordWrap();
4608                 }
4609         }
4610
4611         /**
4612          * Return whether the line number ruler column should be visible according
4613          * to the preference store settings.
4614          * 
4615          * @return <code>true</code> if the line numbers should be visible
4616          */
4617         // protected boolean isLineNumberRulerVisible() {
4618         // IPreferenceStore store = getPreferenceStore();
4619         // return store.getBoolean(LINE_NUMBER_RULER);
4620         // }
4621         /**
4622          * Hides the line number ruler column.
4623          */
4624         // private void hideLineNumberRuler() {
4625         // IVerticalRuler v = getVerticalRuler();
4626         // if (v instanceof CompositeRuler) {
4627         // CompositeRuler c = (CompositeRuler) v;
4628         // try {
4629         // c.removeDecorator(1);
4630         // } catch (Throwable e) {
4631         // }
4632         // }
4633         // }
4634         /*
4635          * @see AbstractTextEditor#handleCursorPositionChanged()
4636          */
4637         // protected void handleCursorPositionChanged() {
4638         // super.handleCursorPositionChanged();
4639         // if (!isEditingScriptRunning() && fUpdater != null)
4640         // fUpdater.post();
4641         // }
4642         /*
4643          * @see org.eclipse.ui.texteditor.AbstractTextEditor#handleElementContentReplaced()
4644          */
4645         protected void handleElementContentReplaced() {
4646                 super.handleElementContentReplaced();
4647                 if (fProjectionModelUpdater != null)
4648                         fProjectionModelUpdater.initialize();
4649         }
4650
4651         /**
4652          * Initializes the given line number ruler column from the preference store.
4653          * 
4654          * @param rulerColumn
4655          *            the ruler column to be initialized
4656          */
4657         // protected void initializeLineNumberRulerColumn(LineNumberRulerColumn
4658         // rulerColumn) {
4659         // JavaTextTools textTools =
4660         // PHPeclipsePlugin.getDefault().getJavaTextTools();
4661         // IColorManager manager = textTools.getColorManager();
4662         //
4663         // IPreferenceStore store = getPreferenceStore();
4664         // if (store != null) {
4665         //
4666         // RGB rgb = null;
4667         // // foreground color
4668         // if (store.contains(LINE_NUMBER_COLOR)) {
4669         // if (store.isDefault(LINE_NUMBER_COLOR))
4670         // rgb = PreferenceConverter.getDefaultColor(store, LINE_NUMBER_COLOR);
4671         // else
4672         // rgb = PreferenceConverter.getColor(store, LINE_NUMBER_COLOR);
4673         // }
4674         // rulerColumn.setForeground(manager.getColor(rgb));
4675         //
4676         // rgb = null;
4677         // // background color
4678         // if (!store.getBoolean(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
4679         // if (store.contains(PREFERENCE_COLOR_BACKGROUND)) {
4680         // if (store.isDefault(PREFERENCE_COLOR_BACKGROUND))
4681         // rgb = PreferenceConverter.getDefaultColor(store,
4682         // PREFERENCE_COLOR_BACKGROUND);
4683         // else
4684         // rgb = PreferenceConverter.getColor(store, PREFERENCE_COLOR_BACKGROUND);
4685         // }
4686         // }
4687         // rulerColumn.setBackground(manager.getColor(rgb));
4688         // }
4689         // }
4690         /**
4691          * Creates a new line number ruler column that is appropriately initialized.
4692          */
4693         // protected IVerticalRulerColumn createLineNumberRulerColumn() {
4694         // fLineNumberRulerColumn = new LineNumberRulerColumn();
4695         // initializeLineNumberRulerColumn(fLineNumberRulerColumn);
4696         // return fLineNumberRulerColumn;
4697         // }
4698         /*
4699          * @see AbstractTextEditor#createVerticalRuler()
4700          */
4701         // protected IVerticalRuler createVerticalRuler() {
4702         // CompositeRuler ruler = new CompositeRuler();
4703         // ruler.addDecorator(0, new AnnotationRulerColumn(VERTICAL_RULER_WIDTH));
4704         // if (isLineNumberRulerVisible())
4705         // ruler.addDecorator(1, createLineNumberRulerColumn());
4706         // return ruler;
4707         // }
4708         // private static IRegion getSignedSelection(ITextViewer viewer) {
4709         //
4710         // StyledText text = viewer.getTextWidget();
4711         // int caretOffset = text.getCaretOffset();
4712         // Point selection = text.getSelection();
4713         //
4714         // // caret left
4715         // int offset, length;
4716         // if (caretOffset == selection.x) {
4717         // offset = selection.y;
4718         // length = selection.x - selection.y;
4719         //
4720         // // caret right
4721         // } else {
4722         // offset = selection.x;
4723         // length = selection.y - selection.x;
4724         // }
4725         //
4726         // return new Region(offset, length);
4727         // }
4728         protected IRegion getSignedSelection(ISourceViewer sourceViewer) {
4729                 StyledText text = sourceViewer.getTextWidget();
4730                 Point selection = text.getSelectionRange();
4731
4732                 if (text.getCaretOffset() == selection.x) {
4733                         selection.x = selection.x + selection.y;
4734                         selection.y = -selection.y;
4735                 }
4736
4737                 selection.x = widgetOffset2ModelOffset(sourceViewer, selection.x);
4738
4739                 return new Region(selection.x, selection.y);
4740         }
4741
4742         /** Preference key for matching brackets */
4743         protected final static String MATCHING_BRACKETS = PreferenceConstants.EDITOR_MATCHING_BRACKETS;
4744
4745         /** Preference key for matching brackets color */
4746         protected final static String MATCHING_BRACKETS_COLOR = PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
4747
4748         /** Preference key for highlighting current line */
4749         // protected final static String CURRENT_LINE =
4750         // PreferenceConstants.EDITOR_CURRENT_LINE;
4751         /** Preference key for highlight color of current line */
4752         // protected final static String CURRENT_LINE_COLOR =
4753         // PreferenceConstants.EDITOR_CURRENT_LINE_COLOR;
4754         /** Preference key for showing print marging ruler */
4755         // protected final static String PRINT_MARGIN =
4756         // PreferenceConstants.EDITOR_PRINT_MARGIN;
4757         /** Preference key for print margin ruler color */
4758         // protected final static String PRINT_MARGIN_COLOR =
4759         // PreferenceConstants.EDITOR_PRINT_MARGIN_COLOR;
4760         /** Preference key for print margin ruler column */
4761         // protected final static String PRINT_MARGIN_COLUMN =
4762         // PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN;
4763         /** Preference key for error indication */
4764         // protected final static String ERROR_INDICATION =
4765         // PreferenceConstants.EDITOR_PROBLEM_INDICATION;
4766         /** Preference key for error color */
4767         // protected final static String ERROR_INDICATION_COLOR =
4768         // PreferenceConstants.EDITOR_PROBLEM_INDICATION_COLOR;
4769         /** Preference key for warning indication */
4770         // protected final static String WARNING_INDICATION =
4771         // PreferenceConstants.EDITOR_WARNING_INDICATION;
4772         /** Preference key for warning color */
4773         // protected final static String WARNING_INDICATION_COLOR =
4774         // PreferenceConstants.EDITOR_WARNING_INDICATION_COLOR;
4775         /** Preference key for task indication */
4776         protected final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
4777
4778         /** Preference key for task color */
4779         protected final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
4780
4781         /** Preference key for bookmark indication */
4782         protected final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
4783
4784         /** Preference key for bookmark color */
4785         protected final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
4786
4787         /** Preference key for search result indication */
4788         protected final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
4789
4790         /** Preference key for search result color */
4791         protected final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
4792
4793         /** Preference key for unknown annotation indication */
4794         protected final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
4795
4796         /** Preference key for unknown annotation color */
4797         protected final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
4798
4799         /** Preference key for shwoing the overview ruler */
4800         protected final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
4801
4802         /** Preference key for error indication in overview ruler */
4803         protected final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
4804
4805         /** Preference key for warning indication in overview ruler */
4806         protected final static String WARNING_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
4807
4808         /** Preference key for task indication in overview ruler */
4809         protected final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
4810
4811         /** Preference key for bookmark indication in overview ruler */
4812         protected final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
4813
4814         /** Preference key for search result indication in overview ruler */
4815         protected final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
4816
4817         /** Preference key for unknown annotation indication in overview ruler */
4818         protected final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
4819
4820         // /** Preference key for compiler task tags */
4821         // private final static String COMPILER_TASK_TAGS=
4822         // JavaCore.COMPILER_TASK_TAGS;
4823         /** Preference key for browser like links */
4824         private final static String BROWSER_LIKE_LINKS = PreferenceConstants.EDITOR_BROWSER_LIKE_LINKS;
4825
4826         /** Preference key for key modifier of browser like links */
4827         private final static String BROWSER_LIKE_LINKS_KEY_MODIFIER = PreferenceConstants.EDITOR_BROWSER_LIKE_LINKS_KEY_MODIFIER;
4828
4829         /**
4830          * Preference key for key modifier mask of browser like links. The value is
4831          * only used if the value of <code>EDITOR_BROWSER_LIKE_LINKS</code> cannot
4832          * be resolved to valid SWT modifier bits.
4833          * 
4834          * @since 2.1.1
4835          */
4836         private final static String BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK = PreferenceConstants.EDITOR_BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK;
4837
4838         private final static char[] BRACKETS = { '{', '}', '(', ')', '[', ']' };
4839
4840         private static boolean isBracket(char character) {
4841                 for (int i = 0; i != BRACKETS.length; ++i)
4842                         if (character == BRACKETS[i])
4843                                 return true;
4844                 return false;
4845         }
4846
4847         private static boolean isSurroundedByBrackets(IDocument document, int offset) {
4848                 if (offset == 0 || offset == document.getLength())
4849                         return false;
4850
4851                 try {
4852                         return isBracket(document.getChar(offset - 1))
4853                                         && isBracket(document.getChar(offset));
4854
4855                 } catch (BadLocationException e) {
4856                         return false;
4857                 }
4858         }
4859
4860         // protected void configureSourceViewerDecorationSupport() {
4861         //
4862         // fSourceViewerDecorationSupport.setCharacterPairMatcher(fBracketMatcher);
4863         //
4864         // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
4865         // AnnotationType.UNKNOWN,
4866         // UNKNOWN_INDICATION_COLOR,
4867         // UNKNOWN_INDICATION,
4868         // UNKNOWN_INDICATION_IN_OVERVIEW_RULER,
4869         // 0);
4870         // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
4871         // AnnotationType.BOOKMARK,
4872         // BOOKMARK_INDICATION_COLOR,
4873         // BOOKMARK_INDICATION,
4874         // BOOKMARK_INDICATION_IN_OVERVIEW_RULER,
4875         // 1);
4876         // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
4877         // AnnotationType.TASK,
4878         // TASK_INDICATION_COLOR,
4879         // TASK_INDICATION,
4880         // TASK_INDICATION_IN_OVERVIEW_RULER,
4881         // 2);
4882         // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
4883         // AnnotationType.SEARCH,
4884         // SEARCH_RESULT_INDICATION_COLOR,
4885         // SEARCH_RESULT_INDICATION,
4886         // SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER,
4887         // 3);
4888         // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
4889         // AnnotationType.WARNING,
4890         // WARNING_INDICATION_COLOR,
4891         // WARNING_INDICATION,
4892         // WARNING_INDICATION_IN_OVERVIEW_RULER,
4893         // 4);
4894         // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
4895         // AnnotationType.ERROR,
4896         // ERROR_INDICATION_COLOR,
4897         // ERROR_INDICATION,
4898         // ERROR_INDICATION_IN_OVERVIEW_RULER,
4899         // 5);
4900         //
4901         // fSourceViewerDecorationSupport.setCursorLinePainterPreferenceKeys(CURRENT_LINE,
4902         // CURRENT_LINE_COLOR);
4903         // fSourceViewerDecorationSupport.setMarginPainterPreferenceKeys(PRINT_MARGIN,
4904         // PRINT_MARGIN_COLOR, PRINT_MARGIN_COLUMN);
4905         // fSourceViewerDecorationSupport.setMatchingCharacterPainterPreferenceKeys(MATCHING_BRACKETS,
4906         // MATCHING_BRACKETS_COLOR);
4907         //
4908         // fSourceViewerDecorationSupport.setSymbolicFontName(getFontPropertyPreferenceKey());
4909         //
4910         // }
4911         /**
4912          * Returns the Java element wrapped by this editors input.
4913          * 
4914          * @return the Java element wrapped by this editors input.
4915          * @since 3.0
4916          */
4917         abstract protected IJavaElement getInputJavaElement();
4918
4919         protected void updateStatusLine() {
4920                 ITextSelection selection = (ITextSelection) getSelectionProvider()
4921                                 .getSelection();
4922                 Annotation annotation = getAnnotation(selection.getOffset(), selection
4923                                 .getLength());
4924                 setStatusLineErrorMessage(null);
4925                 setStatusLineMessage(null);
4926                 if (annotation != null) {
4927                         try {
4928                                 fIsUpdatingAnnotationViews = true;
4929                                 updateAnnotationViews(annotation);
4930                         } finally {
4931                                 fIsUpdatingAnnotationViews = false;
4932                         }
4933                         if (annotation instanceof IJavaAnnotation
4934                                         && ((IJavaAnnotation) annotation).isProblem())
4935                                 setStatusLineMessage(annotation.getText());
4936                 }
4937         }
4938
4939         /**
4940          * Jumps to the matching bracket.
4941          */
4942         public void gotoMatchingBracket() {
4943
4944                 ISourceViewer sourceViewer = getSourceViewer();
4945                 IDocument document = sourceViewer.getDocument();
4946                 if (document == null)
4947                         return;
4948
4949                 IRegion selection = getSignedSelection(sourceViewer);
4950
4951                 int selectionLength = Math.abs(selection.getLength());
4952                 if (selectionLength > 1) {
4953                         setStatusLineErrorMessage(PHPEditorMessages
4954                                         .getString("GotoMatchingBracket.error.invalidSelection")); //$NON-NLS-1$
4955                         sourceViewer.getTextWidget().getDisplay().beep();
4956                         return;
4957                 }
4958
4959                 // #26314
4960                 int sourceCaretOffset = selection.getOffset() + selection.getLength();
4961                 if (isSurroundedByBrackets(document, sourceCaretOffset))
4962                         sourceCaretOffset -= selection.getLength();
4963
4964                 IRegion region = fBracketMatcher.match(document, sourceCaretOffset);
4965                 if (region == null) {
4966                         setStatusLineErrorMessage(PHPEditorMessages
4967                                         .getString("GotoMatchingBracket.error.noMatchingBracket")); //$NON-NLS-1$
4968                         sourceViewer.getTextWidget().getDisplay().beep();
4969                         return;
4970                 }
4971
4972                 int offset = region.getOffset();
4973                 int length = region.getLength();
4974
4975                 if (length < 1)
4976                         return;
4977
4978                 int anchor = fBracketMatcher.getAnchor();
4979                 int targetOffset = (PHPPairMatcher.RIGHT == anchor) ? offset : offset
4980                                 + length - 1;
4981
4982                 boolean visible = false;
4983                 if (sourceViewer instanceof ITextViewerExtension5) {
4984                         ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
4985                         visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
4986                 } else {
4987                         IRegion visibleRegion = sourceViewer.getVisibleRegion();
4988                         visible = (targetOffset >= visibleRegion.getOffset() && targetOffset < visibleRegion
4989                                         .getOffset()
4990                                         + visibleRegion.getLength());
4991                 }
4992
4993                 if (!visible) {
4994                         setStatusLineErrorMessage(PHPEditorMessages
4995                                         .getString("GotoMatchingBracket.error.bracketOutsideSelectedElement")); //$NON-NLS-1$
4996                         sourceViewer.getTextWidget().getDisplay().beep();
4997                         return;
4998                 }
4999
5000                 if (selection.getLength() < 0)
5001                         targetOffset -= selection.getLength();
5002
5003                 sourceViewer.setSelectedRange(targetOffset, selection.getLength());
5004                 sourceViewer.revealRange(targetOffset, selection.getLength());
5005         }
5006
5007         /**
5008          * Ses the given message as error message to this editor's status line.
5009          * 
5010          * @param msg
5011          *            message to be set
5012          */
5013         protected void setStatusLineErrorMessage(String msg) {
5014                 IEditorStatusLine statusLine = (IEditorStatusLine) getAdapter(IEditorStatusLine.class);
5015                 if (statusLine != null)
5016                         statusLine.setMessage(true, msg, null);
5017         }
5018
5019         /**
5020          * Sets the given message as message to this editor's status line.
5021          * 
5022          * @param msg
5023          *            message to be set
5024          * @since 3.0
5025          */
5026         protected void setStatusLineMessage(String msg) {
5027                 IEditorStatusLine statusLine = (IEditorStatusLine) getAdapter(IEditorStatusLine.class);
5028                 if (statusLine != null)
5029                         statusLine.setMessage(false, msg, null);
5030         }
5031
5032         /**
5033          * Returns the annotation closest to the given range respecting the given
5034          * direction. If an annotation is found, the annotations current position is
5035          * copied into the provided annotation position.
5036          * 
5037          * @param offset
5038          *            the region offset
5039          * @param length
5040          *            the region length
5041          * @param forward
5042          *            <code>true</code> for forwards, <code>false</code> for
5043          *            backward
5044          * @param annotationPosition
5045          *            the position of the found annotation
5046          * @return the found annotation
5047          */
5048         private Annotation getNextAnnotation(final int offset, final int length,
5049                         boolean forward, Position annotationPosition) {
5050
5051                 Annotation nextAnnotation = null;
5052                 Position nextAnnotationPosition = null;
5053                 Annotation containingAnnotation = null;
5054                 Position containingAnnotationPosition = null;
5055                 boolean currentAnnotation = false;
5056
5057                 IDocument document = getDocumentProvider()
5058                                 .getDocument(getEditorInput());
5059                 int endOfDocument = document.getLength();
5060                 int distance = Integer.MAX_VALUE;
5061
5062                 IAnnotationModel model = getDocumentProvider().getAnnotationModel(
5063                                 getEditorInput());
5064                 Iterator e = new JavaAnnotationIterator(model, true, true);
5065                 while (e.hasNext()) {
5066                         Annotation a = (Annotation) e.next();
5067                         if ((a instanceof IJavaAnnotation)
5068                                         && ((IJavaAnnotation) a).hasOverlay()
5069                                         || !isNavigationTarget(a))
5070                                 continue;
5071
5072                         Position p = model.getPosition(a);
5073                         if (p == null)
5074                                 continue;
5075
5076                         if (forward && p.offset == offset || !forward
5077                                         && p.offset + p.getLength() == offset + length) {// ||
5078                                 // p.includes(offset))
5079                                 // {
5080                                 if (containingAnnotation == null
5081                                                 || (forward
5082                                                                 && p.length >= containingAnnotationPosition.length || !forward
5083                                                                 && p.length >= containingAnnotationPosition.length)) {
5084                                         containingAnnotation = a;
5085                                         containingAnnotationPosition = p;
5086                                         currentAnnotation = p.length == length;
5087                                 }
5088                         } else {
5089                                 int currentDistance = 0;
5090
5091                                 if (forward) {
5092                                         currentDistance = p.getOffset() - offset;
5093                                         if (currentDistance < 0)
5094                                                 currentDistance = endOfDocument + currentDistance;
5095
5096                                         if (currentDistance < distance
5097                                                         || currentDistance == distance
5098                                                         && p.length < nextAnnotationPosition.length) {
5099                                                 distance = currentDistance;
5100                                                 nextAnnotation = a;
5101                                                 nextAnnotationPosition = p;
5102                                         }
5103                                 } else {
5104                                         currentDistance = offset + length
5105                                                         - (p.getOffset() + p.length);
5106                                         if (currentDistance < 0)
5107                                                 currentDistance = endOfDocument + currentDistance;
5108
5109                                         if (currentDistance < distance
5110                                                         || currentDistance == distance
5111                                                         && p.length < nextAnnotationPosition.length) {
5112                                                 distance = currentDistance;
5113                                                 nextAnnotation = a;
5114                                                 nextAnnotationPosition = p;
5115                                         }
5116                                 }
5117                         }
5118                 }
5119                 if (containingAnnotationPosition != null
5120                                 && (!currentAnnotation || nextAnnotation == null)) {
5121                         annotationPosition.setOffset(containingAnnotationPosition
5122                                         .getOffset());
5123                         annotationPosition.setLength(containingAnnotationPosition
5124                                         .getLength());
5125                         return containingAnnotation;
5126                 }
5127                 if (nextAnnotationPosition != null) {
5128                         annotationPosition.setOffset(nextAnnotationPosition.getOffset());
5129                         annotationPosition.setLength(nextAnnotationPosition.getLength());
5130                 }
5131
5132                 return nextAnnotation;
5133         }
5134
5135         /**
5136          * Returns the annotation overlapping with the given range or
5137          * <code>null</code>.
5138          * 
5139          * @param offset
5140          *            the region offset
5141          * @param length
5142          *            the region length
5143          * @return the found annotation or <code>null</code>
5144          * @since 3.0
5145          */
5146         private Annotation getAnnotation(int offset, int length) {
5147                 IAnnotationModel model = getDocumentProvider().getAnnotationModel(
5148                                 getEditorInput());
5149                 Iterator e = new JavaAnnotationIterator(model, true, true);
5150                 while (e.hasNext()) {
5151                         Annotation a = (Annotation) e.next();
5152                         if (!isNavigationTarget(a))
5153                                 continue;
5154
5155                         Position p = model.getPosition(a);
5156                         if (p != null && p.overlapsWith(offset, length))
5157                                 return a;
5158                 }
5159
5160                 return null;
5161         }
5162
5163         /**
5164          * Returns whether the given annotation is configured as a target for the
5165          * "Go to Next/Previous Annotation" actions
5166          * 
5167          * @param annotation
5168          *            the annotation
5169          * @return <code>true</code> if this is a target, <code>false</code>
5170          *         otherwise
5171          * @since 3.0
5172          */
5173         protected boolean isNavigationTarget(Annotation annotation) {
5174                 Preferences preferences = EditorsUI.getPluginPreferences();
5175                 AnnotationPreference preference = getAnnotationPreferenceLookup()
5176                                 .getAnnotationPreference(annotation);
5177                 // See bug 41689
5178                 // String key= forward ? preference.getIsGoToNextNavigationTargetKey() :
5179                 // preference.getIsGoToPreviousNavigationTargetKey();
5180                 String key = preference == null ? null : preference
5181                                 .getIsGoToNextNavigationTargetKey();
5182                 return (key != null && preferences.getBoolean(key));
5183         }
5184
5185         /**
5186          * Returns a segmentation of the line of the given document appropriate for
5187          * bidi rendering. The default implementation returns only the string
5188          * literals of a php code line as segments.
5189          * 
5190          * @param document
5191          *            the document
5192          * @param lineOffset
5193          *            the offset of the line
5194          * @return the line's bidi segmentation
5195          * @throws BadLocationException
5196          *             in case lineOffset is not valid in document
5197          */
5198         public static int[] getBidiLineSegments(IDocument document, int lineOffset)
5199                         throws BadLocationException {
5200
5201                 IRegion line = document.getLineInformationOfOffset(lineOffset);
5202                 ITypedRegion[] linePartitioning = document.computePartitioning(
5203                                 lineOffset, line.getLength());
5204
5205                 List segmentation = new ArrayList();
5206                 for (int i = 0; i < linePartitioning.length; i++) {
5207                         if (IPHPPartitions.PHP_STRING_DQ.equals(linePartitioning[i]
5208                                         .getType())) {
5209                                 segmentation.add(linePartitioning[i]);
5210                         } else if (IPHPPartitions.PHP_STRING_HEREDOC
5211                                         .equals(linePartitioning[i].getType())) {
5212                                 segmentation.add(linePartitioning[i]);
5213                         }
5214                 }
5215
5216                 if (segmentation.size() == 0)
5217                         return null;
5218
5219                 int size = segmentation.size();
5220                 int[] segments = new int[size * 2 + 1];
5221
5222                 int j = 0;
5223                 for (int i = 0; i < size; i++) {
5224                         ITypedRegion segment = (ITypedRegion) segmentation.get(i);
5225
5226                         if (i == 0)
5227                                 segments[j++] = 0;
5228
5229                         int offset = segment.getOffset() - lineOffset;
5230                         if (offset > segments[j - 1])
5231                                 segments[j++] = offset;
5232
5233                         if (offset + segment.getLength() >= line.getLength())
5234                                 break;
5235
5236                         segments[j++] = offset + segment.getLength();
5237                 }
5238
5239                 if (j < segments.length) {
5240                         int[] result = new int[j];
5241                         System.arraycopy(segments, 0, result, 0, j);
5242                         segments = result;
5243                 }
5244
5245                 return segments;
5246         }
5247
5248         /**
5249          * Returns a segmentation of the given line appropriate for bidi rendering.
5250          * The default implementation returns only the string literals of a php code
5251          * line as segments.
5252          * 
5253          * @param lineOffset
5254          *            the offset of the line
5255          * @param line
5256          *            the content of the line
5257          * @return the line's bidi segmentation
5258          */
5259         protected int[] getBidiLineSegments(int lineOffset, String line) {
5260                 IDocumentProvider provider = getDocumentProvider();
5261                 if (provider != null && line != null && line.length() > 0) {
5262                         IDocument document = provider.getDocument(getEditorInput());
5263                         if (document != null)
5264                                 try {
5265                                         return getBidiLineSegments(document, lineOffset);
5266                                 } catch (BadLocationException x) {
5267                                         // ignore
5268                                 }
5269                 }
5270                 return null;
5271         }
5272
5273         /*
5274          * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler,
5275          *      int)
5276          */
5277         // protected final ISourceViewer createSourceViewer(
5278         // Composite parent,
5279         // IVerticalRuler ruler,
5280         // int styles) {
5281         // ISourceViewer viewer = createJavaSourceViewer(parent, ruler, styles);
5282         // StyledText text = viewer.getTextWidget();
5283         // text.addBidiSegmentListener(new BidiSegmentListener() {
5284         // public void lineGetSegments(BidiSegmentEvent event) {
5285         // event.segments = getBidiLineSegments(event.lineOffset, event.lineText);
5286         // }
5287         // });
5288         // // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
5289         // return viewer;
5290         // }
5291         public final ISourceViewer getViewer() {
5292                 return getSourceViewer();
5293         }
5294
5295         // protected void showOverviewRuler() {
5296         // if (fOverviewRuler != null) {
5297         // if (getSourceViewer() instanceof ISourceViewerExtension) {
5298         // ((ISourceViewerExtension)
5299         // getSourceViewer()).showAnnotationsOverview(true);
5300         // fSourceViewerDecorationSupport.updateOverviewDecorations();
5301         // }
5302         // }
5303         // }
5304         //
5305         // protected void hideOverviewRuler() {
5306         // if (getSourceViewer() instanceof ISourceViewerExtension) {
5307         // fSourceViewerDecorationSupport.hideAnnotationOverview();
5308         // ((ISourceViewerExtension)
5309         // getSourceViewer()).showAnnotationsOverview(false);
5310         // }
5311         // }
5312
5313         // protected boolean isOverviewRulerVisible() {
5314         // IPreferenceStore store = getPreferenceStore();
5315         // return store.getBoolean(OVERVIEW_RULER);
5316         // }
5317         /*
5318          * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler,
5319          *      int)
5320          */
5321         // protected ISourceViewer createJavaSourceViewer(
5322         // Composite parent,
5323         // IVerticalRuler ruler,
5324         // IOverviewRuler overviewRuler,
5325         // boolean isOverviewRulerVisible,
5326         // int styles) {
5327         // return new SourceViewer(parent, ruler, overviewRuler,
5328         // isOverviewRulerVisible(), styles);
5329         // }
5330         /*
5331          * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler,
5332          *      int)
5333          */
5334         protected ISourceViewer createJavaSourceViewer(Composite parent,
5335                         IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
5336                         boolean isOverviewRulerVisible, int styles, IPreferenceStore store) {
5337                 return new JavaSourceViewer(parent, verticalRuler, getOverviewRuler(),
5338                                 isOverviewRulerVisible(), styles, store);
5339         }
5340
5341         /*
5342          * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler,
5343          *      int)
5344          */
5345         protected final ISourceViewer createSourceViewer(Composite parent,
5346                         IVerticalRuler verticalRuler, int styles) {
5347
5348                 ISourceViewer viewer = createJavaSourceViewer(parent, verticalRuler,
5349                                 getOverviewRuler(), isOverviewRulerVisible(), styles,
5350                                 getPreferenceStore());
5351
5352                 StyledText text = viewer.getTextWidget();
5353                 text.addBidiSegmentListener(new BidiSegmentListener() {
5354                         public void lineGetSegments(BidiSegmentEvent event) {
5355                                 event.segments = getBidiLineSegments(event.lineOffset,
5356                                                 event.lineText);
5357                         }
5358                 });
5359
5360                 // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
5361
5362                 // ensure source viewer decoration support has been created and
5363                 // configured
5364                 getSourceViewerDecorationSupport(viewer);
5365
5366                 return viewer;
5367         }
5368
5369         /*
5370          * @see AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
5371          */
5372         protected boolean affectsTextPresentation(PropertyChangeEvent event) {
5373                 return ((PHPSourceViewerConfiguration) getSourceViewerConfiguration())
5374                                 .affectsTextPresentation(event)
5375                                 || super.affectsTextPresentation(event);
5376         }
5377
5378         //
5379         // protected boolean affectsTextPresentation(PropertyChangeEvent event) {
5380         // JavaTextTools textTools =
5381         // PHPeclipsePlugin.getDefault().getJavaTextTools();
5382         // return textTools.affectsBehavior(event);
5383         // }
5384         /**
5385          * Creates and returns the preference store for this Java editor with the
5386          * given input.
5387          * 
5388          * @param input
5389          *            The editor input for which to create the preference store
5390          * @return the preference store for this editor
5391          * 
5392          * @since 3.0
5393          */
5394         private IPreferenceStore createCombinedPreferenceStore(IEditorInput input) {
5395                 List stores = new ArrayList(3);
5396
5397                 IJavaProject project = EditorUtility.getJavaProject(input);
5398                 if (project != null)
5399                         stores.add(new OptionsAdapter(project.getOptions(false),
5400                                         PHPeclipsePlugin.getDefault().getMockupPreferenceStore(),
5401                                         new OptionsAdapter.IPropertyChangeEventFilter() {
5402
5403                                                 public boolean isFiltered(PropertyChangeEvent event) {
5404                                                         IJavaElement inputJavaElement = getInputJavaElement();
5405                                                         IJavaProject javaProject = inputJavaElement != null ? inputJavaElement
5406                                                                         .getJavaProject()
5407                                                                         : null;
5408                                                         if (javaProject == null)
5409                                                                 return true;
5410
5411                                                         return !javaProject.getProject().equals(
5412                                                                         event.getSource());
5413                                                 }
5414
5415                                         }));
5416
5417                 stores.add(PHPeclipsePlugin.getDefault().getPreferenceStore());
5418                 stores.add(new PreferencesAdapter(JavaCore.getPlugin()
5419                                 .getPluginPreferences()));
5420                 stores.add(EditorsUI.getPreferenceStore());
5421
5422                 return new ChainedPreferenceStore((IPreferenceStore[]) stores
5423                                 .toArray(new IPreferenceStore[stores.size()]));
5424         }
5425
5426         /**
5427          * Jumps to the error next according to the given direction.
5428          */
5429         public void gotoError(boolean forward) {
5430
5431                 ISelectionProvider provider = getSelectionProvider();
5432
5433                 ITextSelection s = (ITextSelection) provider.getSelection();
5434                 Position errorPosition = new Position(0, 0);
5435                 IJavaAnnotation nextError = getNextError(s.getOffset(), forward,
5436                                 errorPosition);
5437
5438                 if (nextError != null) {
5439
5440                         IMarker marker = null;
5441                         if (nextError instanceof MarkerAnnotation)
5442                                 marker = ((MarkerAnnotation) nextError).getMarker();
5443                         else {
5444                                 Iterator e = nextError.getOverlaidIterator();
5445                                 if (e != null) {
5446                                         while (e.hasNext()) {
5447                                                 Object o = e.next();
5448                                                 if (o instanceof MarkerAnnotation) {
5449                                                         marker = ((MarkerAnnotation) o).getMarker();
5450                                                         break;
5451                                                 }
5452                                         }
5453                                 }
5454                         }
5455
5456                         if (marker != null) {
5457                                 IWorkbenchPage page = getSite().getPage();
5458                                 IViewPart view = view = page
5459                                                 .findView("org.eclipse.ui.views.TaskList"); //$NON-NLS-1$
5460                                 if (view instanceof TaskList) {
5461                                         StructuredSelection ss = new StructuredSelection(marker);
5462                                         ((TaskList) view).setSelection(ss, true);
5463                                 }
5464                         }
5465
5466                         selectAndReveal(errorPosition.getOffset(), errorPosition
5467                                         .getLength());
5468                         // setStatusLineErrorMessage(nextError.getMessage());
5469
5470                 } else {
5471
5472                         setStatusLineErrorMessage(null);
5473
5474                 }
5475         }
5476
5477         private IJavaAnnotation getNextError(int offset, boolean forward,
5478                         Position errorPosition) {
5479
5480                 IJavaAnnotation nextError = null;
5481                 Position nextErrorPosition = null;
5482
5483                 IDocument document = getDocumentProvider()
5484                                 .getDocument(getEditorInput());
5485                 int endOfDocument = document.getLength();
5486                 int distance = 0;
5487
5488                 IAnnotationModel model = getDocumentProvider().getAnnotationModel(
5489                                 getEditorInput());
5490                 Iterator e = new JavaAnnotationIterator(model, false);
5491                 while (e.hasNext()) {
5492
5493                         IJavaAnnotation a = (IJavaAnnotation) e.next();
5494                         if (a.hasOverlay() || !a.isProblem())
5495                                 continue;
5496
5497                         Position p = model.getPosition((Annotation) a);
5498                         if (!p.includes(offset)) {
5499
5500                                 int currentDistance = 0;
5501
5502                                 if (forward) {
5503                                         currentDistance = p.getOffset() - offset;
5504                                         if (currentDistance < 0)
5505                                                 currentDistance = endOfDocument - offset
5506                                                                 + p.getOffset();
5507                                 } else {
5508                                         currentDistance = offset - p.getOffset();
5509                                         if (currentDistance < 0)
5510                                                 currentDistance = offset + endOfDocument
5511                                                                 - p.getOffset();
5512                                 }
5513
5514                                 if (nextError == null || currentDistance < distance) {
5515                                         distance = currentDistance;
5516                                         nextError = a;
5517                                         nextErrorPosition = p;
5518                                 }
5519                         }
5520                 }
5521
5522                 if (nextErrorPosition != null) {
5523                         errorPosition.setOffset(nextErrorPosition.getOffset());
5524                         errorPosition.setLength(nextErrorPosition.getLength());
5525                 }
5526
5527                 return nextError;
5528         }
5529
5530         protected void uninstallOverrideIndicator() {
5531                 // if (fOverrideIndicatorManager != null) {
5532                 // fOverrideIndicatorManager.removeAnnotations();
5533                 // fOverrideIndicatorManager= null;
5534                 // }
5535         }
5536
5537         protected void installOverrideIndicator(boolean waitForReconcilation) {
5538                 uninstallOverrideIndicator();
5539                 IAnnotationModel model = getDocumentProvider().getAnnotationModel(
5540                                 getEditorInput());
5541                 final IJavaElement inputElement = getInputJavaElement();
5542
5543                 if (model == null || inputElement == null)
5544                         return;
5545
5546                 // fOverrideIndicatorManager= new OverrideIndicatorManager(model,
5547                 // inputElement, null);
5548                 //
5549                 // if (provideAST) {
5550                 // Job job= new
5551                 // Job(JavaEditorMessages.getString("OverrideIndicatorManager.intallJob"))
5552                 // {
5553                 // //$NON-NLS-1$
5554                 // /*
5555                 // * @see
5556                 // org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
5557                 // * @since 3.0
5558                 // */
5559                 // protected IStatus run(IProgressMonitor monitor) {
5560                 // CompilationUnit ast=
5561                 // JavaPlugin.getDefault().getASTProvider().getAST(inputElement, true,
5562                 // null);
5563                 // if (fOverrideIndicatorManager != null) // editor might have been
5564                 // closed
5565                 // in the meanwhile
5566                 // fOverrideIndicatorManager.reconciled(ast, true, monitor);
5567                 // return Status.OK_STATUS;
5568                 // }
5569                 // };
5570                 // job.setPriority(Job.DECORATE);
5571                 // job.setSystem(true);
5572                 // job.schedule();
5573                 // }
5574         }
5575
5576         /**
5577          * Tells whether override indicators are shown.
5578          * 
5579          * @return <code>true</code> if the override indicators are shown
5580          * @since 3.0
5581          */
5582         // protected boolean isShowingOverrideIndicators() {
5583         // AnnotationPreference preference=
5584         // getAnnotationPreferenceLookup().getAnnotationPreference(OverrideIndicatorManager.ANNOTATION_TYPE);
5585         // IPreferenceStore store= getPreferenceStore();
5586         // return getBoolean(store, preference.getHighlightPreferenceKey())
5587         // || getBoolean(store, preference.getVerticalRulerPreferenceKey())
5588         // || getBoolean(store, preference.getOverviewRulerPreferenceKey())
5589         // || getBoolean(store, preference.getTextPreferenceKey());
5590         // }
5591         /**
5592          * Returns the boolean preference for the given key.
5593          * 
5594          * @param store
5595          *            the preference store
5596          * @param key
5597          *            the preference key
5598          * @return <code>true</code> if the key exists in the store and its value
5599          *         is <code>true</code>
5600          * @since 3.0
5601          */
5602         private boolean getBoolean(IPreferenceStore store, String key) {
5603                 return key != null && store.getBoolean(key);
5604         }
5605
5606         protected boolean isPrefQuickDiffAlwaysOn() {
5607                 return false; // never show change ruler for the non-editable java
5608                                                 // editor.
5609                 // Overridden in subclasses like PHPUnitEditor
5610         }
5611
5612         /*
5613          * @see org.eclipse.ui.texteditor.AbstractTextEditor#createNavigationActions()
5614          */
5615         protected void createNavigationActions() {
5616                 super.createNavigationActions();
5617
5618                 final StyledText textWidget = getSourceViewer().getTextWidget();
5619
5620                 IAction action = new SmartLineStartAction(textWidget, false);
5621                 action.setActionDefinitionId(ITextEditorActionDefinitionIds.LINE_START);
5622                 setAction(ITextEditorActionDefinitionIds.LINE_START, action);
5623
5624                 action = new SmartLineStartAction(textWidget, true);
5625                 action
5626                                 .setActionDefinitionId(ITextEditorActionDefinitionIds.SELECT_LINE_START);
5627                 setAction(ITextEditorActionDefinitionIds.SELECT_LINE_START, action);
5628
5629                 action = new NavigatePreviousSubWordAction();
5630                 action
5631                                 .setActionDefinitionId(ITextEditorActionDefinitionIds.WORD_PREVIOUS);
5632                 setAction(ITextEditorActionDefinitionIds.WORD_PREVIOUS, action);
5633                 textWidget.setKeyBinding(SWT.CTRL | SWT.ARROW_LEFT, SWT.NULL);
5634
5635                 action = new NavigateNextSubWordAction();
5636                 action.setActionDefinitionId(ITextEditorActionDefinitionIds.WORD_NEXT);
5637                 setAction(ITextEditorActionDefinitionIds.WORD_NEXT, action);
5638                 textWidget.setKeyBinding(SWT.CTRL | SWT.ARROW_RIGHT, SWT.NULL);
5639
5640                 action = new SelectPreviousSubWordAction();
5641                 action
5642                                 .setActionDefinitionId(ITextEditorActionDefinitionIds.SELECT_WORD_PREVIOUS);
5643                 setAction(ITextEditorActionDefinitionIds.SELECT_WORD_PREVIOUS, action);
5644                 textWidget.setKeyBinding(SWT.CTRL | SWT.SHIFT | SWT.ARROW_LEFT,
5645                                 SWT.NULL);
5646
5647                 action = new SelectNextSubWordAction();
5648                 action
5649                                 .setActionDefinitionId(ITextEditorActionDefinitionIds.SELECT_WORD_NEXT);
5650                 setAction(ITextEditorActionDefinitionIds.SELECT_WORD_NEXT, action);
5651                 textWidget.setKeyBinding(SWT.CTRL | SWT.SHIFT | SWT.ARROW_RIGHT,
5652                                 SWT.NULL);
5653         }
5654
5655         /*
5656          * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createCompositeRuler()
5657          */
5658         // protected CompositeRuler createCompositeRuler() {
5659         // if
5660         // (!getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_ANNOTATION_ROLL_OVER))
5661         // return super.createCompositeRuler();
5662         //
5663         // CompositeRuler ruler = new CompositeRuler();
5664         // AnnotationRulerColumn column = new
5665         // AnnotationRulerColumn(VERTICAL_RULER_WIDTH, getAnnotationAccess());
5666         // column.setHover(new JavaExpandHover(ruler, getAnnotationAccess(), new
5667         // IDoubleClickListener() {
5668         //
5669         // public void doubleClick(DoubleClickEvent event) {
5670         // // for now: just invoke ruler double click action
5671         // triggerAction(ITextEditorActionConstants.RULER_DOUBLE_CLICK);
5672         // }
5673         //
5674         // private void triggerAction(String actionID) {
5675         // IAction action = getAction(actionID);
5676         // if (action != null) {
5677         // if (action instanceof IUpdate)
5678         // ((IUpdate) action).update();
5679         // // hack to propagate line change
5680         // if (action instanceof ISelectionListener) {
5681         // ((ISelectionListener) action).selectionChanged(null, null);
5682         // }
5683         // if (action.isEnabled())
5684         // action.run();
5685         // }
5686         // }
5687         //
5688         // }));
5689         // ruler.addDecorator(0, column);
5690         //
5691         // if (isLineNumberRulerVisible())
5692         // ruler.addDecorator(1, createLineNumberRulerColumn());
5693         // else if (isPrefQuickDiffAlwaysOn())
5694         // ruler.addDecorator(1, createChangeRulerColumn());
5695         //
5696         // return ruler;
5697         // }
5698         /*
5699          * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createAnnotationRulerColumn(org.eclipse.jface.text.source.CompositeRuler)
5700          * @since 3.2
5701          */
5702         protected IVerticalRulerColumn createAnnotationRulerColumn(
5703                         CompositeRuler ruler) {
5704                 if (!getPreferenceStore().getBoolean(
5705                                 PreferenceConstants.EDITOR_ANNOTATION_ROLL_OVER))
5706                         return super.createAnnotationRulerColumn(ruler);
5707
5708                 AnnotationRulerColumn column = new AnnotationRulerColumn(
5709                                 VERTICAL_RULER_WIDTH, getAnnotationAccess());
5710                 column.setHover(new JavaExpandHover(ruler, getAnnotationAccess(),
5711                                 new IDoubleClickListener() {
5712
5713                                         public void doubleClick(DoubleClickEvent event) {
5714                                                 // for now: just invoke ruler double click action
5715                                                 triggerAction(ITextEditorActionConstants.RULER_DOUBLE_CLICK);
5716                                         }
5717
5718                                         private void triggerAction(String actionID) {
5719                                                 IAction action = getAction(actionID);
5720                                                 if (action != null) {
5721                                                         if (action instanceof IUpdate)
5722                                                                 ((IUpdate) action).update();
5723                                                         // hack to propagate line change
5724                                                         if (action instanceof ISelectionListener) {
5725                                                                 ((ISelectionListener) action).selectionChanged(
5726                                                                                 null, null);
5727                                                         }
5728                                                         if (action.isEnabled())
5729                                                                 action.run();
5730                                                 }
5731                                         }
5732
5733                                 }));
5734
5735                 return column;
5736         }
5737
5738         /**
5739          * Returns the folding action group, or <code>null</code> if there is
5740          * none.
5741          * 
5742          * @return the folding action group, or <code>null</code> if there is none
5743          * @since 3.0
5744          */
5745         protected FoldingActionGroup getFoldingActionGroup() {
5746                 return fFoldingGroup;
5747         }
5748
5749         /*
5750          * @see org.eclipse.ui.texteditor.AbstractTextEditor#performRevert()
5751          */
5752         protected void performRevert() {
5753                 ProjectionViewer projectionViewer = (ProjectionViewer) getSourceViewer();
5754                 projectionViewer.setRedraw(false);
5755                 try {
5756
5757                         boolean projectionMode = projectionViewer.isProjectionMode();
5758                         if (projectionMode) {
5759                                 projectionViewer.disableProjection();
5760                                 if (fProjectionModelUpdater != null)
5761                                         fProjectionModelUpdater.uninstall();
5762                         }
5763
5764                         super.performRevert();
5765
5766                         if (projectionMode) {
5767                                 if (fProjectionModelUpdater != null)
5768                                         fProjectionModelUpdater.install(this, projectionViewer);
5769                                 projectionViewer.enableProjection();
5770                         }
5771
5772                 } finally {
5773                         projectionViewer.setRedraw(true);
5774                 }
5775         }
5776
5777         /**
5778          * React to changed selection.
5779          * 
5780          * @since 3.0
5781          */
5782         protected void selectionChanged() {
5783                 if (getSelectionProvider() == null)
5784                         return;
5785                 ISourceReference element = computeHighlightRangeSourceReference();
5786                 if (getPreferenceStore().getBoolean(
5787                                 PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE))
5788                         synchronizeOutlinePage(element);
5789                 setSelection(element, false);
5790                 updateStatusLine();
5791         }
5792
5793         private boolean isJavaOutlinePageActive() {
5794                 IWorkbenchPart part = getActivePart();
5795                 return part instanceof ContentOutline
5796                                 && ((ContentOutline) part).getCurrentPage() == fOutlinePage;
5797         }
5798
5799         private IWorkbenchPart getActivePart() {
5800                 IWorkbenchWindow window = getSite().getWorkbenchWindow();
5801                 IPartService service = window.getPartService();
5802                 IWorkbenchPart part = service.getActivePart();
5803                 return part;
5804         }
5805
5806         /**
5807          * Computes and returns the source reference that includes the caret and
5808          * serves as provider for the outline page selection and the editor range
5809          * indication.
5810          * 
5811          * @return the computed source reference
5812          * @since 3.0
5813          */
5814         protected ISourceReference computeHighlightRangeSourceReference() {
5815                 ISourceViewer sourceViewer = getSourceViewer();
5816                 if (sourceViewer == null)
5817                         return null;
5818
5819                 StyledText styledText = sourceViewer.getTextWidget();
5820                 if (styledText == null)
5821                         return null;
5822
5823                 int caret = 0;
5824                 if (sourceViewer instanceof ITextViewerExtension5) {
5825                         ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
5826                         caret = extension.widgetOffset2ModelOffset(styledText
5827                                         .getCaretOffset());
5828                 } else {
5829                         int offset = sourceViewer.getVisibleRegion().getOffset();
5830                         caret = offset + styledText.getCaretOffset();
5831                 }
5832
5833                 IJavaElement element = getElementAt(caret, false);
5834
5835                 if (!(element instanceof ISourceReference))
5836                         return null;
5837
5838                 if (element.getElementType() == IJavaElement.IMPORT_DECLARATION) {
5839
5840                         IImportDeclaration declaration = (IImportDeclaration) element;
5841                         IImportContainer container = (IImportContainer) declaration
5842                                         .getParent();
5843                         ISourceRange srcRange = null;
5844
5845                         try {
5846                                 srcRange = container.getSourceRange();
5847                         } catch (JavaModelException e) {
5848                         }
5849
5850                         if (srcRange != null && srcRange.getOffset() == caret)
5851                                 return container;
5852                 }
5853
5854                 return (ISourceReference) element;
5855         }
5856
5857         /**
5858          * Returns the most narrow java element including the given offset.
5859          * 
5860          * @param offset
5861          *            the offset inside of the requested element
5862          * @param reconcile
5863          *            <code>true</code> if editor input should be reconciled in
5864          *            advance
5865          * @return the most narrow java element
5866          * @since 3.0
5867          */
5868         protected IJavaElement getElementAt(int offset, boolean reconcile) {
5869                 return getElementAt(offset);
5870         }
5871
5872         public ShowInContext getShowInContext() {
5873                 IFile file;
5874                 if(getEditorInput() instanceof FileStoreEditorInput){
5875                         FileStoreEditorInput fei = (FileStoreEditorInput) getEditorInput();
5876                         file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(fei.getURI().getPath()));
5877                 } else {
5878                         FileEditorInput fei = (FileEditorInput) getEditorInput();
5879                         // added to fix ticket 637
5880                         file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(fei.getURI().getPath()));
5881                 }
5882                 ShowInContext context = BrowserUtil.getShowInContext(file,
5883                                 false, "");
5884                 if (context != null) {
5885                         return context;
5886                 }
5887                 return new ShowInContext(file, null);
5888         }
5889
5890         public String[] getShowInTargetIds() {
5891                 return new String[] { BrowserView.ID_BROWSER };
5892         }
5893
5894         /**
5895          * Updates the occurrences annotations based on the current selection.
5896          * 
5897          * @param selection
5898          *            the text selection
5899          * @param astRoot
5900          *            the compilation unit AST
5901          * @since 3.0
5902          */
5903         protected void updateOccurrenceAnnotations(ITextSelection selection) {// ,
5904                 // CompilationUnit
5905                 // astRoot)
5906                 // {
5907
5908                 if (fOccurrencesFinderJob != null)
5909                         fOccurrencesFinderJob.cancel();
5910
5911                 if (!fMarkOccurrenceAnnotations)
5912                         return;
5913
5914                 // if (astRoot == null || selection == null)
5915                 if (selection == null)
5916                         return;
5917
5918                 IDocument document = getSourceViewer().getDocument();
5919                 if (document == null)
5920                         return;
5921
5922                 fMarkOccurrenceTargetRegion = null;
5923                 if (document instanceof IDocumentExtension4) {
5924                         int offset = selection.getOffset();
5925                         long currentModificationStamp = ((IDocumentExtension4) document)
5926                                         .getModificationStamp();
5927                         if (fMarkOccurrenceTargetRegion != null
5928                                         && currentModificationStamp == fMarkOccurrenceModificationStamp) {
5929                                 if (fMarkOccurrenceTargetRegion.getOffset() <= offset
5930                                                 && offset <= fMarkOccurrenceTargetRegion.getOffset()
5931                                                                 + fMarkOccurrenceTargetRegion.getLength())
5932                                         return;
5933                         }
5934                         fMarkOccurrenceTargetRegion = JavaWordFinder.findWord(document,
5935                                         offset);
5936                         fMarkOccurrenceModificationStamp = currentModificationStamp;
5937                 }
5938
5939                 if (fMarkOccurrenceTargetRegion == null
5940                                 || fMarkOccurrenceTargetRegion.getLength() == 0) {
5941                         return;
5942                 }
5943
5944                 List matches = null;
5945
5946                 if (matches == null) {
5947                         try {
5948                                 matches = new ArrayList();
5949
5950                                 Scanner fScanner = new Scanner();
5951                                 fScanner.setSource(document.get().toCharArray());
5952                                 fScanner.setPHPMode(false);
5953                                 String wordStr;
5954                                 char[] word;
5955
5956                                 wordStr = document.get(fMarkOccurrenceTargetRegion.getOffset(),
5957                                                 fMarkOccurrenceTargetRegion.getLength());
5958                                 if (wordStr != null) {
5959                                         word = wordStr.toCharArray();
5960                                         int fToken = ITerminalSymbols.TokenNameEOF;
5961                                         try {
5962                                                 fToken = fScanner.getNextToken();
5963                                                 while (fToken != ITerminalSymbols.TokenNameEOF) { // &&
5964                                                                                                                                                         // fToken
5965                                                                                                                                                         // !=
5966                                                         // TokenNameERROR) {
5967                                                         if (fToken == ITerminalSymbols.TokenNameVariable
5968                                                                         || fToken == ITerminalSymbols.TokenNameIdentifier) {
5969                                                                 // global variable
5970                                                                 if (fScanner.equalsCurrentTokenSource(word)) {
5971                                                                         matches
5972                                                                                         .add(new Region(
5973                                                                                                         fScanner
5974                                                                                                                         .getCurrentTokenStartPosition(),
5975                                                                                                         fScanner
5976                                                                                                                         .getCurrentTokenEndPosition()
5977                                                                                                                         - fScanner
5978                                                                                                                                         .getCurrentTokenStartPosition()
5979                                                                                                                         + 1));
5980                                                                 }
5981                                                         }
5982                                                         fToken = fScanner.getNextToken();
5983                                                 }
5984                                         } catch (InvalidInputException e) {
5985                                                 // ignore errors
5986                                         } catch (SyntaxError e) {
5987                                                 // ignore errors
5988                                         }
5989                                 }
5990                         } catch (BadLocationException e1) {
5991                                 // ignore errors
5992                         } catch (Exception e) {
5993                                 e.printStackTrace();
5994                                 // ignore errors
5995                         }
5996
5997                 }
5998
5999                 if (matches == null || matches.size() == 0) {
6000                         if (!fStickyOccurrenceAnnotations)
6001                                 removeOccurrenceAnnotations();
6002                         return;
6003                 }
6004
6005                 Position[] positions = new Position[matches.size()];
6006                 int i = 0;
6007                 for (Iterator each = matches.iterator(); each.hasNext();) {
6008                         IRegion currentNode = (IRegion) each.next();
6009                         positions[i++] = new Position(currentNode.getOffset(), currentNode
6010                                         .getLength());
6011                 }
6012
6013                 fOccurrencesFinderJob = new OccurrencesFinderJob(document, positions,
6014                                 selection);
6015                 // fOccurrencesFinderJob.setPriority(Job.DECORATE);
6016                 // fOccurrencesFinderJob.setSystem(true);
6017                 // fOccurrencesFinderJob.schedule();
6018                 fOccurrencesFinderJob.run(new NullProgressMonitor());
6019         }
6020
6021         protected void installOccurrencesFinder() {
6022                 fMarkOccurrenceAnnotations = true;
6023
6024                 fPostSelectionListenerWithAST = new ISelectionListenerWithAST() {
6025                         public void selectionChanged(IEditorPart part,
6026                                         ITextSelection selection) { // ,
6027                                 // CompilationUnit
6028                                 // astRoot)
6029                                 // {
6030                                 updateOccurrenceAnnotations(selection);// , astRoot);
6031                         }
6032                 };
6033                 SelectionListenerWithASTManager.getDefault().addListener(this,
6034                                 fPostSelectionListenerWithAST);
6035                 if (getSelectionProvider() != null) {
6036                         fForcedMarkOccurrencesSelection = getSelectionProvider()
6037                                         .getSelection();
6038                         SelectionListenerWithASTManager.getDefault().forceSelectionChange(
6039                                         this, (ITextSelection) fForcedMarkOccurrencesSelection);
6040                 }
6041
6042                 if (fOccurrencesFinderJobCanceler == null) {
6043                         fOccurrencesFinderJobCanceler = new OccurrencesFinderJobCanceler();
6044                         fOccurrencesFinderJobCanceler.install();
6045                 }
6046         }
6047
6048         protected void uninstallOccurrencesFinder() {
6049                 fMarkOccurrenceAnnotations = false;
6050
6051                 if (fOccurrencesFinderJob != null) {
6052                         fOccurrencesFinderJob.cancel();
6053                         fOccurrencesFinderJob = null;
6054                 }
6055
6056                 if (fOccurrencesFinderJobCanceler != null) {
6057                         fOccurrencesFinderJobCanceler.uninstall();
6058                         fOccurrencesFinderJobCanceler = null;
6059                 }
6060
6061                 if (fPostSelectionListenerWithAST != null) {
6062                         SelectionListenerWithASTManager.getDefault().removeListener(this,
6063                                         fPostSelectionListenerWithAST);
6064                         fPostSelectionListenerWithAST = null;
6065                 }
6066
6067                 removeOccurrenceAnnotations();
6068         }
6069
6070         protected boolean isMarkingOccurrences() {
6071                 return fMarkOccurrenceAnnotations;
6072         }
6073
6074         void removeOccurrenceAnnotations() {
6075                 fMarkOccurrenceModificationStamp = IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP;
6076                 fMarkOccurrenceTargetRegion = null;
6077
6078                 IDocumentProvider documentProvider = getDocumentProvider();
6079                 if (documentProvider == null)
6080                         return;
6081
6082                 IAnnotationModel annotationModel = documentProvider
6083                                 .getAnnotationModel(getEditorInput());
6084                 if (annotationModel == null || fOccurrenceAnnotations == null)
6085                         return;
6086
6087                 synchronized (getLockObject(annotationModel)) {
6088                         if (annotationModel instanceof IAnnotationModelExtension) {
6089                                 ((IAnnotationModelExtension) annotationModel)
6090                                                 .replaceAnnotations(fOccurrenceAnnotations, null);
6091                         } else {
6092                                 for (int i = 0, length = fOccurrenceAnnotations.length; i < length; i++)
6093                                         annotationModel.removeAnnotation(fOccurrenceAnnotations[i]);
6094                         }
6095                         fOccurrenceAnnotations = null;
6096                 }
6097         }
6098 }