KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > console > ConsoleDocument


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui.console;
12
13 /**
14  * Simple circular buffer that stores a fix number of lines.
15  */

16 public class ConsoleDocument {
17     public static final int COMMAND = 0; // command text
18
public static final int MESSAGE = 1; // message received
19
public static final int ERROR = 2; // error received
20
public static final int STATUS = 3; // status text
21
public static final int DELIMITER = 4; // delimiter text between runs
22

23     private int[] lineTypes;
24     private String JavaDoc[] 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 JavaDoc line;
33         public int type;
34         ConsoleLine(String JavaDoc line, int type) {
35             this.line = line;
36             this.type = type;
37         }
38     }
39     
40     /**
41      * Creates an empty console document.
42      */

43     public ConsoleDocument() {
44     }
45     
46     /**
47      * Clears the console document.
48      */

49     public void clear() {
50         lineTypes = null;
51         lines = null;
52         writeIndex = 0;
53         readIndex = 0;
54     }
55     
56     /**
57      * Appends a line of the specified type to the end of the console.
58      */

59     public void appendConsoleLine(int type, String JavaDoc line) {
60         if(lines == null) {
61             lines = new String JavaDoc[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