1 11 package org.eclipse.team.internal.ccvs.ui.console; 12 13 16 public class ConsoleDocument { 17 public static final int COMMAND = 0; public static final int MESSAGE = 1; public static final int ERROR = 2; public static final int STATUS = 3; public static final int DELIMITER = 4; 23 private int[] lineTypes; 24 private String [] lines; 25 26 private int writeIndex = 0; 27 private int readIndex = 0; 28 29 private static final int BUFFER_SIZE = 200; 30 31 protected static class ConsoleLine { 32 public String line; 33 public int type; 34 ConsoleLine(String line, int type) { 35 this.line = line; 36 this.type = type; 37 } 38 } 39 40 43 public ConsoleDocument() { 44 } 45 46 49 public void clear() { 50 lineTypes = null; 51 lines = null; 52 writeIndex = 0; 53 readIndex = 0; 54 } 55 56 59 public void appendConsoleLine(int type, String line) { 60 if(lines == null) { 61 lines = new String [BUFFER_SIZE]; 62 lineTypes = new int[BUFFER_SIZE]; 63 } 64 lines[writeIndex] = line; 65 lineTypes[writeIndex] = type; 66 67 if(++writeIndex >= BUFFER_SIZE) { 68 writeIndex = 0; 69 } 70 if(writeIndex == readIndex) { 71 if(++readIndex >= BUFFER_SIZE) { 72 readIndex = 0; 73 } 74 } 75 } 76 77 public ConsoleLine[] getLines() { 78 if(isEmpty()) return new ConsoleLine[0]; 79 ConsoleLine[] docLines = new ConsoleLine[readIndex > writeIndex ? BUFFER_SIZE : writeIndex]; 80 int index = readIndex; 81 for (int i = 0; i < docLines.length; i++) { 82 docLines[i] = new ConsoleLine(lines[index], lineTypes[index]); 83 if (++index >= BUFFER_SIZE) { 84 index = 0; 85 } 86 } 87 return docLines; 88 } 89 90 public boolean isEmpty() { 91 return writeIndex == readIndex; 92 } 93 } 94 | Popular Tags |