001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.console;
011:
012: import org.eclipse.jface.text.ITypedRegion;
013: import org.eclipse.swt.custom.StyleRange;
014: import org.eclipse.swt.graphics.Color;
015: import org.eclipse.ui.console.ConsolePlugin;
016: import org.eclipse.ui.console.IOConsoleInputStream;
017: import org.eclipse.ui.console.IOConsoleOutputStream;
018:
019: /**
020: * A region in an IOConsole's document.
021: * @since 3.1
022: *
023: */
024: public class IOConsolePartition implements ITypedRegion {
025: public static final String OUTPUT_PARTITION_TYPE = ConsolePlugin
026: .getUniqueIdentifier()
027: + ".io_console_output_partition_type"; //$NON-NLS-1$
028: public static final String INPUT_PARTITION_TYPE = ConsolePlugin
029: .getUniqueIdentifier()
030: + ".io_console_input_partition_type"; //$NON-NLS-1$
031:
032: /**
033: * The data contained by this partition.
034: */
035: private StringBuffer buffer;
036: private String type;
037: private int offset;
038: /**
039: * Output partitions are all read only.
040: * Input partitions are read only once they have been appended to the console's input stream.
041: */
042: private boolean readOnly;
043:
044: /**
045: * Only one of inputStream or outputStream will be null depending on the partitions type.
046: */
047: private IOConsoleOutputStream outputStream;
048: private IOConsoleInputStream inputStream;
049: private int length;
050:
051: /**
052: * Creates a new partition to contain output to console.
053: */
054: public IOConsolePartition(IOConsoleOutputStream outputStream,
055: int length) {
056: this .outputStream = outputStream;
057: this .length = length;
058: this .type = OUTPUT_PARTITION_TYPE;
059: this .readOnly = true;
060: }
061:
062: /**
063: * Creates a new partition to contain input from a console
064: */
065: public IOConsolePartition(IOConsoleInputStream inputStream,
066: String text) {
067: this .inputStream = inputStream;
068: buffer = new StringBuffer(text);
069: length = text.length();
070: this .type = INPUT_PARTITION_TYPE;
071: this .readOnly = false;
072: }
073:
074: /**
075: * Inserts a string into this partition
076: * @param s The string to insert
077: * @param offset the offset in the partition
078: */
079: public void insert(String s, int insertOffset) {
080: buffer.insert(insertOffset, s);
081: length += s.length();
082: }
083:
084: /**
085: * Deletes data from this partition.
086: * @param delOffset
087: * @param delLength
088: */
089: public void delete(int delOffset, int delLength) {
090: buffer.delete(delOffset, delOffset + delLength);
091: length -= delLength;
092: }
093:
094: /*
095: * (non-Javadoc)
096: * @see org.eclipse.jface.text.ITypedRegion#getType()
097: */
098: public String getType() {
099: return type;
100: }
101:
102: /*
103: * (non-Javadoc)
104: * @see org.eclipse.jface.text.IRegion#getLength()
105: */
106: public int getLength() {
107: return length;
108: }
109:
110: /*
111: * (non-Javadoc)
112: * @see org.eclipse.jface.text.IRegion#getOffset()
113: */
114: public int getOffset() {
115: return offset;
116: }
117:
118: /**
119: * Sets this partitions offset in the document.
120: * @param offset This partitions offset in the document.
121: */
122: public void setOffset(int offset) {
123: this .offset = offset;
124: }
125:
126: /**
127: * Sets this partitions length
128: *
129: * @param length
130: */
131: public void setLength(int length) {
132: this .length = length;
133: }
134:
135: /**
136: * Returns the data contained in this partition.
137: * @return The data contained in this partition.
138: */
139: public String getString() {
140: return buffer != null ? buffer.toString() : ""; //$NON-NLS-1$
141: }
142:
143: /**
144: * Returns a StyleRange object which may be used for setting the style
145: * of this partition in a viewer.
146: */
147: public StyleRange getStyleRange(int rangeOffset, int rangeLength) {
148: return new StyleRange(rangeOffset, rangeLength, getColor(),
149: null, getFontStyle());
150: }
151:
152: private int getFontStyle() {
153: if (type.equals(INPUT_PARTITION_TYPE)) {
154: return inputStream.getFontStyle();
155: }
156: return outputStream.getFontStyle();
157: }
158:
159: public Color getColor() {
160: if (type.equals(INPUT_PARTITION_TYPE)) {
161: return inputStream.getColor();
162: }
163: return outputStream.getColor();
164: }
165:
166: public boolean isReadOnly() {
167: return readOnly;
168: }
169:
170: public void setReadOnly() {
171: readOnly = true;
172: }
173:
174: public void clearBuffer() {
175: buffer = null;
176: }
177:
178: IOConsoleOutputStream getStream() {
179: return outputStream;
180: }
181: }
|