193
|
1 package remoteeditor.editors;
|
|
2
|
|
3 import java.util.LinkedList;
|
|
4 import java.util.List;
|
|
5
|
|
6 import org.eclipse.jface.text.BadLocationException;
|
|
7 import org.eclipse.jface.text.DocumentEvent;
|
|
8 import org.eclipse.jface.text.IDocument;
|
|
9 import org.eclipse.jface.text.IDocumentListener;
|
|
10 import org.eclipse.swt.widgets.Display;
|
|
11
|
|
12 public class REPTextImpl2 implements REPText, IDocumentListener {
|
|
13
|
|
14 private IDocument document;
|
|
15 private Display display;
|
|
16 private String BR = System.getProperty("line.separator");
|
|
17 private List<REPTextListener> textListenerList;
|
|
18 private boolean myInput = false;
|
|
19
|
|
20 public REPTextImpl2(IDocument document, Display display){
|
|
21 this.document = document;
|
|
22 this.display = display;
|
|
23 textListenerList = new LinkedList<REPTextListener>();
|
|
24 document.addDocumentListener(this);
|
|
25 }
|
|
26
|
|
27 public String delete(int lineno) {
|
|
28 int d = lineno - document.getNumberOfLines();
|
|
29 for(int i = 0; i <= d; i++){
|
|
30 increaseLine();
|
|
31 }
|
|
32
|
|
33 String deleted = getLineText(lineno);
|
|
34 try {
|
|
35 final int length = document.getLineLength(lineno);
|
|
36 final int offset = document.getLineOffset(lineno);
|
|
37 edit(offset, length, "");
|
|
38 } catch (BadLocationException e) {
|
|
39 e.printStackTrace();
|
|
40 }
|
|
41 return deleted;
|
|
42 }
|
|
43
|
|
44 public void insert(int lineno, String text) {
|
|
45 int d = lineno - document.getNumberOfLines();
|
|
46 for(int i = 0; i <= d; i++){
|
|
47 increaseLine();
|
|
48 }
|
|
49
|
|
50 try {
|
|
51 final int offset = document.getLineOffset(lineno);
|
|
52 final String changedText = text + BR;
|
|
53 edit(offset, 0, changedText);
|
|
54 } catch (BadLocationException e) {
|
|
55 e.printStackTrace();
|
|
56 }
|
|
57 }
|
|
58
|
|
59 private void increaseLine() {
|
|
60 edit(document.getLength(), 0, BR);
|
|
61 }
|
|
62
|
|
63 private void edit(final int offset, final int length, final String text) {
|
|
64 display.syncExec(new Runnable(){
|
|
65 public void run(){
|
|
66 try {
|
|
67 myInput = true;
|
|
68 document.replace(offset, length, text);
|
|
69 myInput = false;
|
|
70 } catch (BadLocationException e) {
|
|
71 e.printStackTrace();
|
|
72 }
|
|
73 }
|
|
74 });
|
|
75 }
|
|
76
|
|
77 public List<String> list() {
|
|
78 LinkedList<String> list = new LinkedList<String>();
|
|
79 int lines = document.getNumberOfLines();
|
|
80 for(int i = 0; i < lines; i++){
|
|
81 String text = getLineText(i);
|
|
82 list.add(text);
|
|
83 }
|
|
84 return list;
|
|
85 }
|
|
86
|
|
87 private String getLineText(int lineno){
|
|
88 int offset = 0;
|
|
89 int length = 0;
|
|
90 String text = null;
|
|
91 try {
|
|
92 offset = document.getLineOffset(lineno);
|
|
93 length = document.getLineLength(lineno);
|
|
94 String tmp = document.get(offset, length);
|
|
95 text = tmp.replace(BR, "");
|
|
96 } catch (BadLocationException e) {
|
|
97 e.printStackTrace();
|
|
98 }
|
|
99 return text;
|
|
100 }
|
|
101
|
|
102 public void documentAboutToBeChanged(DocumentEvent event) {
|
|
103 if(myInput) return;
|
|
104 int offset = event.getOffset();
|
|
105 int length = event.getLength();
|
|
106
|
|
107 try {
|
|
108 int startLine = document.getLineOfOffset(offset);
|
|
109 int endLine = document.getLineOfOffset(offset + length);
|
|
110 for(int i = startLine; i <= endLine; i++){
|
|
111 String text = getLineText(i);
|
|
112 for(REPTextListener listener : textListenerList){
|
|
113 listener.textDeleted(new REPTextEvent(i, text));
|
|
114 }
|
|
115 }
|
|
116 } catch (BadLocationException e) {
|
|
117 e.printStackTrace();
|
|
118 }
|
|
119
|
|
120 }
|
|
121
|
|
122 public void documentChanged(DocumentEvent event) {
|
|
123 if(myInput) return;
|
|
124 int offset = event.getOffset();
|
|
125 int length = event.getText().length();
|
|
126
|
|
127 try {
|
|
128 int startLine = document.getLineOfOffset(offset);
|
|
129 int endLine = document.getLineOfOffset(offset + length);
|
|
130 for(int i = startLine; i <= endLine; i++){
|
|
131 String text = getLineText(i);
|
|
132 for(REPTextListener listener : textListenerList){
|
|
133 listener.textInserted(new REPTextEvent(i, text));
|
|
134 }
|
|
135 }
|
|
136 } catch (BadLocationException e) {
|
|
137 e.printStackTrace();
|
|
138 }
|
|
139
|
|
140 }
|
|
141
|
|
142 public synchronized void addTextListener(REPTextListener listener) {
|
|
143 Logger.print(listener);
|
|
144 textListenerList.add(listener);
|
|
145 }
|
|
146
|
|
147 public String get(int lineno) {
|
|
148 return getLineText(lineno);
|
|
149 }
|
|
150
|
|
151 public int size() {
|
|
152 return document.getNumberOfLines();
|
|
153 }
|
|
154
|
|
155 }
|