Package at.kugel.tool.buildtray

Source Code of at.kugel.tool.buildtray.TrayIconFactory

package at.kugel.tool.buildtray;

import java.awt.AWTException;
import java.awt.Image;
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.util.EnumMap;

import at.kugel.tool.buildtray.status.Status;
import at.kugel.tool.buildtray.status.TrayIconStatus;

/**
* Setup the tray with popup menu and image icon list.
*
* @author <a href="http://www.code-cop.org/">Peter Kofler</a>
*/
class TrayIconFactory {

   private final Status initialStatus = Status.BUSY;
   private final String initialTip = "Build Status Tray";

   private EnumMap<Status, Image> images;
   private TrayIcon trayIcon;

   public TrayIconFactory() {
      loadImages();
      createTrayIcon();
   }

   private void loadImages() {
      images = new EnumMap<Status, Image>(Status.class);
      images.put(Status.OK, loadImage("res/build_green.gif"));
      images.put(Status.BUSY, loadImage("res/build_white.gif"));
      images.put(Status.FAILED, loadImage("res/build_red.gif"));
   }

   private Image loadImage(String fileName) {
      return Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource(fileName));
   }

   private void createTrayIcon() {
      Image image = images.get(initialStatus);
      trayIcon = new TrayIcon(image, initialTip);
   }

   public void setRightClickPopup(PopupMenu popup) {
      trayIcon.setPopupMenu(popup);
   }

   public void setLeftClickDefault(final IndividualActions actions) {
      trayIcon.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            actions.openBrowser();
         }
      });
   }

   public TrayIconStatus create() throws AWTException {
      addToSystemTray();
      return new TrayIconStatus(trayIcon, images);
   }

   private void addToSystemTray() throws AWTException {
      SystemTray systemTray = SystemTray.getSystemTray();
      systemTray.add(trayIcon);
   }

   public static void handleTrayNotSupported() {
      if (!SystemTray.isSupported()) {
         System.err.println("System Tray not supported");
         System.exit(1);
      }
   }

}
TOP

Related Classes of at.kugel.tool.buildtray.TrayIconFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.