1 package net.sourceforge.phpeclipse.phpeditor;
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
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
19 import java.util.ResourceBundle;
20 import java.util.StringTokenizer;
22 import net.sourceforge.phpdt.core.ICompilationUnit;
23 import net.sourceforge.phpdt.core.IJavaElement;
24 import net.sourceforge.phpdt.core.IMember;
25 import net.sourceforge.phpdt.core.ISourceRange;
26 import net.sourceforge.phpdt.core.ISourceReference;
27 import net.sourceforge.phpdt.core.JavaModelException;
28 import net.sourceforge.phpdt.core.JavaCore;
29 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
30 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
31 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
32 import net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider;
33 import net.sourceforge.phpdt.ui.IContextMenuConstants;
34 import net.sourceforge.phpdt.ui.JavaUI;
35 import net.sourceforge.phpdt.ui.PreferenceConstants;
36 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
37 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
38 import net.sourceforge.phpdt.ui.text.JavaTextTools;
39 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
40 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
42 import org.eclipse.core.resources.IMarker;
43 import org.eclipse.core.resources.IResource;
44 import org.eclipse.core.runtime.CoreException;
45 import org.eclipse.core.runtime.IProgressMonitor;
46 import org.eclipse.core.runtime.IStatus;
47 import org.eclipse.core.runtime.Preferences;
48 import org.eclipse.core.runtime.Status;
49 import org.eclipse.core.runtime.jobs.Job;
50 import org.eclipse.jface.action.Action;
51 import org.eclipse.jface.action.GroupMarker;
52 import org.eclipse.jface.action.IAction;
53 import org.eclipse.jface.action.MenuManager;
54 import org.eclipse.jface.action.Separator;
55 import org.eclipse.jface.preference.IPreferenceStore;
56 import org.eclipse.jface.preference.PreferenceConverter;
57 import org.eclipse.jface.text.BadLocationException;
58 import org.eclipse.jface.text.DefaultInformationControl;
59 import org.eclipse.jface.text.DocumentEvent;
60 import org.eclipse.jface.text.IDocument;
61 import org.eclipse.jface.text.IDocumentListener;
62 import org.eclipse.jface.text.IInformationControl;
63 import org.eclipse.jface.text.IInformationControlCreator;
64 import org.eclipse.jface.text.IRegion;
65 import org.eclipse.jface.text.ITextHover;
66 import org.eclipse.jface.text.ITextInputListener;
67 import org.eclipse.jface.text.ITextOperationTarget;
68 import org.eclipse.jface.text.ITextSelection;
69 import org.eclipse.jface.text.ITextViewer;
70 import org.eclipse.jface.text.ITextViewerExtension2;
71 import org.eclipse.jface.text.ITextViewerExtension3;
72 import org.eclipse.jface.text.ITypedRegion;
73 import org.eclipse.jface.text.Position;
74 import org.eclipse.jface.text.Region;
75 import org.eclipse.jface.text.TextSelection;
76 import org.eclipse.jface.text.information.IInformationProvider;
77 import org.eclipse.jface.text.information.InformationPresenter;
78 import org.eclipse.jface.text.source.Annotation;
79 import org.eclipse.jface.text.source.IAnnotationModel;
80 import org.eclipse.jface.text.source.IAnnotationModelExtension;
81 import org.eclipse.jface.text.source.IOverviewRuler;
82 import org.eclipse.jface.text.source.ISourceViewer;
83 import org.eclipse.jface.text.source.IVerticalRuler;
84 import org.eclipse.jface.text.source.OverviewRuler;
85 import org.eclipse.jface.text.source.SourceViewerConfiguration;
86 import org.eclipse.jface.util.IPropertyChangeListener;
87 import org.eclipse.jface.util.PropertyChangeEvent;
88 import org.eclipse.jface.viewers.ISelection;
89 import org.eclipse.jface.viewers.ISelectionChangedListener;
90 import org.eclipse.jface.viewers.ISelectionProvider;
91 import org.eclipse.jface.viewers.IStructuredSelection;
92 import org.eclipse.jface.viewers.SelectionChangedEvent;
93 import org.eclipse.jface.viewers.StructuredSelection;
94 import org.eclipse.swt.SWT;
95 import org.eclipse.swt.custom.BidiSegmentEvent;
96 import org.eclipse.swt.custom.BidiSegmentListener;
97 import org.eclipse.swt.custom.StyleRange;
98 import org.eclipse.swt.custom.StyledText;
99 import org.eclipse.swt.events.FocusEvent;
100 import org.eclipse.swt.events.FocusListener;
101 import org.eclipse.swt.events.KeyEvent;
102 import org.eclipse.swt.events.KeyListener;
103 import org.eclipse.swt.events.MouseEvent;
104 import org.eclipse.swt.events.MouseListener;
105 import org.eclipse.swt.events.MouseMoveListener;
106 import org.eclipse.swt.events.PaintEvent;
107 import org.eclipse.swt.events.PaintListener;
108 import org.eclipse.swt.graphics.Color;
109 import org.eclipse.swt.graphics.Cursor;
110 import org.eclipse.swt.graphics.GC;
111 import org.eclipse.swt.graphics.Image;
112 import org.eclipse.swt.graphics.Point;
113 import org.eclipse.swt.graphics.RGB;
114 import org.eclipse.swt.widgets.Composite;
115 import org.eclipse.swt.widgets.Control;
116 import org.eclipse.swt.widgets.Display;
117 import org.eclipse.swt.widgets.Shell;
118 import org.eclipse.ui.IEditorInput;
119 import org.eclipse.ui.IPageLayout;
120 import org.eclipse.ui.IPartService;
121 import org.eclipse.ui.IViewPart;
122 import org.eclipse.ui.IWorkbenchPage;
123 import org.eclipse.ui.IWorkbenchPart;
124 import org.eclipse.ui.IWorkbenchWindow;
125 import org.eclipse.ui.actions.ActionContext;
126 import org.eclipse.ui.actions.ActionGroup;
127 import org.eclipse.ui.editors.text.DefaultEncodingSupport;
128 import org.eclipse.ui.editors.text.IEncodingSupport;
129 import org.eclipse.ui.part.IShowInTargetList;
130 import org.eclipse.ui.texteditor.ContentAssistAction;
131 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
132 import org.eclipse.ui.texteditor.ExtendedTextEditor;
133 import org.eclipse.ui.texteditor.IDocumentProvider;
134 import org.eclipse.ui.texteditor.IEditorStatusLine;
135 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
136 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
137 import org.eclipse.ui.texteditor.MarkerAnnotation;
138 import org.eclipse.ui.texteditor.ResourceAction;
139 import org.eclipse.ui.texteditor.TextEditorAction;
140 import org.eclipse.ui.texteditor.TextOperationAction;
141 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
142 import org.eclipse.ui.views.tasklist.TaskList;
145 * PHP specific text editor.
147 public abstract class PHPEditor extends ExtendedTextEditor implements IViewPartInputProvider {
148 //extends StatusTextEditor implements IViewPartInputProvider { // extends TextEditor {
150 * "Smart" runnable for updating the outline page's selection.
152 class OutlinePageSelectionUpdater implements Runnable {
154 /** Has the runnable already been posted? */
155 private boolean fPosted = false;
157 public OutlinePageSelectionUpdater() {
161 * @see Runnable#run()
164 synchronizeOutlinePageSelection();
169 * Posts this runnable into the event queue.
175 Shell shell = getSite().getShell();
176 if (shell != null & !shell.isDisposed()) {
178 shell.getDisplay().asyncExec(this);
182 class SelectionChangedListener implements ISelectionChangedListener {
183 public void selectionChanged(SelectionChangedEvent event) {
184 doSelectionChanged(event);
191 class MouseClickListener
192 implements KeyListener, MouseListener, MouseMoveListener, FocusListener, PaintListener, IPropertyChangeListener, IDocumentListener, ITextInputListener {
194 /** The session is active. */
195 private boolean fActive;
197 /** The currently active style range. */
198 private IRegion fActiveRegion;
199 /** The currently active style range as position. */
200 private Position fRememberedPosition;
201 /** The hand cursor. */
202 private Cursor fCursor;
204 /** The link color. */
205 private Color fColor;
206 /** The key modifier mask. */
207 private int fKeyModifierMask;
209 public void deactivate() {
213 public void deactivate(boolean redrawAll) {
217 repairRepresentation(redrawAll);
221 public void install() {
223 ISourceViewer sourceViewer = getSourceViewer();
224 if (sourceViewer == null)
227 StyledText text = sourceViewer.getTextWidget();
228 if (text == null || text.isDisposed())
231 updateColor(sourceViewer);
233 sourceViewer.addTextInputListener(this);
235 IDocument document = sourceViewer.getDocument();
236 if (document != null)
237 document.addDocumentListener(this);
239 text.addKeyListener(this);
240 text.addMouseListener(this);
241 text.addMouseMoveListener(this);
242 text.addFocusListener(this);
243 text.addPaintListener(this);
245 updateKeyModifierMask();
247 IPreferenceStore preferenceStore = getPreferenceStore();
248 preferenceStore.addPropertyChangeListener(this);
251 private void updateKeyModifierMask() {
252 String modifiers = getPreferenceStore().getString(BROWSER_LIKE_LINKS_KEY_MODIFIER);
253 fKeyModifierMask = computeStateMask(modifiers);
254 if (fKeyModifierMask == -1) {
255 // Fallback to stored state mask
256 fKeyModifierMask = getPreferenceStore().getInt(BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK);
260 private int computeStateMask(String modifiers) {
261 if (modifiers == null)
264 if (modifiers.length() == 0)
268 StringTokenizer modifierTokenizer = new StringTokenizer(modifiers, ",;.:+-* "); //$NON-NLS-1$
269 while (modifierTokenizer.hasMoreTokens()) {
270 int modifier = EditorUtility.findLocalizedModifier(modifierTokenizer.nextToken());
271 if (modifier == 0 || (stateMask & modifier) == modifier)
273 stateMask = stateMask | modifier;
278 public void uninstall() {
280 if (fColor != null) {
285 if (fCursor != null) {
290 ISourceViewer sourceViewer = getSourceViewer();
291 if (sourceViewer == null)
294 sourceViewer.removeTextInputListener(this);
296 IDocument document = sourceViewer.getDocument();
297 if (document != null)
298 document.removeDocumentListener(this);
300 IPreferenceStore preferenceStore = getPreferenceStore();
301 if (preferenceStore != null)
302 preferenceStore.removePropertyChangeListener(this);
304 StyledText text = sourceViewer.getTextWidget();
305 if (text == null || text.isDisposed())
308 text.removeKeyListener(this);
309 text.removeMouseListener(this);
310 text.removeMouseMoveListener(this);
311 text.removeFocusListener(this);
312 text.removePaintListener(this);
316 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
318 public void propertyChange(PropertyChangeEvent event) {
319 if (event.getProperty().equals(PHPEditor.LINK_COLOR)) {
320 ISourceViewer viewer = getSourceViewer();
323 } else if (event.getProperty().equals(BROWSER_LIKE_LINKS_KEY_MODIFIER)) {
324 updateKeyModifierMask();
328 private void updateColor(ISourceViewer viewer) {
332 StyledText text = viewer.getTextWidget();
333 if (text == null || text.isDisposed())
336 Display display = text.getDisplay();
337 fColor = createColor(getPreferenceStore(), PHPEditor.LINK_COLOR, display);
341 * Creates a color from the information stored in the given preference store.
342 * Returns <code>null</code> if there is no such information available.
344 private Color createColor(IPreferenceStore store, String key, Display display) {
348 if (store.contains(key)) {
350 if (store.isDefault(key))
351 rgb = PreferenceConverter.getDefaultColor(store, key);
353 rgb = PreferenceConverter.getColor(store, key);
356 return new Color(display, rgb);
362 private void repairRepresentation() {
363 repairRepresentation(false);
366 private void repairRepresentation(boolean redrawAll) {
368 if (fActiveRegion == null)
371 ISourceViewer viewer = getSourceViewer();
372 if (viewer != null) {
375 int offset = fActiveRegion.getOffset();
376 int length = fActiveRegion.getLength();
379 if (!redrawAll && viewer instanceof ITextViewerExtension2)
380 ((ITextViewerExtension2) viewer).invalidateTextPresentation(offset, length);
382 viewer.invalidateTextPresentation();
385 if (viewer instanceof ITextViewerExtension3) {
386 ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
387 offset = extension.modelOffset2WidgetOffset(offset);
389 offset -= viewer.getVisibleRegion().getOffset();
392 StyledText text = viewer.getTextWidget();
394 text.redrawRange(offset, length, true);
395 } catch (IllegalArgumentException x) {
396 PHPeclipsePlugin.log(x);
400 fActiveRegion = null;
403 // will eventually be replaced by a method provided by jdt.core
404 private IRegion selectWord(IDocument document, int anchor) {
410 while (offset >= 0) {
411 c = document.getChar(offset);
412 if (!Character.isJavaIdentifierPart(c))
420 int length = document.getLength();
422 while (offset < length) {
423 c = document.getChar(offset);
424 if (!Character.isJavaIdentifierPart(c))
432 return new Region(start, 0);
434 return new Region(start + 1, end - start - 1);
436 } catch (BadLocationException x) {
441 IRegion getCurrentTextRegion(ISourceViewer viewer) {
443 int offset = getCurrentTextOffset(viewer);
448 // IJavaElement input= SelectionConverter.getInput(PHPEditor.this);
449 // if (input == null)
454 // IJavaElement[] elements= null;
455 // synchronized (input) {
456 // elements= ((ICodeAssist) input).codeSelect(offset, 0);
459 // if (elements == null || elements.length == 0)
462 // return selectWord(viewer.getDocument(), offset);
464 // } catch (JavaModelException e) {
469 private int getCurrentTextOffset(ISourceViewer viewer) {
472 StyledText text = viewer.getTextWidget();
473 if (text == null || text.isDisposed())
476 Display display = text.getDisplay();
477 Point absolutePosition = display.getCursorLocation();
478 Point relativePosition = text.toControl(absolutePosition);
480 int widgetOffset = text.getOffsetAtLocation(relativePosition);
481 if (viewer instanceof ITextViewerExtension3) {
482 ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
483 return extension.widgetOffset2ModelOffset(widgetOffset);
485 return widgetOffset + viewer.getVisibleRegion().getOffset();
488 } catch (IllegalArgumentException e) {
493 private void highlightRegion(ISourceViewer viewer, IRegion region) {
495 if (region.equals(fActiveRegion))
498 repairRepresentation();
500 StyledText text = viewer.getTextWidget();
501 if (text == null || text.isDisposed())
508 if (viewer instanceof ITextViewerExtension3) {
509 ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
510 IRegion widgetRange = extension.modelRange2WidgetRange(region);
511 if (widgetRange == null)
514 offset = widgetRange.getOffset();
515 length = widgetRange.getLength();
518 offset = region.getOffset() - viewer.getVisibleRegion().getOffset();
519 length = region.getLength();
522 StyleRange oldStyleRange = text.getStyleRangeAtOffset(offset);
523 Color foregroundColor = fColor;
524 Color backgroundColor = oldStyleRange == null ? text.getBackground() : oldStyleRange.background;
525 StyleRange styleRange = new StyleRange(offset, length, foregroundColor, backgroundColor);
526 text.setStyleRange(styleRange);
529 text.redrawRange(offset, length, true);
531 fActiveRegion = region;
534 private void activateCursor(ISourceViewer viewer) {
535 StyledText text = viewer.getTextWidget();
536 if (text == null || text.isDisposed())
538 Display display = text.getDisplay();
540 fCursor = new Cursor(display, SWT.CURSOR_HAND);
541 text.setCursor(fCursor);
544 private void resetCursor(ISourceViewer viewer) {
545 StyledText text = viewer.getTextWidget();
546 if (text != null && !text.isDisposed())
547 text.setCursor(null);
549 if (fCursor != null) {
556 * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
558 public void keyPressed(KeyEvent event) {
565 if (event.keyCode != fKeyModifierMask) {
572 // removed for #25871
574 // ISourceViewer viewer= getSourceViewer();
575 // if (viewer == null)
578 // IRegion region= getCurrentTextRegion(viewer);
579 // if (region == null)
582 // highlightRegion(viewer, region);
583 // activateCursor(viewer);
587 * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
589 public void keyReleased(KeyEvent event) {
598 * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
600 public void mouseDoubleClick(MouseEvent e) {
603 * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
605 public void mouseDown(MouseEvent event) {
610 if (event.stateMask != fKeyModifierMask) {
615 if (event.button != 1) {
622 * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
624 public void mouseUp(MouseEvent e) {
634 boolean wasActive = fCursor != null;
639 IAction action = getAction("OpenEditor"); //$NON-NLS-1$
646 * @see org.eclipse.swt.events.MouseMoveListener#mouseMove(org.eclipse.swt.events.MouseEvent)
648 public void mouseMove(MouseEvent event) {
650 if (event.widget instanceof Control && !((Control) event.widget).isFocusControl()) {
656 if (event.stateMask != fKeyModifierMask)
658 // modifier was already pressed
662 ISourceViewer viewer = getSourceViewer();
663 if (viewer == null) {
668 StyledText text = viewer.getTextWidget();
669 if (text == null || text.isDisposed()) {
674 if ((event.stateMask & SWT.BUTTON1) != 0 && text.getSelectionCount() != 0) {
679 IRegion region = getCurrentTextRegion(viewer);
680 if (region == null || region.getLength() == 0) {
681 repairRepresentation();
685 highlightRegion(viewer, region);
686 activateCursor(viewer);
690 * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
692 public void focusGained(FocusEvent e) {
696 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
698 public void focusLost(FocusEvent event) {
703 * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
705 public void documentAboutToBeChanged(DocumentEvent event) {
706 if (fActive && fActiveRegion != null) {
707 fRememberedPosition = new Position(fActiveRegion.getOffset(), fActiveRegion.getLength());
709 event.getDocument().addPosition(fRememberedPosition);
710 } catch (BadLocationException x) {
711 fRememberedPosition = null;
717 * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
719 public void documentChanged(DocumentEvent event) {
720 if (fRememberedPosition != null && !fRememberedPosition.isDeleted()) {
721 event.getDocument().removePosition(fRememberedPosition);
722 fActiveRegion = new Region(fRememberedPosition.getOffset(), fRememberedPosition.getLength());
724 fRememberedPosition = null;
726 ISourceViewer viewer = getSourceViewer();
727 if (viewer != null) {
728 StyledText widget = viewer.getTextWidget();
729 if (widget != null && !widget.isDisposed()) {
730 widget.getDisplay().asyncExec(new Runnable() {
740 * @see org.eclipse.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.IDocument)
742 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {
743 if (oldInput == null)
746 oldInput.removeDocumentListener(this);
750 * @see org.eclipse.jface.text.ITextInputListener#inputDocumentChanged(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.IDocument)
752 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
753 if (newInput == null)
755 newInput.addDocumentListener(this);
759 * @see PaintListener#paintControl(PaintEvent)
761 public void paintControl(PaintEvent event) {
762 if (fActiveRegion == null)
765 ISourceViewer viewer = getSourceViewer();
769 StyledText text = viewer.getTextWidget();
770 if (text == null || text.isDisposed())
776 if (viewer instanceof ITextViewerExtension3) {
778 ITextViewerExtension3 extension = (ITextViewerExtension3) viewer;
779 IRegion widgetRange = extension.modelRange2WidgetRange(new Region(offset, length));
780 if (widgetRange == null)
783 offset = widgetRange.getOffset();
784 length = widgetRange.getLength();
788 IRegion region = viewer.getVisibleRegion();
789 if (!includes(region, fActiveRegion))
792 offset = fActiveRegion.getOffset() - region.getOffset();
793 length = fActiveRegion.getLength();
797 Point minLocation = getMinimumLocation(text, offset, length);
798 Point maxLocation = getMaximumLocation(text, offset, length);
800 int x1 = minLocation.x;
801 int x2 = minLocation.x + maxLocation.x - minLocation.x - 1;
802 int y = minLocation.y + text.getLineHeight() - 1;
805 if (fColor != null && !fColor.isDisposed())
806 gc.setForeground(fColor);
807 gc.drawLine(x1, y, x2, y);
810 private boolean includes(IRegion region, IRegion position) {
811 return position.getOffset() >= region.getOffset() && position.getOffset() + position.getLength() <= region.getOffset() + region.getLength();
814 private Point getMinimumLocation(StyledText text, int offset, int length) {
815 Point minLocation = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
817 for (int i = 0; i <= length; i++) {
818 Point location = text.getLocationAtOffset(offset + i);
820 if (location.x < minLocation.x)
821 minLocation.x = location.x;
822 if (location.y < minLocation.y)
823 minLocation.y = location.y;
829 private Point getMaximumLocation(StyledText text, int offset, int length) {
830 Point maxLocation = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
832 for (int i = 0; i <= length; i++) {
833 Point location = text.getLocationAtOffset(offset + i);
835 if (location.x > maxLocation.x)
836 maxLocation.x = location.x;
837 if (location.y > maxLocation.y)
838 maxLocation.y = location.y;
846 * This action dispatches into two behaviours: If there is no current text
847 * hover, the javadoc is displayed using information presenter. If there is
848 * a current text hover, it is converted into a information presenter in
849 * order to make it sticky.
851 class InformationDispatchAction extends TextEditorAction {
853 /** The wrapped text operation action. */
854 private final TextOperationAction fTextOperationAction;
857 * Creates a dispatch action.
859 public InformationDispatchAction(ResourceBundle resourceBundle, String prefix, final TextOperationAction textOperationAction) {
860 super(resourceBundle, prefix, PHPEditor.this);
861 if (textOperationAction == null)
862 throw new IllegalArgumentException();
863 fTextOperationAction = textOperationAction;
867 * @see org.eclipse.jface.action.IAction#run()
871 ISourceViewer sourceViewer = getSourceViewer();
872 if (sourceViewer == null) {
873 fTextOperationAction.run();
877 if (!(sourceViewer instanceof ITextViewerExtension2)) {
878 fTextOperationAction.run();
882 ITextViewerExtension2 textViewerExtension2 = (ITextViewerExtension2) sourceViewer;
884 // does a text hover exist?
885 ITextHover textHover = textViewerExtension2.getCurrentTextHover();
886 if (textHover == null) {
887 fTextOperationAction.run();
891 Point hoverEventLocation = textViewerExtension2.getHoverEventLocation();
892 int offset = computeOffsetAtLocation(sourceViewer, hoverEventLocation.x, hoverEventLocation.y);
894 fTextOperationAction.run();
899 // get the text hover content
900 IDocument document = sourceViewer.getDocument();
901 String contentType = document.getContentType(offset);
903 final IRegion hoverRegion = textHover.getHoverRegion(sourceViewer, offset);
904 if (hoverRegion == null)
907 final String hoverInfo = textHover.getHoverInfo(sourceViewer, hoverRegion);
909 // with information provider
910 IInformationProvider informationProvider = new IInformationProvider() {
912 * @see org.eclipse.jface.text.information.IInformationProvider#getSubject(org.eclipse.jface.text.ITextViewer, int)
914 public IRegion getSubject(ITextViewer textViewer, int offset) {
918 * @see org.eclipse.jface.text.information.IInformationProvider#getInformation(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
920 public String getInformation(ITextViewer textViewer, IRegion subject) {
925 fInformationPresenter.setOffset(offset);
926 fInformationPresenter.setInformationProvider(informationProvider, contentType);
927 fInformationPresenter.showInformation();
929 } catch (BadLocationException e) {
933 // modified version from TextViewer
934 private int computeOffsetAtLocation(ITextViewer textViewer, int x, int y) {
936 StyledText styledText = textViewer.getTextWidget();
937 IDocument document = textViewer.getDocument();
939 if (document == null)
943 int widgetLocation = styledText.getOffsetAtLocation(new Point(x, y));
944 if (textViewer instanceof ITextViewerExtension3) {
945 ITextViewerExtension3 extension = (ITextViewerExtension3) textViewer;
946 return extension.widgetOffset2ModelOffset(widgetLocation);
948 IRegion visibleRegion = textViewer.getVisibleRegion();
949 return widgetLocation + visibleRegion.getOffset();
951 } catch (IllegalArgumentException e) {
958 // static protected class AnnotationAccess implements IAnnotationAccess {
960 // * @see org.eclipse.jface.text.source.IAnnotationAccess#getType(org.eclipse.jface.text.source.Annotation)
962 // public Object getType(Annotation annotation) {
963 // if (annotation instanceof IJavaAnnotation) {
964 // IJavaAnnotation javaAnnotation = (IJavaAnnotation) annotation;
965 // // if (javaAnnotation.isRelevant())
966 // // return javaAnnotation.getAnnotationType();
972 // * @see org.eclipse.jface.text.source.IAnnotationAccess#isMultiLine(org.eclipse.jface.text.source.Annotation)
974 // public boolean isMultiLine(Annotation annotation) {
979 // * @see org.eclipse.jface.text.source.IAnnotationAccess#isTemporary(org.eclipse.jface.text.source.Annotation)
981 // public boolean isTemporary(Annotation annotation) {
982 // if (annotation instanceof IJavaAnnotation) {
983 // IJavaAnnotation javaAnnotation = (IJavaAnnotation) annotation;
984 // if (javaAnnotation.isRelevant())
985 // return javaAnnotation.isTemporary();
991 private class PropertyChangeListener implements org.eclipse.core.runtime.Preferences.IPropertyChangeListener {
993 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
995 public void propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
996 handlePreferencePropertyChanged(event);
1001 * Finds and marks occurrence annotations.
1005 class OccurrencesFinderJob extends Job implements IDocumentListener {
1007 private IDocument fDocument;
1008 private boolean fCancelled= false;
1009 private IProgressMonitor fProgressMonitor;
1010 private Position[] fPositions;
1012 public OccurrencesFinderJob(IDocument document, Position[] positions) {
1013 super("Occurrences Marker"); //$NON-NLS-1$
1014 fDocument= document;
1015 fPositions= positions;
1016 fDocument.addDocumentListener(this);
1019 private boolean isCancelled() {
1020 return fCancelled || fProgressMonitor.isCanceled();
1024 * @see Job#run(org.eclipse.core.runtime.IProgressMonitor)
1026 public IStatus run(IProgressMonitor progressMonitor) {
1028 fProgressMonitor= progressMonitor;
1033 return Status.CANCEL_STATUS;
1035 ITextViewer textViewer= getViewer();
1036 if (textViewer == null)
1037 return Status.CANCEL_STATUS;
1039 IDocument document= textViewer.getDocument();
1040 if (document == null)
1041 return Status.CANCEL_STATUS;
1043 IDocumentProvider documentProvider= getDocumentProvider();
1044 if (documentProvider == null)
1045 return Status.CANCEL_STATUS;
1047 IAnnotationModel annotationModel= documentProvider.getAnnotationModel(getEditorInput());
1048 if (annotationModel == null)
1049 return Status.CANCEL_STATUS;
1051 // Add occurrence annotations
1052 int length= fPositions.length;
1053 Map annotationMap= new HashMap(length);
1054 for (int i= 0; i < length; i++) {
1057 return Status.CANCEL_STATUS;
1060 Position position= fPositions[i];
1062 // Create & add annotation
1064 message= document.get(position.offset, position.length);
1065 } catch (BadLocationException ex) {
1070 new Annotation("net.sourceforge.phpdt.ui.occurrences", false, message), //$NON-NLS-1$
1075 return Status.CANCEL_STATUS;
1077 synchronized (annotationModel) {
1078 if (annotationModel instanceof IAnnotationModelExtension) {
1079 ((IAnnotationModelExtension)annotationModel).replaceAnnotations(fOccurrenceAnnotations, annotationMap);
1081 removeOccurrenceAnnotations();
1082 Iterator iter= annotationMap.entrySet().iterator();
1083 while (iter.hasNext()) {
1084 Map.Entry mapEntry= (Map.Entry)iter.next();
1085 annotationModel.addAnnotation((Annotation)mapEntry.getKey(), (Position)mapEntry.getValue());
1088 fOccurrenceAnnotations= (Annotation[])annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
1091 fDocument.removeDocumentListener(this);
1093 return Status.OK_STATUS;
1097 * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
1099 public void documentAboutToBeChanged(DocumentEvent event) {
1104 * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
1106 public void documentChanged(DocumentEvent event) {
1112 * Holds the current occurrence annotations.
1115 private Annotation[] fOccurrenceAnnotations= null;
1117 private Job fOccurrencesFinderJob;
1119 /** Preference key for showing the line number ruler */
1120 // private final static String LINE_NUMBER_RULER = PreferenceConstants.EDITOR_LINE_NUMBER_RULER;
1121 /** Preference key for the foreground color of the line numbers */
1122 // private final static String LINE_NUMBER_COLOR = PreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR;
1123 /** Preference key for the link color */
1124 private final static String LINK_COLOR = PreferenceConstants.EDITOR_LINK_COLOR;
1125 /** Preference key for compiler task tags */
1126 private final static String COMPILER_TASK_TAGS = JavaCore.COMPILER_TASK_TAGS;
1128 // protected PHPActionGroup fActionGroups;
1129 // /** The outline page */
1130 // private AbstractContentOutlinePage fOutlinePage;
1131 /** The outline page */
1132 protected JavaOutlinePage fOutlinePage;
1133 /** Outliner context menu Id */
1134 protected String fOutlinerContextMenuId;
1135 /** Indicates whether this editor should react on outline page selection changes */
1136 private int fIgnoreOutlinePageSelection;
1138 /** The outline page selection updater */
1139 private OutlinePageSelectionUpdater fUpdater;
1140 // protected PHPSyntaxParserThread fValidationThread = null;
1142 // private IPreferenceStore fPHPPrefStore;
1143 /** The selection changed listener */
1144 protected ISelectionChangedListener fSelectionChangedListener = new SelectionChangedListener();
1146 /** The editor's bracket matcher */
1147 private PHPPairMatcher fBracketMatcher = new PHPPairMatcher(BRACKETS);
1150 /** The line number ruler column */
1151 // private LineNumberRulerColumn fLineNumberRulerColumn;
1152 /** This editor's encoding support */
1153 private DefaultEncodingSupport fEncodingSupport;
1154 /** The mouse listener */
1155 private MouseClickListener fMouseListener;
1157 protected CompositeActionGroup fActionGroups;
1158 /** The standard action groups added to the menu */
1159 protected GenerateActionGroup fGenerateActionGroup;
1160 protected CompositeActionGroup fContextMenuGroup;
1162 /** The information presenter. */
1163 private InformationPresenter fInformationPresenter;
1164 /** The annotation access */
1165 // protected IAnnotationAccess fAnnotationAccess = new AnnotationAccess();
1166 /** The overview ruler */
1167 protected OverviewRuler isOverviewRulerVisible;
1168 /** The source viewer decoration support */
1169 //protected SourceViewerDecorationSupport fSourceViewerDecorationSupport;
1170 /** The overview ruler */
1171 //protected OverviewRuler fOverviewRuler;
1173 /** The preference property change listener for java core. */
1174 private org.eclipse.core.runtime.Preferences.IPropertyChangeListener fPropertyChangeListener = new PropertyChangeListener();
1176 * Returns the most narrow java element including the given offset
1178 * @param offset the offset inside of the requested element
1180 abstract protected IJavaElement getElementAt(int offset);
1183 * Returns the java element of this editor's input corresponding to the given IJavaElement
1185 abstract protected IJavaElement getCorrespondingElement(IJavaElement element);
1187 * Sets the input of the editor's outline page.
1189 abstract protected void setOutlinePageInput(JavaOutlinePage page, IEditorInput input);
1192 * Default constructor.
1194 public PHPEditor() {
1196 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
1197 setSourceViewerConfiguration(new PHPSourceViewerConfiguration(textTools, this));
1198 setRangeIndicator(new DefaultRangeIndicator());
1199 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
1201 // don't activate this scope without synchronizing plugin.xml !!!
1202 // setKeyBindingScopes(new String[] { "net.sourceforge.phpdt.ui.phpEditorScope" }); //$NON-NLS-1$
1204 if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE))
1205 fUpdater= new OutlinePageSelectionUpdater();
1209 // * @see IMember#getCompilationUnit()
1211 // public ICompilationUnit getCompilationUnit() {
1215 // * @see org.phpeclipse.phpdt.internal.compiler.env.ICompilationUnit#getContents()
1217 // public char[] getContents() {
1218 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
1220 // return doc.get().toCharArray();
1223 * @see org.eclipse.ui.texteditor.AbstractTextEditor#updatePropertyDependentActions()
1225 protected void updatePropertyDependentActions() {
1226 super.updatePropertyDependentActions();
1227 if (fEncodingSupport != null)
1228 fEncodingSupport.reset();
1232 * Update the hovering behavior depending on the preferences.
1234 private void updateHoverBehavior() {
1235 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
1236 String[] types = configuration.getConfiguredContentTypes(getSourceViewer());
1238 for (int i = 0; i < types.length; i++) {
1240 String t = types[i];
1242 int[] stateMasks = configuration.getConfiguredTextHoverStateMasks(getSourceViewer(), t);
1244 ISourceViewer sourceViewer = getSourceViewer();
1245 if (sourceViewer instanceof ITextViewerExtension2) {
1246 if (stateMasks != null) {
1247 for (int j = 0; j < stateMasks.length; j++) {
1248 int stateMask = stateMasks[j];
1249 ITextHover textHover = configuration.getTextHover(sourceViewer, t, stateMask);
1250 ((ITextViewerExtension2) sourceViewer).setTextHover(textHover, t, stateMask);
1253 ITextHover textHover = configuration.getTextHover(sourceViewer, t);
1254 ((ITextViewerExtension2) sourceViewer).setTextHover(textHover, t, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
1257 sourceViewer.setTextHover(configuration.getTextHover(sourceViewer, t), t);
1261 public void updatedTitleImage(Image image) {
1262 setTitleImage(image);
1265 * @see net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
1267 public Object getViewPartInput() {
1268 return getEditorInput().getAdapter(IResource.class);
1271 * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSetSelection(ISelection)
1273 protected void doSetSelection(ISelection selection) {
1274 super.doSetSelection(selection);
1275 synchronizeOutlinePageSelection();
1278 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.
1279 * widgets.Composite)
1281 public void createPartControl(Composite parent) {
1282 super.createPartControl(parent);
1284 //fSourceViewerDecorationSupport.install(getPreferenceStore());
1286 Preferences preferences = PHPeclipsePlugin.getDefault().getPluginPreferences();
1287 preferences.addPropertyChangeListener(fPropertyChangeListener);
1289 IInformationControlCreator informationControlCreator = new IInformationControlCreator() {
1290 public IInformationControl createInformationControl(Shell parent) {
1291 boolean cutDown = false;
1292 int style = cutDown ? SWT.NONE : (SWT.V_SCROLL | SWT.H_SCROLL);
1293 return new DefaultInformationControl(parent, SWT.RESIZE, style, new HTMLTextPresenter(cutDown));
1297 fInformationPresenter = new InformationPresenter(informationControlCreator);
1298 fInformationPresenter.setSizeConstraints(60, 10, true, true);
1299 fInformationPresenter.install(getSourceViewer());
1304 * Returns this document's complete text.
1306 * @return the document's complete text
1308 public String get() {
1309 IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
1314 * Sets the outliner's context menu ID.
1316 protected void setOutlinerContextMenuId(String menuId) {
1317 fOutlinerContextMenuId = menuId;
1321 * Returns the standard action group of this editor.
1323 protected ActionGroup getActionGroup() {
1324 return fActionGroups;
1327 // public JavaOutlinePage getfOutlinePage() {
1328 // return fOutlinePage;
1331 /** The <code>PHPEditor</code> implementation of this
1332 * <code>AbstractTextEditor</code> method extend the
1333 * actions to add those specific to the receiver
1335 protected void createActions() {
1336 super.createActions();
1338 ResourceAction resAction = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "ShowJavaDoc.", this, ISourceViewer.INFORMATION, true); //$NON-NLS-1$
1339 resAction = new InformationDispatchAction(PHPEditorMessages.getResourceBundle(), "ShowJavaDoc.", (TextOperationAction) resAction); //$NON-NLS-1$
1340 resAction.setActionDefinitionId(PHPEditorActionDefinitionIds.SHOW_JAVADOC);
1341 setAction("ShowJavaDoc", resAction); //$NON-NLS-1$
1342 // WorkbenchHelp.setHelp(resAction, IJavaHelpContextIds.SHOW_JAVADOC_ACTION);
1348 new TextOperationAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip.", this, ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
1350 action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
1351 action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
1352 setAction("ContentAssistProposal", action); //$NON-NLS-1$
1353 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
1354 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.CONTENT_ASSIST_ACTION);
1356 fEncodingSupport = new DefaultEncodingSupport();
1357 fEncodingSupport.initialize(this);
1359 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$
1360 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
1361 setAction("Comment", action); //$NON-NLS-1$
1362 markAsStateDependentAction("Comment", true); //$NON-NLS-1$
1363 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.COMMENT_ACTION);
1365 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX); //$NON-NLS-1$
1366 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
1367 setAction("Uncomment", action); //$NON-NLS-1$
1368 markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
1369 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.UNCOMMENT_ACTION);
1371 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
1372 action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
1373 setAction("Format", action); //$NON-NLS-1$
1374 markAsStateDependentAction("Format", true); //$NON-NLS-1$
1375 markAsSelectionDependentAction("Format", true); //$NON-NLS-1$
1376 // WorkbenchHelp.setHelp(action, IJavaHelpContextIds.FORMAT_ACTION);
1378 action = new GotoMatchingBracketAction(this);
1379 action.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
1380 setAction(GotoMatchingBracketAction.GOTO_MATCHING_BRACKET, action);
1382 fGenerateActionGroup = new GenerateActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
1384 fActionGroups = new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup });
1386 // We have to keep the context menu group separate to have better control over positioning
1387 fContextMenuGroup = new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup });
1389 // new LocalHistoryActionGroup(this, ITextEditorActionConstants.GROUP_EDIT)});
1391 // add annotation actions
1392 action= new JavaSelectMarkerRulerAction2(PHPEditorMessages.getResourceBundle(), "Editor.RulerAnnotationSelection.", this); //$NON-NLS-1$
1393 setAction("AnnotationAction", action); //$NON-NLS-1$
1396 /** The <code>PHPEditor</code> implementation of this
1397 * <code>AbstractTextEditor</code> method performs any extra
1398 * disposal actions required by the php editor.
1400 public void dispose() {
1401 // PHPEditorEnvironment.disconnect(this);
1402 if (fOutlinePage != null)
1403 fOutlinePage.setInput(null);
1405 if (fActionGroups != null)
1406 fActionGroups.dispose();
1408 if (isBrowserLikeLinks())
1409 disableBrowserLikeLinks();
1411 if (fEncodingSupport != null) {
1412 fEncodingSupport.dispose();
1413 fEncodingSupport = null;
1416 if (fPropertyChangeListener != null) {
1417 Preferences preferences = PHPeclipsePlugin.getDefault().getPluginPreferences();
1418 preferences.removePropertyChangeListener(fPropertyChangeListener);
1419 fPropertyChangeListener = null;
1422 // if (fSourceViewerDecorationSupport != null) {
1423 // fSourceViewerDecorationSupport.dispose();
1424 // fSourceViewerDecorationSupport = null;
1427 if (fBracketMatcher != null) {
1428 fBracketMatcher.dispose();
1429 fBracketMatcher = null;
1434 /** The <code>PHPEditor</code> implementation of this
1435 * <code>AbstractTextEditor</code> method performs any extra
1436 * revert behavior required by the php editor.
1438 // public void doRevertToSaved() {
1439 // super.doRevertToSaved();
1440 // if (fOutlinePage != null)
1441 // fOutlinePage.update();
1444 /** The <code>PHPEditor</code> implementation of this
1445 * <code>AbstractTextEditor</code> method performs any extra
1446 * save behavior required by the php editor.
1448 // public void doSave(IProgressMonitor monitor) {
1449 // super.doSave(monitor);
1450 // compile or not, according to the user preferences
1451 // IPreferenceStore store = getPreferenceStore();
1453 // the parse on save was changed to the eclipse "builders" concept
1454 // if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
1455 // IAction a = PHPParserAction.getInstance();
1460 // if (SWT.getPlatform().equals("win32")) {
1461 // IAction a = ShowExternalPreviewAction.getInstance();
1465 // if (fOutlinePage != null)
1466 // fOutlinePage.update();
1469 /** The <code>PHPEditor</code> implementation of this
1470 * <code>AbstractTextEditor</code> method performs any extra
1471 * save as behavior required by the php editor.
1473 // public void doSaveAs() {
1474 // super.doSaveAs();
1475 // if (fOutlinePage != null)
1476 // fOutlinePage.update();
1479 * @see StatusTextEditor#getStatusHeader(IStatus)
1481 protected String getStatusHeader(IStatus status) {
1482 if (fEncodingSupport != null) {
1483 String message = fEncodingSupport.getStatusHeader(status);
1484 if (message != null)
1487 return super.getStatusHeader(status);
1491 * @see StatusTextEditor#getStatusBanner(IStatus)
1493 protected String getStatusBanner(IStatus status) {
1494 if (fEncodingSupport != null) {
1495 String message = fEncodingSupport.getStatusBanner(status);
1496 if (message != null)
1499 return super.getStatusBanner(status);
1503 * @see StatusTextEditor#getStatusMessage(IStatus)
1505 protected String getStatusMessage(IStatus status) {
1506 if (fEncodingSupport != null) {
1507 String message = fEncodingSupport.getStatusMessage(status);
1508 if (message != null)
1511 return super.getStatusMessage(status);
1513 /** The <code>PHPEditor</code> implementation of this
1514 * <code>AbstractTextEditor</code> method performs sets the
1515 * input of the outline page after AbstractTextEditor has set input.
1517 // protected void doSetInput(IEditorInput input) throws CoreException {
1518 // super.doSetInput(input);
1520 // if (fEncodingSupport != null)
1521 // fEncodingSupport.reset();
1522 // if (fOutlinePage != null)
1523 // fOutlinePage.setInput(input);
1524 // // setOutlinePageInput(fOutlinePage, input);
1526 protected void doSetInput(IEditorInput input) throws CoreException {
1527 super.doSetInput(input);
1528 if (fEncodingSupport != null)
1529 fEncodingSupport.reset();
1530 setOutlinePageInput(fOutlinePage, input);
1533 * @see org.phpeclipse.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
1535 // public Object getViewPartInput() {
1536 // return getEditorInput().getAdapter(IFile.class);
1539 /** The <code>PHPEditor</code> implementation of this
1540 * <code>AbstractTextEditor</code> method adds any
1541 * PHPEditor specific entries.
1543 public void editorContextMenuAboutToShow(MenuManager menu) {
1544 super.editorContextMenuAboutToShow(menu);
1545 menu.appendToGroup(ITextEditorActionConstants.GROUP_UNDO, new Separator(IContextMenuConstants.GROUP_OPEN));
1546 menu.insertAfter(IContextMenuConstants.GROUP_OPEN, new GroupMarker(IContextMenuConstants.GROUP_SHOW));
1548 ActionContext context = new ActionContext(getSelectionProvider().getSelection());
1549 fContextMenuGroup.setContext(context);
1550 fContextMenuGroup.fillContextMenu(menu);
1551 fContextMenuGroup.setContext(null);
1552 // addAction(menu, ITextEditorActionConstants.GROUP_EDIT, "Format"); //$NON-NLS-1$
1554 // ActionContext context =
1555 // new ActionContext(getSelectionProvider().getSelection());
1556 // fContextMenuGroup.setContext(context);
1557 // fContextMenuGroup.fillContextMenu(menu);
1558 // fContextMenuGroup.setContext(null);
1562 * Creates the outline page used with this editor.
1564 protected JavaOutlinePage createOutlinePage() {
1566 // AbstractContentOutlinePage page = new PHPContentOutlinePage(getDocumentProvider(), this);
1568 // page.addSelectionChangedListener(fSelectionChangedListener);
1569 // // setOutlinePageInput(page, getEditorInput());
1570 // if (getEditorInput() != null)
1571 // fOutlinePage.setInput(getEditorInput());
1574 JavaOutlinePage page = new JavaOutlinePage(fOutlinerContextMenuId, this);
1576 page.addSelectionChangedListener(fSelectionChangedListener);
1577 setOutlinePageInput(page, getEditorInput());
1583 * Informs the editor that its outliner has been closed.
1585 public void outlinePageClosed() {
1586 if (fOutlinePage != null) {
1587 fOutlinePage.removeSelectionChangedListener(fSelectionChangedListener);
1588 fOutlinePage = null;
1589 resetHighlightRange();
1593 * Synchronizes the outliner selection with the actual cursor
1594 * position in the editor.
1596 public void synchronizeOutlinePageSelection() {
1598 // if (isEditingScriptRunning())
1601 ISourceViewer sourceViewer = getSourceViewer();
1602 if (sourceViewer == null || fOutlinePage == null)
1605 StyledText styledText = sourceViewer.getTextWidget();
1606 if (styledText == null)
1610 if (sourceViewer instanceof ITextViewerExtension3) {
1611 ITextViewerExtension3 extension = (ITextViewerExtension3) sourceViewer;
1612 caret = extension.widgetOffset2ModelOffset(styledText.getCaretOffset());
1614 int offset = sourceViewer.getVisibleRegion().getOffset();
1615 caret = offset + styledText.getCaretOffset();
1618 IJavaElement element = getElementAt(caret);
1619 if (element instanceof ISourceReference) {
1620 fOutlinePage.removeSelectionChangedListener(fSelectionChangedListener);
1621 fOutlinePage.select((ISourceReference) element);
1622 fOutlinePage.addSelectionChangedListener(fSelectionChangedListener);
1625 protected void updateStateDependentActions() {
1626 super.updateStateDependentActions();
1627 fGenerateActionGroup.editorStateChanged();
1630 protected void setSelection(ISourceReference reference, boolean moveCursor) {
1632 ISelection selection = getSelectionProvider().getSelection();
1633 if (selection instanceof TextSelection) {
1634 TextSelection textSelection = (TextSelection) selection;
1635 if (textSelection.getOffset() != 0 || textSelection.getLength() != 0)
1636 markInNavigationHistory();
1639 if (reference != null) {
1641 StyledText textWidget = null;
1643 ISourceViewer sourceViewer = getSourceViewer();
1644 if (sourceViewer != null)
1645 textWidget = sourceViewer.getTextWidget();
1647 if (textWidget == null)
1652 ISourceRange range = reference.getSourceRange();
1656 int offset = range.getOffset();
1657 int length = range.getLength();
1659 if (offset < 0 || length < 0)
1662 textWidget.setRedraw(false);
1664 setHighlightRange(offset, length, moveCursor);
1672 if (reference instanceof IMember) {
1673 range = ((IMember) reference).getNameRange();
1674 if (range != null) {
1675 offset = range.getOffset();
1676 length = range.getLength();
1679 // else if (reference instanceof IImportDeclaration) {
1680 // String name= ((IImportDeclaration) reference).getElementName();
1681 // if (name != null && name.length() > 0) {
1682 // String content= reference.getSource();
1683 // if (content != null) {
1684 // offset= range.getOffset() + content.indexOf(name);
1685 // length= name.length();
1688 // } else if (reference instanceof IPackageDeclaration) {
1689 // String name= ((IPackageDeclaration) reference).getElementName();
1690 // if (name != null && name.length() > 0) {
1691 // String content= reference.getSource();
1692 // if (content != null) {
1693 // offset= range.getOffset() + content.indexOf(name);
1694 // length= name.length();
1699 if (offset > -1 && length > 0) {
1700 sourceViewer.revealRange(offset, length);
1701 sourceViewer.setSelectedRange(offset, length);
1704 } catch (JavaModelException x) {
1705 } catch (IllegalArgumentException x) {
1707 if (textWidget != null)
1708 textWidget.setRedraw(true);
1711 } else if (moveCursor) {
1712 resetHighlightRange();
1715 markInNavigationHistory();
1718 public void setSelection(IJavaElement element) {
1720 if (element == null || element instanceof ICompilationUnit) { // || element instanceof IClassFile) {
1722 * If the element is an ICompilationUnit this unit is either the input
1723 * of this editor or not being displayed. In both cases, nothing should
1724 * happened. (http://dev.eclipse.org/bugs/show_bug.cgi?id=5128)
1729 IJavaElement corresponding = getCorrespondingElement(element);
1730 if (corresponding instanceof ISourceReference) {
1731 ISourceReference reference = (ISourceReference) corresponding;
1732 // set hightlight range
1733 setSelection(reference, true);
1734 // set outliner selection
1735 if (fOutlinePage != null) {
1736 fOutlinePage.removeSelectionChangedListener(fSelectionChangedListener);
1737 fOutlinePage.select(reference);
1738 fOutlinePage.addSelectionChangedListener(fSelectionChangedListener);
1743 public synchronized void editingScriptStarted() {
1744 ++ fIgnoreOutlinePageSelection;
1747 public synchronized void editingScriptEnded() {
1748 -- fIgnoreOutlinePageSelection;
1751 public synchronized boolean isEditingScriptRunning() {
1752 return (fIgnoreOutlinePageSelection > 0);
1755 /** The <code>PHPEditor</code> implementation of this
1756 * <code>AbstractTextEditor</code> method performs gets
1757 * the java content outline page if request is for a an
1760 public Object getAdapter(Class required) {
1762 if (IContentOutlinePage.class.equals(required)) {
1763 if (fOutlinePage == null)
1764 fOutlinePage = createOutlinePage();
1765 return fOutlinePage;
1768 if (IEncodingSupport.class.equals(required))
1769 return fEncodingSupport;
1771 if (required == IShowInTargetList.class) {
1772 return new IShowInTargetList() {
1773 public String[] getShowInTargetIds() {
1774 return new String[] { JavaUI.ID_PACKAGES, IPageLayout.ID_OUTLINE, IPageLayout.ID_RES_NAV };
1780 return super.getAdapter(required);
1782 // public Object getAdapter(Class required) {
1783 // if (IContentOutlinePage.class.equals(required)) {
1784 // if (fOutlinePage == null) {
1785 // fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
1786 // if (getEditorInput() != null)
1787 // fOutlinePage.setInput(getEditorInput());
1789 // return fOutlinePage;
1792 // if (IEncodingSupport.class.equals(required))
1793 // return fEncodingSupport;
1795 // return super.getAdapter(required);
1798 protected void doSelectionChanged(SelectionChangedEvent event) {
1799 ISourceReference reference = null;
1801 ISelection selection = event.getSelection();
1802 Iterator iter = ((IStructuredSelection) selection).iterator();
1803 while (iter.hasNext()) {
1804 Object o = iter.next();
1805 if (o instanceof ISourceReference) {
1806 reference = (ISourceReference) o;
1811 if (!isActivePart() && PHPeclipsePlugin.getActivePage() != null)
1812 PHPeclipsePlugin.getActivePage().bringToTop(this);
1815 editingScriptStarted();
1816 setSelection(reference, !isActivePart());
1818 editingScriptEnded();
1822 * @see AbstractTextEditor#adjustHighlightRange(int, int)
1824 protected void adjustHighlightRange(int offset, int length) {
1828 IJavaElement element = getElementAt(offset);
1829 while (element instanceof ISourceReference) {
1830 ISourceRange range = ((ISourceReference) element).getSourceRange();
1831 if (offset < range.getOffset() + range.getLength() && range.getOffset() < offset + length) {
1832 setHighlightRange(range.getOffset(), range.getLength(), true);
1833 if (fOutlinePage != null) {
1834 fOutlinePage.removeSelectionChangedListener(fSelectionChangedListener);
1835 fOutlinePage.select((ISourceReference) element);
1836 fOutlinePage.addSelectionChangedListener(fSelectionChangedListener);
1840 element = element.getParent();
1843 } catch (JavaModelException x) {
1844 PHPeclipsePlugin.log(x.getStatus());
1847 resetHighlightRange();
1849 protected boolean isActivePart() {
1850 IWorkbenchWindow window = getSite().getWorkbenchWindow();
1851 IPartService service = window.getPartService();
1852 IWorkbenchPart part = service.getActivePart();
1853 return part != null && part.equals(this);
1857 // public void openContextHelp() {
1858 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
1859 // ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
1860 // int pos = selection.getOffset();
1861 // String word = getFunctionName(doc, pos);
1862 // openContextHelp(word);
1865 // private void openContextHelp(String word) {
1869 // public static void open(String word) {
1870 // IHelp help = WorkbenchHelp.getHelpSupport();
1871 // if (help != null) {
1872 // IHelpResource helpResource = new PHPFunctionHelpResource(word);
1873 // WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
1875 // // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
1879 // private String getFunctionName(IDocument doc, int pos) {
1880 // Point word = PHPWordExtractor.findWord(doc, pos);
1881 // if (word != null) {
1883 // return doc.get(word.x, word.y).replace('_', '-');
1884 // } catch (BadLocationException e) {
1891 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
1893 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1897 ISourceViewer sourceViewer = getSourceViewer();
1898 if (sourceViewer == null)
1901 String property = event.getProperty();
1903 if (PreferenceConstants.EDITOR_TAB_WIDTH.equals(property)) {
1904 Object value = event.getNewValue();
1905 if (value instanceof Integer) {
1906 sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
1907 } else if (value instanceof String) {
1908 sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
1913 // if (OVERVIEW_RULER.equals(property)) {
1914 // if (isOverviewRulerVisible())
1915 // showOverviewRuler();
1917 // hideOverviewRuler();
1921 // if (LINE_NUMBER_RULER.equals(property)) {
1922 // if (isLineNumberRulerVisible())
1923 // showLineNumberRuler();
1925 // hideLineNumberRuler();
1929 // if (fLineNumberRulerColumn != null
1930 // && (LINE_NUMBER_COLOR.equals(property) || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property) || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
1932 // initializeLineNumberRulerColumn(fLineNumberRulerColumn);
1935 if (isJavaEditorHoverProperty(property))
1936 updateHoverBehavior();
1938 if (BROWSER_LIKE_LINKS.equals(property)) {
1939 if (isBrowserLikeLinks())
1940 enableBrowserLikeLinks();
1942 disableBrowserLikeLinks();
1947 super.handlePreferenceStoreChanged(event);
1952 // * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
1954 // protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1958 // ISourceViewer sourceViewer = getSourceViewer();
1959 // if (sourceViewer == null)
1962 // String property = event.getProperty();
1964 // // if (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
1965 // // Object value= event.getNewValue();
1966 // // if (value instanceof Integer) {
1967 // // sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
1968 // // } else if (value instanceof String) {
1969 // // sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
1974 // if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
1975 // if (isLineNumberRulerVisible())
1976 // showLineNumberRuler();
1978 // hideLineNumberRuler();
1982 // if (fLineNumberRulerColumn != null
1983 // && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
1984 // || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
1985 // || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
1987 // initializeLineNumberRulerColumn(fLineNumberRulerColumn);
1991 // super.handlePreferenceStoreChanged(event);
1995 // private boolean isJavaEditorHoverProperty(String property) {
1996 // return PreferenceConstants.EDITOR_DEFAULT_HOVER.equals(property)
1997 // || PreferenceConstants.EDITOR_NONE_HOVER.equals(property)
1998 // || PreferenceConstants.EDITOR_CTRL_HOVER.equals(property)
1999 // || PreferenceConstants.EDITOR_SHIFT_HOVER.equals(property)
2000 // || PreferenceConstants.EDITOR_CTRL_ALT_HOVER.equals(property)
2001 // || PreferenceConstants.EDITOR_CTRL_SHIFT_HOVER.equals(property)
2002 // || PreferenceConstants.EDITOR_CTRL_ALT_SHIFT_HOVER.equals(property)
2003 // || PreferenceConstants.EDITOR_ALT_SHIFT_HOVER.equals(property);
2007 * Shows the line number ruler column.
2009 // private void showLineNumberRuler() {
2010 // IVerticalRuler v = getVerticalRuler();
2011 // if (v instanceof CompositeRuler) {
2012 // CompositeRuler c = (CompositeRuler) v;
2013 // c.addDecorator(1, createLineNumberRulerColumn());
2016 private boolean isJavaEditorHoverProperty(String property) {
2017 return PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS.equals(property);
2021 * Return whether the browser like links should be enabled
2022 * according to the preference store settings.
2023 * @return <code>true</code> if the browser like links should be enabled
2025 private boolean isBrowserLikeLinks() {
2026 IPreferenceStore store = getPreferenceStore();
2027 return store.getBoolean(BROWSER_LIKE_LINKS);
2031 * Enables browser like links.
2033 private void enableBrowserLikeLinks() {
2034 if (fMouseListener == null) {
2035 fMouseListener = new MouseClickListener();
2036 fMouseListener.install();
2041 * Disables browser like links.
2043 private void disableBrowserLikeLinks() {
2044 if (fMouseListener != null) {
2045 fMouseListener.uninstall();
2046 fMouseListener = null;
2050 * Handles a property change event describing a change
2051 * of the java core's preferences and updates the preference
2052 * related editor properties.
2054 * @param event the property change event
2056 protected void handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
2057 if (COMPILER_TASK_TAGS.equals(event.getProperty())) {
2058 ISourceViewer sourceViewer = getSourceViewer();
2059 if (sourceViewer != null
2060 && affectsTextPresentation(new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event.getNewValue())))
2061 sourceViewer.invalidateTextPresentation();
2066 * Return whether the line number ruler column should be
2067 * visible according to the preference store settings.
2068 * @return <code>true</code> if the line numbers should be visible
2070 // protected boolean isLineNumberRulerVisible() {
2071 // IPreferenceStore store = getPreferenceStore();
2072 // return store.getBoolean(LINE_NUMBER_RULER);
2075 * Hides the line number ruler column.
2077 // private void hideLineNumberRuler() {
2078 // IVerticalRuler v = getVerticalRuler();
2079 // if (v instanceof CompositeRuler) {
2080 // CompositeRuler c = (CompositeRuler) v;
2082 // c.removeDecorator(1);
2083 // } catch (Throwable e) {
2089 * @see AbstractTextEditor#handleCursorPositionChanged()
2091 protected void handleCursorPositionChanged() {
2092 super.handleCursorPositionChanged();
2093 if (!isEditingScriptRunning() && fUpdater != null)
2098 * Initializes the given line number ruler column from the preference store.
2099 * @param rulerColumn the ruler column to be initialized
2101 // protected void initializeLineNumberRulerColumn(LineNumberRulerColumn rulerColumn) {
2102 // JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
2103 // IColorManager manager = textTools.getColorManager();
2105 // IPreferenceStore store = getPreferenceStore();
2106 // if (store != null) {
2109 // // foreground color
2110 // if (store.contains(LINE_NUMBER_COLOR)) {
2111 // if (store.isDefault(LINE_NUMBER_COLOR))
2112 // rgb = PreferenceConverter.getDefaultColor(store, LINE_NUMBER_COLOR);
2114 // rgb = PreferenceConverter.getColor(store, LINE_NUMBER_COLOR);
2116 // rulerColumn.setForeground(manager.getColor(rgb));
2119 // // background color
2120 // if (!store.getBoolean(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
2121 // if (store.contains(PREFERENCE_COLOR_BACKGROUND)) {
2122 // if (store.isDefault(PREFERENCE_COLOR_BACKGROUND))
2123 // rgb = PreferenceConverter.getDefaultColor(store, PREFERENCE_COLOR_BACKGROUND);
2125 // rgb = PreferenceConverter.getColor(store, PREFERENCE_COLOR_BACKGROUND);
2128 // rulerColumn.setBackground(manager.getColor(rgb));
2133 * Creates a new line number ruler column that is appropriately initialized.
2135 // protected IVerticalRulerColumn createLineNumberRulerColumn() {
2136 // fLineNumberRulerColumn = new LineNumberRulerColumn();
2137 // initializeLineNumberRulerColumn(fLineNumberRulerColumn);
2138 // return fLineNumberRulerColumn;
2142 * @see AbstractTextEditor#createVerticalRuler()
2144 // protected IVerticalRuler createVerticalRuler() {
2145 // CompositeRuler ruler = new CompositeRuler();
2146 // ruler.addDecorator(0, new AnnotationRulerColumn(VERTICAL_RULER_WIDTH));
2147 // if (isLineNumberRulerVisible())
2148 // ruler.addDecorator(1, createLineNumberRulerColumn());
2152 private static IRegion getSignedSelection(ITextViewer viewer) {
2154 StyledText text = viewer.getTextWidget();
2155 int caretOffset = text.getCaretOffset();
2156 Point selection = text.getSelection();
2160 if (caretOffset == selection.x) {
2161 offset = selection.y;
2162 length = selection.x - selection.y;
2166 offset = selection.x;
2167 length = selection.y - selection.x;
2170 return new Region(offset, length);
2173 /** Preference key for matching brackets */
2174 protected final static String MATCHING_BRACKETS = PreferenceConstants.EDITOR_MATCHING_BRACKETS;
2175 /** Preference key for matching brackets color */
2176 protected final static String MATCHING_BRACKETS_COLOR = PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
2177 /** Preference key for highlighting current line */
2178 protected final static String CURRENT_LINE = PreferenceConstants.EDITOR_CURRENT_LINE;
2179 /** Preference key for highlight color of current line */
2180 protected final static String CURRENT_LINE_COLOR = PreferenceConstants.EDITOR_CURRENT_LINE_COLOR;
2181 /** Preference key for showing print marging ruler */
2182 protected final static String PRINT_MARGIN = PreferenceConstants.EDITOR_PRINT_MARGIN;
2183 /** Preference key for print margin ruler color */
2184 protected final static String PRINT_MARGIN_COLOR = PreferenceConstants.EDITOR_PRINT_MARGIN_COLOR;
2185 /** Preference key for print margin ruler column */
2186 protected final static String PRINT_MARGIN_COLUMN = PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN;
2187 /** Preference key for error indication */
2188 // protected final static String ERROR_INDICATION = PreferenceConstants.EDITOR_PROBLEM_INDICATION;
2189 /** Preference key for error color */
2190 // protected final static String ERROR_INDICATION_COLOR = PreferenceConstants.EDITOR_PROBLEM_INDICATION_COLOR;
2191 /** Preference key for warning indication */
2192 // protected final static String WARNING_INDICATION = PreferenceConstants.EDITOR_WARNING_INDICATION;
2193 /** Preference key for warning color */
2194 // protected final static String WARNING_INDICATION_COLOR = PreferenceConstants.EDITOR_WARNING_INDICATION_COLOR;
2195 /** Preference key for task indication */
2196 protected final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
2197 /** Preference key for task color */
2198 protected final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
2199 /** Preference key for bookmark indication */
2200 protected final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
2201 /** Preference key for bookmark color */
2202 protected final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
2203 /** Preference key for search result indication */
2204 protected final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
2205 /** Preference key for search result color */
2206 protected final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
2207 /** Preference key for unknown annotation indication */
2208 protected final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
2209 /** Preference key for unknown annotation color */
2210 protected final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
2211 /** Preference key for shwoing the overview ruler */
2212 protected final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
2213 /** Preference key for error indication in overview ruler */
2214 protected final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
2215 /** Preference key for warning indication in overview ruler */
2216 protected final static String WARNING_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
2217 /** Preference key for task indication in overview ruler */
2218 protected final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
2219 /** Preference key for bookmark indication in overview ruler */
2220 protected final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
2221 /** Preference key for search result indication in overview ruler */
2222 protected final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
2223 /** Preference key for unknown annotation indication in overview ruler */
2224 protected final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
2225 // /** Preference key for compiler task tags */
2226 // private final static String COMPILER_TASK_TAGS= JavaCore.COMPILER_TASK_TAGS;
2227 /** Preference key for browser like links */
2228 private final static String BROWSER_LIKE_LINKS = PreferenceConstants.EDITOR_BROWSER_LIKE_LINKS;
2229 /** Preference key for key modifier of browser like links */
2230 private final static String BROWSER_LIKE_LINKS_KEY_MODIFIER = PreferenceConstants.EDITOR_BROWSER_LIKE_LINKS_KEY_MODIFIER;
2232 * Preference key for key modifier mask of browser like links.
2233 * The value is only used if the value of <code>EDITOR_BROWSER_LIKE_LINKS</code>
2234 * cannot be resolved to valid SWT modifier bits.
2238 private final static String BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK = PreferenceConstants.EDITOR_BROWSER_LIKE_LINKS_KEY_MODIFIER_MASK;
2240 private final static char[] BRACKETS = { '{', '}', '(', ')', '[', ']' };
2242 private static boolean isBracket(char character) {
2243 for (int i = 0; i != BRACKETS.length; ++i)
2244 if (character == BRACKETS[i])
2249 private static boolean isSurroundedByBrackets(IDocument document, int offset) {
2250 if (offset == 0 || offset == document.getLength())
2254 return isBracket(document.getChar(offset - 1)) && isBracket(document.getChar(offset));
2256 } catch (BadLocationException e) {
2261 // protected void configureSourceViewerDecorationSupport() {
2263 // fSourceViewerDecorationSupport.setCharacterPairMatcher(fBracketMatcher);
2265 // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2266 // AnnotationType.UNKNOWN,
2267 // UNKNOWN_INDICATION_COLOR,
2268 // UNKNOWN_INDICATION,
2269 // UNKNOWN_INDICATION_IN_OVERVIEW_RULER,
2271 // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2272 // AnnotationType.BOOKMARK,
2273 // BOOKMARK_INDICATION_COLOR,
2274 // BOOKMARK_INDICATION,
2275 // BOOKMARK_INDICATION_IN_OVERVIEW_RULER,
2277 // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2278 // AnnotationType.TASK,
2279 // TASK_INDICATION_COLOR,
2281 // TASK_INDICATION_IN_OVERVIEW_RULER,
2283 // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2284 // AnnotationType.SEARCH,
2285 // SEARCH_RESULT_INDICATION_COLOR,
2286 // SEARCH_RESULT_INDICATION,
2287 // SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER,
2289 // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2290 // AnnotationType.WARNING,
2291 // WARNING_INDICATION_COLOR,
2292 // WARNING_INDICATION,
2293 // WARNING_INDICATION_IN_OVERVIEW_RULER,
2295 // fSourceViewerDecorationSupport.setAnnotationPainterPreferenceKeys(
2296 // AnnotationType.ERROR,
2297 // ERROR_INDICATION_COLOR,
2298 // ERROR_INDICATION,
2299 // ERROR_INDICATION_IN_OVERVIEW_RULER,
2302 // fSourceViewerDecorationSupport.setCursorLinePainterPreferenceKeys(CURRENT_LINE, CURRENT_LINE_COLOR);
2303 // fSourceViewerDecorationSupport.setMarginPainterPreferenceKeys(PRINT_MARGIN, PRINT_MARGIN_COLOR, PRINT_MARGIN_COLUMN);
2304 // fSourceViewerDecorationSupport.setMatchingCharacterPainterPreferenceKeys(MATCHING_BRACKETS, MATCHING_BRACKETS_COLOR);
2306 // fSourceViewerDecorationSupport.setSymbolicFontName(getFontPropertyPreferenceKey());
2310 * Jumps to the matching bracket.
2312 public void gotoMatchingBracket() {
2314 ISourceViewer sourceViewer = getSourceViewer();
2315 IDocument document = sourceViewer.getDocument();
2316 if (document == null)
2319 IRegion selection = getSignedSelection(sourceViewer);
2321 int selectionLength = Math.abs(selection.getLength());
2322 if (selectionLength > 1) {
2323 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.invalidSelection")); //$NON-NLS-1$
2324 sourceViewer.getTextWidget().getDisplay().beep();
2329 int sourceCaretOffset = selection.getOffset() + selection.getLength();
2330 if (isSurroundedByBrackets(document, sourceCaretOffset))
2331 sourceCaretOffset -= selection.getLength();
2333 IRegion region = fBracketMatcher.match(document, sourceCaretOffset);
2334 if (region == null) {
2335 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.noMatchingBracket")); //$NON-NLS-1$
2336 sourceViewer.getTextWidget().getDisplay().beep();
2340 int offset = region.getOffset();
2341 int length = region.getLength();
2346 int anchor = fBracketMatcher.getAnchor();
2347 int targetOffset = (PHPPairMatcher.RIGHT == anchor) ? offset : offset + length - 1;
2349 boolean visible = false;
2350 if (sourceViewer instanceof ITextViewerExtension3) {
2351 ITextViewerExtension3 extension = (ITextViewerExtension3) sourceViewer;
2352 visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
2354 IRegion visibleRegion = sourceViewer.getVisibleRegion();
2355 visible = (targetOffset >= visibleRegion.getOffset() && targetOffset < visibleRegion.getOffset() + visibleRegion.getLength());
2359 setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.bracketOutsideSelectedElement")); //$NON-NLS-1$
2360 sourceViewer.getTextWidget().getDisplay().beep();
2364 if (selection.getLength() < 0)
2365 targetOffset -= selection.getLength();
2367 sourceViewer.setSelectedRange(targetOffset, selection.getLength());
2368 sourceViewer.revealRange(targetOffset, selection.getLength());
2371 * Ses the given message as error message to this editor's status line.
2372 * @param msg message to be set
2374 protected void setStatusLineErrorMessage(String msg) {
2375 IEditorStatusLine statusLine = (IEditorStatusLine) getAdapter(IEditorStatusLine.class);
2376 if (statusLine != null)
2377 statusLine.setMessage(true, msg, null);
2381 * Returns a segmentation of the line of the given document appropriate for bidi rendering.
2382 * The default implementation returns only the string literals of a php code line as segments.
2384 * @param document the document
2385 * @param lineOffset the offset of the line
2386 * @return the line's bidi segmentation
2387 * @throws BadLocationException in case lineOffset is not valid in document
2389 public static int[] getBidiLineSegments(IDocument document, int lineOffset) throws BadLocationException {
2391 IRegion line = document.getLineInformationOfOffset(lineOffset);
2392 ITypedRegion[] linePartitioning = document.computePartitioning(lineOffset, line.getLength());
2394 List segmentation = new ArrayList();
2395 for (int i = 0; i < linePartitioning.length; i++) {
2396 if (IPHPPartitionScannerConstants.PHP_STRING.equals(linePartitioning[i].getType()))
2397 segmentation.add(linePartitioning[i]);
2400 if (segmentation.size() == 0)
2403 int size = segmentation.size();
2404 int[] segments = new int[size * 2 + 1];
2407 for (int i = 0; i < size; i++) {
2408 ITypedRegion segment = (ITypedRegion) segmentation.get(i);
2413 int offset = segment.getOffset() - lineOffset;
2414 if (offset > segments[j - 1])
2415 segments[j++] = offset;
2417 if (offset + segment.getLength() >= line.getLength())
2420 segments[j++] = offset + segment.getLength();
2423 if (j < segments.length) {
2424 int[] result = new int[j];
2425 System.arraycopy(segments, 0, result, 0, j);
2432 * Returns a segmentation of the given line appropriate for bidi rendering. The default
2433 * implementation returns only the string literals of a php code line as segments.
2435 * @param lineOffset the offset of the line
2436 * @param line the content of the line
2437 * @return the line's bidi segmentation
2439 protected int[] getBidiLineSegments(int lineOffset, String line) {
2440 IDocumentProvider provider = getDocumentProvider();
2441 if (provider != null && line != null && line.length() > 0) {
2442 IDocument document = provider.getDocument(getEditorInput());
2443 if (document != null)
2445 return getBidiLineSegments(document, lineOffset);
2446 } catch (BadLocationException x) {
2454 * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
2456 // protected final ISourceViewer createSourceViewer(
2457 // Composite parent,
2458 // IVerticalRuler ruler,
2460 // ISourceViewer viewer = createJavaSourceViewer(parent, ruler, styles);
2461 // StyledText text = viewer.getTextWidget();
2462 // text.addBidiSegmentListener(new BidiSegmentListener() {
2463 // public void lineGetSegments(BidiSegmentEvent event) {
2464 // event.segments = getBidiLineSegments(event.lineOffset, event.lineText);
2467 // // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
2471 public final ISourceViewer getViewer() {
2472 return getSourceViewer();
2475 protected final ISourceViewer createSourceViewer(Composite parent, IVerticalRuler verticalRuler, int styles) {
2476 ISourceViewer viewer= createJavaSourceViewer(parent, verticalRuler, getOverviewRuler(), isOverviewRulerVisible(), styles);
2478 StyledText text= viewer.getTextWidget();
2479 text.addBidiSegmentListener(new BidiSegmentListener() {
2480 public void lineGetSegments(BidiSegmentEvent event) {
2481 event.segments= getBidiLineSegments(event.lineOffset, event.lineText);
2485 // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
2487 // ensure source viewer decoration support has been created and configured
2488 getSourceViewerDecorationSupport(viewer);
2492 // ISharedTextColors sharedColors = PHPeclipsePlugin.getDefault().getJavaTextTools().getColorManager();
2494 // fOverviewRuler = new OverviewRuler(fAnnotationAccess, VERTICAL_RULER_WIDTH, sharedColors);
2495 // fOverviewRuler.addHeaderAnnotationType(AnnotationType.WARNING);
2496 // fOverviewRuler.addHeaderAnnotationType(AnnotationType.ERROR);
2498 // ISourceViewer viewer = createJavaSourceViewer(parent, verticalRuler, fOverviewRuler, isOverviewRulerVisible(), styles);
2500 // StyledText text = viewer.getTextWidget();
2501 // text.addBidiSegmentListener(new BidiSegmentListener() {
2502 // public void lineGetSegments(BidiSegmentEvent event) {
2503 // event.segments = getBidiLineSegments(event.lineOffset, event.lineText);
2507 // // JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
2509 // fSourceViewerDecorationSupport = new SourceViewerDecorationSupport(viewer, fOverviewRuler, fAnnotationAccess, sharedColors);
2510 // configureSourceViewerDecorationSupport();
2515 // protected void showOverviewRuler() {
2516 // if (fOverviewRuler != null) {
2517 // if (getSourceViewer() instanceof ISourceViewerExtension) {
2518 // ((ISourceViewerExtension) getSourceViewer()).showAnnotationsOverview(true);
2519 // fSourceViewerDecorationSupport.updateOverviewDecorations();
2524 // protected void hideOverviewRuler() {
2525 // if (getSourceViewer() instanceof ISourceViewerExtension) {
2526 // fSourceViewerDecorationSupport.hideAnnotationOverview();
2527 // ((ISourceViewerExtension) getSourceViewer()).showAnnotationsOverview(false);
2531 // protected boolean isOverviewRulerVisible() {
2532 // IPreferenceStore store = getPreferenceStore();
2533 // return store.getBoolean(OVERVIEW_RULER);
2536 * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
2538 // protected ISourceViewer createJavaSourceViewer(
2539 // Composite parent,
2540 // IVerticalRuler ruler,
2541 // IOverviewRuler overviewRuler,
2542 // boolean isOverviewRulerVisible,
2544 // return new SourceViewer(parent, ruler, overviewRuler, isOverviewRulerVisible(), styles);
2548 * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
2550 protected ISourceViewer createJavaSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean isOverviewRulerVisible, int styles) {
2551 return new JavaSourceViewer(parent, verticalRuler, getOverviewRuler(), isOverviewRulerVisible(), styles);
2554 * @see AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
2556 protected boolean affectsTextPresentation(PropertyChangeEvent event) {
2557 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
2558 return textTools.affectsBehavior(event);
2562 * Jumps to the error next according to the given direction.
2564 public void gotoError(boolean forward) {
2566 ISelectionProvider provider = getSelectionProvider();
2568 ITextSelection s = (ITextSelection) provider.getSelection();
2569 Position errorPosition = new Position(0, 0);
2570 IJavaAnnotation nextError = getNextError(s.getOffset(), forward, errorPosition);
2572 if (nextError != null) {
2574 IMarker marker = null;
2575 if (nextError instanceof MarkerAnnotation)
2576 marker = ((MarkerAnnotation) nextError).getMarker();
2578 Iterator e = nextError.getOverlaidIterator();
2580 while (e.hasNext()) {
2581 Object o = e.next();
2582 if (o instanceof MarkerAnnotation) {
2583 marker = ((MarkerAnnotation) o).getMarker();
2590 if (marker != null) {
2591 IWorkbenchPage page = getSite().getPage();
2592 IViewPart view = view = page.findView("org.eclipse.ui.views.TaskList"); //$NON-NLS-1$
2593 if (view instanceof TaskList) {
2594 StructuredSelection ss = new StructuredSelection(marker);
2595 ((TaskList) view).setSelection(ss, true);
2599 selectAndReveal(errorPosition.getOffset(), errorPosition.getLength());
2600 // setStatusLineErrorMessage(nextError.getMessage());
2604 setStatusLineErrorMessage(null);
2609 private IJavaAnnotation getNextError(int offset, boolean forward, Position errorPosition) {
2611 IJavaAnnotation nextError = null;
2612 Position nextErrorPosition = null;
2614 IDocument document = getDocumentProvider().getDocument(getEditorInput());
2615 int endOfDocument = document.getLength();
2618 IAnnotationModel model = getDocumentProvider().getAnnotationModel(getEditorInput());
2619 Iterator e = new JavaAnnotationIterator(model, false);
2620 while (e.hasNext()) {
2622 IJavaAnnotation a = (IJavaAnnotation) e.next();
2623 if (a.hasOverlay() || !a.isProblem())
2626 Position p = model.getPosition((Annotation) a);
2627 if (!p.includes(offset)) {
2629 int currentDistance = 0;
2632 currentDistance = p.getOffset() - offset;
2633 if (currentDistance < 0)
2634 currentDistance = endOfDocument - offset + p.getOffset();
2636 currentDistance = offset - p.getOffset();
2637 if (currentDistance < 0)
2638 currentDistance = offset + endOfDocument - p.getOffset();
2641 if (nextError == null || currentDistance < distance) {
2642 distance = currentDistance;
2644 nextErrorPosition = p;
2649 if (nextErrorPosition != null) {
2650 errorPosition.setOffset(nextErrorPosition.getOffset());
2651 errorPosition.setLength(nextErrorPosition.getLength());
2656 void removeOccurrenceAnnotations() {
2657 IDocumentProvider documentProvider= getDocumentProvider();
2658 if (documentProvider == null)
2661 IAnnotationModel annotationModel= documentProvider.getAnnotationModel(getEditorInput());
2662 if (annotationModel == null || fOccurrenceAnnotations == null)
2665 synchronized (annotationModel) {
2666 if (annotationModel instanceof IAnnotationModelExtension) {
2667 ((IAnnotationModelExtension)annotationModel).replaceAnnotations(fOccurrenceAnnotations, null);
2669 for (int i= 0, length= fOccurrenceAnnotations.length; i < length; i++)
2670 annotationModel.removeAnnotation(fOccurrenceAnnotations[i]);
2672 fOccurrenceAnnotations= null;