Package de.chris_soft.nanodoa

Source Code of de.chris_soft.nanodoa.TrayIconAndMenu

/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.nanodoa;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JOptionPane;
import de.chris_soft.nanoarchive.Archive;
import de.chris_soft.utilities.AppProperties;
import de.chris_soft.utilities.FileUtils;
import de.chris_soft.utilities.LogUtils;
import de.chris_soft.utilities.swing.SearchItemInputFrame;
import de.chris_soft.utilities.swing.TextInputListener;

/**
* Creates a micro user interface via system tray.
* @author Christian Packenius.
*/
public class TrayIconAndMenu implements ActionListener, MouseListener, TextInputListener {
  /**
   * System Tray.
   */
  private SystemTray systemTray;

  /**
   * Menu item "About".
   */
  private MenuItem aboutItem;

  /**
   * Menu item "Configuration wizard".
   */
  private MenuItem configureItem;

  /**
   * Menu item "Exit".
   */
  private MenuItem exitItem;

  /**
   * Application icon in the system tray.
   */
  private TrayIcon trayIcon;

  /**
   * ID of the tray image.
   */
  private int imageID = 0;

  /**
   * Tray icon images.
   */
  private final BufferedImage[] images;

  /**
   * Main application object for stopping application.
   */
  private final NanoDoAMain nanoDoAMain;

  /**
   * Frame for search input.
   */
  private SearchItemInputFrame searchFrame = null;

  /**
   * Constructor.
   * @param nanoDoAMain
   * @throws AWTException
   */
  public TrayIconAndMenu(NanoDoAMain nanoDoAMain) throws AWTException {
    if (!SystemTray.isSupported()) {
      LogUtils.log("System tray is not provided by this system!");
      this.nanoDoAMain.stopSystem();
    }

    this.nanoDoAMain = nanoDoAMain;

    images = createTrayIconImage();
    trayIcon = new TrayIcon(images[0]);
    systemTray = SystemTray.getSystemTray();

    PopupMenu popup = new PopupMenu();
    aboutItem = new MenuItem("About");
    aboutItem.addActionListener(this);
    configureItem = new MenuItem("Configuration wizard");
    configureItem.addActionListener(this);
    exitItem = new MenuItem("Exit");
    exitItem.addActionListener(this);

    popup.add(aboutItem);
    popup.add(configureItem);
    popup.addSeparator();
    popup.add(exitItem);

    trayIcon.setPopupMenu(popup);
    trayIcon.setToolTip("NanoDoA");
    trayIcon.addMouseListener(this);

    systemTray.add(trayIcon);
  }

  private BufferedImage[] createTrayIconImage() {
    BufferedImage[] image = new BufferedImage[10];
    for (int i = 0; i < 10; i++) {
      image[i] = new BufferedImage(16, 16, BufferedImage.TYPE_4BYTE_ABGR_PRE);
      Graphics g = image[i].getGraphics();
      Graphics2D g2d = (Graphics2D) g;
      g2d.setBackground(Color.WHITE);
      g2d.fillRect(0, 0, 16, 16);
      g2d.setColor(Color.LIGHT_GRAY);
      g2d.fillRoundRect(2, 2, 11, 11, 5, 5);
      g2d.setColor(Color.DARK_GRAY);
      g2d.drawRoundRect(2, 2, 11, 11, 5, 5);
      int count = 0;
      for (int y = 4; y < 12; y += 3) {
        for (int x = 4; x < 12; x += 3) {
          if (count == i) {
            y = x = 100;
          }
          else {
            g2d.drawRect(x, y, 1, 1);
            count++;
          }
        }
      }
      g2d.dispose();
    }
    // try {
    // ImageIO.write(image[4], "PNG", new File("icon.png"));
    // } catch (IOException exception) {
    // // Ignore.
    // }
    return image;
  }

  /**
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == exitItem) {
      stopSystem();
    }
    else if (e.getSource() == aboutItem) {
      showAboutWindow();
    }
    else if (e.getSource() == configureItem) {
      showConfigurationWizard();
    }
  }

  private void showConfigurationWizard() {
    ConfigurationWizard cw = new ConfigurationWizard();
    Properties props = cw.start();
    try {
      if (props != null) {
        FileUtils.storeProperties(NanoDoAMain.nanodoaPropertiesFile, props);
        AppProperties.setPropertiesFile(NanoDoAMain.nanodoaPropertiesFile);
        JOptionPane.showMessageDialog(null, "Please stop and start NanoDoA for full configuration change...",
          "Restart NanoDoA", JOptionPane.INFORMATION_MESSAGE);
      }
    } catch (IOException e) {
      JOptionPane.showMessageDialog(null, "The configurations couldn't be stored!", "Error!", JOptionPane.ERROR_MESSAGE);
    }
  }

  private void stopSystem() {
    nanoDoAMain.stopSystem();
  }

  private void showAboutWindow() {
    String txt1 = "<html>NanoDoA has been written";
    String txt2 = "<br>by Christian Packenius in 2011/2012</html>";
    JOptionPane.showMessageDialog(null, txt1 + txt2, "About...", JOptionPane.INFORMATION_MESSAGE);
  }

  /**
   * Set next image to the tray icon to show the user that there happens anything.
   */
  public void setNextImage() {
    imageID++;
    imageID %= images.length;
    trayIcon.setImage(images[imageID]);
  }

  /**
   * Set a special image in tray.
   * @param imageID Number of the image to set (>= 0).
   */
  public void setImage(int imageID) {
    this.imageID = imageID;
    if (imageID >= images.length) {
      imageID = images.length - 1;
    }
    else if (imageID < 0) {
      imageID = 0;
    }
    trayIcon.setImage(images[imageID]);
  }

  /**
   * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
   */
  @Override
  public void mouseClicked(MouseEvent event) {
    if (event.getSource() == trayIcon) {
      if (event.getButton() == MouseEvent.BUTTON1) {
        if (searchFrame == null || searchFrame.isVisible()) {
          new SearchItemInputFrame(this);
        }
      }
    }
  }

  /**
   * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
   */
  @Override
  public void mouseEntered(MouseEvent arg0) {
    // Ignore.
  }

  /**
   * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
   */
  @Override
  public void mouseExited(MouseEvent arg0) {
    // Ignore.
  }

  /**
   * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
   */
  @Override
  public void mousePressed(MouseEvent arg0) {
    // Ignore.
  }

  /**
   * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
   */
  @Override
  public void mouseReleased(MouseEvent arg0) {
    // Ignore.
  }

  /**
   * @see de.chris_soft.utilities.swing.TextInputListener#textInputFinished(java.lang.String)
   */
  @Override
  public void textInputFinished(String text) {
    if (text != null && text.trim().length() > 0) {
      Archive archive = nanoDoAMain.archive;
      new FulltextSearchWindows(text, archive);
    }
  }

  /**
   * Removes the tray icon from system tray.
   */
  public void removeSystemTrayIcon() {
    systemTray.remove(trayIcon);
  }
}
TOP

Related Classes of de.chris_soft.nanodoa.TrayIconAndMenu

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.