PHP highlighting colors could now be managed by the new PreferencePage
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPEditor.java
index c86919b..fc5da20 100644 (file)
@@ -16,16 +16,15 @@ import java.util.List;
 
 import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
-import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
 import net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider;
+import net.sourceforge.phpdt.ui.PreferenceConstants;
 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
+import net.sourceforge.phpdt.ui.text.IColorManager;
 import net.sourceforge.phpdt.ui.text.JavaTextTools;
-import net.sourceforge.phpeclipse.IPreferenceConstants;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
-import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
 
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
@@ -41,8 +40,10 @@ import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IInformationControl;
 import org.eclipse.jface.text.IInformationControlCreator;
 import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextHover;
 import org.eclipse.jface.text.ITextOperationTarget;
 import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.ITextViewerExtension2;
 import org.eclipse.jface.text.ITextViewerExtension3;
 import org.eclipse.jface.text.ITypedRegion;
 import org.eclipse.jface.text.Region;
@@ -53,7 +54,7 @@ import org.eclipse.jface.text.source.ISourceViewer;
 import org.eclipse.jface.text.source.IVerticalRuler;
 import org.eclipse.jface.text.source.IVerticalRulerColumn;
 import org.eclipse.jface.text.source.LineNumberRulerColumn;
-import org.eclipse.jface.util.IPropertyChangeListener;
+import org.eclipse.jface.text.source.SourceViewerConfiguration;
 import org.eclipse.jface.util.PropertyChangeEvent;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.BidiSegmentEvent;
