现在的位置: 首页 > 综合 > 正文

java 托盘提醒

2018年05月19日 ⁄ 综合 ⁄ 共 8621字 ⁄ 字号 评论关闭
/*
 * $Id: BizReminder.java,v 1.0 2012/02/28 09:47:39 mk Exp $
 *
 * Copyright (c) 2012 Jason
 * All Rights Reserved.
 * Confidential and for internal use only.
 */
package com.bizatworktech.bizessence.in.web.common.rm;


import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class BizReminder extends JPanel implements Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private SystemTray sysTray = SystemTray.getSystemTray();

    private TrayIcon trayIcon; // 托盘对象

    private static BizReminder instance;

    private boolean isNews = false; // 消息闪动提醒线程标记

    private JFrame jf;

    private JPanel jp;

    private LinkLabel lb; // 超链接

    private Image iconTray; // 托盘图标

    private Image iconWin; // 主题窗口图标

    private final String ICON_PATH = "com/bizatworktech/bizessence/in/web/common/rm/";

    public boolean isOpened = true; // 主线程标记

    public static int count = 0;

    public static String userId = null;

    public synchronized static BizReminder getInstance() throws Exception {
        if (instance == null)
            instance = new BizReminder();
        return instance;
    }

    private BizReminder(){
        try {
            this.createTray();
        } catch (Exception e) {
            System.out.println("BizReminder() 初始化失败 " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void createTray(){
        if (count > 0 && null != userId) {
            init();            
            readMs();
        }
    }

    /**
     * 初始化
     * 
     * @throws Exception
     */
    private void init(){
        
        this.iconTray = new ImageIcon(getRes(ICON_PATH + "BIZlogo02.gif"))
                .getImage();

        this.iconWin = new ImageIcon(getRes(ICON_PATH + "BIZlogo.gif"))
                .getImage();

        this.trayIcon = new TrayIcon(iconTray);

        PopupMenu popupMenu = new PopupMenu();// 弹出菜单

        MenuItem exit = new MenuItem("Exit");

        popupMenu.add(exit);

        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                sysTray.remove(trayIcon);

                trayIcon = null;

                stop();// 停止线程

            }
        });

        this.trayIcon.setPopupMenu(popupMenu);

        jf = new JFrame("温馨提示(" + count + ")");

        jp = new JPanel();

        lb = new LinkLabel(count, userId);

        jp.add(lb);

        jf.setIconImage(iconWin);

        jf.setContentPane(jp);

        jf.setExtendedState(JFrame.NORMAL);
        
        jf.setVisible(true);

        jf.setResizable(false);

        jf.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width - 255,
                Toolkit.getDefaultToolkit().getScreenSize().height - 135, 250,
                100); // 右下角弹出

        jf.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {// 窗口关闭事件

                sysTray.remove(trayIcon);

                trayIcon = null;

                stop();// 停止线程, 移除托盘图标

            }
        });

        // jf.setDefaultCloseOperation(HIDE_ON_CLOSE);// 设置默认关闭隐藏窗口

        this.trayIcon.setToolTip("消息盒子(" + count + ")");

        try {
            this.sysTray.add(trayIcon);
        } catch (AWTException e1) {
            System.out.println("托盘初始化出现故障:" + e1.getMessage());
            e1.printStackTrace();
        } // 以上完成托盘初始化
        
        this.setVisible(true);
    }

    /**
     * 查看消息提醒
     * 
     * @throws Exception
     */
    private synchronized void readMs(){

        this.trayIcon.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {

                if (e.getClickCount() == 2) { // 鼠标双击

                    if (count == 0)
                        JOptionPane.showMessageDialog(jf, "没有新的待办事项!", "提示",
                                JOptionPane.INFORMATION_MESSAGE);

                    else {

                        setToolTipText("消息盒子(" + count + ")");
                        //Title("消息盒子(" + count + ")");
                        isNews = false;// 消息已经打开
                        BizReminder.this.lb.setInfo(count, userId);
                        BizReminder.this.jf.setVisible(true);

                    }
                }
            }
        });

    }

    public URL getRes(String str) {
        return this.getClass().getClassLoader().getResource(str);
    }

    /**
     * 判断线程是否关闭 返回true表示关闭;否则表示打开
     * 
     * @return
     */
    public synchronized boolean isClosed() {
        return isOpened == false ? true : false;
    }

    /**
     * 中断线程
     */
    public synchronized void stop() {
        isOpened = false;
        isNews = false;// 中断子线程
    }

    /**
     * 启动线程
     */
    public synchronized void start() {
        isOpened = true;
    }

    @Override
    public synchronized void run() {
        while (isOpened) {
            System.out.println("启动线程......");
            if (count > 0) {
                isNews = true;
            }

            if (null == this.trayIcon) {
                try {
                    createTray(); // 用户已经关闭提醒,需要重新初始化
                } catch (Exception e) {
                    System.out.println("托盘对象为空: " + e.getMessage());
                }
            }
            while (isNews) {// 有新消息
                try {
                    System.out.println("有新的消息了,开始提醒......");
                    if (!jf.isShowing())
                        BizReminder.this.jf.setVisible(true);

                    wait();// 下一次消息提醒之前,等待用户查看消息,类推

                } catch (Exception e1) {
                    e1.printStackTrace();
                    System.out.println("提醒消息产生异常" + e1.getMessage());
                }
            }
            if (!isNews) {
                try {// 没有新的消息或消息已经查看了;System.out.println("消息查看过了,线程重启......");
                    notifyAll();
                } catch (Exception e) {
                    System.out.println("nofifyAll exception: " + e.getMessage());
                }
            }
        }
    }

    /**
     * Test code
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
         BizReminder.count = 1;
         BizReminder.userId = "张三";
         BizReminder.getInstance();
//         new Thread(BizReminder.getInstance()).start();
    }
}

 

///: Action

/*
 * $Id: ReminderAction.java,v 1.0 2012/02/28 09:47:39 mk Exp $
 *
 * Copyright (c) 2012 Jason

 * All Rights Reserved.
 * Confidential and for internal use only.
 */
