1 22 23 package org.aspectj.debugger.gui; 24 25 import org.aspectj.debugger.base.*; 26 27 import java.awt.*; 28 import java.io.*; 29 import javax.swing.*; 30 import javax.swing.text.*; 31 32 public abstract class AbstractTextArea extends JPanel { 33 34 protected final static int TEXT_ROWS = 30; 35 protected final static int TEXT_COLS = 50; 36 protected final static int TEXT_SIZE = 12; 37 protected final static Dimension TEXT_DIM = new Dimension(TEXT_ROWS, TEXT_COLS); 38 protected final static int OUT = 0; 39 protected final static int NOTICE = 1; 40 protected final static int ERR = 2; 41 protected final static String NEWLINE = System.getProperty("line.separator"); 42 43 protected PrintStream printStream = null; 44 protected JScrollPane scrollPane = null; 45 protected DefaultStyledDocument dsd = new DefaultStyledDocument(); 46 protected JTextArea text; protected SimpleAttributeSet[] attrs = null; 48 protected Color[] colors = { Color.black, 49 Color.blue.darker(), 50 Color.red.darker() }; 51 protected int textSize = TEXT_SIZE; 52 55 protected GUIDebugger guiDebugger; 56 57 private int count = 0; 58 59 public final void superInit(GUIDebugger guiDebugger) { 60 this.guiDebugger = guiDebugger; 61 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 62 text = new JTextArea(0,0); 63 text.setEditable(false); 64 printStream = createPrintStream(); 65 scrollPane = new JScrollPane(text); 66 scrollPane.setVerticalScrollBarPolicy 69 (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 70 add(scrollPane); 71 initAttrs(); 72 } 73 74 76 protected abstract PrintStream createPrintStream(); 77 78 public PrintStream getPrintStream() { 79 return printStream; 80 } 81 82 public void setTextSize(int textSize) { 83 this.textSize = textSize; 84 } 85 86 public abstract void restorePrintStream(); 87 88 public void shutDown() { 89 restorePrintStream(); 90 } 91 92 protected void out(String s) { 93 append(s, OUT); 94 } 95 96 protected void outln(String s) { 97 append(s, OUT); 98 newline(); 99 } 100 101 protected void err(String s) { 102 append(s, ERR); 103 } 104 105 protected void errln(String s) { 106 append(s, ERR); 107 newline(); 108 } 109 110 protected void notice(String s) { 111 append(s, NOTICE); 112 } 113 114 protected void noticeln(String s) { 115 append(s, NOTICE); 116 newline(); 117 } 118 119 protected void append(String s, int i) { 120 text.append(s); 121 } 124 125 protected void newline() { 126 append(NEWLINE, -1); 127 } 128 129 protected void initAttrs() { 130 attrs = new SimpleAttributeSet[colors.length]; 131 for (int i = 0; i < colors.length; i++) { 132 attrs[i] = getAttr(colors[i]); 133 } 134 } 135 136 protected SimpleAttributeSet getAttr(Color c) { 137 SimpleAttributeSet attr = new SimpleAttributeSet(); 138 StyleConstants.setForeground(attr, c); 139 StyleConstants.setFontSize(attr, textSize); 140 return attr; 141 } 142 143 protected static class TextPrintOutStream extends PrintStream { 144 public TextPrintOutStream(final AbstractTextArea text) { 145 super(new OutputStream() { 146 public void write(int b) throws IOException { 147 text.out(String.valueOf((char)b)); 148 } 149 }); 150 151 } 152 } 153 154 protected static class TextPrintNoticeStream extends PrintStream { 155 public TextPrintNoticeStream(final AbstractTextArea text) { 156 super(new OutputStream() { 157 public void write(int b) throws IOException { 158 text.notice(String.valueOf((char)b)); 159 } 160 }); 161 162 } 163 } 164 165 protected static class TextPrintErrStream extends PrintStream { 166 public TextPrintErrStream(final AbstractTextArea text) { 167 super(new OutputStream() { 168 public void write(int b) throws IOException { 169 text.err(String.valueOf((char)b)); 170 } 171 }); 172 } 173 } 174 } 175 | Popular Tags |