improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / actions / GenerateActionGroup.java
index 8f68e78..58bf8da 100644 (file)
@@ -14,11 +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;
@@ -36,6 +36,7 @@ import org.eclipse.ui.actions.AddBookmarkAction;
 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
@@ -48,8 +49,15 @@ import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
  * @since 2.0
  */
 public class GenerateActionGroup extends ActionGroup {
+  /**
+        * 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 boolean fEditorIsOwner;
+  private PHPEditor fEditor;
+//     private boolean fEditorIsOwner;
        private IWorkbenchSite fSite;
        private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
        private List fRegisteredSelectionListeners;
@@ -76,8 +84,9 @@ public class GenerateActionGroup extends ActionGroup {
         */
        public GenerateActionGroup(PHPEditor editor, String groupName) {
                fSite= editor.getSite();
-               fEditorIsOwner= true;
+               fEditor= editor;
                fGroupName= groupName;
+                               
                ISelectionProvider provider= fSite.getSelectionProvider();
                ISelection selection= provider.getSelection();
 
@@ -212,15 +221,7 @@ 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();
@@ -239,41 +240,80 @@ public class GenerateActionGroup extends ActionGroup {
        /* (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
         */
@@ -285,6 +325,7 @@ public class GenerateActionGroup extends ActionGroup {
                                provider.removeSelectionChangedListener(listener);
                        }
                }
+    fEditor= null;
                super.dispose();
        }
        
@@ -298,10 +339,10 @@ 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);
@@ -323,4 +364,23 @@ public class GenerateActionGroup extends ActionGroup {
                }
                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;
+  }    
 }