SwingBuilder example: timesheet.groovy
import java.util.*
import groovy.swing.SwingBuilder
import javax.swing.*
import java.awt.*
import java.awt.event.*
import groovy.sql.Sql
sql = Sql.newInstance("jdbc:oracle:thin:@atlantis:1521:dev", "scott", "tiger", "oracle.jdbc.driver.OracleDriver")
def names() {
names = []
sql.eachRow("select employee_name from employees order by employee_name") { row |
names.add(row.employee_name)
}
names
}
class Project {
months = new TreeSet()
name
Project(name) { this.name = name }
addMonth(month) { months.add(month) }
String toString() { name }
}
def projects(name) {
nameToProjectMap = new TreeMap()
sql.eachRow("select project_name, to_char(ts_date, 'YYYY-MM') month from timesheets where employee_name = '"+name+"'") { row |
if (!nameToProjectMap.containsKey(row.project_name)) {
nameToProjectMap[row.project_name] = new Project(row.project_name)
}
nameToProjectMap[row.project_name].addMonth(row.month)
}
new Vector(nameToProjectMap.values())
}
def startDate(name) {
startDate = null
sql.eachRow("select to_char(min(ts_date), 'YYYY-MM-DD') start_date from timesheets where employee_name = '"+name+"'") { row |
startDate = row.start_date
}
startDate
}
components = [:]
def text(name) {
monthsInSelectedProjects = new TreeSet()
components["list"].selectedValues.each { monthsInSelectedProjects.addAll(it.months) }
"Timesheet Start Date: " + startDate(name) + "\n" +
"Months In Selected Projects: " + monthsInSelectedProjects.size() + " (months with multiple projects are counted once)"
}
def updateTextArea() {
components["textArea"].setText(text(components["comboBox"].selectedItem))
}
frame = new SwingBuilder().frame(title:"Timesheet Analyzer", windowClosing:{ sql.close(); System.exit(0) }) {
panel(layout:new BorderLayout()) {
components["comboBox"] = comboBox(items:names(), constraints:BorderLayout.NORTH, actionPerformed:{
components["list"].setListData(projects(components["comboBox"].selectedItem))
updateTextArea()
})
scrollPane(constraints:BorderLayout.CENTER) {
components["list"] = list(valueChanged:{ updateTextArea() })
}
scrollPane(preferredSize:[400, 100], constraints:BorderLayout.SOUTH) {
components["textArea"] = textArea()
}
}
}
frame.pack()
frame.show()
components["comboBox"].selectedItem = "Jonathan Aquino"
import groovy.swing.SwingBuilder
import javax.swing.*
import java.awt.*
import java.awt.event.*
import groovy.sql.Sql
sql = Sql.newInstance("jdbc:oracle:thin:@atlantis:1521:dev", "scott", "tiger", "oracle.jdbc.driver.OracleDriver")
def names() {
names = []
sql.eachRow("select employee_name from employees order by employee_name") { row |
names.add(row.employee_name)
}
names
}
class Project {
months = new TreeSet()
name
Project(name) { this.name = name }
addMonth(month) { months.add(month) }
String toString() { name }
}
def projects(name) {
nameToProjectMap = new TreeMap()
sql.eachRow("select project_name, to_char(ts_date, 'YYYY-MM') month from timesheets where employee_name = '"+name+"'") { row |
if (!nameToProjectMap.containsKey(row.project_name)) {
nameToProjectMap[row.project_name] = new Project(row.project_name)
}
nameToProjectMap[row.project_name].addMonth(row.month)
}
new Vector(nameToProjectMap.values())
}
def startDate(name) {
startDate = null
sql.eachRow("select to_char(min(ts_date), 'YYYY-MM-DD') start_date from timesheets where employee_name = '"+name+"'") { row |
startDate = row.start_date
}
startDate
}
components = [:]
def text(name) {
monthsInSelectedProjects = new TreeSet()
components["list"].selectedValues.each { monthsInSelectedProjects.addAll(it.months) }
"Timesheet Start Date: " + startDate(name) + "\n" +
"Months In Selected Projects: " + monthsInSelectedProjects.size() + " (months with multiple projects are counted once)"
}
def updateTextArea() {
components["textArea"].setText(text(components["comboBox"].selectedItem))
}
frame = new SwingBuilder().frame(title:"Timesheet Analyzer", windowClosing:{ sql.close(); System.exit(0) }) {
panel(layout:new BorderLayout()) {
components["comboBox"] = comboBox(items:names(), constraints:BorderLayout.NORTH, actionPerformed:{
components["list"].setListData(projects(components["comboBox"].selectedItem))
updateTextArea()
})
scrollPane(constraints:BorderLayout.CENTER) {
components["list"] = list(valueChanged:{ updateTextArea() })
}
scrollPane(preferredSize:[400, 100], constraints:BorderLayout.SOUTH) {
components["textArea"] = textArea()
}
}
}
frame.pack()
frame.show()
components["comboBox"].selectedItem = "Jonathan Aquino"
0 Comments:
Post a Comment
<< Home