hkbmwcn

rcp 程序最小化窗口到系统托盘

在RCP程序中最小化窗口到系统托盘并不像SWT程序那样直接。在网上找了一段代码,自己试验通过了,现在贴出来大家分享。
在RCP中继承WorkbenchAdvisor的子类中添加下面代码就可以了。

 public void postStartup() {
  super.postStartup();
  final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
  shell.addShellListener(new ShellAdapter() {
   public void shellIconified(ShellEvent e) {
    shell.setVisible(false);
   }
  });
  createSystemTray();

 }

 private void createSystemTray() {
  Tray tray = Display.getDefault().getSystemTray();
  TrayItem item = new TrayItem(tray, SWT.NONE);
  item.setText("JDishNetwork");
  item.setToolTipText("JDishNetwork");
  Image image = new Image(Display.getDefault(), 16, 16);
  item.setImage(image);
  this.trayManager = new TrayItemManager();
  item.addSelectionListener(this.trayManager);
  item.addListener(SWT.MenuDetect, this.trayManager);
 }

 private class TrayItemManager implements SelectionListener, Listener {
  // TODO: Convert to one class
  private final class WindowStateListener extends SelectionAdapter {
   public void widgetSelected(SelectionEvent e) {
    minimizeWindow();
   }
  }
  private final class RestoreWindowListener extends SelectionAdapter {
   public void widgetSelected(SelectionEvent e) {
    restoreWindow();
   }
  }
  private boolean minimized = false;
  private Menu menu;
  private MenuItem[] menuItems = new MenuItem[0];
  private RestoreWindowListener restoreWindowListener;
  private WindowStateListener minimizeWindowListener;

  public TrayItemManager() {
   this.menu = new Menu(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.POP_UP);
   this.restoreWindowListener = new RestoreWindowListener();
   this.minimizeWindowListener = new WindowStateListener();

  }

  protected void closeApplication() {
   PlatformUI.getWorkbench().close();
  }

  public void widgetSelected(SelectionEvent e) {
   //
  }

  public void widgetDefaultSelected(SelectionEvent e) {
   if (this.minimized) {
    restoreWindow();
   } else {
    minimizeWindow();
   }
  }

  protected void minimizeWindow() {
   PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setMinimized(true);
   this.minimized = true;
  }

  protected void restoreWindow() {
   Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
   shell.open();
   shell.setMinimized(false);
   shell.forceActive();
   shell.forceFocus();
   this.minimized = false;
  }

  public void showMenu() {
   clearItems();
   MenuItem mi;
   MenuItem closeItem;
   mi = 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[] { mi, closeItem };

   if (this.minimized) {
    mi.setText("Maximize");
    mi.addSelectionListener(this.restoreWindowListener);
   } else {
    mi.setText("Minimize");
    mi.addSelectionListener(this.minimizeWindowListener);
   }
   this.menu.setVisible(true);
  }

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

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

 }

posted on 2006-01-13 15:31 亘古顽石 阅读(878) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: