Skip to main content
summaryrefslogtreecommitdiffstats
blob: 79891dfe49874e2baa9c987aab8db77dc531945e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.text.AbstractDocument;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DefaultLineTracker;
import org.eclipse.jface.text.GapTextStore;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;

public class ConsoleDocument extends AbstractDocument {
	public static final int COMMAND = 0; // command text
	public static final int MESSAGE = 1; // message received
	public static final int ERROR = 2;   // error received
	public static final int STATUS = 3;  // status text
	public static final int DELIMITER = 4; // delimiter text between runs

	private int[] lineTypes = null;
	private int   currentLine = 0;

	/**
	 * Creates an empty console document.
	 */
	public ConsoleDocument() {
		setTextStore(new GapTextStore(512, 1024));
		setLineTracker(new DefaultLineTracker());
		completeInitialization();
	}
	
	/**
	 * Clears the console document.
	 */
	public void clear() {
		lineTypes = null;
		currentLine = 0;
		set(""); //$NON-NLS-1$
	}
	
	/**
	 * Gets the line type for the line containing the specified offset.
	 */
	public int getLineType(int offset) {
		try {
			int line = getLineOfOffset(offset);
			if (line < currentLine) return lineTypes[line];
		} catch (BadLocationException e) {
			CVSProviderPlugin.log(CVSException.wrapException(e));
		}
		return 0;
	}
	
	/**
	 * Appends a line of the specified type to the end of the console.
	 */
	public void appendConsoleLine(int type, String line) {
		if (lineTypes == null) {
			lineTypes = new int[16];
		} else if (currentLine >= lineTypes.length) {
			int[] oldLineTypes = lineTypes;
			lineTypes = new int[oldLineTypes.length * 2];
			System.arraycopy(oldLineTypes, 0, lineTypes, 0, oldLineTypes.length);
		}
		lineTypes[currentLine++] = type;
		try { 
			replace(getLength(), 0, line + "\n"); //$NON-NLS-1$
		} catch (BadLocationException e) {
			CVSProviderPlugin.log(CVSException.wrapException(e));
		}
	}
	
	/**
	 * Return the indicies of the lines that contain command strings
	 */
	private int[] getCommandLines() {
		List commandLineList = new ArrayList();
		for (int i = 0; i < currentLine; i++) {
			if (lineTypes[i] == COMMAND) {
				commandLineList.add(new Integer(i));
			}			
		}
		int[] commandLines = new int[commandLineList.size()];
		int i = 0;
		for (Iterator iter = commandLineList.iterator(); iter.hasNext(); ) {
			commandLines[i++] = ((Integer) iter.next()).intValue();
		}
		return commandLines;
	}	
}

Back to the top