fixed accelerator problem; slightly improved PHP Perspective
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPEditor.java
index f32a1e8..de893a8 100644 (file)
@@ -11,130 +11,442 @@ Contributors:
     IBM Corporation - Initial implementation
     Klaus Hartlage - www.eclipseproject.de
 **********************************************************************/
-import java.util.ResourceBundle;
+import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup;
+import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
+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.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.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.actions.ActionContext;
+import org.eclipse.ui.actions.ActionGroup;
 import org.eclipse.ui.editors.text.TextEditor;
+import org.eclipse.ui.texteditor.ContentAssistAction;
 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
+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 {
 
-       /** The outline page */
-       private PHPContentOutlinePage fOutlinePage;
+  // protected PHPActionGroup fActionGroups;
+  /** The outline page */
+  private PHPContentOutlinePage fOutlinePage;
+  private IPreferenceStore phpPrefStore;
 
-       /**
-        * Default constructor.
-        */
-       public PHPEditor() {
-               super();
-       }
-       
-       /** The <code>JavaEditor</code> implementation of this 
-        * <code>AbstractTextEditor</code> method extend the 
-        * actions to add those specific to the receiver
-        */
-       protected void createActions() {
-               super.createActions();
-               setAction("ContentAssistProposal", new TextOperationAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal.", this, ISourceViewer.CONTENTASSIST_PROPOSALS)); 
-               setAction("ContentAssistTip", new TextOperationAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip.", this, ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION)); 
-       }
-       
-       /** The <code>JavaEditor</code> implementation of this 
-        * <code>AbstractTextEditor</code> method performs any extra 
-        * disposal actions required by the java editor.
-        */
-       public void dispose() {
-               PHPEditorEnvironment.disconnect(this);
-               if (fOutlinePage != null)
-                       fOutlinePage.setInput(null);
-               super.dispose();
-       }
-       
-       /** The <code>JavaEditor</code> implementation of this 
-        * <code>AbstractTextEditor</code> method performs any extra 
-        * revert behavior required by the java editor.
-        */
-       public void doRevertToSaved() {
-               super.doRevertToSaved();
-               if (fOutlinePage != null)
-                       fOutlinePage.update();
-       }
+  /** The line number ruler column */
+  private LineNumberRulerColumn fLineNumberRulerColumn;
+
+  protected CompositeActionGroup fActionGroups;
+  /** The standard action groups added to the menu */
+  private GenerateActionGroup fGenerateActionGroup;
+  private CompositeActionGroup fContextMenuGroup;
+  /**
+   * Default constructor.
+   */
+  public PHPEditor() {
+    super();
+    setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
+  }
+
+  /**
+   *  Returns the standard action group of this editor.
+   */
+  protected ActionGroup getActionGroup() {
+    return fActionGroups;
+  } 
+  
+  public PHPContentOutlinePage getfOutlinePage() {
+    return fOutlinePage;
+  }
+
+  /** The <code>PHPEditor</code> implementation of this 
+   * <code>AbstractTextEditor</code> method extend the 
+   * actions to add those specific to the receiver
+   */
+  protected void createActions() {
+    super.createActions();
+    
+       Action action;
+//    setAction(
+//      "ContentAssistProposal",
+//      new TextOperationAction(
+//        PHPEditorMessages.getResourceBundle(),
+//        "ContentAssistProposal.",
+//        this,
+//        ISourceViewer.CONTENTASSIST_PROPOSALS));
+       action= new ContentAssistAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
+       action.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);            
+       setAction("ContentAssistProposal", action); //$NON-NLS-1$
        
-       /** The <code>JavaEditor</code> implementation of this 
-        * <code>AbstractTextEditor</code> method performs any extra 
-        * save behavior required by the java editor.
-        */
-       public void doSave(IProgressMonitor monitor) {
-               super.doSave(monitor);
-               if (fOutlinePage != null)
-                       fOutlinePage.update();
-       }
+
+//     System.out.println(action.getId());
+//     System.out.println(action.getActionDefinitionId());
        
-       /** The <code>JavaEditor</code> implementation of this 
-        * <code>AbstractTextEditor</code> method performs any extra 
-        * save as behavior required by the java editor.
-        */
-       public void doSaveAs() {
-               super.doSaveAs();
-               if (fOutlinePage != null)
-                       fOutlinePage.update();
-       }
+    setAction(
+      "ContentAssistTip",
+      new TextOperationAction(
+        PHPEditorMessages.getResourceBundle(),
+        "ContentAssistTip.",
+        this,
+        ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
+
+    action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX);
+    action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
+    setAction("Comment", action);
        
-       /** The <code>JavaEditor</code> implementation of this 
-        * <code>AbstractTextEditor</code> method performs sets the 
-        * input of the outline page after AbstractTextEditor has set input.
-        */ 
-       public void doSetInput(IEditorInput input) throws CoreException {
-               super.doSetInput(input);
-               if (fOutlinePage != null)
-                       fOutlinePage.setInput(input);
-       }
+//     System.out.println(action.getId());
+//     System.out.println(action.getActionDefinitionId());
        
-       /** The <code>JavaEditor</code> implementation of this 
-        * <code>AbstractTextEditor</code> method adds any 
-        * JavaEditor specific entries.
-        */ 
-       public void editorContextMenuAboutToShow(MenuManager menu) {
-               super.editorContextMenuAboutToShow(menu);
-               addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
-               addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
-       }
+    action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX);
+    action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
+    setAction("Uncomment", action);
+
+//     System.out.println(action.getId());
+//     System.out.println(action.getActionDefinitionId());
        
-       /** The <code>JavaEditor</code> implementation of this 
-        * <code>AbstractTextEditor</code> method performs gets
-        * the java content outline page if request is for a an 
-        * outline page.
-        */ 
-       public Object getAdapter(Class required) {
-               if (IContentOutlinePage.class.equals(required)) {
-                       if (fOutlinePage == null) {
-                               fOutlinePage= new PHPContentOutlinePage(getDocumentProvider(), this);
-                               if (getEditorInput() != null)
-                                       fOutlinePage.setInput(getEditorInput());
-                       }
-                       return fOutlinePage;
-               }
-               return super.getAdapter(required);
-       }
-               
-       /* (non-Javadoc)
-        * Method declared on AbstractTextEditor
-        */
-       protected void initializeEditor() {
-
-               PHPEditorEnvironment.connect(this);
-
-               setSourceViewerConfiguration(new PHPSourceViewerConfiguration());
-               setRangeIndicator(new DefaultRangeIndicator());
-               setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
-               setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
-       }
+
+  //  fActionGroups = new PHPActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
+
+    markAsStateDependentAction("Comment", true); //$NON-NLS-1$
+    markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
+
+    fGenerateActionGroup= new GenerateActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
+
+    fActionGroups= new CompositeActionGroup(
+      new ActionGroup[] { fGenerateActionGroup }
+      );
+  
+    
+    // We have to keep the context menu group separate to have better control over positioning
+    fContextMenuGroup= new CompositeActionGroup(new ActionGroup[] {
+      fGenerateActionGroup });
+//      rg, 
+//      new LocalHistoryActionGroup(this, ITextEditorActionConstants.GROUP_EDIT)});
+  }
+
+  /** The <code>PHPEditor</code> implementation of this 
+   * <code>AbstractTextEditor</code> method performs any extra 
+   * disposal actions required by the php editor.
+   */
+  public void dispose() {
+    PHPEditorEnvironment.disconnect(this);
+    if (fOutlinePage != null)
+      fOutlinePage.setInput(null);
+      
+    if (fActionGroups != null)
+      fActionGroups.dispose();  
+      
+    super.dispose();
+  }
+
+  /** The <code>PHPEditor</code> implementation of this 
+   * <code>AbstractTextEditor</code> method performs any extra 
+   * revert behavior required by the php editor.
+   */
+  public void doRevertToSaved() {
+    super.doRevertToSaved();
+    if (fOutlinePage != null)
+      fOutlinePage.update();
+  }
+
+  /** The <code>PHPEditor</code> implementation of this 
+   * <code>AbstractTextEditor</code> method performs any extra 
+   * save behavior required by the php editor.
+   */
+  public void doSave(IProgressMonitor monitor) {
+    super.doSave(monitor);
+    // compile or not, according to the user preferences
+    IPreferenceStore store = phpPrefStore;
+    if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
+      IAction a = PHPParserAction.getInstance();
+      if (a != null)
+        a.run();
+    }
+    if (fOutlinePage != null)
+      fOutlinePage.update();
+  }
+
+  /** The <code>PHPEditor</code> implementation of this 
+   * <code>AbstractTextEditor</code> method performs any extra 
+   * save as behavior required by the php editor.
+   */
+  public void doSaveAs() {
+    super.doSaveAs();
+    if (fOutlinePage != null)
+      fOutlinePage.update();
+  }
+
+  /** The <code>PHPEditor</code> implementation of this 
+   * <code>AbstractTextEditor</code> method performs sets the 
+   * input of the outline page after AbstractTextEditor has set input.
+   */
+  public void doSetInput(IEditorInput input) throws CoreException {
+    super.doSetInput(input);
+    if (fOutlinePage != null)
+      fOutlinePage.setInput(input);
+  }
+
+  /** The <code>PHPEditor</code> implementation of this 
+   * <code>AbstractTextEditor</code> method adds any 
+   * PHPEditor specific entries.
+   */
+  public void editorContextMenuAboutToShow(MenuManager menu) {
+    super.editorContextMenuAboutToShow(menu);
+    //  addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
+    //  addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
+    
+    // fActionGroups.fillContextMenu(menu);
+    
+    ActionContext context= new ActionContext(getSelectionProvider().getSelection());
+    fContextMenuGroup.setContext(context);
+    fContextMenuGroup.fillContextMenu(menu);
+    fContextMenuGroup.setContext(null);
+  }
+
+  protected void updateStateDependentActions() {
+    super.updateStateDependentActions();
+    fGenerateActionGroup.editorStateChanged();
+  }
+  
+  /** The <code>PHPEditor</code> implementation of this 
+   * <code>AbstractTextEditor</code> method performs gets
+   * the java content outline page if request is for a an 
+   * outline page.
+   */
+  public Object getAdapter(Class required) {
+    if (IContentOutlinePage.class.equals(required)) {
+      if (fOutlinePage == null) {
+        fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
+        if (getEditorInput() != null)
+          fOutlinePage.setInput(getEditorInput());
+      }
+      return fOutlinePage;
+    }
+    return super.getAdapter(required);
+  }
+
+  //  public void openContextHelp() {
+  //    IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
+  //    ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
+  //    int pos = selection.getOffset();
+  //    String word = getFunctionName(doc, pos);
+  //    openContextHelp(word);
+  //  }
+  //
+  //  private void openContextHelp(String word) {
+  //    open(word);
+  //  }
+  //
+  //  public static void open(String word) {
+  //    IHelp help = WorkbenchHelp.getHelpSupport();
+  //    if (help != null) {
+  //      IHelpResource helpResource = new PHPFunctionHelpResource(word);
+  //      WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
+  //    } else {
+  //      //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
+  //    }
+  //  }
+
+  private String getFunctionName(IDocument doc, int pos) {
+    Point word = PHPWordExtractor.findWord(doc, pos);
+    if (word != null) {
+      try {
+        return doc.get(word.x, word.y).replace('_', '-');
+      } catch (BadLocationException e) {
+      }
+    }
+    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;
+      try {
+        c.removeDecorator(1);
+      } catch (Throwable e) {
+      }
+    }
+  }
+
+  /**
+  * 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());
+    setRangeIndicator(new DefaultRangeIndicator());
+    setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
+    setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
+    // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider());
+    phpPrefStore = PHPeclipsePlugin.getDefault().getPreferenceStore();
+
+    phpPrefStore.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;
+        }
+      }
+    });
+  }
 }