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