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


import static com.redhat.ceylon.eclipse.code.editor.DynamicMenuItem.collapseMenuItems;
import static com.redhat.ceylon.eclipse.ui.CeylonResources.LAST_EDIT;
import static com.redhat.ceylon.eclipse.ui.CeylonResources.NEXT_ANN;
import static com.redhat.ceylon.eclipse.ui.CeylonResources.PREV_ANN;
import static com.redhat.ceylon.eclipse.util.EditorUtil.getCurrentEditor;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.actions.CompoundContributionItem;
import org.eclipse.ui.internal.texteditor.TextEditorPlugin;

import com.redhat.ceylon.eclipse.code.editor.CeylonEditor;
import com.redhat.ceylon.eclipse.code.editor.DynamicMenuItem;
import com.redhat.ceylon.eclipse.code.open.SelectedDeclarationMenuItems;
import com.redhat.ceylon.eclipse.ui.CeylonPlugin;

public class NavigateMenuItems extends CompoundContributionItem {
    
    private static final ImageRegistry imageRegistry = CeylonPlugin.getInstance()
            .getImageRegistry();
    
    private static final ImageDescriptor EDIT = imageRegistry.getDescriptor(LAST_EDIT);
    private static final ImageDescriptor NEXT = imageRegistry.getDescriptor(NEXT_ANN);
    private static final ImageDescriptor PREV = imageRegistry.getDescriptor(PREV_ANN);
    
    public NavigateMenuItems() {}
    
    public NavigateMenuItems(String id) {
        super(id);
    }
    
    @Override
    public IContributionItem[] getContributionItems() {
        IContributionItem[] items = getItems(getCurrentEditor());
        if (collapseMenuItems(getParent())) {
            MenuManager submenu = new MenuManager("Navigate");
            submenu.setActionDefinitionId(CeylonEditor.NAVIGATE_MENU_ID);
            for (IContributionItem item: items) {
                submenu.add(item);
            }
            return new IContributionItem[] { submenu };
        }
        else {
            return items;
        }
    }

    private IContributionItem[] getItems(IEditorPart editor) {
        boolean lastEditEnabled = TextEditorPlugin.getDefault().getLastEditPosition() != null;
        IContributionItem[] items = new SelectedDeclarationMenuItems().getContributionItems();
        return new IContributionItem[] {
                items[0], items[1], items[2], items[3], items[4], items[5], items[6],
                new Separator(),
                new DynamicMenuItem("org.eclipse.ui.navigate.next", 
                        "Ne&xt Annotation", true, NEXT),
                new DynamicMenuItem("org.eclipse.ui.navigate.previous", 
                        "Pre&vious Annotation", true, PREV),
                new Separator(),
                new DynamicMenuItem("org.eclipse.ui.edit.text.gotoLastEditPosition", 
                        "Last Edit Lo&cation", lastEditEnabled, EDIT),
                new DynamicMenuItem("org.eclipse.ui.edit.text.goto.line", 
                        "&Go to Line...", true),
                new DynamicMenuItem(CeylonPlugin.PLUGIN_ID + ".editor.gotoMatchingFence", 
                        "Go to &Matching Bracket", true)
        };
    }

}

@@ -419,16 +419,192 @@
 Editor;%0D
+%0Aimport static org.eclipse.ui.ISharedImages.IMG_TOOL_BACK;%0D%0Aimport static org.eclipse.ui.ISharedImages.IMG_TOOL_FORWARD;%0D%0Aimport static org.eclipse.ui.PlatformUI.getWorkbench;%0D
 %0A%0D%0Aimpor
@@ -878,16 +878,105 @@
 rPart;%0D%0A
+import org.eclipse.ui.ISharedImages;%0D%0Aimport org.eclipse.ui.IWorkbenchCommandConstants;%0D%0A
 import o
@@ -1084,16 +1084,82 @@
 Plugin;%0D
+%0Aimport org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;%0D
 %0A%0D%0Aimpor
@@ -1531,16 +1531,30 @@
 istry = 
+%0D%0A            
 CeylonPl
@@ -1571,30 +1571,16 @@
 stance()
-%0D%0A            
 .getImag
@@ -2797,24 +2797,90 @@
 onItems();%0D%0A
+        ISharedImages images = getWorkbench().getSharedImages();%0D%0A
         retu
@@ -3070,38 +3070,48 @@
 tem(
-%22org.eclipse.ui.navigate.next%22
+IWorkbenchCommandConstants.NAVIGATE_NEXT
 , %0D%0A
@@ -3208,42 +3208,52 @@
 tem(
-%22org.eclipse.ui.navigate.previous%22
+IWorkbenchCommandConstants.NAVIGATE_PREVIOUS
 , %0D%0A
@@ -3388,55 +3388,451 @@
 tem(
-%22org.eclipse.ui.edit.text.gotoLastEditPosition%22
+IWorkbenchCommandConstants.NAVIGATE_BACKWARD_HISTORY, %0D%0A                        %22&Back%22, true, images.getImageDescriptor(IMG_TOOL_BACK)),%0D%0A                new DynamicMenuItem(IWorkbenchCommandConstants.NAVIGATE_FORWARD_HISTORY, %0D%0A                        %22&Forward%22, true, images.getImageDescriptor(IMG_TOOL_FORWARD)),%0D%0A                new Separator(),%0D%0A                new DynamicMenuItem(ITextEditorActionDefinitionIds.GOTO_LAST_EDIT_POSITION
 , %0D%0A
@@ -3943,44 +3943,48 @@
 tem(
-%22org.eclipse.ui.edit.text.goto.line%22
+ITextEditorActionDefinitionIds.LINE_GOTO
 , %0D%0A

improve navigate quick menu