1 30 package org.jruby.javasupport.bsf; 31 32 import java.awt.BorderLayout ; 33 import java.awt.FlowLayout ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 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 ; 43 44 import org.apache.bsf.BSFException; 45 import org.apache.bsf.BSFManager; 46 47 50 public class BSFExample { 51 private BSFManager manager; 52 53 public static void main(String [] args) { 54 57 BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String [] { "rb" }); 58 59 62 new BSFExample(new BSFManager()); 63 } 64 65 public BSFExample(BSFManager manager) { 66 this.manager = manager; 67 68 71 initUI(); 72 } 73 74 private void initUI() { 75 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"); 83 84 try { 85 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()); 98 } 99 100 frame.getContentPane().setLayout(new BorderLayout (12, 12)); 101 frame.getContentPane().add(input, BorderLayout.CENTER); 102 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); 107 108 try { 109 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 () { 119 public void actionPerformed(ActionEvent e) { 120 try { 121 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 () { 133 public void actionPerformed(ActionEvent e) { 134 try { 135 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()); 143 } 144 } 145 }); 146 147 frame.pack(); 148 frame.setVisible(true); 149 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 150 } 151 } 152 | Popular Tags |