Skip to content

Instantly share code, notes, and snippets.

@andy1138
Created April 14, 2013 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy1138/5382910 to your computer and use it in GitHub Desktop.
Save andy1138/5382910 to your computer and use it in GitHub Desktop.
package org.lsug.csvswing
import swing._
import Swing._
import scala.swing.event.ButtonClicked
import java.io.File
import scala.util.{Failure, Success, Try}
import javax.swing.UIManager
object CSVSwing extends SimpleSwingApplication {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName)
val results = new TextArea("")
def top = new MainFrame {
title = "CSV Validator GUI"
contents = new BorderPanel {
import BorderPanel.Position._
val filePanel:BoxPanel = new BoxPanel(Orientation.Vertical) {
border = Swing.EmptyBorder(5,10,5,10)
val schemaFilePath = new TextField("/Users/andy/Projects/csv-validator/csv-validator-core/src/test/resources/uk/gov/tna/dri/validator/schema.txt")
val schemaFile = new BorderPanel {
layout(new Label("Schema File")) = West
layout(schemaFilePath) = Center
layout(new Button("...") {
reactions += {
case ButtonClicked(_) => {
new FileChooser(new File(".")) {
title = "Schema File"
selectedFile = new File(schemaFilePath.text)
showOpenDialog(filePanel) match {
case FileChooser.Result.Approve => schemaFilePath.text = selectedFile.getAbsolutePath
case FileChooser.Result.Cancel => println("cancel")
}
}
}
}
}
) = East
}
contents += schemaFile
val csvFilePath = new TextField("/Users/andy/Projects/csv-validator/csv-validator-core/src/test/resources/uk/gov/tna/dri/validator/metaData.csv")
val csvFile = new BorderPanel {
layout(new Label("CSV File")) = West
layout(csvFilePath) = Center
layout(new Button("...") {
reactions += {
case ButtonClicked(_) => {
new FileChooser(new File(".")) {
title = "CSV File"
selectedFile = new File(csvFilePath.text)
showOpenDialog(filePanel) match {
case FileChooser.Result.Approve => csvFilePath.text = selectedFile.getAbsolutePath
case FileChooser.Result.Cancel => println("cancel")
}
}
}
}
}
) = East
}
contents += csvFile
val runPanel = new BorderPanel {
layout( new Button("Validate") {
reactions += {
case ButtonClicked(_) => executeCSV(schemaFilePath.text, csvFilePath.text)
}
} ) = East
}
contents += runPanel
}
layout(filePanel) = North
layout(new BorderPanel {
border = Swing.EmptyBorder(5,10,5,10)
layout(results) = Center
}) = Center
val cmdLine = new FlowPanel {
contents += new Button("Save")
contents += new Button("Close") {
reactions += {
case ButtonClicked(_) => System.exit(0)
}
}
}
layout(new BorderPanel {
layout(cmdLine) = East
}) = South
}
location = (100, 100)
size = (800,500)
}
def executeCSV(schema: String, csv: String) {
val pb = new ProcessBuilder("""/Users/andy/Projects/csv-validator/csv-validator-core/validate""", csv, schema)
pb.directory( new File("/Users/andy/Projects/csv-validator/csv-validator-core/"))
val pt = Try{ pb.start}
pt match {
case Success(p) =>
p.waitFor
val out = p.getInputStream
val buff = Array[Byte](100)
var x = out.read(buff)
while( x != -1) {
results.text += new String(buff)
x = out.read(buff)
}
case Failure(e) => results.text += "Error: " + e
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment