在RCP中实现系统托盘功能

在RCP中实现系统托盘功能

在RCP中加入系统托盘功能,类似msn和QQ。

功能就不说了。

在RCP的WorkbenchWindowAdvisor的postWindowOpen()中创建系统托盘,另外要override preWindowShellClose()方法(按窗口右上角X关闭),否则按程序右上角的X,整个程序就会被关闭,重写后实现程序最小化的功能。

主要是重载ApplicationWorkbenchWindowAdvisor.java中的方法

/**
 * ApplicationWorkbenchWindowAdvisor.java
 */
package com.mi.pwdgate;

import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import com.mi.pwdgate.util.SystemTrayMaster;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

  private SystemTrayMaster trayMaster;
  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowOpen()
   */
  public void postWindowOpen() {
    createSystemTray();
  }

  private void createSystemTray() {
    trayMaster = new SystemTrayMaster();
    trayMaster.createSystemTray();
  }

        /*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#preWindowShellClose()
	 * 按窗口右上角X关闭


	 */
	public boolean preWindowShellClose() {
		System.out.println("shell going to closed!");
		trayMaster.minimizeWindow();
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowClose()
	 * 所有关闭窗口的事件都从这里走,收尾工作的代码在这里写


	 */
	public void postWindowClose() {
        System.out.println("closed!");
    }

}

/**
 * SystemTrayMaster.java
 */
package com.mi.pwdgate.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.PlatformUI;

/**
 * @author moneyice 2006-5-9
 */
public class SystemTrayMaster implements SelectionListener, Listener {
  private final class RestoreWindowListener extends SelectionAdapter {
    public void widgetSelected(SelectionEvent e) {
      restoreWindow();
    }
  }

  private Menu menu;

  private MenuItem[] menuItems = new MenuItem[0];

  private RestoreWindowListener restoreWindowListener;

  public SystemTrayMaster() {
    this.restoreWindowListener = new RestoreWindowListener();
  }

  // Closes the Application
  protected void closeApplication() {
    PlatformUI.getWorkbench().close();
  }

  // click the tray
  public void widgetSelected(SelectionEvent e) {
  }

  // double click the tray
  public void widgetDefaultSelected(SelectionEvent e) {
    restoreWindow();
  }

  // Gets system shell
  private Shell getShell() {
    return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
  }

  // Minimizes the Window
  public void minimizeWindow() {
    getShell().setMinimized(true);
    getShell().setVisible(false);
  }

  // Restores the window
  protected void restoreWindow() {
    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
        .getShell();
    shell.open();
    shell.setMinimized(false);
    shell.forceActive();
    shell.forceFocus();
  }

  public void showMenu() {
    clearItems();
    MenuItem openItem;
    MenuItem closeItem;
    openItem = new MenuItem(this.menu, SWT.PUSH);
    closeItem = new MenuItem(this.menu, SWT.NONE);
    closeItem.setText("Close");
    closeItem.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        closeApplication();
      }
    });
    this.menuItems = new MenuItem[] { openItem, closeItem };

    openItem.setText("Open Password Gate");
    openItem.addSelectionListener(this.restoreWindowListener);
    this.menu.setVisible(true);
  }

  private void clearItems() {
    for (int i = 0; i < this.menuItems.length; i++) {
      MenuItem item = this.menuItems[i];
      item.removeSelectionListener(this.restoreWindowListener);
      this.menuItems[i].dispose();
    }
  }

  public void handleEvent(Event event) {
    showMenu();
  }

  public void createSystemTray() {
    // Gets system tray
    Tray tray = Display.getDefault().getSystemTray();
    // Creates tray item
    TrayItem item = new TrayItem(tray, SWT.NONE);
    item.setText("Password Gate");
    item.setToolTipText("Password Gate");
    // Sets image for tray
    Image image = ImageShop.get("default");
    item.setImage(image);

    item.addSelectionListener(this);
    item.addListener(SWT.MenuDetect, this);

    menu = new Menu(getShell(), SWT.POP_UP);
  }
} 
 

 

你可能感兴趣的:(eclipse,工作,UI,qq,UP)