1 /***** BEGIN LICENSE BLOCK *****
2 * Version: CPL 1.0/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Common 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/cpl-v10.html
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.
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>
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 CPL, 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 CPL, the GPL or the LGPL.
29 ***** END LICENSE BLOCK *****/
30 package org
.jruby
.javasupport
.bsf
;
32 import java
.awt
.BorderLayout
;
33 import java
.awt
.FlowLayout
;
34 import java
.awt
.event
.ActionEvent
;
35 import java
.awt
.event
.ActionListener
;
37 import javax
.swing
.JButton
;
38 import javax
.swing
.JFrame
;
39 import javax
.swing
.JMenuBar
;
40 import javax
.swing
.JOptionPane
;
41 import javax
.swing
.JPanel
;
42 import javax
.swing
.JTextArea
;
44 import org
.apache
.bsf
.BSFException
;
45 import org
.apache
.bsf
.BSFManager
;
47 /** An example demonstrating the use of JRuby and BSF.
50 public class BSFExample
{
51 private BSFManager manager
;
53 public static void main(String
[] args
) {
55 * First we need to register the JRuby engine.
57 BSFManager
.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String
[] { "rb" });
60 * Now we create a new BSFManager.
62 new BSFExample(new BSFManager());
65 public BSFExample(BSFManager manager
) {
66 this.manager
= manager
;
69 * Initialize a simple Frame.
74 private void initUI() {
76 * Initialize some components.
78 final JFrame frame
= new JFrame("A sample BSF application");
79 final JMenuBar menubar
= new JMenuBar();
80 final JTextArea input
= new JTextArea("$frame.setTitle(\"A new title\")");
81 final JButton execute
= new JButton("Execute");
82 final JButton eval
= new JButton("Eval");
86 * Declare those components as beans in BSF. Then it will be
87 * possible to access those components in Ruby as global
88 * variables ($frame, $menubar, ...)
90 manager
.declareBean("frame", frame
, JFrame
.class);
91 manager
.declareBean("menubar", menubar
, JMenuBar
.class);
92 manager
.declareBean("input", input
, JTextArea
.class);
93 manager
.declareBean("execute", execute
, JButton
.class);
94 manager
.declareBean("eval", eval
, JButton
.class);
95 } catch (BSFException excptn
) {
96 excptn
.printStackTrace();
97 JOptionPane
.showMessageDialog(null, excptn
.getMessage());
100 frame
.getContentPane().setLayout(new BorderLayout(12, 12));
101 frame
.getContentPane().add(input
, BorderLayout
.CENTER
);
103 JPanel buttonPane
= new JPanel(new FlowLayout(12));
104 frame
.getContentPane().add(buttonPane
, BorderLayout
.SOUTH
);
105 buttonPane
.add(execute
, BorderLayout
.EAST
);
106 buttonPane
.add(eval
, BorderLayout
.EAST
);
110 * Execute a Ruby script (add the menubar to the frame).
112 manager
.exec("ruby", "initUI", 1, 1, "$frame.setJMenuBar($menubar)");
113 } catch (BSFException excptn
) {
114 excptn
.printStackTrace();
115 JOptionPane
.showMessageDialog(null, excptn
.getMessage());
118 execute
.addActionListener(new ActionListener() {
119 public void actionPerformed(ActionEvent e
) {
122 * Execute Ruby statements.
124 manager
.exec("ruby", "initUI", 1, 1, input
.getText());
125 } catch (BSFException excptn
) {
126 excptn
.printStackTrace();
127 JOptionPane
.showMessageDialog(frame
, excptn
.getMessage());
132 eval
.addActionListener(new ActionListener() {
133 public void actionPerformed(ActionEvent e
) {
136 * Evaluates a Ruby expression and display the result.
138 String expression
= JOptionPane
.showInputDialog(frame
, "Please enter a Ruby expression:");
139 input
.setText(String
.valueOf(manager
.eval("ruby", "initUI", 1, 1, expression
)));
140 } catch (BSFException excptn
) {
141 excptn
.printStackTrace();
142 JOptionPane
.showMessageDialog(frame
, excptn
.getMessage());
148 frame
.setVisible(true);
149 frame
.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);