Added a souce context menu to the editor; moved "Goto Matching Bracket" to "Navigate...
[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     Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
18 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
19 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
20 import net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider;
21 import net.sourceforge.phpdt.ui.IContextMenuConstants;
22 import net.sourceforge.phpdt.ui.PreferenceConstants;
23 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
24 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
25 import net.sourceforge.phpdt.ui.text.IColorManager;
26 import net.sourceforge.phpdt.ui.text.JavaTextTools;
27 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
28 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
29
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.jface.action.GroupMarker;
35 import org.eclipse.jface.action.IAction;
36 import org.eclipse.jface.action.MenuManager;
37 import org.eclipse.jface.action.Separator;
38 import org.eclipse.jface.preference.IPreferenceStore;
39 import org.eclipse.jface.preference.PreferenceConverter;
40 import org.eclipse.jface.text.BadLocationException;
41 import org.eclipse.jface.text.DefaultInformationControl;
42 import org.eclipse.jface.text.IDocument;
43 import org.eclipse.jface.text.IInformationControl;
44 import org.eclipse.jface.text.IInformationControlCreator;
45 import org.eclipse.jface.text.IRegion;
46 import org.eclipse.jface.text.ITextHover;
47 import org.eclipse.jface.text.ITextOperationTarget;
48 import org.eclipse.jface.text.ITextViewer;
49 import org.eclipse.jface.text.ITextViewerExtension2;
50 import org.eclipse.jface.text.ITextViewerExtension3;
51 import org.eclipse.jface.text.ITypedRegion;
52 import org.eclipse.jface.text.Region;
53 import org.eclipse.jface.text.information.InformationPresenter;
54 import org.eclipse.jface.text.source.AnnotationRulerColumn;
55 import org.eclipse.jface.text.source.CompositeRuler;
56 import org.eclipse.jface.text.source.ISourceViewer;
57 import org.eclipse.jface.text.source.IVerticalRuler;
58 import org.eclipse.jface.text.source.IVerticalRulerColumn;
59 import org.eclipse.jface.text.source.LineNumberRulerColumn;
60 import org.eclipse.jface.text.source.SourceViewerConfiguration;
61 import org.eclipse.jface.util.PropertyChangeEvent;
62 import org.eclipse.swt.SWT;
63 import org.eclipse.swt.custom.BidiSegmentEvent;
64 import org.eclipse.swt.custom.BidiSegmentListener;
65 import org.eclipse.swt.custom.StyledText;
66 import org.eclipse.swt.graphics.Point;
67 import org.eclipse.swt.graphics.RGB;
68 import org.eclipse.swt.widgets.Composite;
69 import org.eclipse.swt.widgets.Shell;
70 import org.eclipse.ui.IEditorInput;
71 import org.eclipse.ui.actions.ActionContext;
72 import org.eclipse.ui.actions.ActionGroup;
73 import org.eclipse.ui.texteditor.ContentAssistAction;
74 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
75 import org.eclipse.ui.texteditor.IDocumentProvider;
76 import org.eclipse.ui.texteditor.IEditorStatusLine;
77 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
78 import org.eclipse.ui.texteditor.StatusTextEditor;
79 import org.eclipse.ui.texteditor.TextOperationAction;
80 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
81 /**
82  * PHP specific text editor.
83  */
84 public class PHPEditor
85   extends StatusTextEditor
86   implements IViewPartInputProvider { // extends TextEditor {
87
88   /** Preference key for showing the line number ruler */
89   private final static String LINE_NUMBER_RULER =
90     PreferenceConstants.EDITOR_LINE_NUMBER_RULER;
91   /** Preference key for the foreground color of the line numbers */
92   private final static String LINE_NUMBER_COLOR =
93     PreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR;
94   /** Preference key for the link color */
95   private final static String LINK_COLOR =
96     PreferenceConstants.EDITOR_LINK_COLOR;
97
98   // protected PHPActionGroup fActionGroups;
99   /** The outline page */
100   private AbstractContentOutlinePage fOutlinePage;
101
102   //  protected PHPSyntaxParserThread fValidationThread = null;
103
104   // private IPreferenceStore fPHPPrefStore;
105
106   /** The editor's bracket matcher */
107   private PHPPairMatcher fBracketMatcher;
108   /** The line number ruler column */
109   private LineNumberRulerColumn fLineNumberRulerColumn;
110
111   protected CompositeActionGroup fActionGroups;
112   /** The standard action groups added to the menu */
113   protected GenerateActionGroup fGenerateActionGroup;
114   protected CompositeActionGroup fContextMenuGroup;
115
116   /** The information presenter. */
117   private InformationPresenter fInformationPresenter;
118
119   /**
120    * Default constructor.
121    */
122   public PHPEditor() {
123     super();
124     JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
125     setSourceViewerConfiguration(
126       new PHPSourceViewerConfiguration(textTools, this));
127     setRangeIndicator(new DefaultRangeIndicator());
128     setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
129
130     //    if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE))
131     //      fUpdater= new OutlinePageSelectionUpdater();
132
133     initializeEditor();
134   }
135   //
136   //    /**
137   //     * @see IMember#getCompilationUnit()
138   //     */
139   //    public ICompilationUnit getCompilationUnit() {
140   //            return this; 
141   //    }
142   //    /**
143   //     * @see org.phpeclipse.phpdt.internal.compiler.env.ICompilationUnit#getContents()
144   //     */
145   //    public char[] getContents() {
146   //            IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
147   //    
148   //            return doc.get().toCharArray();
149   //    }
150
151   /*
152    * Update the hovering behavior depending on the preferences.
153    */
154   private void updateHoverBehavior() {
155     SourceViewerConfiguration configuration = getSourceViewerConfiguration();
156     String[] types = configuration.getConfiguredContentTypes(getSourceViewer());
157
158     for (int i = 0; i < types.length; i++) {
159
160       String t = types[i];
161
162       int[] stateMasks =
163         configuration.getConfiguredTextHoverStateMasks(getSourceViewer(), t);
164
165       ISourceViewer sourceViewer = getSourceViewer();
166       if (sourceViewer instanceof ITextViewerExtension2) {
167         if (stateMasks != null) {
168           for (int j = 0; j < stateMasks.length; j++) {
169             int stateMask = stateMasks[j];
170             ITextHover textHover =
171               configuration.getTextHover(sourceViewer, t, stateMask);
172             ((ITextViewerExtension2) sourceViewer).setTextHover(
173               textHover,
174               t,
175               stateMask);
176           }
177         } else {
178           ITextHover textHover = configuration.getTextHover(sourceViewer, t);
179           ((ITextViewerExtension2) sourceViewer).setTextHover(
180             textHover,
181             t,
182             ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
183         }
184       } else
185         sourceViewer.setTextHover(
186           configuration.getTextHover(sourceViewer, t),
187           t);
188     }
189   }
190
191   /*
192    * @see net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
193    */
194   public Object getViewPartInput() {
195     return getEditorInput().getAdapter(IResource.class);
196   }
197
198   /*
199    * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.
200    * widgets.Composite)
201    */
202   public void createPartControl(Composite parent) {
203     super.createPartControl(parent);
204
205     IInformationControlCreator informationControlCreator =
206       new IInformationControlCreator() {
207       public IInformationControl createInformationControl(Shell parent) {
208         boolean cutDown = false;
209         int style = cutDown ? SWT.NONE : (SWT.V_SCROLL | SWT.H_SCROLL);
210         return new DefaultInformationControl(
211           parent,
212           SWT.RESIZE,
213           style,
214           new HTMLTextPresenter(cutDown));
215       }
216     };
217
218     fInformationPresenter = new InformationPresenter(informationControlCreator);
219     fInformationPresenter.setSizeConstraints(60, 10, true, true);
220     fInformationPresenter.install(getSourceViewer());
221   }
222
223   /**
224    * Returns this document's complete text.
225    *
226    * @return the document's complete text
227    */
228   public String get() {
229     IDocument doc =
230       this.getDocumentProvider().getDocument(this.getEditorInput());
231     return doc.get();
232   }
233
234   /**
235    *  Returns the standard action group of this editor.
236    */
237   protected ActionGroup getActionGroup() {
238     return fActionGroups;
239   }
240
241   public AbstractContentOutlinePage getfOutlinePage() {
242     return fOutlinePage;
243   }
244
245   /** The <code>PHPEditor</code> implementation of this 
246    * <code>AbstractTextEditor</code> method extend the 
247    * actions to add those specific to the receiver
248    */
249   protected void createActions() {
250     super.createActions();
251
252     Action action;
253
254     setAction(
255       "ContentAssistTip",
256       new TextOperationAction(
257         PHPEditorMessages.getResourceBundle(),
258         "ContentAssistTip.",
259         this,
260         ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
261
262     action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
263     action.setActionDefinitionId(
264       PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
265     setAction("ContentAssistProposal", action); //$NON-NLS-1$
266     markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
267     //  WorkbenchHelp.setHelp(action, IJavaHelpContextIds.CONTENT_ASSIST_ACTION);
268
269     action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$
270     action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
271     setAction("Comment", action); //$NON-NLS-1$
272     markAsStateDependentAction("Comment", true); //$NON-NLS-1$
273     //          WorkbenchHelp.setHelp(action, IJavaHelpContextIds.COMMENT_ACTION);
274
275     action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX); //$NON-NLS-1$
276     action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
277     setAction("Uncomment", action); //$NON-NLS-1$
278     markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
279     //          WorkbenchHelp.setHelp(action, IJavaHelpContextIds.UNCOMMENT_ACTION);
280
281     action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
282     action.setActionDefinitionId(PHPEditorActionDefinitionIds.FORMAT);
283     setAction("Format", action); //$NON-NLS-1$
284     markAsStateDependentAction("Format", true); //$NON-NLS-1$
285     markAsSelectionDependentAction("Format", true); //$NON-NLS-1$               
286     //  WorkbenchHelp.setHelp(action, IJavaHelpContextIds.FORMAT_ACTION);
287
288     action = new GotoMatchingBracketAction(this);
289     action.setActionDefinitionId(
290       PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
291     setAction(GotoMatchingBracketAction.GOTO_MATCHING_BRACKET, action);
292
293     fGenerateActionGroup =
294       new GenerateActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
295
296     fActionGroups =
297       new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup });
298
299     // We have to keep the context menu group separate to have better control over positioning
300     fContextMenuGroup =
301       new CompositeActionGroup(new ActionGroup[] { fGenerateActionGroup });
302     //      rg, 
303     //      new LocalHistoryActionGroup(this, ITextEditorActionConstants.GROUP_EDIT)});
304
305     //    if (fValidationThread == null) {
306     //      fValidationThread =
307     //        new PHPSyntaxParserThread(this, getSourceViewer());
308     //      //Thread defaults
309     //
310     //      fValidationThread.start();
311     //    }
312     //
313     //    fValidationThread.setText(getSourceViewer().getTextWidget().getText());
314   }
315
316   /** The <code>PHPEditor</code> implementation of this 
317    * <code>AbstractTextEditor</code> method performs any extra 
318    * disposal actions required by the php editor.
319    */
320   public void dispose() {
321     //   PHPEditorEnvironment.disconnect(this);
322     if (fOutlinePage != null)
323       fOutlinePage.setInput(null);
324
325     if (fActionGroups != null)
326       fActionGroups.dispose();
327
328     super.dispose();
329   }
330
331   /** The <code>PHPEditor</code> implementation of this 
332    * <code>AbstractTextEditor</code> method performs any extra 
333    * revert behavior required by the php editor.
334    */
335   public void doRevertToSaved() {
336     super.doRevertToSaved();
337     if (fOutlinePage != null)
338       fOutlinePage.update();
339   }
340
341   /** The <code>PHPEditor</code> implementation of this 
342    * <code>AbstractTextEditor</code> method performs any extra 
343    * save behavior required by the php editor.
344    */
345   public void doSave(IProgressMonitor monitor) {
346     super.doSave(monitor);
347     // compile or not, according to the user preferences
348     IPreferenceStore store = getPreferenceStore(); // fPHPPrefStore;
349     if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
350       IAction a = PHPParserAction.getInstance();
351       if (a != null)
352         a.run();
353     }
354     //    if (SWT.getPlatform().equals("win32")) {
355     //      IAction a = ShowExternalPreviewAction.getInstance();
356     //      if (a != null)
357     //        a.run();
358     //    }
359     if (fOutlinePage != null)
360       fOutlinePage.update();
361   }
362
363   /** The <code>PHPEditor</code> implementation of this 
364    * <code>AbstractTextEditor</code> method performs any extra 
365    * save as behavior required by the php editor.
366    */
367   public void doSaveAs() {
368     super.doSaveAs();
369     if (fOutlinePage != null)
370       fOutlinePage.update();
371   }
372
373   /** The <code>PHPEditor</code> implementation of this 
374    * <code>AbstractTextEditor</code> method performs sets the 
375    * input of the outline page after AbstractTextEditor has set input.
376    */
377   protected void doSetInput(IEditorInput input) throws CoreException {
378     super.doSetInput(input);
379     if (fOutlinePage != null)
380       fOutlinePage.setInput(input);
381   }
382
383   /*
384    * @see org.phpeclipse.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
385    */
386   //  public Object getViewPartInput() {
387   //    return getEditorInput().getAdapter(IFile.class);
388   //  }
389
390   /** The <code>PHPEditor</code> implementation of this 
391    * <code>AbstractTextEditor</code> method adds any 
392    * PHPEditor specific entries.
393    */
394   public void editorContextMenuAboutToShow(MenuManager menu) {
395     super.editorContextMenuAboutToShow(menu);
396                 menu.appendToGroup(ITextEditorActionConstants.GROUP_UNDO, new Separator(IContextMenuConstants.GROUP_OPEN));     
397                 menu.insertAfter(IContextMenuConstants.GROUP_OPEN, new GroupMarker(IContextMenuConstants.GROUP_SHOW));  
398                 
399                 ActionContext context= new ActionContext(getSelectionProvider().getSelection());
400                 fContextMenuGroup.setContext(context);
401                 fContextMenuGroup.fillContextMenu(menu);
402                 fContextMenuGroup.setContext(null);
403 //    addAction(menu, ITextEditorActionConstants.GROUP_EDIT, "Format"); //$NON-NLS-1$
404 //
405 //    ActionContext context =
406 //      new ActionContext(getSelectionProvider().getSelection());
407 //    fContextMenuGroup.setContext(context);
408 //    fContextMenuGroup.fillContextMenu(menu);
409 //    fContextMenuGroup.setContext(null);
410   }
411
412   protected void updateStateDependentActions() {
413     super.updateStateDependentActions();
414     fGenerateActionGroup.editorStateChanged();
415   }
416
417   /** The <code>PHPEditor</code> implementation of this 
418    * <code>AbstractTextEditor</code> method performs gets
419    * the java content outline page if request is for a an 
420    * outline page.
421    */
422   public Object getAdapter(Class required) {
423     if (IContentOutlinePage.class.equals(required)) {
424       if (fOutlinePage == null) {
425         fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
426         if (getEditorInput() != null)
427           fOutlinePage.setInput(getEditorInput());
428       }
429       return fOutlinePage;
430     }
431     return super.getAdapter(required);
432   }
433
434   //  public void openContextHelp() {
435   //    IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
436   //    ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
437   //    int pos = selection.getOffset();
438   //    String word = getFunctionName(doc, pos);
439   //    openContextHelp(word);
440   //  }
441   //
442   //  private void openContextHelp(String word) {
443   //    open(word);
444   //  }
445   //
446   //  public static void open(String word) {
447   //    IHelp help = WorkbenchHelp.getHelpSupport();
448   //    if (help != null) {
449   //      IHelpResource helpResource = new PHPFunctionHelpResource(word);
450   //      WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
451   //    } else {
452   //      //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
453   //    }
454   //  }
455
456   //    private String getFunctionName(IDocument doc, int pos) {
457   //            Point word = PHPWordExtractor.findWord(doc, pos);
458   //            if (word != null) {
459   //                    try {
460   //                            return doc.get(word.x, word.y).replace('_', '-');
461   //                    } catch (BadLocationException e) {
462   //                    }
463   //            }
464   //            return "";
465   //    }
466
467   /*
468    * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
469    */
470   protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
471
472     try {
473
474       ISourceViewer sourceViewer = getSourceViewer();
475       if (sourceViewer == null)
476         return;
477
478       String property = event.getProperty();
479
480       if (PreferenceConstants.EDITOR_TAB_WIDTH.equals(property)) {
481         Object value = event.getNewValue();
482         if (value instanceof Integer) {
483           sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
484         } else if (value instanceof String) {
485           sourceViewer.getTextWidget().setTabs(
486             Integer.parseInt((String) value));
487         }
488         return;
489       }
490
491       if (LINE_NUMBER_RULER.equals(property)) {
492         if (isLineNumberRulerVisible())
493           showLineNumberRuler();
494         else
495           hideLineNumberRuler();
496         return;
497       }
498
499       if (fLineNumberRulerColumn != null
500         && (LINE_NUMBER_COLOR.equals(property)
501           || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
502           || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
503
504         initializeLineNumberRulerColumn(fLineNumberRulerColumn);
505       }
506
507       if (isJavaEditorHoverProperty(property)) {
508         updateHoverBehavior();
509       }
510
511     } finally {
512       super.handlePreferenceStoreChanged(event);
513     }
514   }
515
516   //  /*
517   //     * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
518   //     */
519   //  protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
520   //
521   //    try {
522   //
523   //      ISourceViewer sourceViewer = getSourceViewer();
524   //      if (sourceViewer == null)
525   //        return;
526   //
527   //      String property = event.getProperty();
528   //
529   //      //      if (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
530   //      //        Object value= event.getNewValue();
531   //      //        if (value instanceof Integer) {
532   //      //          sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
533   //      //        } else if (value instanceof String) {
534   //      //          sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
535   //      //        }
536   //      //        return;
537   //      //      }
538   //
539   //      if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
540   //        if (isLineNumberRulerVisible())
541   //          showLineNumberRuler();
542   //        else
543   //          hideLineNumberRuler();
544   //        return;
545   //      }
546   //
547   //      if (fLineNumberRulerColumn != null
548   //        && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
549   //          || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
550   //          || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
551   //
552   //        initializeLineNumberRulerColumn(fLineNumberRulerColumn);
553   //      }
554   //
555   //    } finally {
556   //      super.handlePreferenceStoreChanged(event);
557   //    }
558   //  }
559
560   private boolean isJavaEditorHoverProperty(String property) {
561     return PreferenceConstants.EDITOR_DEFAULT_HOVER.equals(property)
562       || PreferenceConstants.EDITOR_NONE_HOVER.equals(property)
563       || PreferenceConstants.EDITOR_CTRL_HOVER.equals(property)
564       || PreferenceConstants.EDITOR_SHIFT_HOVER.equals(property)
565       || PreferenceConstants.EDITOR_CTRL_ALT_HOVER.equals(property)
566       || PreferenceConstants.EDITOR_CTRL_SHIFT_HOVER.equals(property)
567       || PreferenceConstants.EDITOR_CTRL_ALT_SHIFT_HOVER.equals(property)
568       || PreferenceConstants.EDITOR_ALT_SHIFT_HOVER.equals(property);
569   }
570
571   /**
572    * Shows the line number ruler column.
573    */
574   private void showLineNumberRuler() {
575     IVerticalRuler v = getVerticalRuler();
576     if (v instanceof CompositeRuler) {
577       CompositeRuler c = (CompositeRuler) v;
578       c.addDecorator(1, createLineNumberRulerColumn());
579     }
580   }
581
582   /**
583    * Return whether the line number ruler column should be 
584    * visible according to the preference store settings.
585    * @return <code>true</code> if the line numbers should be visible
586    */
587   private boolean isLineNumberRulerVisible() {
588     IPreferenceStore store = getPreferenceStore();
589     return store.getBoolean(LINE_NUMBER_RULER);
590   }
591   /**
592    * Hides the line number ruler column.
593    */
594   private void hideLineNumberRuler() {
595     IVerticalRuler v = getVerticalRuler();
596     if (v instanceof CompositeRuler) {
597       CompositeRuler c = (CompositeRuler) v;
598       try {
599         c.removeDecorator(1);
600       } catch (Throwable e) {
601       }
602     }
603   }
604
605   /**
606    * Initializes the given line number ruler column from the preference store.
607    * @param rulerColumn the ruler column to be initialized
608    */
609   protected void initializeLineNumberRulerColumn(LineNumberRulerColumn rulerColumn) {
610     JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
611     IColorManager manager = textTools.getColorManager();
612
613     IPreferenceStore store = getPreferenceStore();
614     if (store != null) {
615
616       RGB rgb = null;
617       // foreground color
618       if (store.contains(LINE_NUMBER_COLOR)) {
619         if (store.isDefault(LINE_NUMBER_COLOR))
620           rgb = PreferenceConverter.getDefaultColor(store, LINE_NUMBER_COLOR);
621         else
622           rgb = PreferenceConverter.getColor(store, LINE_NUMBER_COLOR);
623       }
624       rulerColumn.setForeground(manager.getColor(rgb));
625
626       rgb = null;
627       // background color
628       if (!store.getBoolean(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
629         if (store.contains(PREFERENCE_COLOR_BACKGROUND)) {
630           if (store.isDefault(PREFERENCE_COLOR_BACKGROUND))
631             rgb =
632               PreferenceConverter.getDefaultColor(
633                 store,
634                 PREFERENCE_COLOR_BACKGROUND);
635           else
636             rgb =
637               PreferenceConverter.getColor(store, PREFERENCE_COLOR_BACKGROUND);
638         }
639       }
640       rulerColumn.setBackground(manager.getColor(rgb));
641     }
642   }
643
644   /**
645    * Creates a new line number ruler column that is appropriately initialized.
646    */
647   protected IVerticalRulerColumn createLineNumberRulerColumn() {
648     fLineNumberRulerColumn = new LineNumberRulerColumn();
649     initializeLineNumberRulerColumn(fLineNumberRulerColumn);
650     return fLineNumberRulerColumn;
651   }
652
653   /*
654    * @see AbstractTextEditor#createVerticalRuler()
655    */
656   protected IVerticalRuler createVerticalRuler() {
657     CompositeRuler ruler = new CompositeRuler();
658     ruler.addDecorator(0, new AnnotationRulerColumn(VERTICAL_RULER_WIDTH));
659     if (isLineNumberRulerVisible())
660       ruler.addDecorator(1, createLineNumberRulerColumn());
661     return ruler;
662   }
663
664   /* (non-Javadoc)
665    * Method declared on TextEditor
666    */
667   protected void initializeEditor() {
668     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
669     //   PHPEditorEnvironment.connect(this);
670
671     //    store.addPropertyChangeListener(new IPropertyChangeListener() {
672     //      public void propertyChange(PropertyChangeEvent event) {
673     //        PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner();
674     //        if (scanner != null) {
675     //          scanner.updateToken(PHPEditorEnvironment.getPHPColorProvider());
676     //        }
677     //        if (getSourceViewer() != null) {
678     //          getSourceViewer().invalidateTextPresentation();
679     //        }
680     //
681     //        String property = event.getProperty();
682     //        if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
683     //          if (isLineNumberRulerVisible())
684     //            showLineNumberRuler();
685     //          else
686     //            hideLineNumberRuler();
687     //          return;
688     //        }
689     //      }
690     //    });
691   }
692
693   private static IRegion getSignedSelection(ITextViewer viewer) {
694
695     StyledText text = viewer.getTextWidget();
696     int caretOffset = text.getCaretOffset();
697     Point selection = text.getSelection();
698
699     // caret left
700     int offset, length;
701     if (caretOffset == selection.x) {
702       offset = selection.y;
703       length = selection.x - selection.y;
704
705       // caret right
706     } else {
707       offset = selection.x;
708       length = selection.y - selection.x;
709     }
710
711     return new Region(offset, length);
712   }
713
714   private final static char[] BRACKETS = { '{', '}', '(', ')', '[', ']' };
715
716   private static boolean isBracket(char character) {
717     for (int i = 0; i != BRACKETS.length; ++i)
718       if (character == BRACKETS[i])
719         return true;
720     return false;
721   }
722
723   private static boolean isSurroundedByBrackets(
724     IDocument document,
725     int offset) {
726     if (offset == 0 || offset == document.getLength())
727       return false;
728
729     try {
730       return isBracket(document.getChar(offset - 1))
731         && isBracket(document.getChar(offset));
732
733     } catch (BadLocationException e) {
734       return false;
735     }
736   }
737   /**
738     * Jumps to the matching bracket.
739     */
740   public void gotoMatchingBracket() {
741
742     if (fBracketMatcher == null)
743       fBracketMatcher = new PHPPairMatcher(BRACKETS);
744
745     ISourceViewer sourceViewer = getSourceViewer();
746     IDocument document = sourceViewer.getDocument();
747     if (document == null)
748       return;
749
750     IRegion selection = getSignedSelection(sourceViewer);
751
752     int selectionLength = Math.abs(selection.getLength());
753     if (selectionLength > 1) {
754       setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.invalidSelection")); //$NON-NLS-1$               
755       sourceViewer.getTextWidget().getDisplay().beep();
756       return;
757     }
758
759     // #26314
760     int sourceCaretOffset = selection.getOffset() + selection.getLength();
761     if (isSurroundedByBrackets(document, sourceCaretOffset))
762       sourceCaretOffset -= selection.getLength();
763
764     IRegion region = fBracketMatcher.match(document, sourceCaretOffset);
765     if (region == null) {
766       setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.noMatchingBracket")); //$NON-NLS-1$              
767       sourceViewer.getTextWidget().getDisplay().beep();
768       return;
769     }
770
771     int offset = region.getOffset();
772     int length = region.getLength();
773
774     if (length < 1)
775       return;
776
777     int anchor = fBracketMatcher.getAnchor();
778     int targetOffset =
779       (PHPPairMatcher.RIGHT == anchor) ? offset : offset + length - 1;
780
781     boolean visible = false;
782     if (sourceViewer instanceof ITextViewerExtension3) {
783       ITextViewerExtension3 extension = (ITextViewerExtension3) sourceViewer;
784       visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
785     } else {
786       IRegion visibleRegion = sourceViewer.getVisibleRegion();
787       visible =
788         (targetOffset >= visibleRegion.getOffset()
789           && targetOffset < visibleRegion.getOffset() + visibleRegion.getLength());
790     }
791
792     if (!visible) {
793       setStatusLineErrorMessage(PHPEditorMessages.getString("GotoMatchingBracket.error.bracketOutsideSelectedElement")); //$NON-NLS-1$          
794       sourceViewer.getTextWidget().getDisplay().beep();
795       return;
796     }
797
798     if (selection.getLength() < 0)
799       targetOffset -= selection.getLength();
800
801     sourceViewer.setSelectedRange(targetOffset, selection.getLength());
802     sourceViewer.revealRange(targetOffset, selection.getLength());
803   }
804   /**
805      * Ses the given message as error message to this editor's status line.
806      * @param msg message to be set
807      */
808   protected void setStatusLineErrorMessage(String msg) {
809     IEditorStatusLine statusLine =
810       (IEditorStatusLine) getAdapter(IEditorStatusLine.class);
811     if (statusLine != null)
812       statusLine.setMessage(true, msg, null);
813   }
814
815   /**
816      * Returns a segmentation of the line of the given document appropriate for bidi rendering.
817      * The default implementation returns only the string literals of a php code line as segments.
818      * 
819      * @param document the document
820      * @param lineOffset the offset of the line
821      * @return the line's bidi segmentation
822      * @throws BadLocationException in case lineOffset is not valid in document
823      */
824   public static int[] getBidiLineSegments(IDocument document, int lineOffset)
825     throws BadLocationException {
826
827     IRegion line = document.getLineInformationOfOffset(lineOffset);
828     ITypedRegion[] linePartitioning =
829       document.computePartitioning(lineOffset, line.getLength());
830
831     List segmentation = new ArrayList();
832     for (int i = 0; i < linePartitioning.length; i++) {
833       if (IPHPPartitionScannerConstants
834         .PHP_STRING
835         .equals(linePartitioning[i].getType()))
836         segmentation.add(linePartitioning[i]);
837     }
838
839     if (segmentation.size() == 0)
840       return null;
841
842     int size = segmentation.size();
843     int[] segments = new int[size * 2 + 1];
844
845     int j = 0;
846     for (int i = 0; i < size; i++) {
847       ITypedRegion segment = (ITypedRegion) segmentation.get(i);
848
849       if (i == 0)
850         segments[j++] = 0;
851
852       int offset = segment.getOffset() - lineOffset;
853       if (offset > segments[j - 1])
854         segments[j++] = offset;
855
856       if (offset + segment.getLength() >= line.getLength())
857         break;
858
859       segments[j++] = offset + segment.getLength();
860     }
861
862     if (j < segments.length) {
863       int[] result = new int[j];
864       System.arraycopy(segments, 0, result, 0, j);
865       segments = result;
866     }
867
868     return segments;
869   }
870   /**
871      * Returns a segmentation of the given line appropriate for bidi rendering. The default
872      * implementation returns only the string literals of a php code line as segments.
873      * 
874      * @param lineOffset the offset of the line
875      * @param line the content of the line
876      * @return the line's bidi segmentation
877      */
878   protected int[] getBidiLineSegments(int lineOffset, String line) {
879     IDocumentProvider provider = getDocumentProvider();
880     if (provider != null && line != null && line.length() > 0) {
881       IDocument document = provider.getDocument(getEditorInput());
882       if (document != null)
883         try {
884           return getBidiLineSegments(document, lineOffset);
885         } catch (BadLocationException x) {
886           // ignore
887         }
888     }
889     return null;
890   }
891
892   /*
893    * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
894    */
895   protected final ISourceViewer createSourceViewer(
896     Composite parent,
897     IVerticalRuler ruler,
898     int styles) {
899     ISourceViewer viewer = createJavaSourceViewer(parent, ruler, styles);
900     StyledText text = viewer.getTextWidget();
901     text.addBidiSegmentListener(new BidiSegmentListener() {
902       public void lineGetSegments(BidiSegmentEvent event) {
903         event.segments = getBidiLineSegments(event.lineOffset, event.lineText);
904       }
905     });
906     //   JavaUIHelp.setHelp(this, text, IJavaHelpContextIds.JAVA_EDITOR);
907     return viewer;
908   }
909
910   /*
911    * @see AbstractTextEditor#createSourceViewer(Composite, IVerticalRuler, int)
912    */
913   protected ISourceViewer createJavaSourceViewer(
914     Composite parent,
915     IVerticalRuler ruler,
916     int styles) {
917     return super.createSourceViewer(parent, ruler, styles);
918   }
919
920   /*
921    * @see AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
922    */
923   protected boolean affectsTextPresentation(PropertyChangeEvent event) {
924     JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
925     return textTools.affectsBehavior(event);
926   }
927 }