外部スクリプトの実行
これらの基本エレメントだけで、既にEclipseアプリケーション内でカスタムスクリプトを実行する手段を実現できます。たとえば、ユーザーがファイルシステムからスクリプトを選択し、プラットフォーム内で実行するためのEclipseアクションを提供できます。図2と図3は、これを実装した場合の画面例です。


「Run Script」アクションは、ファイルセレクタを表示します。このファイルセレクタは、com.devx.scripting.ScriptSupportクラスに対してクエリを実行することで、使用可能なスクリプト言語のみをフィルタを使って抽出します。クエリを受け取ったcom.devx.scripting.ScriptSupportクラスは、javax.script.ScriptEngineManagerに対してサポート言語を要求します。最後に、javax.script.ScriptEngineManagerが、Service Providerメカニズムを使ってプラグインクラスパスとそのフラグメントをスキャンします。
図のような結果を得るためには、org.eclipse.ui.actionSets拡張ポイントに対して拡張を定義する必要があります。この拡張ポイントは、リスト1のcom.devx.scripting.actions.RunScriptActionクラスによって実装されている、アプリケーションに対する追加メニューアクションを提供します。
必要な作業はこれだけです。これで、アプリケーション内でRuby、Groovyなどのスクリプトを実行して、それぞれのスクリプトの性能や特性を利用することができます。たとえば、ワークスペースの一部で一括変更を行うスクリプトを作成したり、ビルドおよび配備プロセスの一部をスクリプト言語で書いて、必要に応じて開発者がプラットフォームから呼び出せるようにしたりできます。
public class RunScriptAction implements IWorkbenchWindowActionDelegate { // unneeded methods public void dispose() {} public void init(IWorkbenchWindow window) {} public void selectionChanged(IAction action, ISelection selection) {} public void run(IAction action) { FileDialog dlg = new FileDialog( PlatformUI.getWorkbench().getActiveWorkbenchWindow(). getShell(), SWT.OPEN); List<String> filterNames = new ArrayList<String>(); List<String> filterExtensions = new ArrayList<String>(); ScriptSupport support = new ScriptSupport(); for (ScriptSupport.Language l : support.getSupportedLanguages()) { filterNames.add( l.getName() + " (*." + l.getExtension().get(0) + ")"); filterExtensions.add("*." +l.getExtension().get(0)); } filterNames.add("All files (*.*)"); filterExtensions.add("*.*"); dlg.setFilterNames( filterNames.toArray(new String[filterNames.size()])); dlg.setFilterExtensions( filterExtensions.toArray( new String[filterExtensions.size()])); String f = dlg.open(); if (f != null) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(f)); support.runScript(br, getExtension(f),null,true ); } catch(IOException ioe) { // exception handling } catch(ScriptException se) { // exception handling } finally { if (br != null) try { br.close(); } catch(Exception ex) {} } } } private String getExtension(String f) { if (f.lastIndexOf('.') != -1) { return f.substring(f.lastIndexOf('.')+1); } else return f; } }
