KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > console > IOConsolePartition


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.ui.internal.console;
12
13 import org.eclipse.jface.text.ITypedRegion;
14 import org.eclipse.swt.custom.StyleRange;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.ui.console.ConsolePlugin;
17 import org.eclipse.ui.console.IOConsoleInputStream;
18 import org.eclipse.ui.console.IOConsoleOutputStream;
19
20 /**
21  * A region in an IOConsole's document.
22  * @since 3.1
23  *
24  */

25 public class IOConsolePartition implements ITypedRegion {
26     public static final String JavaDoc OUTPUT_PARTITION_TYPE = ConsolePlugin.getUniqueIdentifier() + ".io_console_output_partition_type"; //$NON-NLS-1$
27
public static final String JavaDoc INPUT_PARTITION_TYPE = ConsolePlugin.getUniqueIdentifier() + ".io_console_input_partition_type"; //$NON-NLS-1$
28

29     /**
30      * The data contained by this partition.
31      */

32     private StringBuffer JavaDoc buffer;
33     private String JavaDoc type;
34     private int offset;
35     /**
36      * Output partitions are all read only.
37      * Input partitions are read only once they have been appended to the console's input stream.
38      */

39     private boolean readOnly;
40     
41     /**
42      * Only one of inputStream or outputStream will be null depending on the partitions type.
43      */

44     private IOConsoleOutputStream outputStream;
45     private IOConsoleInputStream inputStream;
46     private int length;
47     
48     /**
49      * Creates a new partition to contain output to console.
50      */

51     public IOConsolePartition(IOConsoleOutputStream outputStream, int length) {
52         this.outputStream = outputStream;
53         this.length = length;
54         this.type = OUTPUT_PARTITION_TYPE;
55         this.readOnly = true;
56     }
57     
58     /**
59      * Creates a new partition to contain input from a console
60      */

61     public IOConsolePartition(IOConsoleInputStream inputStream, String JavaDoc text) {
62         this.inputStream = inputStream;
63         buffer = new StringBuffer JavaDoc(text);
64         length = text.length();
65         this.type = INPUT_PARTITION_TYPE;
66         this.readOnly = false;
67     }
68     
69     /**
70      * Inserts a string into this partition
71      * @param s The string to insert
72      * @param offset the offset in the partition
73      */

74     public void insert(String JavaDoc s, int insertOffset) {
75         buffer.insert(insertOffset, s);
76         length += s.length();
77     }
78       
79     /**
80      * Deletes data from this partition.
81      * @param delOffset
82      * @param delLength
83      */

84     public void delete(int delOffset, int delLength) {
85         buffer.delete(delOffset, delOffset+delLength);
86         length -= delLength;
87     }
88     
89     /*
90      * (non-Javadoc)
91      * @see org.eclipse.jface.text.ITypedRegion#getType()
92      */

93     public String JavaDoc getType() {
94         return type;
95     }
96
97     /*
98      * (non-Javadoc)
99      * @see org.eclipse.jface.text.IRegion#getLength()
100      */

101     public int getLength() {
102         return length;
103     }
104
105     /*
106      * (non-Javadoc)
107      * @see org.eclipse.jface.text.IRegion#getOffset()
108      */

109     public int getOffset() {
110         return offset;
111     }
112     
113     /**
114      * Sets this partitions offset in the document.
115      * @param offset This partitions offset in the document.
116      */

117     public void setOffset(int offset) {
118         this.offset = offset;
119     }
120     
121     /**
122      * Sets this partitions length
123      *
124      * @param length
125      */

126     public void setLength(int length) {
127         this.length = length;
128     }
129     
130     /**
131      * Returns the data contained in this partition.
132      * @return The data contained in this partition.
133      */

134     public String JavaDoc getString() {
135         return buffer != null ? buffer.toString() : ""; //$NON-NLS-1$
136
}
137     
138     /**
139      * Returns a StyleRange object which may be used for setting the style
140      * of this partition in a viewer.
141      */

142     public StyleRange getStyleRange(int rangeOffset, int rangeLength) {
143         return new StyleRange(rangeOffset, rangeLength, getColor(), null, getFontStyle());
144     }
145
146     private int getFontStyle() {
147         if (type.equals(INPUT_PARTITION_TYPE)) {
148             return inputStream.getFontStyle();
149         }
150         return outputStream.getFontStyle();
151     }
152
153     public Color getColor() {
154         if (type.equals(INPUT_PARTITION_TYPE)) {
155             return inputStream.getColor();
156         }
157         return outputStream.getColor();
158     }
159
160     public boolean isReadOnly() {
161         return readOnly;
162     }
163     
164     public void setReadOnly() {
165         readOnly = true;
166     }
167
168     public void clearBuffer() {
169         buffer = null;
170     }
171     
172     IOConsoleOutputStream getStream() {
173         return outputStream;
174     }
175 }
176
Popular Tags