KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > demo > IRBConsole


1 package org.jruby.demo;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Font JavaDoc;
6 import java.awt.GraphicsEnvironment JavaDoc;
7 import java.awt.Insets JavaDoc;
8 import java.io.PipedInputStream JavaDoc;
9 import java.io.PrintStream JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Arrays JavaDoc;
12
13 import javax.swing.BorderFactory JavaDoc;
14 import javax.swing.JEditorPane JavaDoc;
15 import javax.swing.JFrame JavaDoc;
16 import javax.swing.JScrollPane JavaDoc;
17 import javax.swing.JTextPane JavaDoc;
18
19 import org.jruby.Ruby;
20 import org.jruby.RubyInstanceConfig;
21 import org.jruby.internal.runtime.ValueAccessor;
22 import org.jruby.javasupport.JavaUtil;
23 import org.jruby.runtime.builtin.IRubyObject;
24
25 public class IRBConsole extends JFrame JavaDoc {
26     public IRBConsole(String JavaDoc title) {
27         super(title);
28     }
29
30     public static void main(String JavaDoc[] args) {
31         final IRBConsole console = new IRBConsole("JRuby IRB Console");
32         final PipedInputStream JavaDoc pipeIn = new PipedInputStream JavaDoc();
33
34         console.getContentPane().setLayout(new BorderLayout JavaDoc());
35         console.setSize(700, 600);
36
37         JEditorPane JavaDoc text = new JTextPane JavaDoc();
38
39         text.setMargin(new Insets JavaDoc(8,8,8,8));
40         text.setCaretColor(new Color JavaDoc(0xa4, 0x00, 0x00));
41         text.setBackground(new Color JavaDoc(0xf2, 0xf2, 0xf2));
42         text.setForeground(new Color JavaDoc(0xa4, 0x00, 0x00));
43         Font JavaDoc font = console.findFont("Monospaced", Font.PLAIN, 14,
44                 new String JavaDoc[] {"Monaco", "Andale Mono"});
45
46         text.setFont(font);
47         JScrollPane JavaDoc pane = new JScrollPane JavaDoc();
48         pane.setViewportView(text);
49         pane.setBorder(BorderFactory.createLineBorder(Color.darkGray));
50         console.getContentPane().add(pane);
51         console.validate();
52
53         final TextAreaReadline tar = new TextAreaReadline(text, " Welcome to the JRuby IRB Console \n\n");
54
55         final RubyInstanceConfig config = new RubyInstanceConfig() {{
56             setInput(pipeIn);
57             setOutput(new PrintStream JavaDoc(tar));
58             setError(new PrintStream JavaDoc(tar));
59             setObjectSpaceEnabled(false);
60             }};
61         final Ruby runtime = Ruby.newInstance(config);
62
63         IRubyObject argumentArray = runtime.newArrayNoCopy(JavaUtil.convertJavaArrayToRuby(runtime, args));
64         runtime.defineGlobalConstant("ARGV", argumentArray);
65         runtime.getGlobalVariables().defineReadonly("$*", new ValueAccessor(argumentArray));
66         runtime.getGlobalVariables().defineReadonly("$$", new ValueAccessor(runtime.newFixnum(System.identityHashCode(runtime))));
67         runtime.getLoadService().init(new ArrayList JavaDoc());
68
69         tar.hookIntoRuntime(runtime);
70
71         Thread JavaDoc t2 = new Thread JavaDoc() {
72             public void run() {
73                 console.setVisible(true);
74                 runtime.evalScript("require 'irb'; require 'irb/completion'; IRB.start");
75             }
76         };
77         t2.start();
78
79         try {
80             t2.join();
81         } catch (InterruptedException JavaDoc ie) {
82             // ignore
83
}
84
85         System.exit(0);
86     }
87
88     private Font JavaDoc findFont(String JavaDoc otherwise, int style, int size, String JavaDoc[] families) {
89         String JavaDoc[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
90         Arrays.sort(fonts);
91         Font JavaDoc font = null;
92         for (int i = 0; i < families.length; i++) {
93             if (Arrays.binarySearch(fonts, families[i]) >= 0) {
94                 font = new Font JavaDoc(families[i], style, size);
95                 break;
96             }
97         }
98         if (font == null)
99             font = new Font JavaDoc(otherwise, style, size);
100         return font;
101     }
102
103     /**
104      *
105      */

106     private static final long serialVersionUID = 3746242973444417387L;
107
108 }
109
Popular Tags