001: //This is free software; for terms and warranty disclaimer see ./COPYING.
002:
003: package gnu.jemacs.swt;
004:
005: import gnu.jemacs.buffer.Buffer;
006: import gnu.jemacs.buffer.Marker;
007: import gnu.lists.CharSeq;
008: import gnu.lists.FString;
009: import gnu.mapping.InPort;
010:
011: import java.io.Reader;
012: import java.io.Writer;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.Set;
016:
017: import org.eclipse.swt.custom.StyledTextContent;
018:
019: /**
020: * @author Christian Surlykke
021: * 11-07-2004
022: */
023: public class SwtBuffer extends Buffer {
024:
025: private BufferContent bufferContent = null;
026:
027: public SwtBuffer(String name) {
028: this (name, new BufferContent());
029: }
030:
031: public SwtBuffer(String name, BufferContent content) {
032: super (name);
033: this .bufferContent = content;
034:
035: pointMarker = new Marker(this , 0, true);
036: markMarker = new Marker();
037: redrawModeline();
038: }
039:
040: /**
041: * @see gnu.jemacs.buffer.Buffer#redrawModeline()
042: */
043: public void redrawModeline() {
044: modeline = new FString("-----JEmacs: " + getName() + " ---");
045: }
046:
047: private FString modeline = new FString("");
048:
049: public FString getModelineFormat() {
050: return modeline;
051: }
052:
053: /**
054: * @see gnu.jemacs.buffer.Buffer#getLength()
055: */
056: public int getLength() {
057: return bufferContent.getCharCount();
058: }
059:
060: // ---------------------- The Dot ------------------------------
061: private int dot = 0;
062:
063: /**
064: * @see gnu.jemacs.buffer.Buffer#getDot()
065: */
066: public int getDot() {
067: return dot;
068: }
069:
070: /**
071: * @see gnu.jemacs.buffer.Buffer#setDot(int)
072: */
073: public void setDot(int dot) {
074: this .dot = dot;
075: }
076:
077: /**
078: * @see gnu.jemacs.buffer.Buffer#maxDot()
079: */
080: public int maxDot() {
081: return length();
082: }
083:
084: // ---------------------- Insertion --------------------------
085:
086: /**
087: * @see gnu.jemacs.buffer.Buffer#insert(java.lang.String, java.lang.Object, int)
088: */
089: public void insert(String string, Object style, int ipos) {
090: // TODO Auto-generated method stub
091: }
092:
093: /**
094: * @see gnu.jemacs.buffer.Buffer#insert(char, int, java.lang.Object)
095: */
096: public void insert(char ch, int count, Object style) {
097: // TODO: Handle styles !
098: char[] charr = new char[count];
099:
100: for (int i = 0; i < charr.length; i++) {
101: charr[i] = ch;
102: }
103:
104: bufferContent.replaceTextRange(getDot(), 0, new String(charr));
105: setDot(getDot() + 1);
106: }
107:
108: /**
109: * @see gnu.jemacs.buffer.Buffer#insert(char, int)
110: */
111: public void insert(char ch, int count) {
112: insert(ch, count, null);
113: }
114:
115: /**
116: * @see gnu.jemacs.buffer.Buffer#removeAll()
117: */
118: public void removeAll() {
119: bufferContent.setText("");
120: }
121:
122: /**
123: * @see gnu.jemacs.buffer.Buffer#insertFile(java.io.Reader)
124: */
125: public void insertFile(Reader in) throws Exception {
126: bufferContent.insertFile(in, getDot());
127: }
128:
129: /**
130: * @see gnu.jemacs.buffer.Buffer#save(java.io.Writer)
131: */
132: public void save(Writer out) throws Exception {
133: bufferContent.save(out);
134: }
135:
136: /**
137: * @see gnu.jemacs.buffer.Buffer#removeChar(int)
138: */
139: public void removeChar(int count) {
140: int start = Math.min(getDot(), getDot() + count);
141: int end = Math.max(getDot(), getDot() + count);
142:
143: // Confine interval to be within [0; size()]
144: start = Math.max(0, Math.min(start, size()));
145: end = Math.max(0, Math.min(end, size()));
146:
147: if (start != end) {
148: count = Math.abs(end - start);
149: start = Math.min(start, end);
150: bufferContent.replaceTextRange(start, count, null);
151: setDot(start);
152: }
153: }
154:
155: /**
156: * @see gnu.jemacs.buffer.Buffer#lineStartOffset(int)
157: */
158: public int lineStartOffset(int offset) {
159: return bufferContent.lineStartPos(offset);
160: }
161:
162: /**
163: * @see gnu.jemacs.buffer.Buffer#scan(char, int, int, int, boolean)
164: */
165: public long scan(char target, int start, int end, int count,
166: boolean allowQuit) {
167: // TODO Auto-generated method stub
168: return 0;
169: }
170:
171: public CharSeq getStringContent() {
172: return bufferContent;
173: }
174:
175: /**
176: * @see gnu.jemacs.buffer.Buffer#openReader(int, int)
177: */
178: public InPort openReader(int start, int count) {
179: return new InPort(new BufferContentReader(bufferContent, start,
180: count));
181: }
182:
183: /**
184: * @see gnu.jemacs.buffer.Buffer#savePointMark()
185: */
186: public long savePointMark() {
187: // TODO Auto-generated method stub
188: return 0;
189: }
190:
191: /**
192: * @see gnu.jemacs.buffer.Buffer#restorePointMark(long)
193: */
194: public void restorePointMark(long pointMark) {
195: // TODO Auto-generated method stub
196:
197: }
198:
199: /**
200: * @see gnu.jemacs.buffer.Buffer#invoke(java.lang.Runnable)
201: */
202: public void invoke(Runnable doRun) {
203: // TODO Auto-generated method stub
204:
205: }
206:
207: /**
208: * @see gnu.lists.AbstractSequence#size()
209: */
210: public int size() {
211: return bufferContent.getCharCount();
212: }
213:
214: /**
215: * @see gnu.lists.AbstractSequence#get(int)
216: */
217: public Object get(int index) {
218: throw new UnsupportedOperationException();
219: }
220:
221: /**
222: * @see gnu.lists.AbstractSequence#createPos(int, boolean)
223: */
224: public int createPos(int index, boolean isAfter) {
225: // TODO Auto-generated method stub
226: return 0;
227: }
228:
229: public StyledTextContent getBufferContent() {
230: return bufferContent;
231: }
232:
233: /**
234: * @see gnu.jemacs.buffer.Buffer#forwardLine(int)
235: */
236: public int forwardLine(int lines) {
237: int currentLine = bufferContent.getLineAtOffset(getDot());
238: int newLine = currentLine + lines;
239: if (newLine < 0) {
240: setDot(bufferContent.getOffsetAtLine(0));
241: return newLine;
242: } else if (newLine > bufferContent.getLineCount()) {
243: setDot(bufferContent.getOffsetAtLine(bufferContent
244: .getLineCount()));
245: return newLine - bufferContent.getLineCount();
246: } else {
247: setDot(bufferContent.getOffsetAtLine(newLine));
248: return 0;
249: }
250: }
251: }
|