BSFExample.java

  1. /***** BEGIN LICENSE BLOCK *****
  2.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Eclipse Public
  5.  * License Version 1.0 (the "License"); you may not use this file
  6.  * except in compliance with the License. You may obtain a copy of
  7.  * the License at http://www.eclipse.org/legal/epl-v10.html
  8.  *
  9.  * Software distributed under the License is distributed on an "AS
  10.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11.  * implied. See the License for the specific language governing
  12.  * rights and limitations under the License.
  13.  *
  14.  * Copyright (C) 2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
  15.  * Copyright (C) 2003-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
  16.  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
  17.  *
  18.  * Alternatively, the contents of this file may be used under the terms of
  19.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  20.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  21.  * in which case the provisions of the GPL or the LGPL are applicable instead
  22.  * of those above. If you wish to allow use of your version of this file only
  23.  * under the terms of either the GPL or the LGPL, and not to allow others to
  24.  * use your version of this file under the terms of the EPL, indicate your
  25.  * decision by deleting the provisions above and replace them with the notice
  26.  * and other provisions required by the GPL or the LGPL. If you do not delete
  27.  * the provisions above, a recipient may use your version of this file under
  28.  * the terms of any one of the EPL, the GPL or the LGPL.
  29.  ***** END LICENSE BLOCK *****/
  30. package org.jruby.javasupport.bsf;

  31. import java.awt.BorderLayout;
  32. import java.awt.FlowLayout;
  33. import java.awt.event.ActionEvent;
  34. import java.awt.event.ActionListener;

  35. import javax.swing.JButton;
  36. import javax.swing.JFrame;
  37. import javax.swing.JMenuBar;
  38. import javax.swing.JOptionPane;
  39. import javax.swing.JPanel;
  40. import javax.swing.JTextArea;

  41. import org.apache.bsf.BSFException;
  42. import org.apache.bsf.BSFManager;

  43. /** An example demonstrating the use of JRuby and BSF.
  44.  *
  45.  */
  46. public class BSFExample {
  47.     private BSFManager manager;

  48.     public static void main(String[] args) {
  49.         /*
  50.          * First we need to register the JRuby engine.
  51.          */
  52.         BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String[] { "rb" });

  53.         /*
  54.          * Now we create a new BSFManager.
  55.          */
  56.         new BSFExample(new BSFManager());
  57.     }

  58.     public BSFExample(BSFManager manager) {
  59.         this.manager = manager;
  60.        
  61.         /*
  62.          * Initialize a simple Frame.
  63.          */
  64.         initUI();
  65.     }

  66.     private void initUI() {
  67.         /*
  68.          * Initialize some components.
  69.          */
  70.         final JFrame frame = new JFrame("A sample BSF application");
  71.         final JMenuBar menubar = new JMenuBar();
  72.         final JTextArea input = new JTextArea("$frame.setTitle(\"A new title\")");
  73.         final JButton execute = new JButton("Execute");
  74.         final JButton eval = new JButton("Eval");

  75.         try {
  76.             /*
  77.              * Declare those components as beans in BSF. Then it will be
  78.              * possible to access those components in Ruby as global
  79.              * variables ($frame, $menubar, ...)
  80.              */
  81.             manager.declareBean("frame", frame, JFrame.class);
  82.             manager.declareBean("menubar", menubar, JMenuBar.class);
  83.             manager.declareBean("input", input, JTextArea.class);
  84.             manager.declareBean("execute", execute, JButton.class);
  85.             manager.declareBean("eval", eval, JButton.class);
  86.         } catch (BSFException excptn) {
  87.             excptn.printStackTrace();
  88.             JOptionPane.showMessageDialog(null, excptn.getMessage());
  89.         }

  90.         frame.getContentPane().setLayout(new BorderLayout(12, 12));
  91.         frame.getContentPane().add(input, BorderLayout.CENTER);
  92.        
  93.         JPanel buttonPane = new JPanel(new FlowLayout(12));
  94.         frame.getContentPane().add(buttonPane, BorderLayout.SOUTH);
  95.         buttonPane.add(execute, BorderLayout.EAST);
  96.         buttonPane.add(eval, BorderLayout.EAST);

  97.         try {
  98.             /*
  99.              * Execute a Ruby script (add the menubar to the frame).
  100.              */
  101.             manager.exec("ruby", "initUI", 1, 1, "$frame.setJMenuBar($menubar)");
  102.         } catch (BSFException excptn) {
  103.             excptn.printStackTrace();
  104.             JOptionPane.showMessageDialog(null, excptn.getMessage());
  105.         }

  106.         execute.addActionListener(new ActionListener() {
  107.             public void actionPerformed(ActionEvent e) {
  108.                 try {
  109.                     /*
  110.                      * Execute Ruby statements.
  111.                      */
  112.                     manager.exec("ruby", "initUI", 1, 1, input.getText());
  113.                 } catch (BSFException excptn) {
  114.                     excptn.printStackTrace();
  115.                     JOptionPane.showMessageDialog(frame, excptn.getMessage());
  116.                 }
  117.             }
  118.         });
  119.        
  120.         eval.addActionListener(new ActionListener() {
  121.             public void actionPerformed(ActionEvent e) {
  122.                 try {
  123.                     /*
  124.                      * Evaluates a Ruby expression and display the result.
  125.                      */
  126.                     String expression = JOptionPane.showInputDialog(frame, "Please enter a Ruby expression:");
  127.                     input.setText(String.valueOf(manager.eval("ruby", "initUI", 1, 1, expression)));
  128.                 } catch (BSFException excptn) {
  129.                     excptn.printStackTrace();
  130.                     JOptionPane.showMessageDialog(frame, excptn.getMessage());
  131.                 }
  132.             }
  133.         });

  134.         frame.pack();
  135.         frame.setVisible(true);
  136.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  137.     }
  138. }