<更新記録>
2008年 1月 22日
執筆

姉妹サイト検索 Web検索


Tray

Trayクラス

org.eclipse.swt.widgets.Widgetパッケージの中に、コンストラクタがない、けれども抽象クラスでもない、変なクラスがあります。 Trayクラスです。

Trayクラスは何をするかというと、 Windows(等のOS)で右下に表示される小さなアイコンの集まりを操作します。 アイコンの1つ1つはTrayItemクラスが表現します。

インスタンスは、Display.getSystemTray()によって取得します。 この時返されるインスタンスは常に一定(シングルトン)で、トレイ機能のないOSではnullを返します。 Tray systemTray = display.getSystemTray();

サンプル

サンプルを示します。 このサンプルは、SystemTraySample.classファイルと同じディレクトリに、 [ アイコンファイル ( zip ) ] を置いておく必要があります。

SystemTraySample.java
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

public class SystemTraySample extends ApplicationWindow {
	
	private final String ICON_ARROW = "arrow.ico";
	// イメージを管理。ウィンドウを閉じる時にdisposeされる。
	private ImageRegistry imageRegistry;
	
	private static Tray tray;

	private TrayItem trayItem;
	
	public static void main(String[] args) {
		
		Display display = new Display();
		
		if ((tray = display.getSystemTray()) == null) {
			MessageDialog.openError(
					new Shell(display), "エラー", 
					"このOS環境にはトレイの機能がない!");
			return;
		}
		
		Window w = new SystemTraySample();
		w.setBlockOnOpen(true);
		w.open();
		display.dispose();
	}
	
	private SystemTraySample() {
		super(null);
	}
	
	protected Control createContents(Composite parent) {
		initImageRegistry();
		
		createTrayItem();
		
		customizeShell();
		
		return parent;
	}
	
	/*
	 * imageRegistryを初期化する。
	 * システムトレイに表示する「矢」の画像を登録
	 */
	private void initImageRegistry() {
		imageRegistry = new ImageRegistry();
		ImageDescriptor arrow =
			ImageDescriptor.createFromFile(getClass(), ICON_ARROW);
		imageRegistry.put(ICON_ARROW, arrow);
	}
	
	/*
	 * システムトレイに表示するアイテムを作成。
	 */
	private void createTrayItem() {

		
		trayItem = new TrayItem(tray, SWT.NONE);
		trayItem.setImage(imageRegistry.get(ICON_ARROW));
		trayItem.setVisible(false);
		
		trayItem.addSelectionListener(new SelectionListener() {
			// クリックされたとき発生
			public void widgetSelected(SelectionEvent event) {
				// do nothing.
			}
			// ダブルクリックされたとき発生
			public void widgetDefaultSelected(SelectionEvent event) {
				Shell shell = getShell();
				
				shell.setVisible(true);
				trayItem.setVisible(false);
				
				shell.setMinimized(false);
			}
		});
	}
	
	/*
	 * シェルについて編集。
	 */
	private void customizeShell() {
		Shell shell = getShell();
		shell.setText("SystemTraySample");
		shell.setImage(imageRegistry.get(ICON_ARROW));
		shell.setSize(300, 300);
		
		shell.addShellListener(new ShellAdapter() {
			// 最小化する際、変わりにトレイにアイコン化。
			public void shellIconified(ShellEvent e) {
				trayItem.setVisible(true);
				getShell().setVisible(false);
			}
			
			public void shellClosed(ShellEvent e) {
				// ウィンドウを閉じる時、リソース開放。
				imageRegistry.dispose();
			}
		});
	}
}
このプログラムを実行して表示されたウィンドウを最小化しようとすると、 最小化の代わりにアイコン化されます。 アイコンをダブルクリックすると、再びウィンドウが現れます。


Powered by VeryEasyCMS