timestamper/src/jdbernard/timestamper/TimeStamperApp.java
Jonathan Bernard e69faa148f Version 1.7 - Added 30-minute auto save
committer: Jonathan Bernard <jdbernard@gmail.com>
2008-11-11 19:29:58 -06:00

184 lines
5.3 KiB
Java
Executable File

/*
* TimeStamperApp.java
*/
package jdbernard.timestamper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.EventObject;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
/**
* The main class of the application.
*/
public class TimeStamperApp extends SingleFrameApplication
implements Application.ExitListener {
private Timeline activeTimeline;
private String currentTimelineFile;
private Logger log;
private Properties config;
public TimeStamperApp() {
super();
// set defaults for fields
activeTimeline = new Timeline();
// set up logger
log = Logger.getLogger("jdbernard.timestamper");
log.setLevel(Level.ALL);
// add console handler
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(Level.INFO);
log.addHandler(ch);
// try to add file handler
try {
FileHandler fh = new FileHandler("TimeStamper.log", true);
fh.setFormatter(new SimpleFormatter());
fh.setLevel(Level.ALL);
log.addHandler(fh);
} catch (IOException ioe) {
log.warning("Could not open log file for writing. Switching console"
+ " logging to verbose.");
ch.setLevel(Level.ALL);
}
// load configuration
try {
config = new Properties();
File cfgFile = new File("timestamper.config");
if (!cfgFile.exists()) cfgFile.createNewFile();
FileInputStream cfgIn = new FileInputStream(cfgFile);
config.load(cfgIn);
cfgIn.close();
} catch (IOException ioe) {
log.warning("Could not load configuration options.");
}
// load the last used timeline
loadTimeline(config.getProperty("lastUsedTimelineFilename"));
}
/**
*
*/
@Override protected void initialize(String[] args) {
if (args.length > 0) {
File inFile = new File(args[0]);
if (!inFile.exists())
try { inFile.createNewFile(); }
catch (IOException ioe) {
log.warning("No file '" + args[0] + " exists and an error"
+ " occurred trying to create it.");
}
currentTimelineFile = args[0];
loadTimeline(currentTimelineFile);
}
}
/**
* At startup create and show the main frame of the application.
*/
@Override protected void startup() {
show(new TimeStamperView(this));
getMainFrame().setSize(325, 60);
getApplication().addExitListener(this);
}
/**
* This method is to initialize the specified window by injecting resources.
* Windows shown in our application come fully initialized from the GUI
* builder, so this additional configuration is not needed.
*/
@Override protected void configureWindow(java.awt.Window root) {
}
/**
* A convenient static getter for the application instance.
* @return the instance of TimeStamperApp
*/
public static TimeStamperApp getApplication() {
return Application.getInstance(TimeStamperApp.class);
}
/**
* Main method launching the application.
*/
public static void main(String[] args) {
launch(TimeStamperApp.class, args);
}
public void saveTimeline() {
saveTimelineToFile(currentTimelineFile);
}
public void saveTimelineToFile(String filename) {
if (filename == null || filename.equals(""))
filename = "default-timeline.txt";
try {
Timeline.writeToFile(filename, activeTimeline);
currentTimelineFile = filename;
} catch (IOException ioe) {
log.warning("Could not save timeline file: "
+ ioe.getLocalizedMessage());
}
}
public void loadTimeline(String filename) {
if (filename == null || filename.equals("")) {
activeTimeline = new Timeline();
return;
}
try {
activeTimeline = Timeline.readFromFile(filename);
currentTimelineFile = filename;
} catch (IOException ioe) {
log.warning("Could not load from the file: " +
ioe.getLocalizedMessage());
}
}
public Timeline getActiveTimeline() {
return activeTimeline;
}
@Override
public void willExit(EventObject e) {
saveTimeline();
config.setProperty("lastUsedTimelineFilename", currentTimelineFile);
try {
FileOutputStream out = new FileOutputStream("timestamper.config");
config.store(out, "");
out.close();
} catch (IOException ioe) {
log.warning("Could not save config file.");
}
for (Handler h : log.getHandlers())
h.close();
}
@Override
public boolean canExit(EventObject e) { return true; }
}