iqq.app.ui.manager.MainManager.java Source code

Java tutorial

Introduction

Here is the source code for iqq.app.ui.manager.MainManager.java

Source

package iqq.app.ui.manager;

import com.alee.utils.SystemUtils;
import iqq.api.bean.IMBuddy;
import iqq.api.bean.IMEntity;
import iqq.app.core.context.IMContext;
import iqq.app.core.service.EventService;
import iqq.app.core.service.ResourceService;
import iqq.app.core.service.SkinService;
import iqq.app.core.service.TimerService;
import iqq.app.ui.event.UIEvent;
import iqq.app.ui.event.UIEventDispatcher;
import iqq.app.ui.event.UIEventHandler;
import iqq.app.ui.event.UIEventType;
import iqq.app.ui.frame.MainFrame;
import iqq.app.util.UIUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Map;

/**
 * Project  : iqq-projects
 * Author   :  < 6208317@qq.com >
 * Created  : 14-5-12
 * License  : Apache License 2.0
 */
@Component
public class MainManager {
    private final Logger logger = LoggerFactory.getLogger(MainManager.class);
    private SystemTray tray;
    private TrayIcon icon;
    private PopupMenu menu;
    private MainFrame mainFrame;

    private Runnable flashTimer;
    private Image flashImage; //??
    private Image defaultImage; //?
    private Image blankImage; //?
    private IMEntity flashOwner; //??
    private Deque<IMEntity> flashQueue; //?

    @Resource
    private ChatManager chatManager;
    @Resource
    private EventService eventService;
    @Resource
    private ResourceService resourceService;
    @Resource
    private SkinService skinService;
    @Resource
    private TimerService timerService;

    @PostConstruct
    public void init() {
        flashQueue = new LinkedList<>();
        flashTimer = new FlashTrayTimer();
        timerService.setInterval(flashTimer, 500);

        UIEventDispatcher uiEventDispatcher = new UIEventDispatcher(this);
        eventService.register(uiEventDispatcher.getEventTypes(), uiEventDispatcher);
    }

    @UIEventHandler(UIEventType.FLASH_USER_START)
    protected void processIMFlashUserStart(UIEvent event) {
        if (flashQueue == null)
            return;

        IMEntity entity = (IMEntity) event.getTarget();
        entity.setAttachment(event.getDataEx());
        if (flashQueue.contains(entity)) {
            flashQueue.remove(entity);
        }
        if (flashOwner != null && flashOwner != entity) {
            flashQueue.addFirst(flashOwner);
        }
        flashOwner = entity;
        flashImage = getTrayFace(entity);
        flashTimer.run();
    }

    @UIEventHandler(UIEventType.FLASH_USER_STOP)
    protected void processIMFlashUserStop(UIEvent event) {
        if (flashQueue == null)
            return;

        IMEntity entity = (IMEntity) event.getTarget();
        if (flashQueue.isEmpty()) {
            flashOwner = null;
            flashImage = null;
            icon.setImage(defaultImage);
        } else if (flashOwner != entity) {
            flashQueue.remove(flashOwner);
        } else {
            flashOwner = flashQueue.poll();
            flashImage = getTrayFace(flashOwner);
            flashTimer.run();
        }
    }

    public void show() {
        if (mainFrame == null) {
            mainFrame = new MainFrame();
            mainFrame.setVisible(true);
            mainFrame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
            enableTray();
        }
        if (!mainFrame.isVisible()) {
            mainFrame.setVisible(true);// ??
            // ??(??)
            mainFrame.setExtendedState(Frame.NORMAL);
        }
    }

    public void hide() {
        if (mainFrame.isVisible()) {
            mainFrame.setVisible(false);
        }
    }

    public void enableTray() {
        if (SystemTray.isSupported() && tray == null) {
            menu = new PopupMenu();
            MenuItem restore = new MenuItem("  ?  ");
            restore.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    show();
                }
            });
            MenuItem exit = new MenuItem("  ?  ");
            exit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            menu.add(restore);
            menu.addSeparator();
            menu.add(exit);

            if (SystemUtils.isMac()) {
                defaultImage = skinService.getIconByKey("window/titleWIconBlack").getImage();
            } else {
                defaultImage = mainFrame.getIconImage();
            }
            blankImage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
            tray = SystemTray.getSystemTray();
            icon = new TrayIcon(defaultImage, "IQQ");
            icon.setImageAutoSize(true);
            if (!SystemUtils.isMac()) {
                icon.setPopupMenu(menu);
            }
            try {
                tray.add(icon);
                icon.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        logger.debug("MouseEvent " + e.getButton() + " " + e.getClickCount());
                        //??
                        if (e.getButton() == MouseEvent.BUTTON1) {
                            if (flashOwner != null) {
                                Map<String, Object> data = (Map<String, Object>) flashOwner.getAttachment();
                                if (data != null && data.containsKey("action")
                                        && data.get("action").equals("ADD_BUDDY_REQUEST")) {
                                    IMContext.getBean(FrameManager.class).showGetFriendRequest((IMBuddy) flashOwner,
                                            data.get("buddy_request_id").toString());
                                    eventService.broadcast(new UIEvent(UIEventType.FLASH_USER_STOP, flashOwner));
                                    return;
                                } else {
                                    // ?
                                    chatManager.addChat(flashOwner);
                                }
                            } else {
                                show();
                            }
                        }
                    }
                });
            } catch (AWTException e) {
                logger.error("SystemTray add icon.", e);
            }
        }

    }

    private Image getTrayFace(IMEntity owner) {
        BufferedImage avatar = null;
        if (owner.getAvatar() != null) {
            avatar = owner.getAvatarBuffered();
        } else {
            avatar = UIUtils.getDefaultAvatarBuffer();
        }
        return avatar.getScaledInstance(32, 32, 100);
    }

    private class FlashTrayTimer implements Runnable {
        @Override
        public void run() {
            if (flashOwner != null && tray != null && icon != null && flashImage != null) {
                Image curImg = icon.getImage();
                icon.setImage(curImg == flashImage ? blankImage : flashImage);
            }
        }
    }

}