package com.redhat.ceylon.eclipse.code.refactor;

import static com.redhat.ceylon.eclipse.code.editor.DynamicMenuItem.collapseMenuItems;
import static com.redhat.ceylon.eclipse.code.editor.EditorUtil.getCurrentEditor;
import static com.redhat.ceylon.eclipse.ui.CeylonPlugin.PLUGIN_ID;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.actions.CompoundContributionItem;

import com.redhat.ceylon.eclipse.code.editor.CeylonEditor;
import com.redhat.ceylon.eclipse.code.editor.DynamicMenuItem;

public class RefactorMenuItems extends CompoundContributionItem {
    
    public RefactorMenuItems() {}
    
    public RefactorMenuItems(String id) {
        super(id);
    }
    
    @Override
    public IContributionItem[] getContributionItems() {
        IContributionItem[] items = getItems(getCurrentEditor());
        if (collapseMenuItems(getParent())) {
            MenuManager submenu = new MenuManager("Refactor");
            submenu.setActionDefinitionId(CeylonEditor.REFACTOR_MENU_ID);
            for (IContributionItem item: items) {
                submenu.add(item);
            }
            return new IContributionItem[] { submenu };
        }
        else {
            return items;
        }
    }

    private IContributionItem[] getItems(IEditorPart editor) {
        return new IContributionItem[] {
                //new Separator(),
                new DynamicMenuItem(PLUGIN_ID + ".action.rename", "Re&name...",
                        editor!=null && new RenameRefactoringAction(editor).isEnabled(), 
                        AbstractRefactoring.RENAME),
                new Separator(),
                new DynamicMenuItem(PLUGIN_ID + ".action.moveDeclarationToNewUnit", 
                        "Move to &New Unit...", new MoveToNewUnitHandler().isEnabled(), 
                        AbstractRefactoring.MOVE),
                new DynamicMenuItem(PLUGIN_ID + ".action.moveDeclarationToUnit", 
                        "Move to &Unit...", new MoveToUnitHandler().isEnabled(), 
                        AbstractRefactoring.MOVE),
                new Separator(),
                new DynamicMenuItem(PLUGIN_ID + ".action.inline", "Inlin&e...",
                        editor!=null && new InlineRefactoringAction(editor).isEnabled(),
                        AbstractRefactoring.COMP_CHANGE),
                new DynamicMenuItem(PLUGIN_ID + ".action.changeParameters", "Change &Parameters...",
                        editor!=null && new ChangeParametersRefactoringAction(editor).isEnabled(),
                        AbstractRefactoring.REORDER),
                new DynamicMenuItem(PLUGIN_ID + ".action.extractValue", 
                        "Extract &Value...",
                        editor!=null && new ExtractValueRefactoringAction(editor).isEnabled(),
                        AbstractRefactoring.CHANGE),
                new DynamicMenuItem(PLUGIN_ID + ".action.extractFunction", 
                        "Extract &Function...",
                        editor!=null && new ExtractFunctionRefactoringAction(editor).isEnabled(),
                        AbstractRefactoring.CHANGE),
                new Separator(),
                new DynamicMenuItem(PLUGIN_ID + ".action.moveOut", 
                        "Move &Out...",
                        editor!=null && new MoveOutRefactoringAction(editor).isEnabled(),
                        AbstractRefactoring.CHANGE),
                new DynamicMenuItem(PLUGIN_ID + ".action.makeReceiver", 
                        "Make Receiver...",
                        editor!=null && new MakeReceiverRefactoringAction(editor).isEnabled(),
                        AbstractRefactoring.CHANGE),
            };
    }
    
}

@@ -1767,42 +1767,8 @@
 ),%0D%0A
-                new Separator(),%0D%0A
     
@@ -1824,328 +1824,137 @@
 ion.
-moveDeclarationToNewUnit%22, %0D%0A                        %22Move to &New Unit...%22, new MoveToNewUnitHandler().isEnabled(), %0D%0A                        AbstractRefactoring.MOVE),%0D%0A                new DynamicMenuItem(PLUGIN_ID + %22.action.moveDeclarationToUnit%22, %0D%0A                        %22Move to &Unit...%22, new MoveToUnitHandler(
+changeParameters%22, %22Change &Parameters...%22,%0D%0A                        editor!=null && new ChangeParametersRefactoringAction(editor
 ).is
@@ -1955,33 +1955,32 @@
 or).isEnabled(),
- 
 %0D%0A              
@@ -2013,12 +2013,15 @@
 ing.
-MOVE
+REORDER
 ),%0D%0A
@@ -2288,265 +2288,8 @@
 ),%0D%0A
-                new DynamicMenuItem(PLUGIN_ID + %22.action.changeParameters%22, %22Change &Parameters...%22,%0D%0A                        editor!=null && new ChangeParametersRefactoringAction(editor).isEnabled(),%0D%0A                        AbstractRefactoring.REORDER),%0D%0A
     
@@ -3383,32 +3383,512 @@
 oring.CHANGE),%0D%0A
+                new Separator(),%0D%0A                new DynamicMenuItem(PLUGIN_ID + %22.action.moveDeclarationToNewUnit%22, %0D%0A                        %22Move to &New Unit...%22, new MoveToNewUnitHandler().isEnabled(), %0D%0A                        AbstractRefactoring.MOVE),%0D%0A                new DynamicMenuItem(PLUGIN_ID + %22.action.moveDeclarationToUnit%22, %0D%0A                        %22Move to &Unit...%22, new MoveToUnitHandler().isEnabled(), %0D%0A                        AbstractRefactoring.MOVE),%0D%0A
             %7D;%0D%0A

reorder refactoring menu items