001 package org.meta_environment.eclipse.terms; 002 003 import java.util.List; 004 005 import org.eclipse.core.runtime.IPath; 006 import org.eclipse.imp.editor.UniversalEditor; 007 import org.eclipse.imp.model.ISourceProject; 008 import org.eclipse.imp.parser.IParseController; 009 import org.eclipse.imp.services.base.DefaultLanguageActionsContributor; 010 import org.eclipse.jface.action.Action; 011 import org.eclipse.jface.action.ControlContribution; 012 import org.eclipse.jface.action.IMenuManager; 013 import org.eclipse.jface.action.IStatusLineManager; 014 import org.eclipse.swt.SWT; 015 import org.eclipse.swt.events.SelectionEvent; 016 import org.eclipse.swt.events.SelectionListener; 017 import org.eclipse.swt.widgets.Combo; 018 import org.eclipse.swt.widgets.Composite; 019 import org.eclipse.swt.widgets.Control; 020 021 public class EditorActionContributor extends DefaultLanguageActionsContributor { 022 public static final String TERM_LANGUAGE_SELECTOR = "term-language-selector"; 023 024 public void contributeToEditorMenu(UniversalEditor editor, 025 IMenuManager menuManager) { 026 String file = getFilename(editor); 027 String language = TermEditorTools.getInstance().getLanguage(file); 028 List<Action> actions = TermEditorTools.getInstance().getDynamicActions(language, file); 029 030 for (Action action : actions) { 031 menuManager.add(action); 032 } 033 } 034 035 public void contributeToStatusLine(final UniversalEditor editor, IStatusLineManager statusLineManager) { 036 contributeLanguageSelector(statusLineManager, editor); 037 } 038 039 private void contributeLanguageSelector(IStatusLineManager statusLineManager, final UniversalEditor ed) { 040 statusLineManager.add(new ControlContribution(TERM_LANGUAGE_SELECTOR) { 041 protected Control createControl(Composite parent) { 042 List<String> languages = TermEditorTools.getInstance().getLanguages(); 043 final Combo combo = new Combo(parent, SWT.NONE); 044 045 combo.setToolTipText("Select an SDF module to parse this term with"); 046 for (String language : languages) { 047 combo.add(language); 048 } 049 050 final String filename = getFilename(ed); 051 final String current = TermEditorTools.getInstance().getLanguage(filename); 052 if (languages.contains(current)) { 053 combo.select(combo.indexOf(current)); 054 } 055 056 combo.addSelectionListener(new SelectionListener() { 057 058 public void widgetDefaultSelected(SelectionEvent e) { 059 widgetSelected(e); 060 } 061 062 public void widgetSelected(SelectionEvent e) { 063 if (combo.getText() != null && combo.getText().length() != 0) { 064 TermEditorTools.getInstance().setLanguage(filename, combo.getText()); 065 } 066 } 067 068 069 }); 070 return combo; 071 } 072 }); 073 } 074 075 private String getFilename(final UniversalEditor ed) { 076 IParseController controller = ed.getParseController(); 077 ISourceProject project = controller.getProject(); 078 IPath path = controller.getPath(); 079 String filename; 080 081 if (project != null) { 082 filename = project.getRawProject().getLocation().append(path).makeAbsolute().toOSString(); 083 } 084 else { 085 filename = ed.getParseController().getPath().toOSString(); 086 } 087 return filename; 088 } 089 }