001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.bsh;
020
021import bsh.Interpreter;
022import org.jdom2.Element;
023import org.jpos.ui.UI;
024
025import javax.swing.*;
026
027/**
028 * UI subclass that runs the {@code <bsh>} content of a UI configuration through
029 * a BeanShell interpreter to produce a Swing component.
030 *
031 * @author Alejandro Revilla
032 */
033public class BSHUI extends UI {
034    /** Default constructor; no instance state to initialise. */
035    public BSHUI() {}
036    protected JComponent doScript (JComponent component, Element e) {
037        try {
038            Interpreter bsh = new Interpreter ();
039            bsh.set ("component", component);
040            bsh.set ("config", e);
041            bsh.set ("log", getLog());
042            bsh.set ("ui", this);
043            bsh.eval ("import java.awt.*;");
044            bsh.eval ("import java.awt.event.*;");
045            bsh.eval ("import javax.swing.*;");
046            bsh.eval ("import org.jpos.ui.*;");
047            bsh.eval (e.getText ());
048            return (JComponent) bsh.get ("component");
049        } catch (Exception ex) {
050            warn (ex);
051            return new JLabel (ex.toString ());
052        }
053    }
054}
055