⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 desktopindicatorlink.java

📁 一款即时通讯软件
💻 JAVA
字号:
package edu.ou.kmi.buddyspace.gui.systemtray;

import java.awt.Frame;
import java.awt.event.*;

import edu.ou.kmi.buddyspace.gui.*;

public class DesktopIndicatorLink implements DesktopIndicatorListener, ActionListener {
    
    DesktopIndicator indicator = null;
    BSMainFrame mainFrame = null;
    static int onlineImage;
    static int onlineMsgImage;
    static int awayImage;
    static int awayMsgImage;
    static int offlineImage;
    protected javax.swing.Timer timer = null;
    protected boolean lastScreenSaverActive = false;
    
    public DesktopIndicatorLink(BSMainFrame mainFrame) {
        this.mainFrame = mainFrame;
    }
    
    public boolean init() {
        // Initialize JNI extension
        if (!DesktopIndicator.initialize()) {
                System.err.println( "Either you are not on Windows, or there is a problem with the DesktopIndicator library!" );
                return false;
        }

        // Loads image
        onlineImage = DesktopIndicator.loadImage( "online.ico" );
        if (onlineImage == -1) {
                System.err.println( "Could not load the image file \"online.ico\"!");
                return false;
        }

        // Loads image
        onlineMsgImage = DesktopIndicator.loadImage( "onlineMsg.ico" );
        if (onlineMsgImage == -1) {
                System.err.println( "Could not load the image file \"onlineMsg.ico\"!");
                return false;
        }
        
        // Loads image
        awayImage = DesktopIndicator.loadImage( "away.ico" );
        if (awayImage == -1) {
                System.err.println( "Could not load the image file \"away.ico\"!");
                return false;
        }
        
        // Loads image
        awayMsgImage = DesktopIndicator.loadImage( "awayMsg.ico" );
        if (awayMsgImage == -1) {
                System.err.println( "Could not load the image file \"awayMsg.ico\"!");
                return false;
        }
        
        // Loads image
        offlineImage = DesktopIndicator.loadImage( "offline.ico" );
        if (offlineImage == -1) {
                System.err.println( "Could not load the image file \"offline.ico\"!");
                return false;
        }

        // Create the indicator
        indicator = new DesktopIndicator(offlineImage, "BuddySpace - Offline" );
        indicator.addDesktopIndicatorListener(this);
        indicator.show();

        // Instructions
        System.out.println( "DesktopIndicator ready" );
        
        if (timer == null) {
            timer = new javax.swing.Timer(60 * 1000, this);
            timer.setRepeats(true);
            timer.start();
        }
        
        return true;
    }
    
    
    public void setIcon(boolean connected, String showString, boolean newMessage) {
        if (indicator == null) return;
        
        int img;
        if (!connected || (showString != null && showString.equals(BSMainFrame.SHOW_OFFLINE_STR)))
            img = offlineImage;
        else if (showString == null || showString.equals(BSMainFrame.SHOW_ONLINE_STR))
            img = newMessage? onlineMsgImage: onlineImage;
        else if (showString.equals(BSMainFrame.SHOW_CHAT_STR))
            img = newMessage? onlineMsgImage: onlineImage;
        else if (showString.equals(BSMainFrame.SHOW_AWAY_STR))
            img = newMessage? awayMsgImage: awayImage;
        else if (showString.equals(BSMainFrame.SHOW_XA_STR))
            img = newMessage? awayMsgImage: awayImage;
        else if (showString.equals(BSMainFrame.SHOW_DND_STR))
            img = newMessage? awayMsgImage: awayImage;
        else 
            img = offlineImage;
        
        String tooltip = "BuddySpace - " + ((!connected)? "Offline" : ((showString != null)? showString : "Online"));
        indicator.update(img, tooltip);
    }
    
    
    public void finish() {
        if (indicator == null) return;

        indicator.removeDesktopIndicatorListener(this);
        indicator.hide();

        System.err.println("DesktopIndicator finished");
        
        if (timer != null) {
            timer.stop();
            timer.removeActionListener(this);
        }
    }
        
    
    public void onDesktopIndicatorClicked(DesktopIndicator source) {
        if (source != indicator) return;
        
        System.out.println("DesktopIndicator clicked");
        
        if (mainFrame == null) return;
        
        setMainFrameVisible(!mainFrame.isVisible());
    }
    
    
    public void setMainFrameVisible(boolean visible) {
        if (indicator == null) return;
        
        if (visible) {
            mainFrame.setVisible(true);
            mainFrame.toFront();
            mainFrame.setState(Frame.NORMAL);
        }
        else
            mainFrame.setVisible(false);
    }
    
    
    /**
    * Called when presence was changed.
    **/
    public void onDesktopIndicatorPresenceChanged(DesktopIndicator source, String presenceStr) {
        if (source != indicator) return;
        mainFrame.setPresence(presenceStr, presenceStr, true);
    }
    
    
    /**
    * Notifies all listeners that exit was clicked.
    **/	
    public void onDesktopIndicatorExit(DesktopIndicator source) {
        if (source != indicator) return;
        Thread t = new Thread() {
            public void run() {
                mainFrame.close();
            }
        };
        t.start();
    }

    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == timer) {
            boolean result = indicator.isScreenSaverActive();
            if (result && !lastScreenSaverActive) {
                System.out.println("screensaver started");
                //mainFrame.setAutoPresence(BSMainFrame.SHOW_AWAY_STR, "Saving screen");
                mainFrame.setAutoPresence(BSMainFrame.SHOW_AWAY_STR, "Auto-away, screen-saver running");
            }
            else if (!result && lastScreenSaverActive) {
                System.out.println("screensaver closed");
                mainFrame.restorePresence();
            }
            lastScreenSaverActive = result;
        }
    }
    
	/*public void onDesktopIndicatorClicked( DesktopIndicator source )
	{
		System.err.println( String.valueOf( clicks ) + " click(s) left..." );
		
		// Countdown to death
		clicks--;
		
		if( clicks % 2 == 1 )
		{
			// Comic!
			source.update( comicImage, "A message for you, sir!" );
		}
		else
		{
			// Quick!
			source.update( quickImage, "Quick! Click me!" );
		}
		
		if( clicks == 0 )
		{
			// The end is nigh
			synchronized( listener )
			{
				listener.notifyAll();
			}
		}
	}*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -