001 package org.meta_environment.eclipse.terms; 002 003 import java.util.HashMap; 004 import java.util.LinkedList; 005 import java.util.List; 006 import java.util.Map; 007 008 import org.eclipse.core.resources.IFile; 009 import org.eclipse.core.resources.ResourcesPlugin; 010 import org.eclipse.core.runtime.Path; 011 import org.eclipse.imp.editor.UniversalEditor; 012 import org.eclipse.jface.action.Action; 013 import org.eclipse.ui.IWorkbench; 014 import org.eclipse.ui.IWorkbenchPage; 015 import org.eclipse.ui.IWorkbenchWindow; 016 import org.eclipse.ui.PartInitException; 017 import org.eclipse.ui.PlatformUI; 018 import org.eclipse.ui.part.FileEditorInput; 019 import org.meta_environment.eclipse.sdf.Activator; 020 021 import toolbus.adapter.eclipse.EclipseTool; 022 import aterm.ATerm; 023 import aterm.ATermAppl; 024 import aterm.ATermList; 025 026 public class TermEditorTools extends EclipseTool { 027 private static TermEditorTools sInstance; 028 029 private Map<String, Map<String, String>> actions = new HashMap<String, Map<String, String>>(); 030 031 private TermEditorTools(){ 032 super("term-language-registrar"); 033 } 034 035 public static TermEditorTools getInstance() { 036 if (sInstance == null) { 037 sInstance = new TermEditorTools(); 038 sInstance.connect(); 039 } 040 return sInstance; 041 } 042 043 public List<String> getLanguages() { 044 ATermAppl response = sendRequest(factory.make("get-languages")); 045 046 ATermList result = (ATermList) response.getArgument(0); 047 List<String> list = new LinkedList<String>(); 048 049 for (; !result.isEmpty(); result = result.getNext()) { 050 list.add(((ATermAppl) result.getFirst()).getName()); 051 } 052 053 return list; 054 } 055 056 public String getLanguage(String filename) { 057 ATermAppl response = sendRequest(factory.make("get-language(<str>)", filename)); 058 ATerm language = response.getArgument(0); 059 060 return ((ATermAppl) language).getName(); 061 } 062 063 public void setLanguage(String filename, String language) { 064 sendEvent(factory.make("set-language(<str>,<str>)", filename, language)); 065 } 066 067 public void open(String filename) { 068 IWorkbench wb = PlatformUI.getWorkbench(); 069 IWorkbenchWindow win = wb.getActiveWorkbenchWindow(); 070 071 if (win != null) { 072 IWorkbenchPage page = win.getActivePage(); 073 074 if (page != null) { 075 IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename)); 076 try { 077 page.openEditor(new FileEditorInput(file), UniversalEditor.EDITOR_ID); 078 } catch (PartInitException e) { 079 Activator.getInstance().logException("Could not open editor for: " + filename, e); 080 } 081 } 082 } 083 } 084 085 public void registerAction(String language, String label, String tooltip, String action) { 086 System.err.println("registering " + language + " " + label + " " + action); 087 Map<String, String> map = getActionMap(language); 088 map.put(label, action); 089 } 090 091 private String canonical(String label) { 092 int i = label.lastIndexOf('/'); 093 if (i != -1) { 094 return label.substring(i+1); 095 } 096 return label; 097 } 098 099 private Map<String, String> getActionMap(String language) { 100 Map<String, String> map = actions.get(canonical(language)); 101 102 if (map == null) { 103 map = new HashMap<String,String>(); 104 actions.put(canonical(language), map); 105 } 106 107 return map; 108 } 109 110 public List<Action> getDynamicActions(final String language, final String filename) { 111 Map<String, String> map = getActionMap(language); 112 List<Action> result = new LinkedList<Action>(); 113 114 for (String label : map.keySet()) { 115 final String action = map.get(label); 116 result.add(new Action(label) { 117 public void run() { 118 System.err.println("dynamic term action triggered: " + action + " for language " + language + " on file " + filename); 119 performAction(action, language, filename); 120 } 121 }); 122 } 123 124 return result; 125 } 126 127 public void unregisterAction(String language, String label) { 128 System.err.println("unregister " + language + " " + label); 129 Map<String, String> map = getActionMap(language); 130 map.remove(label); 131 } 132 133 private void performAction (String Action, String language, String Filename) { 134 this.sendEvent(factory.make("perform-action(<str>,<str>,<str>)", Action, language, Filename)); 135 } 136 }