1 package org.netbeans.modules.ruby.rubyproject; 2 3 import java.awt.Color ; 4 import java.awt.Font ; 5 import java.awt.Insets ; 6 import java.awt.Rectangle ; 7 import java.awt.event.MouseAdapter ; 8 import java.awt.event.MouseEvent ; 9 import java.io.PipedInputStream ; 10 import java.io.PrintStream ; 11 import java.util.ArrayList ; 12 import javax.swing.BorderFactory ; 13 import javax.swing.JScrollPane ; 14 import javax.swing.JTextPane ; 15 import javax.swing.SwingUtilities ; 16 import javax.swing.SwingUtilities ; 17 import javax.swing.text.BadLocationException ; 18 import org.jruby.Ruby; 19 import org.jruby.RubyInstanceConfig; 20 import org.jruby.internal.runtime.ValueAccessor; 21 import org.jruby.javasupport.JavaUtil; 22 import org.jruby.runtime.builtin.IRubyObject; 23 import java.io.Serializable ; 24 import javax.swing.UIManager ; 25 import javax.swing.text.Caret ; 26 import org.jruby.demo.TextAreaReadline; 27 import org.netbeans.editor.Settings; 28 import org.netbeans.modules.ruby.rubyproject.api.RubyInstallation; 29 import org.openide.ErrorManager; 30 import org.openide.util.Exceptions; 31 import org.openide.util.NbBundle; 32 import org.openide.util.RequestProcessor; 33 import org.openide.util.Task; 34 import org.openide.util.TaskListener; 35 import org.openide.windows.TopComponent; 36 import org.openide.windows.WindowManager; 37 import org.openide.util.Utilities; 38 import org.netbeans.editor.SettingsNames; 39 import org.netbeans.editor.SettingsUtil; 40 import org.netbeans.editor.SettingsDefaults; 41 import org.netbeans.editor.BaseKit; 42 43 60 final class IrbTopComponent extends TopComponent { 61 private boolean finished = true; 62 private JTextPane text; 63 64 private static IrbTopComponent instance; 65 66 static final String ICON_PATH = "org/netbeans/modules/ruby/rubyproject/jruby.png"; 68 private static final String PREFERRED_ID = "IrbTopComponent"; 70 private IrbTopComponent() { 71 initComponents(); 72 setName(NbBundle.getMessage(IrbTopComponent.class, "CTL_IrbTopComponent")); 73 setToolTipText(NbBundle.getMessage(IrbTopComponent.class, "HINT_IrbTopComponent")); 74 setIcon(Utilities.loadImage(ICON_PATH, true)); 75 } 76 77 82 private void initComponents() { 84 85 setLayout(new java.awt.BorderLayout ()); 86 } 88 89 92 97 public static synchronized IrbTopComponent getDefault() { 98 if (instance == null) { 99 instance = new IrbTopComponent(); 100 } 101 return instance; 102 } 103 104 107 public static synchronized IrbTopComponent findInstance() { 108 TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID); 109 if (win == null) { 110 ErrorManager.getDefault().log(ErrorManager.WARNING, 111 "Cannot find MyWindow component. It will not be located properly in the window system."); 112 return getDefault(); 113 } 114 if (win instanceof IrbTopComponent) { 115 return (IrbTopComponent)win; 116 } 117 ErrorManager.getDefault().log(ErrorManager.WARNING, 118 "There seem to be multiple components with the '" + PREFERRED_ID + 119 "' ID. That is a potential source of errors and unexpected behavior."); 120 return getDefault(); 121 } 122 123 public int getPersistenceType() { 124 return TopComponent.PERSISTENCE_ALWAYS; 125 } 126 127 public void componentOpened() { 128 if (finished) { 129 finished = false; 131 removeAll(); 132 createTerminal(); 133 } 134 } 135 136 public void componentClosed() { 137 } 139 140 @Override 141 public void componentActivated() { 142 if (text != null) { 144 Caret caret = text.getCaret(); 145 if (caret != null) { 146 caret.setVisible(true); 147 } 148 } 149 } 150 151 @Override 152 public void componentDeactivated() { 153 if (text != null) { 158 Caret caret = text.getCaret(); 159 if (caret != null) { 160 caret.setVisible(false); 161 } 162 } 163 } 164 165 166 public Object writeReplace() { 167 return new ResolvableHelper(); 168 } 169 170 protected String preferredID() { 171 return PREFERRED_ID; 172 } 173 174 final static class ResolvableHelper implements Serializable { 175 private static final long serialVersionUID = 1L; 176 public Object readResolve() { 177 return IrbTopComponent.getDefault(); 178 } 179 } 180 181 public void createTerminal() { 182 final PipedInputStream pipeIn = new PipedInputStream (); 183 184 text = new JTextPane (); 185 186 text.setMargin(new Insets (8,8,8,8)); 187 text.setCaretColor(new Color (0xa4, 0x00, 0x00)); 188 text.setBackground(new Color (0xf2, 0xf2, 0xf2)); 189 text.setForeground(new Color (0xa4, 0x00, 0x00)); 190 191 Integer i = (Integer ) UIManager.get("customFontSize"); int size; 194 if (i != null) { 195 size = i.intValue(); 196 } else { 197 Font f = (Font ) UIManager.get("controlFont"); size = f != null ? f.getSize() : 11; 199 } 200 text.setFont(new Font ("Monospaced", Font.PLAIN, size)); setBorder (BorderFactory.createEmptyBorder()); 202 203 Color c = UIManager.getColor("nb.output.selectionBackground"); if (c != null) { 206 text.setSelectionColor(c); 207 } 208 209 240 241 JScrollPane pane = new JScrollPane (); 242 pane.setViewportView(text); 243 pane.setBorder(BorderFactory.createLineBorder(Color.darkGray)); 244 add(pane); 245 validate(); 246 247 final TextAreaReadline tar = new TextAreaReadline(text, " " + NbBundle.getMessage(IrbTopComponent.class, "IrbWelcome") + " \n\n"); 250 RubyInstallation.getInstance().setJRubyLoadPaths(); 252 253 final RubyInstanceConfig config = new RubyInstanceConfig() {{ 254 setInput(pipeIn); 255 setOutput(new PrintStream (tar)); 256 setError(new PrintStream (tar)); 257 setObjectSpaceEnabled(false); 258 }}; 259 final Ruby runtime = Ruby.newInstance(config); 260 261 IRubyObject argumentArray = runtime.newArray(JavaUtil.convertJavaArrayToRuby(runtime, new String [0])); 262 runtime.defineGlobalConstant("ARGV", argumentArray); runtime.getGlobalVariables().defineReadonly("$*", new ValueAccessor(argumentArray)); runtime.getGlobalVariables().defineReadonly("$$", new ValueAccessor(runtime.newFixnum(System.identityHashCode(runtime)))); runtime.getLoadService().init(new ArrayList ()); 266 267 tar.hookIntoRuntime(runtime); 268 269 RequestProcessor.Task task = RequestProcessor.getDefault().create(new Runnable () { 270 public void run() { 272 runtime.evalScript("require 'irb'; require 'irb/completion'; IRB.start"); } 274 }); 275 task.addTaskListener(new TaskListener() { 276 277 public void taskFinished(Task task) { 278 finished = true; 279 text.setEditable(false); 281 SwingUtilities.invokeLater(new Runnable () { 282 public void run() { 283 IrbTopComponent.this.close(); 284 IrbTopComponent.this.removeAll(); 285 text = null; 286 } 287 }); 288 } 289 }); 290 task.schedule(10); 291 292 text.addMouseListener(new MouseAdapter () { 294 @Override 295 public void mouseClicked(MouseEvent ev) { 296 final int mouseX = ev.getX(); 297 final int mouseY = ev.getY(); 298 SwingUtilities.invokeLater(new Runnable () { 300 public void run() { 301 int pos = text.getDocument().getEndPosition().getOffset()-1; 303 if (pos == -1) { 304 return; 305 } 306 307 try { 308 Rectangle r = text.modelToView(pos); 309 310 if (mouseY >= r.y) { 311 r.x = mouseX; 317 pos = text.viewToModel(r.getLocation()); 318 } 319 320 text.getCaret().setDot(pos); 321 } catch (BadLocationException ble) { 322 Exceptions.printStackTrace(ble); 323 } 324 } 325 }); 326 } 327 }); 328 } 329 330 @Override 331 public void requestFocus() { 332 if (text != null) { 333 text.requestFocus(); 334 } 335 } 336 337 @Override 338 public boolean requestFocusInWindow() { 339 if (text != null) { 340 return text.requestFocusInWindow(); 341 } 342 343 return false; 344 } 345 } 346 | Popular Tags |