/*
* File: test.java
* Author: Daniel Rogers
* Created on Oct 19, 2007
*
*/
package gri.tasks.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import gri.tasks.ParameterDef;
import gri.tasks.TaskDef;
import gri.tasks.Task;
import gri.tasks.managers.TaskEntry;
import gri.tasks.java.xml.FunctionXMLParser;
import gri.tasks.java.xml.FunctionEntry;
import gri.tasks.gui.widgets.InputParameterWidgetFactory;
import gri.tasks.gui.widgets.OutputParameterWidgetFactory;
import java.util.List;
import java.util.Map;
/**
* Performs a simple test using a TaskEntry loaded from a file.
* An input panel for the task is displayed. Upon submission the
* job will be executed and the outputs displayed in an output
* panel. The job is executed synchronously without using a
* JobManager.
*
* @author rogersda
*/
public class test extends JPanel implements ActionListener {
Task task;
ParameterPanel pnlTask;
// --------------------------------------------------- Constructors
public test() throws Exception {
super(new BorderLayout());
//String fileName = "config.xml";
String fileName = "incava-diff.xml";
FunctionXMLParser parser = new FunctionXMLParser();
List functions = parser.parseFunctionEntries(new java.io.File(fileName));
FunctionEntry function = (FunctionEntry)functions.get(2);
TaskEntry taskEntry = function.createTaskEntry();
this.task = taskEntry.getTask();
TaskDef taskDef = task.getTaskDef();
this.pnlTask = new ParameterPanel(taskDef.getInputsArray(), new InputParameterWidgetFactory());
this.add(pnlTask, BorderLayout.CENTER);
JButton btnSubmit = new JButton("Run");
btnSubmit.addActionListener(this);
this.add(btnSubmit, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
//get/validate inputs:
Map inputs = this.pnlTask.getValues();
TaskDef taskDef = task.getTaskDef();
ParameterDef [] inputDefs = taskDef.getInputsArray();
for (int i=0; i<inputDefs.length; i++) {
String paramName = inputDefs[i].getName();
if (inputDefs[i].isRequired() && inputs.get(paramName) == null) {
error("Required parameter not entered: " + paramName);
return;
}
}
Map outputs = null;
//execute:
try {
outputs = task.execute(inputs);
}
catch(Exception ex) {
ex.printStackTrace();
return;
}
//display outputs:
ParameterDef [] outputDefs = taskDef.getOutputsArray();
for (int i=0; i<outputDefs.length; i++) {
String name = outputDefs[i].getName();
Object value = outputs.get(name);
System.out.println(name + " = " + value);
}
ParameterPanel panel = new ParameterPanel(outputDefs, new OutputParameterWidgetFactory());
panel.setValues(outputs);
JFrame frame = new JFrame("Outputs");
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
// ------------------------------------------------------ Execution
public void showAsWindow(String title) {
JFrame frame = new JFrame(title);
frame.getContentPane().add(this, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String [] args) {
try {
test t = new test();
t.showAsWindow("Test");
}
catch(Exception e) {
e.printStackTrace();
}
}
protected void error(String msg) {
JOptionPane.showMessageDialog(this, msg, "ERROR", JOptionPane.ERROR_MESSAGE);
}
}