1 package org.jruby.demo; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Color ; 5 import java.awt.Font ; 6 import java.awt.GraphicsEnvironment ; 7 import java.awt.Insets ; 8 import java.io.PipedInputStream ; 9 import java.io.PrintStream ; 10 import java.util.ArrayList ; 11 import java.util.Arrays ; 12 13 import javax.swing.BorderFactory ; 14 import javax.swing.JEditorPane ; 15 import javax.swing.JFrame ; 16 import javax.swing.JScrollPane ; 17 import javax.swing.JTextPane ; 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 { 26 public IRBConsole(String title) { 27 super(title); 28 } 29 30 public static void main(String [] args) { 31 final IRBConsole console = new IRBConsole("JRuby IRB Console"); 32 final PipedInputStream pipeIn = new PipedInputStream (); 33 34 console.getContentPane().setLayout(new BorderLayout ()); 35 console.setSize(700, 600); 36 37 JEditorPane text = new JTextPane (); 38 39 text.setMargin(new Insets (8,8,8,8)); 40 text.setCaretColor(new Color (0xa4, 0x00, 0x00)); 41 text.setBackground(new Color (0xf2, 0xf2, 0xf2)); 42 text.setForeground(new Color (0xa4, 0x00, 0x00)); 43 Font font = console.findFont("Monospaced", Font.PLAIN, 14, 44 new String [] {"Monaco", "Andale Mono"}); 45 46 text.setFont(font); 47 JScrollPane pane = new JScrollPane (); 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 (tar)); 58 setError(new PrintStream (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 ()); 68 69 tar.hookIntoRuntime(runtime); 70 71 Thread t2 = new Thread () { 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 ie) { 82 } 84 85 System.exit(0); 86 } 87 88 private Font findFont(String otherwise, int style, int size, String [] families) { 89 String [] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 90 Arrays.sort(fonts); 91 Font font = null; 92 for (int i = 0; i < families.length; i++) { 93 if (Arrays.binarySearch(fonts, families[i]) >= 0) { 94 font = new Font (families[i], style, size); 95 break; 96 } 97 } 98 if (font == null) 99 font = new Font (otherwise, style, size); 100 return font; 101 } 102 103 106 private static final long serialVersionUID = 3746242973444417387L; 107 108 } 109 | Popular Tags |