1 11 12 package org.eclipse.ui.console; 13 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.ui.WorkbenchEncoding; 20 import org.eclipse.ui.internal.console.IOConsolePage; 21 import org.eclipse.ui.internal.console.IOConsolePartitioner; 22 import org.eclipse.ui.part.IPageBookViewPage; 23 24 33 public class IOConsole extends TextConsole { 34 37 private IOConsolePartitioner partitioner; 38 39 42 private IOConsoleInputStream inputStream; 43 44 47 private List openStreams; 48 49 52 private String fEncoding = WorkbenchEncoding.getWorkbenchDefaultEncoding(); 53 54 55 65 public IOConsole(String name, String consoleType, ImageDescriptor imageDescriptor, boolean autoLifecycle) { 66 this(name, consoleType, imageDescriptor, null, autoLifecycle); 67 } 68 69 78 public IOConsole(String name, String consoleType, ImageDescriptor imageDescriptor, String encoding, boolean autoLifecycle) { 79 super(name, consoleType, imageDescriptor, autoLifecycle); 80 if (encoding != null) { 81 fEncoding = encoding; 82 } 83 openStreams = new ArrayList (); 84 inputStream = new IOConsoleInputStream(this); 85 synchronized (openStreams) { 86 openStreams.add(inputStream); 87 } 88 89 partitioner = new IOConsolePartitioner(inputStream, this); 90 partitioner.connect(getDocument()); 91 } 92 93 102 public IOConsole(String name, String consoleType, ImageDescriptor imageDescriptor) { 103 this(name, consoleType, imageDescriptor, true); 104 } 105 106 114 public IOConsole(String name, ImageDescriptor imageDescriptor) { 115 this(name, null, imageDescriptor); 116 } 117 118 121 public IPageBookViewPage createPage(IConsoleView view) { 122 return new IOConsolePage(this, view); 123 } 124 125 137 public IOConsoleOutputStream newOutputStream() { 138 IOConsoleOutputStream outputStream = new IOConsoleOutputStream(this); 139 outputStream.setEncoding(fEncoding); 140 synchronized(openStreams) { 141 openStreams.add(outputStream); 142 } 143 return outputStream; 144 } 145 146 151 public IOConsoleInputStream getInputStream() { 152 return inputStream; 153 } 154 155 160 protected IConsoleDocumentPartitioner getPartitioner() { 161 return partitioner; 162 } 163 164 171 public int getHighWaterMark() { 172 return partitioner.getHighWaterMark(); 173 } 174 175 182 public int getLowWaterMark() { 183 return partitioner.getLowWaterMark(); 184 } 185 186 198 public void setWaterMarks(int low, int high) { 199 if (low >= 0) { 200 if (low >= high) { 201 throw new IllegalArgumentException ("High water mark must be greater than low water mark"); } 203 } 204 partitioner.setWaterMarks(low, high); 205 } 206 207 211 private void checkFinished() { 212 if (openStreams.isEmpty()) { 213 partitioner.streamsClosed(); 214 } 215 } 216 217 222 void streamClosed(IOConsoleOutputStream stream) { 223 synchronized (openStreams) { 224 openStreams.remove(stream); 225 checkFinished(); 226 } 227 } 228 229 234 void streamClosed(IOConsoleInputStream stream) { 235 synchronized (openStreams) { 236 openStreams.remove(stream); 237 checkFinished(); 238 } 239 } 240 241 244 public void clearConsole() { 245 if (partitioner != null) { 246 partitioner.clearBuffer(); 247 } 248 } 249 250 253 protected void dispose() { 254 super.dispose(); 255 partitioner.disconnect(); 256 Object [] allStreams= openStreams.toArray(); 260 for (int i = 0; i < allStreams.length; i++) { 261 Object stream = allStreams[i]; 262 if (stream instanceof IOConsoleInputStream) { 263 IOConsoleInputStream is = (IOConsoleInputStream) stream; 264 try { 265 is.close(); 266 } catch (IOException e) { 267 } 268 } else if (stream instanceof IOConsoleOutputStream) { 269 IOConsoleOutputStream os = (IOConsoleOutputStream) stream; 270 try { 271 os.close(); 272 } catch (IOException e) { 273 } 274 } 275 } 276 inputStream = null; 277 } 278 279 287 public String getEncoding() { 288 return fEncoding; 289 } 290 } 291 | Popular Tags |