S-JIS[2013-01-04] 変更履歴

Eclipseプラグイン Activatorクラス

Eclipseプラグイン開発のActivatorクラスについて。


Activatorクラスとは

Activatorは、プラグインのライフサイクルを管理する為のクラス。

プラグインを構成する各クラスからグローバルな情報にアクセスする為にActivatorクラスを使用する。


Activatorクラスの内容

Eclipseプラグインのプロジェクトを作る際にActivatorクラス(のパッケージ・クラス名)を指定することにより、Activatorクラスを自動生成することが出来る。
Eclipse3.7で自動生成されたActivatorクラスの内容は以下の通り。

package com.example.eclipse.plugin.hello;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.example.eclipse.plugin.hello"; //$NON-NLS-1$

	// The shared instance
	private static Activator plugin;
	/**
	 * The constructor
	 */
	public Activator() {
	}
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}
	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}
	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}
}

start()stop()には@Overrideアノテーションは付いていないが、親クラスのメソッドをオーバーライドしている。
start()にて、Activatorインスタンス(自分自身)をstaticなフィールドに保持している。
そして、このフィールドの値をgetDefault()で返すようになっている。

したがって、他のクラスからは「Activator.getDefault()」によってActivatorインスタンスを取得できる。


Activatorのメソッド・フィールド

Activator(および、その親クラス)には以下のようなメソッド/フィールドがある。

メソッド 戻り型 説明
static PLUGIN_ID String プラグインのID。自動生成されたActivatorクラスに定義されている。
static getDefault() Activator Activatorインスタンスを返す。自動生成されたActivatorクラスに定義されている。
getBundle() Bundle  
getDialogSettings() IDialogSettings  
getImageRegistry() ImageRegistry 画像を管理(キャッシュ・破棄)するクラス。
getLog() ILog ログ出力を行う為のオブジェクトを返す。
getPreferenceStore() IPreferenceStore Preference(設定)の値を保持するオブジェクトを返す。
getStateLocation() IPath プラグインが使える範囲のローカルファイルシステム(.metadata)のパスを返す。
getWorkbench() IWorkbench  

Eclipseプラグインへ戻る / Eclipseへ戻る / 技術メモへ戻る
メールの送信先:ひしだま