package net.sf.jpluck.util.el;
import java.awt.event.ActionEvent;
import javax.imageio.ImageIO;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import net.sf.jpluck.swing.GUIUtil;
import net.sf.jpluck.ui.ExceptionDialog;
import org.apache.commons.el.ExpressionEvaluatorImpl;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.FormLayout;
public class TestingTool extends JFrame {
private JTextField elText;
private JTextField resultText;
private JTextField uriText;
public TestingTool() {
super("EL Testing Tool");
setResizable(false);
try {
setIconImage(ImageIO.read(TestingTool.class.getClassLoader().getResource("net/sf/jpluck/res/jpluck16.gif")));
} catch (Exception e) {
// Should not occur;
}
uriText = new JTextField(50);
elText = new JTextField(50);
resultText = new JTextField(50);
JButton evalButton = new JButton(new Evaluate());
FormLayout formLayout = new FormLayout("pref, 4dlu, fill:pref:grow",
"pref, 4dlu, pref, 4dlu, pref, 7dlu, pref");
PanelBuilder panelBuilder = new PanelBuilder(formLayout);
getRootPane().setDefaultButton(evalButton);
panelBuilder.addLabel("URI", "1,1");
panelBuilder.add(uriText, "3,1");
panelBuilder.addLabel("Expression", "1,3");
panelBuilder.add(elText, "3,3");
panelBuilder.add(evalButton, "3,5,1,1,left,center");
panelBuilder.addLabel("Result", "1,7");
panelBuilder.add(resultText, "3,7");
panelBuilder.setDefaultDialogBorder();
setContentPane(panelBuilder.getPanel());
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
GUIUtil.configureEscapeKey(getRootPane(),
new AbstractAction() {
public void actionPerformed(ActionEvent ev) {
hide();
dispose();
}
});
pack();
}
private class Evaluate extends AbstractAction {
Evaluate() {
putValue(Action.NAME, "Evaluate");
}
public void actionPerformed(ActionEvent ev) {
try {
StaticFunctionMapper functionMapper = StaticFunctionMapper.getDefault();
ExpressionEvaluator eval = new ExpressionEvaluatorImpl();
VariableResolver resolver = new DefaultVariables(uriText.getText(), null, null);
resultText.setText((String) eval.evaluate(elText.getText(), String.class, resolver, functionMapper));
} catch (ELException e) {
ExceptionDialog.show(TestingTool.this, e);
}
}
}
}