Example #1
0
  public static void main(String[] args) {
    System.out.println(ResourceUsage.getStatus());
    final String ipAddress = Util.getIp();
    final Thread listener;
    final SocketListener socketListener = new SocketListener();
    listener = new Thread(socketListener);
    if (!SystemTray.isSupported()) {
      System.err.println("System tray is not supported.");
      return;
    }

    SystemTray systemTray = SystemTray.getSystemTray();

    Image image =
        Toolkit.getDefaultToolkit().getImage(ServiceDriver.class.getResource("pause.png"));

    final TrayIcon trayIcon = new TrayIcon(image);

    final PopupMenu trayPopupMenu = new PopupMenu();

    MenuItem startService = new MenuItem("Start Service");
    startService.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(
                null, "Service Started", "Surrogate Service", JOptionPane.INFORMATION_MESSAGE);
            try {
              listener.start();
              Image image =
                  Toolkit.getDefaultToolkit()
                      .getImage(ServiceDriver.class.getResource("cyber.gif"));
              trayIcon.setImage(image);
            } catch (Exception err) {
              Image image =
                  Toolkit.getDefaultToolkit()
                      .getImage(ServiceDriver.class.getResource("cyber.gif"));
              trayIcon.setImage(image);
              socketListener.resume();
            }
          }
        });
    trayPopupMenu.add(startService);

    MenuItem action = new MenuItem("Stop Service");
    action.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(
                null, "Service Stopped", "Surrogate Service", JOptionPane.INFORMATION_MESSAGE);
            try {
              socketListener.pause();
              Image image =
                  Toolkit.getDefaultToolkit()
                      .getImage(ServiceDriver.class.getResource("pause.png"));
              trayIcon.setImage(image);
            } catch (Exception e1) {
              System.err.println("Service has not stared yet");
            }
          }
        });
    trayPopupMenu.add(action);

    MenuItem close = new MenuItem("Close");
    close.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            System.exit(0);
          }
        });
    trayPopupMenu.add(close);

    trayIcon.setPopupMenu(trayPopupMenu);

    trayIcon.addMouseMotionListener(
        new MouseMotionAdapter() {
          @Override
          public void mouseMoved(MouseEvent e) {
            trayIcon.setToolTip(Util.getStatus(ipAddress));
          }
        });
    trayIcon.setImageAutoSize(true);

    try {
      systemTray.add(trayIcon);
    } catch (AWTException awtException) {
      awtException.printStackTrace();
    }
  }
  private void trayIconListener(final JPopupMenu popup, TrayIcon icon) {

    Timer notificationTimer = new Timer();
    notificationTimer.schedule(
        new TimerTask() {
          @Override
          public void run() {
            if (visible && !inBound) {
              visible = !visible;
              popup.setVisible(visible);
            }
            inBound = false;
          }
        },
        250,
        1500);

    icon.addMouseMotionListener(
        new MouseMotionAdapter() {
          @Override
          public void mouseMoved(MouseEvent e) {
            inBound = true;
          }
        });

    popup.addMouseMotionListener(
        new MouseMotionAdapter() {
          @Override
          public void mouseMoved(MouseEvent e) {
            inBound = true;
          }
        });

    icon.addMouseListener(
        new MouseAdapter() {

          @Override
          public void mouseReleased(MouseEvent e) {

            if (e.getButton() != MouseEvent.BUTTON1) {
              return;
            }

            Point point = e.getPoint();
            Rectangle bounds = getScreenViewableBounds(getGraphicsDeviceAt(point));
            int x = point.x;
            int y = point.y;
            if (y < bounds.y) {
              y = bounds.y;
            } else if (y > bounds.y + bounds.height) {
              y = bounds.y + bounds.height;
            }
            if (x < bounds.x) {
              x = bounds.x;
            } else if (x > bounds.x + bounds.width) {
              x = bounds.x + bounds.width;
            }

            if (x + popup.getWidth() > bounds.x + bounds.width) {
              x = (bounds.x + bounds.width) - popup.getWidth();
            }
            if (y + popup.getWidth() > bounds.y + bounds.height) {
              y = (bounds.y + bounds.height) - popup.getHeight();
            }

            visible = !visible;

            if (visible) {
              popup.setLocation(x, y);
            }
            popup.setVisible(visible);
          }

          @Override
          public void mouseExited(MouseEvent e) {
            visible = false;
            popup.setVisible(visible);
          }
        });

    popup.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseExited(MouseEvent e) {
            if ((e.getX() < popup.getBounds().getMaxX())
                && (e.getX() >= popup.getBounds().getMinX())
                && (e.getY() < popup.getBounds().getMaxY())
                && (e.getY() >= popup.getBounds().getMinY())) {
              return;
            }
            visible = false;
            popup.setVisible(visible);
          }
        });
  }