package com.bizatworktech.bizessence.in.web.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bizatworktech.bizessence.cm.xc.BEException;
import com.bizatworktech.bizessence.in.web.common.action.BaseAction;
import com.bizatworktech.bizessence.in.web.common.rm.BizReminder;

/**
 * 登录界面初始化: 主要是取得待办事项数
 * 
 * @author mk
 * @version $Revision: 1.0 $
 */
public class ReminderAction extends BaseAction {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException, BEException {
        
        int count = 0;// 记录用户任务个数
        
        BizReminder biz = null;
        
        HttpSession session = request.getSession();
        
        String id = session.getId();        
        
        try {
            
            count = getStationDelegator(request).getTasksCountByUser();
            
            if (count > 0){
                BizReminder.count = count;
                BizReminder.id = id;
            }            
            
            if (null == session.getAttribute("biz")) {
                
                biz = BizReminder.getInstance();
                
                session.setAttribute("biz", biz);
                
            } else{
                biz = (BizReminder) session.getAttribute("biz");
            }
            
            //System.out.println(biz.isClosed());
            
            if(!biz.isClosed()){                
                new Thread(biz).start();
            }else{
                biz.start();
            }                
            //response.getWriter().print(count);
        } catch (Exception e) {
            System.out.println("BizReminder wrong: " + e.getMessage());
        }
        return null;
    }

}


// js

function refreshMs(delay){
        var xmlHttp = getXmlHttp();        
        if( null != xmlHttp){    
            var url = "${pageContext.request.contextPath}/AjaxAction0.do?ID="+Math.random();        
            xmlHttp.open("GET", url ,true );
            xmlHttp.onreadystatechange = function (){
                if ( xmlHttp.readyState == 4 ){
                    if ( xmlHttp.status == 200 ){                    
                        document.getElementById('theMsgId').innerHTML= xmlHttp.responseText;
                    } 
                }
            }
            xmlHttp.send( null );
        }
        window.setTimeout("refreshMs("+ delay + ")", delay);    
    }
    refreshMs(5000);

 

 

// link


/*
 * $Id: LinkLabel.java,v 1.0 2012/02/28 09:47:39 mk Exp $
 *
 * Copyright (c) 2012 Jason

 * All Rights Reserved.
 * Confidential and for internal use only.
 */
package com.bizatworktech.bizessence.in.web.common.rm;

import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
/*
 * $Id: LinkLabel.java,v 1.0 2012/02/28 09:47:39 mk Exp $
 *
 * Copyright (c) 2012 SHANGHAI NEWTECH FABRIC PRINTING LTD
 * All Rights Reserved.
 * Confidential and for internal use only.
 */
package com.bizatworktech.bizessence.in.web.common.rm;

import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * 实现提醒用户的快捷方式
 * 
 * @author mk
 * 
 */
public class LinkLabel extends JLabel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int count;
    private String name;
    private boolean isSupported;

    public LinkLabel(int count, String name) {
        this.count = count;
        this.name = name;
        try {
            this.isSupported = Desktop.isDesktopSupported()
                    && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE);
        } catch (Exception e) {
            this.isSupported = false;
            System.out.println("该系统不支持");
        }
        if (this.isSupported) {
            this.setInfo(count, name);

            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    JOptionPane.showMessageDialog(null, "请到流程管理软件中进行处理!",
                            "流程管理软件", JOptionPane.INFORMATION_MESSAGE);
                }
            });
        }
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setInfo(int count, String name) {
        setText("<html><div style='font-size:10px serif;' color='#0000FF' >"
                + name
                + ", 你好!</div><div style='font:10px;margin-top:5px;' color='#0000FF'>您当前有(<font style=color:#FF0000>"
                + count + "</font>)条待办事项需要处理!</div></html>");

    }

    /**
     * 测试
     * 
     * @param args
     */
    public static void main(String[] args) {
        JFrame jf = new JFrame("温馨提示(" + 1 + ")");

        JPanel jp = new JPanel();

        JLabel lb = new LinkLabel(1, "张三");

        jp.add(lb);
        jf.add(jp);
        jf.setVisible(true);
        // jf.setSize(300,300);
        jf.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width - 255,
                Toolkit.getDefaultToolkit().getScreenSize().height - 135, 250,
                100);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

 

抱歉!评论已关闭.