@@ -81,6 +82,16 @@ public class PHPEditor
   extends StatusTextEditor
   implements IViewPartInputProvider { // extends TextEditor {
 
+  /** Preference key for showing the line number ruler */
+  private final static String LINE_NUMBER_RULER =
+    PreferenceConstants.EDITOR_LINE_NUMBER_RULER;
+  /** Preference key for the foreground color of the line numbers */
+  private final static String LINE_NUMBER_COLOR =
+    PreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR;
+  /** Preference key for the link color */
+  private final static String LINK_COLOR =
+    PreferenceConstants.EDITOR_LINK_COLOR;
+
   // protected PHPActionGroup fActionGroups;
   /** The outline page */
   private PHPContentOutlinePage fOutlinePage;
@@ -135,6 +146,36 @@ public class PHPEditor
   //   }
 
   /*
+   * Update the hovering behavior depending on the preferences.
+   */
+  private void updateHoverBehavior() {
+    SourceViewerConfiguration configuration= getSourceViewerConfiguration();
+    String[] types= configuration.getConfiguredContentTypes(getSourceViewer());
+
+    for (int i= 0; i < types.length; i++) {
+                       
+      String t= types[i];
+                               
+      int[] stateMasks= configuration.getConfiguredTextHoverStateMasks(getSourceViewer(), t);
+
+      ISourceViewer sourceViewer= getSourceViewer();
+      if (sourceViewer instanceof ITextViewerExtension2) {
+        if (stateMasks != null) {
+          for (int j= 0; j < stateMasks.length; j++)   {
+            int stateMask= stateMasks[j];
+            ITextHover textHover= configuration.getTextHover(sourceViewer, t, stateMask);
+            ((ITextViewerExtension2)sourceViewer).setTextHover(textHover, t, stateMask);
+          }
+        } else {
+          ITextHover textHover= configuration.getTextHover(sourceViewer, t);
+          ((ITextViewerExtension2)sourceViewer).setTextHover(textHover, t, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
+        }
+      } else
+        sourceViewer.setTextHover(configuration.getTextHover(sourceViewer, t), t);
+    }
+  }
+  
+  /*
    * @see net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider#getViewPartInput()
    */
   public Object getViewPartInput() {
@@ -412,8 +453,8 @@ public class PHPEditor
   //   }
 
   /*
-     * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
-     */
+   * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
+   */
   protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
 
     try {
@@ -424,17 +465,18 @@ public class PHPEditor
 
       String property = event.getProperty();
 
-      //      if (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
-      //        Object value= event.getNewValue();
-      //        if (value instanceof Integer) {
-      //          sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
-      //        } else if (value instanceof String) {
-      //          sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
-      //        }
-      //        return;
-      //      }
-
-      if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
+      if (PreferenceConstants.EDITOR_TAB_WIDTH.equals(property)) {
+        Object value = event.getNewValue();
+        if (value instanceof Integer) {
+          sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
+        } else if (value instanceof String) {
+          sourceViewer.getTextWidget().setTabs(
+            Integer.parseInt((String) value));
+        }
+        return;
+      }
+
+      if (LINE_NUMBER_RULER.equals(property)) {
         if (isLineNumberRulerVisible())
           showLineNumberRuler();
         else
@@ -443,17 +485,77 @@ public class PHPEditor
       }
 
       if (fLineNumberRulerColumn != null
-        && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
+        && (LINE_NUMBER_COLOR.equals(property)
           || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
           || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
 
         initializeLineNumberRulerColumn(fLineNumberRulerColumn);
       }
 
+      if (isJavaEditorHoverProperty(property)) {
+        updateHoverBehavior();
+      }
+
     } finally {
       super.handlePreferenceStoreChanged(event);
     }
   }
+
+  //  /*
+  //     * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
+  //     */
+  //  protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
+  //
+  //    try {
+  //
+  //      ISourceViewer sourceViewer = getSourceViewer();
+  //      if (sourceViewer == null)
+  //        return;
+  //
+  //      String property = event.getProperty();
+  //
+  //      //      if (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
+  //      //        Object value= event.getNewValue();
+  //      //        if (value instanceof Integer) {
+  //      //          sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
+  //      //        } else if (value instanceof String) {
+  //      //          sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
+  //      //        }
+  //      //        return;
+  //      //      }
+  //
+  //      if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
+  //        if (isLineNumberRulerVisible())
+  //          showLineNumberRuler();
+  //        else
+  //          hideLineNumberRuler();
+  //        return;
+  //      }
+  //
+  //      if (fLineNumberRulerColumn != null
+  //        && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
+  //          || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
+  //          || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
+  //
+  //        initializeLineNumberRulerColumn(fLineNumberRulerColumn);
+  //      }
+  //
+  //    } finally {
+  //      super.handlePreferenceStoreChanged(event);
+  //    }
+  //  }
+
+  private boolean isJavaEditorHoverProperty(String property) {
+    return PreferenceConstants.EDITOR_DEFAULT_HOVER.equals(property)
+      || PreferenceConstants.EDITOR_NONE_HOVER.equals(property)
+      || PreferenceConstants.EDITOR_CTRL_HOVER.equals(property)
+      || PreferenceConstants.EDITOR_SHIFT_HOVER.equals(property)
+      || PreferenceConstants.EDITOR_CTRL_ALT_HOVER.equals(property)
+      || PreferenceConstants.EDITOR_CTRL_SHIFT_HOVER.equals(property)
+      || PreferenceConstants.EDITOR_CTRL_ALT_SHIFT_HOVER.equals(property)
+      || PreferenceConstants.EDITOR_ALT_SHIFT_HOVER.equals(property);
+  }
+
   /**
    * Shows the line number ruler column.
    */
@@ -466,14 +568,13 @@ public class PHPEditor
   }
 
   /**
-  * Return whether the line number ruler column should be 
-  * visible according to the preference store settings.
-  * @return <code>true</code> if the line numbers should be visible
-  */
+   * Return whether the line number ruler column should be 
+   * visible according to the preference store settings.
+   * @return <code>true</code> if the line numbers should be visible
+   */
   private boolean isLineNumberRulerVisible() {
-    // IPreferenceStore store= getPreferenceStore();
-    return getPreferenceStore().getBoolean(
-      IPreferenceConstants.LINE_NUMBER_RULER);
+    IPreferenceStore store = getPreferenceStore();
+    return store.getBoolean(LINE_NUMBER_RULER);
   }
   /**
    * Hides the line number ruler column.
@@ -490,54 +591,41 @@ public class PHPEditor
   }
 
   /**
-  * Initializes the given line number ruler column from the preference store.
-  * @param rulerColumn the ruler column to be initialized
-  */
+   * Initializes the given line number ruler column from the preference store.
+   * @param rulerColumn the ruler column to be initialized
+   */
   protected void initializeLineNumberRulerColumn(LineNumberRulerColumn rulerColumn) {
     JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
-    // PHPColorProvider manager = PHPEditorEnvironment.getPHPColorProvider();
-    JavaColorManager manager = textTools.getColorManager();
+    IColorManager manager = textTools.getColorManager();
 
     IPreferenceStore store = getPreferenceStore();
     if (store != null) {
 
       RGB rgb = null;
       // foreground color
-      if (store.contains(IPreferenceConstants.LINE_NUMBER_COLOR)) {
-        if (store.isDefault(IPreferenceConstants.LINE_NUMBER_COLOR))
-          rgb =
-            PreferenceConverter.getDefaultColor(
-              store,
-              IPreferenceConstants.LINE_NUMBER_COLOR);
+      if (store.contains(LINE_NUMBER_COLOR)) {
+        if (store.isDefault(LINE_NUMBER_COLOR))
+          rgb = PreferenceConverter.getDefaultColor(store, LINE_NUMBER_COLOR);
         else
-          rgb =
-            PreferenceConverter.getColor(
-              store,
-              IPreferenceConstants.LINE_NUMBER_COLOR);
+          rgb = PreferenceConverter.getColor(store, LINE_NUMBER_COLOR);
       }
       rulerColumn.setForeground(manager.getColor(rgb));
 
       rgb = null;
       // background color
-      if (!store
-        .getBoolean(
-          IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
-        if (store.contains(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND)) {
-          if (store
-            .isDefault(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND))
+      if (!store.getBoolean(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
+        if (store.contains(PREFERENCE_COLOR_BACKGROUND)) {
+          if (store.isDefault(PREFERENCE_COLOR_BACKGROUND))
             rgb =
               PreferenceConverter.getDefaultColor(
                 store,
-                IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND);
+                PREFERENCE_COLOR_BACKGROUND);
           else
             rgb =
-              PreferenceConverter.getColor(
-                store,
-                IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND);
+              PreferenceConverter.getColor(store, PREFERENCE_COLOR_BACKGROUND);
         }
-        rulerColumn.setBackground(manager.getColor(rgb));
       }
-
+      rulerColumn.setBackground(manager.getColor(rgb));
     }
   }
 
@@ -568,30 +656,26 @@ public class PHPEditor
     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
     PHPEditorEnvironment.connect(this);
 
-    setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
-    setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
-    // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider());
-
-    store.addPropertyChangeListener(new IPropertyChangeListener() {
-      public void propertyChange(PropertyChangeEvent event) {
-        PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner();
-        if (scanner != null) {
-          scanner.updateToken(PHPEditorEnvironment.getPHPColorProvider());
-        }
-        if (getSourceViewer() != null) {
-          getSourceViewer().invalidateTextPresentation();
-        }
-
-        String property = event.getProperty();
-        if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
-          if (isLineNumberRulerVisible())
-            showLineNumberRuler();
-          else
-            hideLineNumberRuler();
-          return;
-        }
-      }
-    });
+    //    store.addPropertyChangeListener(new IPropertyChangeListener() {
+    //      public void propertyChange(PropertyChangeEvent event) {
+    //        PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner();
+    //        if (scanner != null) {
+    //          scanner.updateToken(PHPEditorEnvironment.getPHPColorProvider());
+    //        }
+    //        if (getSourceViewer() != null) {
+    //          getSourceViewer().invalidateTextPresentation();
+    //        }
+    //
+    //        String property = event.getProperty();
+    //        if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
+    //          if (isLineNumberRulerVisible())
+    //            showLineNumberRuler();
+    //          else
+    //            hideLineNumberRuler();
+    //          return;
+    //        }
+    //      }
+    //    });
   }
 
   private static IRegion getSignedSelection(ITextViewer viewer) {
@@ -725,49 +809,52 @@ public class PHPEditor
      * @return the line's bidi segmentation
      * @throws BadLocationException in case lineOffset is not valid in document
      */
-    public static int[] getBidiLineSegments(IDocument document, int lineOffset) throws BadLocationException {
-       
-      IRegion line= document.getLineInformationOfOffset(lineOffset);
-      ITypedRegion[] linePartitioning= document.computePartitioning(lineOffset, line.getLength());
-               
-      List segmentation= new ArrayList();
-      for (int i= 0; i < linePartitioning.length; i++) {
-        if (IPHPPartitionScannerConstants.PHP_STRING.equals(linePartitioning[i].getType()))
-          segmentation.add(linePartitioning[i]);
-      }
-               
-               
-      if (segmentation.size() == 0) 
-        return null;
-                       
-      int size= segmentation.size();
-      int[] segments= new int[size * 2 + 1];
-               
-      int j= 0;
-      for (int i= 0; i < size; i++) {
-        ITypedRegion segment= (ITypedRegion) segmentation.get(i);
-                       
-        if (i == 0)
-          segments[j++]= 0;
-                               
-        int offset= segment.getOffset() - lineOffset;
-        if (offset > segments[j - 1])
-          segments[j++]= offset;
-                               
-        if (offset + segment.getLength() >= line.getLength())
-          break;
-                               
-        segments[j++]= offset + segment.getLength();
-      }
-               
-      if (j < segments.length) {
-        int[] result= new int[j];
-        System.arraycopy(segments, 0, result, 0, j);
-        segments= result;
-      }
-               
-      return segments;
+  public static int[] getBidiLineSegments(IDocument document, int lineOffset)
+    throws BadLocationException {
+
+    IRegion line = document.getLineInformationOfOffset(lineOffset);
+    ITypedRegion[] linePartitioning =
+      document.computePartitioning(lineOffset, line.getLength());
+
+    List segmentation = new ArrayList();
+    for (int i = 0; i < linePartitioning.length; i++) {
+      if (IPHPPartitionScannerConstants
+        .PHP_STRING
+        .equals(linePartitioning[i].getType()))
+        segmentation.add(linePartitioning[i]);
+    }
+
+    if (segmentation.size() == 0)
+      return null;
+
+    int size = segmentation.size();
+    int[] segments = new int[size * 2 + 1];
+
+    int j = 0;
+    for (int i = 0; i < size; i++) {
+      ITypedRegion segment = (ITypedRegion) segmentation.get(i);
+
+      if (i == 0)
+        segments[j++] = 0;
+
+      int offset = segment.getOffset() - lineOffset;
+      if (offset > segments[j - 1])
+        segments[j++] = offset;
+
+      if (offset + segment.getLength() >= line.getLength())
+        break;
+
+      segments[j++] = offset + segment.getLength();
     }
+
+    if (j < segments.length) {
+      int[] result = new int[j];
+      System.arraycopy(segments, 0, result, 0, j);
+      segments = result;
+    }
+
+    return segments;
+  }
   /**
      * Returns a segmentation of the given line appropriate for bidi rendering. The default
      * implementation returns only the string literals of a php code line as segments.