001    package org.meta_environment.eclipse.actions;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import org.eclipse.core.runtime.IConfigurationElement;
007    import org.eclipse.core.runtime.IExtensionPoint;
008    import org.eclipse.core.runtime.IPath;
009    import org.eclipse.core.runtime.Platform;
010    import org.eclipse.imp.editor.UniversalEditor;
011    import org.eclipse.imp.language.Language;
012    import org.eclipse.imp.model.ISourceProject;
013    import org.eclipse.imp.parser.IParseController;
014    import org.eclipse.imp.services.base.DefaultLanguageActionsContributor;
015    import org.eclipse.jface.action.Action;
016    import org.eclipse.jface.action.IMenuManager;
017    import org.eclipse.jface.action.MenuManager;
018    
019    public class LanguageActions extends DefaultLanguageActionsContributor {
020    
021            private final String extensionPointId = "org.meta_environment.eclipse.actions.EditorMenus";
022    
023            public void contributeToEditorMenu(UniversalEditor editor, IMenuManager menu) {
024                    // TODO: Bugzilla: Request getter for UniversalEditor.fLanguage
025                    final Language language = editor.fLanguage;
026                    IExtensionPoint extensionPoint = Platform.getExtensionRegistry()
027                                    .getExtensionPoint(extensionPointId);
028    
029                    if (extensionPoint != null) {
030                            List<IConfigurationElement> extensions = getExtensionsForLanguage(
031                                            extensionPoint, language);
032                            if (extensions.size() != 0) {
033                                    for (IConfigurationElement ext : extensions) {
034                                            createActionsForEditorMenu(ext, editor, menu);
035                                    }
036                            }
037                    }
038            }
039    
040            private List<Action> createActionsForEditorMenu(
041                            IConfigurationElement editorMenu, final UniversalEditor editor, IMenuManager menu) {
042                    List<Action> actions = new ArrayList<Action>();
043    
044                    IConfigurationElement[] children = editorMenu.getChildren();
045    
046                    for (IConfigurationElement child : children) {
047                            actions.addAll(createActionsForEditorMenuItem(child, editor, menu));
048                    }
049    
050                    return actions;
051            }
052    
053            private List<Action> createActionsForEditorMenuItem(
054                            IConfigurationElement editorMenuItem, 
055                            final UniversalEditor editor, IMenuManager menu) {
056    
057                    List<Action> actions = new ArrayList<Action>();
058                    String name = editorMenuItem.getAttribute("name");
059    
060                    if (editorMenuItem.getName().equals("EditorSubmenu")) {
061                            IConfigurationElement[] children = editorMenuItem.getChildren();
062                            IMenuManager subMenu = new MenuManager(name);
063                            
064                            for (IConfigurationElement child : children) {
065                                    actions.addAll(createActionsForEditorMenuItem(child, editor, subMenu));
066                            }
067                            
068                            menu.add(subMenu);
069                    }
070    
071                    else if (editorMenuItem.getName().equals("EditorMenuAction")) {
072                            addActionForEditorMenuAction(editorMenuItem, name, editor, menu);
073                    }
074    
075                    return actions;
076            }
077    
078            private void addActionForEditorMenuAction(
079                            IConfigurationElement editorMenuAction, String name,
080                            final UniversalEditor editor, IMenuManager menu) {
081                    final String toolbus_action = editorMenuAction
082                                    .getAttribute("toolbus_action");
083                    final String language = editor.fLanguage.getName();
084    
085                    Action action = new Action(name) {
086                            public void run() {
087                                    LanguageActionsTool.getInstance()
088                                                    .PerformAction(toolbus_action, language, getFileName(editor));
089                            }
090                    };
091                    action.setToolTipText(editorMenuAction.getAttribute("description"));
092    
093                    menu.add(action);
094            }
095    
096            public static String getFileName(UniversalEditor editor) {
097                    IParseController controller = editor.getParseController();
098                    ISourceProject project = controller.getProject();
099                    IPath path = controller.getPath();
100                    String filename;
101    
102                    if (project != null) {
103                            filename = project.getRawProject().getLocation().append(path)
104                                            .makeAbsolute().toOSString();
105                    }
106    
107                    else {
108                            filename = editor.getParseController().getPath().toOSString();
109                    }
110    
111                    return filename;
112            }
113    
114            public static List<IConfigurationElement> getExtensionsForLanguage(
115                            IExtensionPoint extensionPoint, Language language) {
116    
117                    IConfigurationElement[] elements = extensionPoint
118                                    .getConfigurationElements();
119                    List<IConfigurationElement> result = new ArrayList<IConfigurationElement>();
120    
121                    if (elements != null) {
122                            for (int n = 0; n < elements.length; n++) {
123                                    IConfigurationElement element = elements[n];
124                                    final String attrValue = element
125                                                    .getAttribute(Language.LANGUAGE_ID_ATTR);
126    
127                                    if (isSameLanguageString(attrValue, language.getName())) {
128                                            result.add(element);
129                                    }
130                            }
131                    }
132    
133                    return result;
134            }
135    
136            public static boolean isSameLanguageString(String l1, String l2) {
137                    return l1.toLowerCase().equals(l2.toLowerCase());
138            }
139    }