2009-12-26 14:49:46 -06:00

106 lines
3.3 KiB
Groovy

package com.jdbernard.timestamper
import com.jdbernard.timestamper.core.TimelineMarker
import com.jdbernard.timestamper.core.TimelineProperties
class TimeStamperMainController {
// these will be injected by Griffon
def model
def view
void mvcGroupInit(Map args) {
logger.traceIfEnabled("Initializing TimeStamperMain MVC...")
def thisMVC = ['model': model, 'view': view, 'controller': this]
model.notesDialogMVC = buildMVCGroup('NotesDialog', 'notesDialog',
'mainMVC': thisMVC)
model.punchcardDialogMVC = buildMVCGroup('PunchcardDialog',
'punchcardDialog', 'mainMVC': thisMVC)
// load application properties
Properties prop = new Properties()
String userHomeDir = System.getProperty('user.home')
model.configFile = new File(userHomeDir, ".timestamperrc")
if (!model.configFile.exists()) model.configFile.createNewFile()
logger.traceIfEnabled("Reading configuration from "
+ "'${model.configFile.name}'")
try { model.configFile.withInputStream { prop.load(it) } }
catch (IOException ioe) {
logger.error('Unable to load configuration', ioe)
}
model.config = prop
// load the last used timeline file
String lastUsed = model.config.getProperty('lastUsed', null)
if (lastUsed == null) {
lastUsed = 'timeline.default.properties'
model.config.setProperty('lastUsed', lastUsed)
}
logger.traceIfEnabled("Reading Timeline properties from '${lastUsed}'")
File propertyFile = new File(lastUsed)
if (!propertyFile.exists()) propertyFile.createNewFile()
load(propertyFile)
}
def load = { File propertiesFile ->
try {
model.config.setProperty('lastUsed', propertiesFile.canonicalPath)
} catch (IOException ioe) { logger.error(ioe) }
// load the properties file
model.timelineProperties = new TimelineProperties(propertiesFile)
// load the main timeline
model.timeline = model.timelineProperties.timeline
// load the last marker
model.currentMarker = model.timeline.getLastMarker(new Date())
}
def saveas = {
}
def exitGracefully = { evt = null ->
logger.traceIfEnabled("Exiting gracefully.")
// save config
logger.debugIfEnabled("Config: ${model.config}")
logger.debugIfEnabled("Storing config to file: ${model.configFile.path}")
try { model.configFile.withOutputStream { out ->
model.config.store(out, null) } }
catch (IOException ioe) {
logger.error("Unable to save the configuration file", ioe)
}
// save timeline and properties
model.timelineProperties.save()
logger.traceIfEnabled("Completed graceful shutdown.")
app.shutdown()
}
def newTask = { mark, notes = "No comments.", timestamp = new Date() ->
TimelineMarker tm = new TimelineMarker(timestamp, mark, notes)
model.timeline.addMarker(tm)
model.currentMarker = model.timeline.getLastMarker(new Date())
}
def deleteTask = { marker ->
model.timeline.removeMarker(marker)
model.currentMarker = model.timeline.getLastMarker(new Date())
}
}