1 package org.jruby.demo; 2 3 import java.applet.Applet ; 4 import java.awt.BorderLayout ; 5 import java.awt.Color ; 6 import java.awt.Font ; 7 import java.awt.GraphicsEnvironment ; 8 import java.awt.Insets ; 9 import java.io.PipedInputStream ; 10 import java.io.PrintStream ; 11 import java.util.ArrayList ; 12 import java.util.Arrays ; 13 14 import javax.swing.BorderFactory ; 15 import javax.swing.JEditorPane ; 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.runtime.builtin.IRubyObject; 22 23 public class IRBApplet extends Applet { 24 25 public void start() { 26 super.start(); 27 28 final PipedInputStream pipeIn = new PipedInputStream (); 29 30 setLayout(new BorderLayout ()); 31 32 JEditorPane text = new JTextPane (); 33 34 text.setMargin(new Insets (8,8,8,8)); 35 text.setCaretColor(new Color (0xa4, 0x00, 0x00)); 36 text.setBackground(new Color (0xf2, 0xf2, 0xf2)); 37 text.setForeground(new Color (0xa4, 0x00, 0x00)); 38 Font font = findFont("Monospaced", Font.PLAIN, 14, 39 new String [] {"Monaco", "Andale Mono"}); 40 41 text.setFont(font); 42 JScrollPane pane = new JScrollPane (); 43 pane.setViewportView(text); 44 pane.setBorder(BorderFactory.createLineBorder(Color.darkGray)); 45 add(pane); 46 this.validate(); 47 48 final TextAreaReadline tar = new TextAreaReadline(text, " Welcome to JRuby for the Web! \n\n"); 49 50 final RubyInstanceConfig config = new RubyInstanceConfig() {{ 51 setInput(pipeIn); 52 setOutput(new PrintStream (tar)); 53 setError(new PrintStream (tar)); 54 setObjectSpaceEnabled(false); 55 }}; 56 final Ruby runtime = Ruby.newInstance(config); 57 58 runtime.defineGlobalConstant("ARGV", runtime.newArrayNoCopy(new IRubyObject[] { 59 runtime.newString("-f") })); 60 runtime.getLoadService().init(new ArrayList (0)); 61 62 tar.hookIntoRuntime(runtime); 63 64 65 Thread t2 = new Thread () { 66 public void run() { 67 runtime.evalScript("require 'irb'; require 'irb/completion'; IRB.start"); 68 } 69 }; 70 t2.start(); 71 } 72 73 private Font findFont(String otherwise, int style, int size, String [] families) { 74 String [] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 75 Arrays.sort(fonts); 76 Font font = null; 77 for (int i = 0; i < families.length; i++) { 78 if (Arrays.binarySearch(fonts, families[i]) >= 0) { 79 font = new Font (families[i], style, size); 80 break; 81 } 82 } 83 if (font == null) 84 font = new Font (otherwise, style, size); 85 return font; 86 } 87 88 91 private static final long serialVersionUID = 3746242973444417387L; 92 93 } 94 | Popular Tags |