1 11 package org.eclipse.ui.console; 12 13 import java.io.IOException ; 14 import java.io.OutputStream ; 15 16 import org.eclipse.swt.graphics.Color; 17 import org.eclipse.ui.WorkbenchEncoding; 18 import org.eclipse.ui.internal.console.IOConsolePartitioner; 19 20 35 public class IOConsoleOutputStream extends OutputStream { 36 39 private boolean closed = false; 40 41 44 private IOConsolePartitioner partitioner; 45 46 49 private IOConsole console; 50 51 55 private boolean activateOnWrite = false; 56 57 60 private Color color; 61 62 65 private int fontStyle; 66 67 private String fEncoding; 68 private String fDefaultEncoding = WorkbenchEncoding.getWorkbenchDefaultEncoding(); 69 70 private boolean fNeedsEncoding = false; 71 72 private boolean prependCR; 73 74 79 IOConsoleOutputStream(IOConsole console) { 80 this.console = console; 81 this.partitioner = (IOConsolePartitioner) console.getPartitioner(); 82 } 83 84 89 public int getFontStyle() { 90 return fontStyle; 91 } 92 93 98 public void setFontStyle(int newFontStyle) { 99 if (newFontStyle != fontStyle) { 100 int old = fontStyle; 101 fontStyle = newFontStyle; 102 console.firePropertyChange(this, IConsoleConstants.P_FONT_STYLE, new Integer (old), new Integer (fontStyle)); 103 } 104 } 105 106 113 public boolean isActivateOnWrite() { 114 return activateOnWrite; 115 } 116 117 124 public void setActivateOnWrite(boolean activateOnWrite) { 125 this.activateOnWrite = activateOnWrite; 126 } 127 128 134 public void setColor(Color newColor) { 135 Color old = color; 136 if (old == null || !old.equals(newColor)) { 137 color = newColor; 138 console.firePropertyChange(this, IConsoleConstants.P_STREAM_COLOR, old, newColor); 139 } 140 } 141 142 148 public Color getColor() { 149 return color; 150 } 151 152 156 public synchronized boolean isClosed() { 157 return closed; 158 } 159 160 164 public synchronized void close() throws IOException { 165 if(closed) { 166 throw new IOException ("Output Stream is closed"); } 168 if (prependCR) { prependCR = false; 170 notifyParitioner("\r"); } 172 console.streamClosed(this); 173 closed = true; 174 partitioner = null; 175 } 176 177 181 public void flush() throws IOException { 182 if(closed) { 183 throw new IOException ("Output Stream is closed"); } 185 } 186 187 191 public void write(byte[] b, int off, int len) throws IOException { 192 if (fNeedsEncoding) { 193 encodedWrite(new String (b, off, len, fEncoding)); 194 } else { 195 encodedWrite(new String (b, off, len)); 196 } 197 } 198 202 public void write(byte[] b) throws IOException { 203 write(b, 0, b.length); 204 } 205 209 public void write(int b) throws IOException { 210 write(new byte[] {(byte)b}, 0, 1); 211 } 212 213 219 public synchronized void write(String str) throws IOException { 220 if (fNeedsEncoding) { 221 byte[] defaultBytes = str.getBytes(); 222 str = new String (defaultBytes, fEncoding); 223 } 224 encodedWrite(str); 225 } 226 227 private void encodedWrite(String encodedString) throws IOException { 228 if(closed) { 229 throw new IOException ("Output Stream is closed"); } 231 if (prependCR){ 232 encodedString="\r"+encodedString; prependCR=false; 234 } 235 if (encodedString.endsWith("\r")) { prependCR = true; 237 encodedString = new String (encodedString.substring(0, encodedString.length()-1)); 238 } 239 notifyParitioner(encodedString); 240 } 241 242 private void notifyParitioner(String encodedString) throws IOException { 243 try { 244 partitioner.streamAppended(this, encodedString); 245 246 if (activateOnWrite) { 247 console.activate(); 248 } else { 249 ConsolePlugin.getDefault().getConsoleManager().warnOfContentChange(console); 250 } 251 } catch (IOException e) { 252 if (!closed) { 253 close(); 254 } 255 throw e; 256 } 257 } 258 259 264 public void setEncoding(String encoding) { 265 fEncoding = encoding; 266 fNeedsEncoding = (fEncoding!=null) && (!fEncoding.equals(fDefaultEncoding)); 267 } 268 } 269 | Popular Tags |