|
import javax.swing.* |
|
import java.awt.* |
|
import java.nio.file.Path |
|
import java.nio.file.Paths |
|
import java.util.List |
|
|
|
import static javax.swing.JOptionPane.* |
|
|
|
/* |
|
Display a checklist selected from a folder and write the user selection in a log file |
|
*/ |
|
|
|
|
|
//---------- SCRIPT ------------- |
|
|
|
def readFolder = "../../../checklist/" |
|
def saveFolder = "../../../target" |
|
|
|
def checkListViewer = new CheckListViewer(checkListFolder: readFolder, logFolder: saveFolder) |
|
checkListViewer.askUserToSelectACheckList() |
|
|
|
//---------- CLASSES ------------- |
|
|
|
class Task { |
|
private String name |
|
private boolean done |
|
|
|
@Override |
|
String toString() { |
|
return "[${done ? "X" : " "}] $name" |
|
} |
|
} |
|
|
|
class TaskCheckBox { |
|
final JCheckBox checkBox |
|
|
|
TaskCheckBox(Task task) { |
|
this.checkBox = new JCheckBox(task.name) |
|
this.checkBox.name = task.name |
|
this.checkBox.selected = task.done |
|
} |
|
|
|
Task toTask() { |
|
new Task( |
|
name: checkBox.name, |
|
done: checkBox.selected |
|
) |
|
} |
|
} |
|
|
|
class CheckList { |
|
private String name |
|
private List<Task> tasks |
|
|
|
static CheckList whiteList(String name, List<String> taskNames) { |
|
new CheckList(name: name, tasks: taskNames.collect { new Task(name: it, false) }) |
|
} |
|
|
|
static CheckList fromFile(Path path) { |
|
def lines = path.readLines() |
|
def name = lines[0] - ~/\s*#*\s*/ |
|
|
|
def pattern = /(\[[\s|X]\]) (.*)/ |
|
|
|
def tasks = lines.subList(1, lines.size()).collect { |
|
it - ~/^\s*-\s/ |
|
}.findAll { |
|
(it =~ pattern).matches() |
|
}.collect { |
|
def m = it =~ pattern |
|
def done = m[0][1] == "[X]" |
|
def taskName = m[0][2] |
|
new Task(name: taskName, done: done) |
|
} |
|
|
|
new CheckList(name: name, tasks: tasks) |
|
} |
|
|
|
CheckListPanel panel() { |
|
List<TaskCheckBox> checkBoxes = tasks.collect { |
|
new TaskCheckBox(it) |
|
} |
|
new CheckListPanel(name: name, checkBoxes: checkBoxes) |
|
} |
|
|
|
@Override |
|
String toString() { |
|
return "$name:\n${tasks.join("\n")}" |
|
} |
|
|
|
void save(String path) { |
|
def timestamp = new Date().format("yyyy-MM-dd_HH-mm-ss") |
|
def fileName = "$path/${name.replaceAll(" ", "_")}-[${timestamp}].checklist" |
|
File dest = new File(fileName) |
|
dest.createNewFile() |
|
dest.setText(this.toString()) |
|
} |
|
} |
|
|
|
class CheckListPanel { |
|
private String name |
|
private List<TaskCheckBox> checkBoxes |
|
|
|
JPanel panel() { |
|
JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5)) |
|
checkBoxes.each { |
|
panel.add(it.checkBox) |
|
} |
|
return panel |
|
} |
|
|
|
CheckList checkList() { |
|
new CheckList( |
|
name: name, |
|
tasks: checkBoxes.collect { it.toTask() } |
|
) |
|
} |
|
|
|
} |
|
|
|
class CheckListDialog { |
|
|
|
private CheckList checkList |
|
|
|
void askUserForValidation(String dest) { |
|
CheckListPanel checkListPanel = this.checkList.panel() |
|
if (OK_OPTION == showDialog(this.checkList.name, checkListPanel.panel())) { |
|
def result = checkListPanel.checkList() |
|
result.save(dest) |
|
} |
|
} |
|
|
|
private showDialog(String name, JPanel panel) { |
|
showConfirmDialog( |
|
null, |
|
panel, |
|
name, |
|
OK_CANCEL_OPTION, |
|
QUESTION_MESSAGE, |
|
new ImageIcon()) |
|
} |
|
} |
|
|
|
class CheckListViewer { |
|
String checkListFolder |
|
String logFolder |
|
|
|
void askUserToSelectACheckList(){ |
|
String[] paths = checkListFiles() |
|
String file = askUserToSelectAFile(paths) |
|
if (file != null) { |
|
checkListDialog(file).askUserForValidation(logFolder) |
|
} |
|
} |
|
|
|
private checkListFiles() { |
|
new File(checkListFolder).listFiles().collect { it.name } |
|
} |
|
|
|
private checkListDialog(String path) { |
|
new CheckListDialog( |
|
checkList: CheckList.fromFile(Paths.get("$checkListFolder$path")) |
|
) |
|
} |
|
|
|
private askUserToSelectAFile(String[] paths) { |
|
(String) JOptionPane.showInputDialog(null, "Select a checklist", |
|
"Checklist viewer", |
|
QUESTION_MESSAGE, |
|
new ImageIcon(), |
|
paths, |
|
paths[0]) |
|
} |
|
} |
|
|