EclipseMonekyでスクリプトを書く基本知識2

この前の続き。

スクリプトローダ

スクリプト内で、他のスクリプトを参照したい場合は度々ある。でもincludeとかimportなんてものはない。

そこで登場するのが、スクリプトローダ。EclipseMonkeyのプラグインDOMで、スクリプト内から他のスクリプトをロードすることができる。

インストール

DOMをインストール。フィーチャーはここ。公開サイトはここ

使い方

宣言

スクリプトのファイルコメントの部分にDOMを追加して、スクリプトローダーを使用することを宣言する。

/*
 * Menu: Test > ScriptLoaderTest
 * Key: CTRL+ALT+L
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 * DOM: http://muellerware.org/projects/em-sl/update/org.muellerware.eclipsemonkey.dom.scriptloader
 */
呼び出し

DOM宣言をすると、ScriptLoaderというオブジェクトが使えるようになる。これにはメソッドが二つ用意されてる。

メソッド名説明
loadFile(String fileName) ファイル名を指定してスクリプトをロード。絶対パスを指定。
loadProjectFile(String projectName, String fileName) 第一引数はワークスペースに登録しているプロジェクトのプロジェクト名を指定。第二引数はプロジェクトルートからのパスを指定。

公開サイトにてalert.jsとConsole.jsを紹介している。それぞれのソースをコピペしたファイルをどこかに保存する。今回は、EclipseMonkeyScriptsプロジェクト内の/lib/に置くことにする。

alert.jsはalert関数を返すので、どっかに保持して使用する。

var alert = ScriptLoader.loadProjectFile("EclipseMonkeyScripts", "/lib/alert.js");
:
:
alert("確認ダイアログが出るよ!");

Console.jsはConsoleクラスを返すので、オブジェクトを作って使用する。

Console = ScriptLoader.loadProjectFile("EclipseMonkeyScripts", "/lib/Console.js");
var myConsole = new Console("MyConsole");
:
:
myConsole.println("コンソールウィンドウに表示されるよ!");

書き方

ロードされるスクリプトを書くには2つの規則がある。

  • パッケージが必要な場合は、スクリプトヘッダに@requires-bundleを記述する
  • スクリプトの一番最後に記述されたオブジェクトが返される

スクリプトヘッダはファイルの先頭に/*..*/で囲んで記述する。

書いてみる

alert関数をちょっと拡張して、Alertsクラスを作成してみました。

ソース

Alerts.jsなどとしたファイルに以下の内容をコピペしてください。

/*
 * returns a Alerts class to create dialog
 */

/**
 *	@brief	Alerts Class
 *
 *	@param	aTitle	[in]	dialog title
 *	@return	none
 *	@author	TeraKen<teraken1982@gmail.com>
 *	@since	2008/04/14
 */
function Alerts(aTitle)
{
  this.title = aTitle;
  this.msgDialog = Packages.org.eclipse.jface.dialogs.MessageDialog;
  this.shell = window.getShell();
}


/**
 *	@brief	Alerts method to create confirm dialog
 *
 *	@param	aMessage	[in]	dialog message
 *	@retval	true	OK was selected by confirm dialog
 *	@retval	false	Cancel was selected by confirm dialog
 *	@author	TeraKen<teraken1982@gmail.com>
 *	@since	2008/04/14
 */
Alerts.prototype.confirm = function(aMessage)
{
  this.msgDialog.openConfirm(this.shell, this.title, aMessage);
}

/**
 *	@brief	Alerts method to create erroe dialog
 *
 *	@param	aMessage	[in]	dialog message
 *	@return	none
 *	@author	TeraKen<teraken1982@gmail.com>
 *	@since	2008/04/14
 */
Alerts.prototype.error = function(aMessage)
{
  this.msgDialog.openError(this.shell, this.title, aMessage);
}

/**
 *	@brief	Alerts method to create infomation dialog
 *
 *	@param	aMessage	[in]	dialog message
 *	@return	none
 *	@author	TeraKen<teraken1982@gmail.com>
 *	@since	2008/04/14
 */
Alerts.prototype.info = function(aMessage)
{
  this.msgDialog.openInformation(this.shell, this.title, aMessage);
}

/**
 *	@brief	Alerts method to create qsuestion dialog
 *
 *	@param	name	[in]	dialog title
 *	@retval	true	Yes was selected by qsuestion dialog
 *	@retval	false	No was selected by qsuestion dialog
 *	@author	TeraKen<teraken1982@gmail.com>
 *	@since	2008/04/14
 */
Alerts.prototype.question = function(aMessage)
{
  return this.msgDialog.openQuestion(this.shell, this.title, aMessage);
}

/**
 *	@brief	Alerts method to create warning dialog
 *
 *	@param	aMessage	[in]	dialog message
 *	@return	none
 *	@author	TeraKen<teraken1982@gmail.com>
 *	@since	2008/04/14
 */
Alerts.prototype.warning = function(aMessage)
{
  this.msgDialog.openWarning(this.shell, this.title, aMessage);
}

Alerts;
使い方

Alertsクラスを返すので、オブジェクトを作って使用する。

Alerts = ScriptLoader.loadProjectFile("EclipseMonkeyScripts", "/lib/Alerts.js");
var myAlerts = new Alerts("MyAlerts");
:
:
myAlerts.confirm("確認ダイアログがでます");
myAlerts.error("エラーダイアログがでます");
myAlerts.info("情報ダイアログがでます");
myAlerts.question("質問ダイアログがでます");
myAlerts.warning("警告ダイアログがでます");

confirmとquestionはダイアログで選択したボタンによって、trueとfalseを返します。

おわり

EclipseMonkeyでスクリプトを書く基本知識は以上です。

Enjoy Eclipse Scripting!