/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfrobot.gui;
import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import pdfrobot.Application;
/**
*
* @author hedsttor
*/
public class GuiManager {
private boolean initializing;
private boolean rulesModified;
private static GuiManager instance;
private Application application;
private MainWindow mainWindow;
private TrayIcon trayIcon;
private GuiManager() {
}
public static GuiManager getInstance() {
if (instance == null) {
instance = new GuiManager();
}
return instance;
}
public void startGui(Application application) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
initializing = true;
this.application = application;
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
mainWindow = new MainWindow();
initSystemTray();
setCloseOperation();
mainWindow.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/config/accept.png")));
mainWindow.setVisible(true);
initializing = false;
}
public Application getApplication() {
return application;
}
public MainWindow getMainWindow() {
return mainWindow;
}
public void displayTrayIconMessage(long secs) {
if(!isInitializing())
trayIcon.displayMessage("Interval", Long.toString(secs) + " seconds", TrayIcon.MessageType.INFO);
}
private void initSystemTray() {
if (SystemTray.isSupported()) {
MenuItem showMenuItem = new MenuItem("Show");
showMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainWindow.setVisible(true);
}
});
MenuItem quitMenuItem = new MenuItem("Quit");
quitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
PopupMenu popupMenu = new PopupMenu("PdfRobot");
popupMenu.add(showMenuItem);
popupMenu.add(quitMenuItem);
TrayIcon trayIcon = new TrayIcon(new ImageIcon(getClass().getResource("/config/accept.png")).getImage());
trayIcon.setPopupMenu(popupMenu);
this.trayIcon = trayIcon;
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException ex) {
Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void setCloseOperation() {
if(SystemTray.isSupported()) {
mainWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
} else {
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public boolean isInitializing() {
return initializing;
}
public boolean isRulesModified() {
return rulesModified;
}
public void setRulesModified(boolean rulesModified) {
this.rulesModified = rulesModified;
}
}