ContextHelp now in new module net.sourceforge.phpeclipse.phphelp
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPEditor.java
index 71ff3fb..adcf53d 100644 (file)
@@ -11,9 +11,11 @@ Contributors:
     IBM Corporation - Initial implementation
     Klaus Hartlage - www.eclipseproject.de
 **********************************************************************/
+import net.sourceforge.phpeclipse.IPreferenceConstants;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
+import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.help.IHelp;
@@ -22,14 +24,21 @@ import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.preference.PreferenceConverter;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.ITextOperationTarget;
 import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.text.source.AnnotationRulerColumn;
+import org.eclipse.jface.text.source.CompositeRuler;
 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.util.PropertyChangeEvent;
 import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.RGB;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.editors.text.TextEditor;
 import org.eclipse.ui.help.WorkbenchHelp;
@@ -38,13 +47,17 @@ import org.eclipse.ui.texteditor.ITextEditorActionConstants;
 import org.eclipse.ui.texteditor.TextOperationAction;
 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
 /**
- * Java specific text editor.
+ * PHP specific text editor.
  */
 public class PHPEditor extends TextEditor {
 
   protected PHPActionGroup actionGroup;
   /** The outline page */
   private PHPContentOutlinePage fOutlinePage;
+  private IPreferenceStore phpPrefStore;
+
+  /** The line number ruler column */
+  private LineNumberRulerColumn fLineNumberRulerColumn;
 
   /**
    * Default constructor.
@@ -53,6 +66,9 @@ public class PHPEditor extends TextEditor {
     super();
   }
 
+  public PHPContentOutlinePage getfOutlinePage() {
+    return fOutlinePage; 
+  }
   /** The <code>JavaEditor</code> implementation of this 
    * <code>AbstractTextEditor</code> method extend the 
    * actions to add those specific to the receiver
@@ -114,7 +130,7 @@ public class PHPEditor extends TextEditor {
   public void doSave(IProgressMonitor monitor) {
     super.doSave(monitor);
     // compile or not, according to the user preferences
-    IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
+    IPreferenceStore store = phpPrefStore;
     if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
       IAction a = PHPParserAction.getInstance();
       if (a != null)
@@ -150,8 +166,8 @@ public class PHPEditor extends TextEditor {
    */
   public void editorContextMenuAboutToShow(MenuManager menu) {
     super.editorContextMenuAboutToShow(menu);
-  //  addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
-  //  addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
+    //  addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
+    //  addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
     actionGroup.fillContextMenu(menu);
   }
 
@@ -205,11 +221,139 @@ public class PHPEditor extends TextEditor {
     return "";
   }
 
+  /*
+     * @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);
+    }
+  }
+  /**
+   * Shows the line number ruler column.
+   */
+  private void showLineNumberRuler() {
+    IVerticalRuler v = getVerticalRuler();
+    if (v instanceof CompositeRuler) {
+      CompositeRuler c = (CompositeRuler) v;
+      c.addDecorator(1, createLineNumberRulerColumn());
+    }
+  }
+
+  /**
+  * 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 phpPrefStore.getBoolean(IPreferenceConstants.LINE_NUMBER_RULER);
+  }
+  /**
+   * Hides the line number ruler column.
+   */
+  private void hideLineNumberRuler() {
+    IVerticalRuler v = getVerticalRuler();
+    if (v instanceof CompositeRuler) {
+      CompositeRuler c = (CompositeRuler) v;
+      c.removeDecorator(1);
+    }
+  }
+
+  /**
+  * 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= JavaPlugin.getDefault().getJavaTextTools();
+    PHPColorProvider manager = PHPEditorEnvironment.getPHPColorProvider();
+
+    if (phpPrefStore != null) {
+
+      RGB rgb = null;
+      // foreground color
+      if (phpPrefStore.contains(IPreferenceConstants.LINE_NUMBER_COLOR)) {
+        if (phpPrefStore.isDefault(IPreferenceConstants.LINE_NUMBER_COLOR))
+          rgb = PreferenceConverter.getDefaultColor(phpPrefStore, IPreferenceConstants.LINE_NUMBER_COLOR);
+        else
+          rgb = PreferenceConverter.getColor(phpPrefStore, IPreferenceConstants.LINE_NUMBER_COLOR);
+      }
+      rulerColumn.setForeground(manager.getColor(rgb));
+
+      rgb = null;
+      // background color
+      if (!phpPrefStore.getBoolean(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
+        if (phpPrefStore.contains(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND)) {
+          if (phpPrefStore.isDefault(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND))
+            rgb = PreferenceConverter.getDefaultColor(phpPrefStore, IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND);
+          else
+            rgb = PreferenceConverter.getColor(phpPrefStore, IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND);
+        }
+        rulerColumn.setBackground(manager.getColor(rgb));
+      }
+
+    }
+  }
+
+  /**
+   * Creates a new line number ruler column that is appropriately initialized.
+   */
+  protected IVerticalRulerColumn createLineNumberRulerColumn() {
+    fLineNumberRulerColumn = new LineNumberRulerColumn();
+    initializeLineNumberRulerColumn(fLineNumberRulerColumn);
+    return fLineNumberRulerColumn;
+  }
+
+  /*
+   * @see AbstractTextEditor#createVerticalRuler()
+   */
+  protected IVerticalRuler createVerticalRuler() {
+    CompositeRuler ruler = new CompositeRuler();
+    ruler.addDecorator(0, new AnnotationRulerColumn(VERTICAL_RULER_WIDTH));
+    if (isLineNumberRulerVisible())
+      ruler.addDecorator(1, createLineNumberRulerColumn());
+    return ruler;
+  }
+
   /* (non-Javadoc)
    * Method declared on AbstractTextEditor
    */
   protected void initializeEditor() {
-
     PHPEditorEnvironment.connect(this);
 
     setSourceViewerConfiguration(new PHPSourceViewerConfiguration());
@@ -217,8 +361,9 @@ public class PHPEditor extends TextEditor {
     setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
     setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
     // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider());
+    phpPrefStore = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
-    PHPeclipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
+    phpPrefStore.addPropertyChangeListener(new IPropertyChangeListener() {
       public void propertyChange(PropertyChangeEvent event) {
         PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner();
         if (scanner != null) {
@@ -227,6 +372,15 @@ public class PHPEditor extends TextEditor {
         if (getSourceViewer() != null) {
           getSourceViewer().invalidateTextPresentation();
         }
+
+        String property = event.getProperty();
+        if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
+          if (isLineNumberRulerVisible())
+            showLineNumberRuler();
+          else
+            hideLineNumberRuler();
+          return;
+        }
       }
     });
   }