/*
* Copyright (C) 2012 FiveHT Media Ltd.
*
* 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
* (at your option) 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 com.fiveht.tick.ui;
import com.fiveht.tick.Bootstrap;
import com.fiveht.tick.ui.event.CloseEvent;
import com.fiveht.tick.ui.event.CloseListener;
import com.fiveht.tick.ui.tray.Tray;
import java.awt.AWTException;
import java.awt.SystemTray;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
/**
* Exactly what you think it is - the desktop application entry point.
*
* @author Nathan Crause
*/
public class Desktop implements Runnable, CloseListener {
/**
* Desktop application entry point
*
* @param args VM runtime arguments
*/
public static void main(String[] args) {
getInstance().run();
}
/**
* Singleton instance of Desktop
*/
private static Desktop instance;
/**
* Gets (or creates) the single instance of the desktop application
*
* @return the singleton instance
*/
public static Desktop getInstance() {
if (instance == null) instance = new Desktop();
return instance;
}
/**
* The visual frame
*/
private Frame frame;
/**
* We store this as a separate value instead of simply using
* SystemTray.isSupported() because Ubuntu doesn't correct support
* system trays, and will report it as being supported, but will throw
* an exception when trying to show it
*/
private boolean hasSystemTray = true;
/**
* Private constructor to prevent unexpected instantiation.
*/
private Desktop() {
}
/**
* The main thread
*/
@Override
public void run() {
// setting the look and feel
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
Logger.getLogger(Desktop.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
// run the bootstrap first, to make sure the environment is there
Bootstrap.getInstance().run();
frame = new Frame();
if (SystemTray.isSupported()) {
try {
SystemTray.getSystemTray().add(new Tray(frame));
}
catch (AWTException ex) {
hasSystemTray = false;
}
}
frame.setVisible(true);
frame.addCloseListener(this);
}
catch (Throwable thrown) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, thrown);
JOptionPane.showMessageDialog(null, thrown.getMessage());
}
}
/**
* This method is triggered if the event model detects that the frame has
* had it's "close" button fired.
*
* @param event the event which was triggered
*/
@Override
public void frameCloseSelected(CloseEvent event) {
if (hasSystemTray) {
// don't dispose it, just hide it (still accessible via the system tray
frame.setVisible(false);
}
else {
frame.dispose();
}
}
}