KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > javasupport > bsf > BSFExample


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
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
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 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;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.FlowLayout JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JFrame JavaDoc;
39 import javax.swing.JMenuBar JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JTextArea JavaDoc;
43
44 import org.apache.bsf.BSFException;
45 import org.apache.bsf.BSFManager;
46
47 /** An example demonstrating the use of JRuby and BSF.
48  *
49  */

50 public class BSFExample {
51     private BSFManager manager;
52
53     public static void main(String JavaDoc[] args) {
54         /*
55          * First we need to register the JRuby engine.
56          */

57         BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String JavaDoc[] { "rb" });
58
59         /*
60          * Now we create a new BSFManager.
61          */

62         new BSFExample(new BSFManager());
63     }
64
65     public BSFExample(BSFManager manager) {
66         this.manager = manager;
67         
68         /*
69          * Initialize a simple Frame.
70          */

71         initUI();
72     }
73
74     private void initUI() {
75         /*
76          * Initialize some components.
77          */

78         final JFrame JavaDoc frame = new JFrame JavaDoc("A sample BSF application");
79         final JMenuBar JavaDoc menubar = new JMenuBar JavaDoc();
80         final JTextArea JavaDoc input = new JTextArea JavaDoc("$frame.setTitle(\"A new title\")");
81         final JButton JavaDoc execute = new JButton JavaDoc("Execute");
82         final JButton JavaDoc eval = new JButton JavaDoc("Eval");
83
84         try {
85             /*
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, ...)
89              */

90             manager.declareBean("frame", frame, JFrame JavaDoc.class);
91             manager.declareBean("menubar", menubar, JMenuBar JavaDoc.class);
92             manager.declareBean("input", input, JTextArea JavaDoc.class);
93             manager.declareBean("execute", execute, JButton JavaDoc.class);
94             manager.declareBean("eval", eval, JButton JavaDoc.class);
95         } catch (BSFException excptn) {
96             excptn.printStackTrace();
97             JOptionPane.showMessageDialog(null, excptn.getMessage());
98         }
99
100         frame.getContentPane().setLayout(new BorderLayout JavaDoc(12, 12));
101         frame.getContentPane().add(input, BorderLayout.CENTER);
102         
103         JPanel JavaDoc buttonPane = new JPanel JavaDoc(new FlowLayout JavaDoc(12));
104         frame.getContentPane().add(buttonPane, BorderLayout.SOUTH);
105         buttonPane.add(execute, BorderLayout.EAST);
106         buttonPane.add(eval, BorderLayout.EAST);
107
108         try {
109             /*
110              * Execute a Ruby script (add the menubar to the frame).
111              */

112             manager.exec("ruby", "initUI", 1, 1, "$frame.setJMenuBar($menubar)");
113         } catch (BSFException excptn) {
114             excptn.printStackTrace();
115             JOptionPane.showMessageDialog(null, excptn.getMessage());
116         }
117
118         execute.addActionListener(new ActionListener JavaDoc() {
119             public void actionPerformed(ActionEvent JavaDoc e) {
120                 try {
121                     /*
122                      * Execute Ruby statements.
123                      */

124                     manager.exec("ruby", "initUI", 1, 1, input.getText());
125                 } catch (BSFException excptn) {
126                     excptn.printStackTrace();
127                     JOptionPane.showMessageDialog(frame, excptn.getMessage());
128                 }
129             }
130         });
131         
132         eval.addActionListener(new ActionListener JavaDoc() {
133             public void actionPerformed(ActionEvent JavaDoc e) {
134                 try {
135                     /*
136                      * Evaluates a Ruby expression and display the result.
137                      */

138                     String JavaDoc 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());
143                 }
144             }
145         });
146
147         frame.pack();
148         frame.setVisible(true);
149         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
150     }
151 }
152
Popular Tags