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


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 static com.redhat.ceylon.eclipse.ui.CeylonResources.CEYLON_DECS;
import static com.redhat.ceylon.eclipse.ui.CeylonResources.CEYLON_REFS;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
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;
import com.redhat.ceylon.eclipse.ui.CeylonPlugin;

public class FindMenuItems extends CompoundContributionItem {
    
    private static ImageRegistry imageRegistry = CeylonPlugin.getInstance()
            .getImageRegistry();
    
    private static ImageDescriptor REFS = imageRegistry.getDescriptor(CEYLON_REFS);
    private static ImageDescriptor DECS = imageRegistry.getDescriptor(CEYLON_DECS);

    public FindMenuItems() {}
    
    public FindMenuItems(String id) {
        super(id);
    }
    
    @Override
    public IContributionItem[] getContributionItems() {
        IContributionItem[] items = getItems(getCurrentEditor());
        if (collapseMenuItems(getParent())) {
            MenuManager submenu = new MenuManager("Find");
            submenu.setActionDefinitionId(CeylonEditor.FIND_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 DynamicMenuItem(PLUGIN_ID + ".action.findReferences", 
                        "Find &References",
                        editor==null ? false : new FindReferencesAction(editor).isEnabled(), 
                                REFS),
                new DynamicMenuItem(PLUGIN_ID + ".action.findAssignments", 
                        "Find Assignments",
                        editor==null ? false : new FindAssignmentsAction(editor).isEnabled(), 
                                REFS),
                new DynamicMenuItem(PLUGIN_ID + ".action.findRefinements", 
                        "Find Refinements",
                        editor==null ? false : new FindRefinementsAction(editor).isEnabled(), 
                                DECS),
                new DynamicMenuItem(PLUGIN_ID + ".action.findSubtypes", 
                        "Find Subtypes",
                        editor==null ? false : new FindSubtypesAction(editor).isEnabled(), 
                                DECS)
        };
    }

}

@@ -211,24 +211,102 @@
 entEditor;%0D%0A
+import static com.redhat.ceylon.eclipse.code.editor.EditorUtil.getSelection;%0D%0A
 import stati
@@ -361,16 +361,16 @@
 IN_ID;%0D%0A
-
 import s
@@ -603,24 +603,68 @@
 nuManager;%0D%0A
+import org.eclipse.jface.action.Separator;%0D%0A
 import org.e
@@ -789,16 +789,16 @@
 rPart;%0D%0A
-
 import o
@@ -845,16 +845,63 @@
 onItem;%0D
+%0Aimport org.eclipse.ui.texteditor.ITextEditor;%0D
 %0A%0D%0Aimpor
@@ -3212,16 +3212,299 @@
    DECS)
+,%0D%0A                new Separator(),%0D%0A                new DynamicMenuItem(%22org.eclipse.search.ui.performTextSearchWorkspace%22,%0D%0A                        %22Find Text in Workspace%22, %0D%0A                        editor instanceof ITextEditor && getSelection((ITextEditor)editor).getLength()%3E0)
 %0D%0A      

Add a useful hidden eclipse feature to the Find menu