package pt.treps;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.fife.ui.autocomplete.AutoCompletion;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceGeminiLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceGraphiteAquaLookAndFeel;
import pt.opensoft.swing.IconFactory;
import pt.treps.autocompletion.TimeAutoCompletion;
import pt.treps.autocompletion.TimeCompletionProvider;
import pt.treps.parser.Project;
import pt.treps.parser.Task;
import pt.treps.ui.MainFrame;
import pt.treps.ui.TrayPopupMenu;
import pt.treps.util.MetadataInitializer;
public class TReps {
private static final long serialVersionUID = 1L;
protected MainFrame mainFrame;
protected AutoCompletion autoCompletionEngine;
private static List<Project> projects;
private static Project selectedProject;
private static Task selectedTask;
Timer timer;
public TReps() {
this.mainFrame = new MainFrame();
this.mainFrame.getComboBoxProjects().addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
//System.out.println("ItemStateChange: " + e.getItem() + " @" + e.getItem().getClass());
mainFrame.getComboBoxTasks().removeAllItems();
for(Task task : ((Project) e.getItem()).getTasks()) {
mainFrame.getComboBoxTasks().addItem(task);
}
}
});
projects = MetadataInitializer.initializeMetadata(mainFrame);
autoCompletionEngine = new TimeAutoCompletion(new TimeCompletionProvider(mainFrame));
autoCompletionEngine.setListCellRenderer(autoCompletionEngine.getCompletionProvider().getListCellRenderer());
autoCompletionEngine.setShowDescWindow(false);
autoCompletionEngine.setParameterAssistanceEnabled(true);
autoCompletionEngine.install(this.mainFrame.timeReportTA);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//TODO nag the guy
}
}, 3600000, 3600000);
}
protected void start() {
this.mainFrame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
TrayIcon trayIcon = null;
if (SystemTray.isSupported()) {
// get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray();
// load an image
Image image = ((ImageIcon) IconFactory.getIconBig("report-80x80.png")).getImage();
trayIcon = new TrayIcon(image, "Time Reporting", new TrayPopupMenu());
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
} else {
JOptionPane.showMessageDialog(null, "Tray Icon Not Supported", "Tray Icon Not Supported", JOptionPane.WARNING_MESSAGE);
}
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
boolean hasLafSpecified = false;
try {
hasLafSpecified = (System.getProperty("swing.defaultlaf") != null);
} catch (Throwable t) {
// JNLP sandbox
}
try {
if (!hasLafSpecified) {
LookAndFeel laf = new SubstanceGeminiLookAndFeel();
UIManager.setLookAndFeel(laf);
}
} catch (Exception e) {
e.printStackTrace();
}
SubstanceLookAndFeel.setToUseConstantThemesOnDialogs(true);
UIManager.put(SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY,Boolean.TRUE);
UIManager.put(SubstanceLookAndFeel.SHOW_EXTRA_WIDGETS,Boolean.TRUE);
JFrame.setDefaultLookAndFeelDecorated(false);
JDialog.setDefaultLookAndFeelDecorated(false);
try {
UIManager.setLookAndFeel(new SubstanceGraphiteAquaLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
new TReps().start();
}
});
}
public static List<Project> getProjects() {
return projects;
}
public static void setSelectedProject(Project selectedProject) {
TReps.selectedProject = selectedProject;
}
public static Project getSelectedProject() {
return selectedProject;
}
public static void setSelectedTask(Task selectedTask) {
TReps.selectedTask = selectedTask;
}
public static Task getSelectedTask() {
return selectedTask;
}
}