StartExplorer Eclipse Plug-inでよくターミナルを開いているのですが、指定していたフォルダの位置ではなくホームディレクトリで開くようになるバグが起きるようになりました。
良い機会なのでEclipseのプラグイン開発の勉強がてら、Macで「ターミナルを開く」プラグインを作ってみました。
1.Plug-in Projectを作成する
- 「File>New>Project…>Plug-in Project」を選択
- プロジェクト名を適当に「jp.kn.runTerminal」と付けて、
- テンプレートは「Plug-in with a popup menu」を選択。
- 他はデフォルトでOK。
2.ファイルではなくフォルダを右クリックした時に実行されるように設定変更
プラグインの設定ファイル(plugin.xml)を開いて
- objectContribution objectClass=”org.eclipse.core.resources.IFile”をIFolderに書き換え
- ついでに、分かりやすいようにmenu label=”拡張機能”とaction label=”ターミナルを開く”に書き換えておく
3.呼び出されるプログラム(NewAction.java)を編集する
ポップアップの代わりに、「open -a /Applications/Utilities/Terminal.app {開きたいフォルダのパス}」コマンドを発行します。 開きたいフォルダのパスはselectionChanged()メソッド内で取得しておきます。
package jp.kn.runterminal.popup.actions;
import java.io.IOException;
import java.util.List;
import org.eclipse.core.internal.resources.Folder;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
public class NewAction implements IObjectActionDelegate {
private Shell shell;
private String folderPath;
/**
* Constructor for Action1.
*/
public NewAction() {
super();
}
/**
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
shell = targetPart.getSite().getShell();
}
/**
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
String[] cmd = { "open", "-a", "/Applications/Utilities/Terminal.app",folderPath};
try {
Process process = Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
//MessageDialog.openInformation(
// shell,
// "RunTerminal",
// "New Action was executed.");
}
/**
* @see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
List<Folder> selectedItem = ((IStructuredSelection) selection).toList();
Folder folder = selectedItem.get(0);
folderPath = folder.getRawLocation().toOSString();
//System.out.println(folderPath);
}
}
4.動作確認
- 自動作成されたファイルMANIFEST.MFを選択
- 「Launch an Eclipse application in Debug mode」を選択
- 新しく立ち上がったEclipseのナビゲータビューで、「フォルダを右クリック>拡張機能>ターミナルを開く」を実行する
参考
@IT > Java Agile > 作って覚えるEclipseプラグイン(1):いちばん簡単なEclipseプラグイン
Eclipse/プラグイン開発のTIPS集/org.eclipse.ui.IObjectActionDelegate(ポップアップメニュー)
stack overflow>Get the absolute path of the currently edited file in Eclipse
バグは、ターミナルで「open -a /Applications/Utilities/Terminal.app {開きたいパス}」を実行するか、StartExplorerをGithubからcloneしてきて「Launch an Eclipse application in Debug mode」でデバッグすれば特定できそうです


