1 46 package groovy.ui; 47 48 import groovy.lang.GroovyShell; 49 50 import java.awt.Color ; 51 52 import javax.swing.JTextPane ; 53 import javax.swing.text.Style ; 54 import javax.swing.text.StyleConstants ; 55 import javax.swing.text.StyleContext ; 56 import javax.swing.text.StyledDocument ; 57 58 64 public abstract class ConsoleSupport { 65 66 Style promptStyle; 67 Style commandStyle; 68 Style outputStyle; 69 private GroovyShell shell; 70 private int counter; 71 72 protected void addStylesToDocument(JTextPane outputArea) { 73 StyledDocument doc = outputArea.getStyledDocument(); 74 75 Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 76 77 Style regular = doc.addStyle("regular", def); 78 StyleConstants.setFontFamily(def, "SansSerif"); 79 80 promptStyle = doc.addStyle("prompt", regular); 81 StyleConstants.setForeground(promptStyle, Color.BLUE); 82 83 commandStyle = doc.addStyle("command", regular); 84 StyleConstants.setForeground(commandStyle, Color.MAGENTA); 85 86 outputStyle = doc.addStyle("output", regular); 87 StyleConstants.setBold(outputStyle, true); 88 } 89 90 public Style getCommandStyle() { 91 return commandStyle; 92 } 93 94 public Style getOutputStyle() { 95 return outputStyle; 96 } 97 98 public Style getPromptStyle() { 99 return promptStyle; 100 } 101 102 public GroovyShell getShell() { 103 return shell; 104 } 105 106 protected Object evaluate(String text) { 107 System.out.println("Evaluating: " + text); 108 109 if (shell == null) { 110 shell = new GroovyShell(); 111 } 112 113 String name = "Script" + counter++; 114 try { 115 return shell.evaluate(text, name); 116 } 117 catch (Exception e) { 118 handleException(text, e); 119 return null; 120 } 121 } 122 123 protected abstract void handleException(String text, Exception e); 124 } 125 | Popular Tags |