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