我的知识库

知识等于力量

« SpringSide的代码规范Eclipse 插件精选介绍 »

最小化 RCP 应用到系统托盘的代码

这个程序可以在点击最小化按钮后, 主窗体最小化到系统托盘,  再点击托盘的图标, 主窗体就恢复显示. 是 Eclipse Tray 插件的原型. 代码修改自 Eclipser.org 的官方展示代码:

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * 
http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 ******************************************************************************
*/

/*
 * Tray example snippet: place an icon with a popup menu on the system tray
 *
 * For a list of all SWT example snippets see
 * 
http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.0
 
*/

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.*;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class Snippet143 {

    
public static void main(String[] args) {
        Display display 
= new Display();
        
final Shell shell = new Shell(display);
        shell.setText(
"Eclipse SDK");
        
        shell.setImage(
new org.eclipse.swt.graphics.Image(Display.getCurrent(),
                
"icons/sample.gif"));

        Image image 
= shell.getImage();
        
final Tray tray = display.getSystemTray();
        
if (tray == null{
            System.out.println(
"The system tray is not available");
        }
 else {
            
final TrayItem item = new TrayItem(tray, SWT.NONE);
            item.setVisible(
false);
            item.setToolTipText(
"SWT TrayItem");
            item.addListener(SWT.Show, 
new Listener() {
                
public void handleEvent(Event event) {
                    System.out.println(
"show");
                }

            }
);
            item.addListener(SWT.Hide, 
new Listener() {
                
public void handleEvent(Event event) {
                    System.out.println(
"hide");
                }

            }
);
            item.addListener(SWT.Selection, 
new Listener() {
                
public void handleEvent(Event event) {
                    System.out.println(
"selection");
                    toggleDisplay(shell, tray);
                }

            }
);
            item.addListener(SWT.DefaultSelection, 
new Listener() {
                
public void handleEvent(Event event) {
                    System.out.println(
"default selection");

                }

            }
);
            
final Menu menu = new Menu(shell, SWT.POP_UP);
            MenuItem mi 
= new MenuItem(menu, SWT.PUSH);
            mi.setText(
"Show " + shell.getText());
            mi.addListener(SWT.Selection, 
new Listener() {
                
public void handleEvent(Event event) {
                    toggleDisplay(shell, tray);
                }

            }
);
            menu.setDefaultItem(mi);
            
            
new MenuItem(menu, SWT.SEPARATOR);
            
            mi 
= new MenuItem(menu, SWT.PUSH);
            mi.setText(
"&About");
            
            mi.addListener(SWT.Selection, 
new Listener() {
                
public void handleEvent(Event event) {
                    MessageDialog.openInformation(
                            shell,
                            
"EclipseTray Plug-in 1.0b by BeanSoft",
                            
"This plugin will minimize Eclipse window to tray.\n" +
                            
"Author: beansoft@126.com\n" +
                            
"Eclipse version tested: 3.2 win32\nDate: 2006-12-14");
                }

            }
);
            
            item.addListener(SWT.MenuDetect, 
new Listener() {
                
public void handleEvent(Event event) {
                    menu.setVisible(
true);
                }

            }
);
            item.setImage(image);
        }


        shell.addShellListener(
new ShellListener() {

            
public void shellActivated(ShellEvent e) {
                
// TODO Auto-generated method stub
                
            }


            
public void shellClosed(ShellEvent e) {
                
// TODO Auto-generated method stub
                
            }


            
public void shellDeactivated(ShellEvent e) {
                
// TODO Auto-generated method stub
                
            }


            
public void shellDeiconified(ShellEvent e) {
                
// TODO Auto-generated method stub
                
            }


            
public void shellIconified(ShellEvent e) {
                toggleDisplay(shell, tray);
            }

            
        }
);
        
        shell.setBounds(
5050300200);
        shell.open();
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

        image.dispose();
        display.dispose();
    }

    
    
/**
     * Toggle the display of current shell and tray icon.
     * 
     * 
@author BeanSoft(beansoft@126.com)
     * 
@param shell
     * 
@param tray
     
*/

    
protected static void toggleDisplay(Shell shell, Tray tray) {
        
try {
            shell.setVisible(
!shell.isVisible());
            tray.getItem(
0).setVisible(!shell.isVisible());
            
// TODO Get focus???! Like QQ?
//            shell.setFocus();
//            shell.setActive();
        }
 catch (Exception e) {
            e.printStackTrace();
            
// TODO: handle exception
        }

    }

}

Search

导航

热门文章

最新文章

Powered By duduwolf's wiki 1.0

Copyright 1999-2006 duduwolf.com Some Rights Reserved.