replaced a lot of deprecated code; if someone runs into a commit conflict afterwards...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / actions / GenerateActionGroup.java
index fa14ba5..46d386a 100644 (file)
@@ -1,10 +1,10 @@
 /*******************************************************************************
  * Copyright (c) 2002 International Business Machines Corp. and others.
- * All rights reserved. This program and the accompanying materials 
- * are made available under the terms of the Common Public License v0.5 
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v0.5
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/cpl-v05.html
- * 
+ *
  * Contributors:
  *     IBM Corporation - initial API and implementation
  ******************************************************************************/
@@ -14,10 +14,11 @@ import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
+import net.sourceforge.phpdt.internal.ui.actions.ActionMessages;
 import net.sourceforge.phpdt.internal.ui.actions.AddTaskAction;
 import net.sourceforge.phpdt.ui.IContextMenuConstants;
 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
-import org.eclipse.debug.internal.ui.actions.ActionMessages;
+
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.MenuManager;
@@ -28,31 +29,39 @@ import org.eclipse.jface.viewers.ISelectionProvider;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.IViewPart;
-import org.eclipse.ui.IWorkbenchActionConstants;
 import org.eclipse.ui.IWorkbenchSite;
 import org.eclipse.ui.actions.ActionGroup;
 import org.eclipse.ui.actions.AddBookmarkAction;
+import org.eclipse.ui.ide.IDEActionFactory;
 import org.eclipse.ui.part.Page;
 import org.eclipse.ui.texteditor.ConvertLineDelimitersAction;
 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
+import org.eclipse.ui.texteditor.IUpdate;
 
 /**
  * Action group that adds the source and generate actions to a part's context
  * menu and installs handlers for the corresponding global menu actions.
- * 
+ *
  * <p>
  * This class may be instantiated; it is not intended to be subclassed.
  * </p>
- * 
+ *
  * @since 2.0
  */
 public class GenerateActionGroup extends ActionGroup {
-       
-       private boolean fEditorIsOwner;
+  /**
+        * Pop-up menu: id of the source sub menu (value <code>net.sourceforge.phpdt.ui.source.menu</code>).
+        *
+        * @since 3.0
+        */
+       public static final String MENU_ID= "net.sourceforge.pheclipse.ui.source.menu"; //$NON-NLS-1$
+
+  private PHPEditor fEditor;
+//     private boolean fEditorIsOwner;
        private IWorkbenchSite fSite;
        private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
        private List fRegisteredSelectionListeners;
-       
+
 //     private AddImportOnSelectionAction fAddImport;
 //     private OverrideMethodsAction fOverrideMethods;
 //     private AddGetterSetterAction fAddGetterSetter;
@@ -63,20 +72,21 @@ public class GenerateActionGroup extends ActionGroup {
 //     private ExternalizeStringsAction fExternalizeStrings;
 //     private FindStringsToExternalizeAction fFindStringsToExternalize;
 //     private SurroundWithTryCatchAction fSurroundWithTryCatch;
-       
+
        // private OrganizeImportsAction fOrganizeImports;
 
        private ConvertLineDelimitersAction fConvertToWindows;
        private ConvertLineDelimitersAction fConvertToUNIX;
        private ConvertLineDelimitersAction fConvertToMac;
-       
+
        /**
         * Note: This constructor is for internal use only. Clients should not call this constructor.
         */
        public GenerateActionGroup(PHPEditor editor, String groupName) {
                fSite= editor.getSite();
-               fEditorIsOwner= true;
+               fEditor= editor;
                fGroupName= groupName;
+
                ISelectionProvider provider= fSite.getSelectionProvider();
                ISelection selection= provider.getSelection();
 
@@ -84,7 +94,7 @@ public class GenerateActionGroup extends ActionGroup {
 //             fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
 //             fAddImport.update();
 //             editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
-               
+
 //             fOrganizeImports= new OrganizeImportsAction(editor);
 //             fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
 //             fOrganizeImports.editorStateChanged();
@@ -94,49 +104,49 @@ public class GenerateActionGroup extends ActionGroup {
 //             fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
 //             fOverrideMethods.editorStateChanged();
 //             editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
-               
+
 //             fAddGetterSetter= new AddGetterSetterAction(editor);
 //             fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
 //             fAddGetterSetter.editorStateChanged();
 //             editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
-               
+
 //             fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
 //             fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
 //             fAddUnimplementedConstructors.editorStateChanged();
-//             editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$          
-               
+//             editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$
+
 //             fAddJavaDocStub= new AddJavaDocStubAction(editor);
 //             fAddJavaDocStub.editorStateChanged();
-//             
+//
 //             fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
 //             fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
 //             fSurroundWithTryCatch.update(selection);
 //             provider.addSelectionChangedListener(fSurroundWithTryCatch);
-//             editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$          
-//             
+//             editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$
+//
 //             fExternalizeStrings= new ExternalizeStringsAction(editor);
 //             fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
 //             fExternalizeStrings.editorStateChanged();
-//             editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$              
-               
+//             editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$
+
                fConvertToWindows= new ConvertLineDelimitersAction(editor, "\r\n"); //$NON-NLS-1$
                fConvertToWindows.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_WINDOWS);
-               editor.setAction("ConvertLineDelimitersToWindows", fConvertToWindows); //$NON-NLS-1$            
-               
+               editor.setAction("ConvertLineDelimitersToWindows", fConvertToWindows); //$NON-NLS-1$
+
                fConvertToUNIX= new ConvertLineDelimitersAction(editor, "\n"); //$NON-NLS-1$
                fConvertToUNIX.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_UNIX);
-               editor.setAction("ConvertLineDelimitersToUNIX", fConvertToUNIX); //$NON-NLS-1$          
+               editor.setAction("ConvertLineDelimitersToUNIX", fConvertToUNIX); //$NON-NLS-1$
 
                fConvertToMac= new ConvertLineDelimitersAction(editor, "\r"); //$NON-NLS-1$
                fConvertToMac.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_MAC);
-               editor.setAction("ConvertLineDelimitersToMac", fConvertToMac); //$NON-NLS-1$            
+               editor.setAction("ConvertLineDelimitersToMac", fConvertToMac); //$NON-NLS-1$
        }
-       
+
        /**
-        * Creates a new <code>GenerateActionGroup</code>. The group 
-        * requires that the selection provided by the page's selection provider 
+        * Creates a new <code>GenerateActionGroup</code>. The group
+        * requires that the selection provided by the page's selection provider
         * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
-        * 
+        *
         * @param page the page that owns this action group
         */
        public GenerateActionGroup(Page page) {
@@ -144,21 +154,21 @@ public class GenerateActionGroup extends ActionGroup {
        }
 
        /**
-        * Creates a new <code>GenerateActionGroup</code>. The group 
-        * requires that the selection provided by the part's selection provider 
+        * Creates a new <code>GenerateActionGroup</code>. The group
+        * requires that the selection provided by the part's selection provider
         * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
-        * 
+        *
         * @param part the view part that owns this action group
         */
        public GenerateActionGroup(IViewPart part) {
                this(part.getSite());
        }
-       
+
        private GenerateActionGroup(IWorkbenchSite site) {
                fSite= site;
                ISelectionProvider provider= fSite.getSelectionProvider();
                ISelection selection= provider.getSelection();
-               
+
 //             fOverrideMethods= new OverrideMethodsAction(site);
 //             fAddGetterSetter= new AddGetterSetterAction(site);
 //             fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
@@ -168,10 +178,10 @@ public class GenerateActionGroup extends ActionGroup {
 //             fExternalizeStrings= new ExternalizeStringsAction(site);
 //             fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
 //             fOrganizeImports= new OrganizeImportsAction(site);
-//             
+//
 //             fOverrideMethods.update(selection);
 //             fAddGetterSetter.update(selection);
-//             fAddUnimplementedConstructors.update(selection);        
+//             fAddUnimplementedConstructors.update(selection);
 //             fAddJavaDocStub.update(selection);
 //             fExternalizeStrings.update(selection);
 //             fFindStringsToExternalize.update(selection);
@@ -183,7 +193,7 @@ public class GenerateActionGroup extends ActionGroup {
                } else {
                        fAddBookmark.setEnabled(false);
                }
-               
+
 //             registerSelectionListener(provider, fOverrideMethods);
 //             registerSelectionListener(provider, fAddGetterSetter);
 //             registerSelectionListener(provider, fAddUnimplementedConstructors);
@@ -194,16 +204,16 @@ public class GenerateActionGroup extends ActionGroup {
 //             registerSelectionListener(provider, fOrganizeImports);
                registerSelectionListener(provider, fAddTaskAction);
        }
-       
+
        private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) {
                if (fRegisteredSelectionListeners == null)
                        fRegisteredSelectionListeners= new ArrayList(12);
                provider.addSelectionChangedListener(listener);
                fRegisteredSelectionListeners.add(listener);
        }
-       
+
        /*
-        * The state of the editor owning this action group has changed. 
+        * The state of the editor owning this action group has changed.
         * This method does nothing if the group's owner isn't an
         * editor.
         */
@@ -211,16 +221,8 @@ public class GenerateActionGroup extends ActionGroup {
         * Note: This method is for internal use only. Clients should not call this method.
         */
        public void editorStateChanged() {
-               Assert.isTrue(fEditorIsOwner);
-//             fAddImport.update();
-//             fExternalizeStrings.editorStateChanged();
-//             fOrganizeImports.editorStateChanged();
-//             fOverrideMethods.editorStateChanged();
-//             fAddUnimplementedConstructors.editorStateChanged();
-//             fAddJavaDocStub.editorStateChanged();
-//             fSurroundWithTryCatch.editorStateChanged();
-//             fAddGetterSetter.editorStateChanged();
-               
+               Assert.isTrue(isEditorOwner());
+
                // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
                fConvertToMac.update();
                fConvertToUNIX.update();
@@ -234,45 +236,84 @@ public class GenerateActionGroup extends ActionGroup {
                super.fillActionBars(actionBar);
                setGlobalActionHandlers(actionBar);
        }
-       
+
        /* (non-Javadoc)
         * Method declared in ActionGroup
         */
+//     public void fillContextMenu(IMenuManager menu) {
+//             super.fillContextMenu(menu);
+//             if (fEditorIsOwner) {
+//                     IMenuManager subMenu= createEditorSubMenu(menu);
+//                     if (subMenu != null)
+//                             menu.appendToGroup(fGroupName, subMenu);
+//             } else {
+////                   appendToGroup(menu, fOrganizeImports);
+////                   appendToGroup(menu, fOverrideMethods);
+////                   appendToGroup(menu, fAddGetterSetter);
+////                   appendToGroup(menu, fAddUnimplementedConstructors);
+////                   appendToGroup(menu, fAddJavaDocStub);
+//                     appendToGroup(menu, fAddBookmark);
+//             }
+//     }
+       /* (non-Javadoc)
+        * Method declared in ActionGroup
+        */
+//     public void fillContextMenu(IMenuManager menu) {
+//             super.fillContextMenu(menu);
+//             IMenuManager subMenu= null;
+//             if (isEditorOwner()) {
+//                     subMenu= fillEditorSubMenu(menu);
+//             } else {
+//     //              subMenu= createViewSubMenu(menu);
+//             }
+//             if (subMenu != null)
+//                     menu.appendToGroup(fGroupName, subMenu);
+//     }
        public void fillContextMenu(IMenuManager menu) {
                super.fillContextMenu(menu);
-               if (fEditorIsOwner) {
-                       IMenuManager subMenu= createEditorSubMenu(menu);
-                       if (subMenu != null)
-                               menu.appendToGroup(fGroupName, subMenu);
-               } else {
-//                     appendToGroup(menu, fOrganizeImports);
-//                     appendToGroup(menu, fOverrideMethods);
-//                     appendToGroup(menu, fAddGetterSetter);
-//                     appendToGroup(menu, fAddUnimplementedConstructors);
-//                     appendToGroup(menu, fAddJavaDocStub);
-                       appendToGroup(menu, fAddBookmark);
-               }
-       }
-       
-       private IMenuManager createEditorSubMenu(IMenuManager mainMenu) {
-               IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
+               String shortCut= null; //$NON-NLS-1$
+//             if (fQuickAccessAction != null) {
+//                     shortCut= fQuickAccessAction.getShortCutString(); //$NON-NLS-1$
+//             }
+               IMenuManager subMenu= new MenuManager(
+                       ActionMessages.getString("SourceMenu.label") + (shortCut != null ? "\t" + shortCut : ""), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+                       MENU_ID);
                int added= 0;
-//             added+= addAction(result, fOrganizeImports);
-//             added+= addAction(result, fAddImport);
-//             result.add(new Separator());
-//             added+= addAction(result, fOverrideMethods);
-//             added+= addAction(result, fAddGetterSetter);
-//             added+= addAction(result, fAddUnimplementedConstructors);
-//             added+= addAction(result, fAddJavaDocStub);
-               added+= addAction(result, fAddBookmark);
-//             result.add(new Separator());            
-//             added+= addAction(result, fSurroundWithTryCatch);
-//             added+= addAction(result, fExternalizeStrings);
-               if (added == 0)
-                       result= null;
-               return result;
+               if (isEditorOwner()) {
+                       added= fillEditorSubMenu(subMenu);
+               }
+//             else {
+//                     added= fillViewSubMenu(subMenu);
+//             }
+               if (added > 0)
+                       menu.appendToGroup(fGroupName, subMenu);
        }
-
+  private int fillEditorSubMenu(IMenuManager source) {
+//    IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
+    int added= 0;
+    added+= addEditorAction(source, "Comment"); //$NON-NLS-1$
+    added+= addEditorAction(source, "Uncomment"); //$NON-NLS-1$
+    added+= addEditorAction(source, "ToggleComment"); //$NON-NLS-1$
+       added+= addEditorAction(source, "AddBlockComment"); //$NON-NLS-1$
+       added+= addEditorAction(source, "RemoveBlockComment"); //$NON-NLS-1$
+       added+= addEditorAction(source, "Format"); //$NON-NLS-1$
+       added+= addEditorAction(source, "Indent"); //$NON-NLS-1$
+//    result.add(new Separator());
+//    added+= addAction(result, fOrganizeImports);
+//    added+= addAction(result, fAddImport);
+//    result.add(new Separator());
+//    added+= addAction(result, fOverrideMethods);
+//    added+= addAction(result, fAddGetterSetter);
+//    added+= addAction(result, fAddUnimplementedConstructors);
+//    added+= addAction(result, fAddJavaDocStub);
+//    added+= addAction(result, fAddBookmark);
+//    result.add(new Separator());
+//    added+= addAction(result, fSurroundWithTryCatch);
+//    added+= addAction(result, fExternalizeStrings);
+//    if (added == 0)
+//      result= null;
+    return added;
+  }
        /* (non-Javadoc)
         * Method declared in ActionGroup
         */
@@ -284,9 +325,10 @@ public class GenerateActionGroup extends ActionGroup {
                                provider.removeSelectionChangedListener(listener);
                        }
                }
+    fEditor= null;
                super.dispose();
        }
-       
+
        private void setGlobalActionHandlers(IActionBars actionBar) {
 //             actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT, fAddImport);
 //             actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH, fSurroundWithTryCatch);
@@ -297,23 +339,23 @@ public class GenerateActionGroup extends ActionGroup {
 //             actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings);
 //             actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE, fFindStringsToExternalize);
 //             actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS, fOrganizeImports);
-//             actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, fConvertToWindows);
-//             actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, fConvertToUNIX);
-//             actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_MAC, fConvertToMac);
-               if (!fEditorIsOwner) {
+               actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, fConvertToWindows);
+               actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, fConvertToUNIX);
+               actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_MAC, fConvertToMac);
+               if (!isEditorOwner()) {
                        // editor provides its own implementation of these actions.
-                       actionBar.setGlobalActionHandler(IWorkbenchActionConstants.BOOKMARK, fAddBookmark);
-                       actionBar.setGlobalActionHandler(IWorkbenchActionConstants.ADD_TASK, fAddTaskAction);
+                       actionBar.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), fAddBookmark);
+                       actionBar.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), fAddTaskAction);
                }
        }
-       
+
        private int appendToGroup(IMenuManager menu, IAction action) {
                if (action != null && action.isEnabled()) {
                        menu.appendToGroup(fGroupName, action);
                        return 1;
                }
                return 0;
-       }       
+       }
 
        private int addAction(IMenuManager menu, IAction action) {
                if (action != null && action.isEnabled()) {
@@ -321,5 +363,24 @@ public class GenerateActionGroup extends ActionGroup {
                        return 1;
                }
                return 0;
-       }       
+       }
+
+  private int addEditorAction(IMenuManager menu, String actionID) {
+    if (fEditor == null)
+      return 0;
+    IAction action= fEditor.getAction(actionID);
+    if (action == null)
+      return 0;
+    if (action instanceof IUpdate)
+      ((IUpdate)action).update();
+    if (action.isEnabled()) {
+      menu.add(action);
+      return 1;
+    }
+    return 0;
+  }
+
+  private boolean isEditorOwner() {
+    return fEditor != null;
+  }
 }