First commit in a looooooong time. I had connectivity problems.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / text / rules / OuterDocumentView.java
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://solareclipse.sourceforge.net/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  * 
11  * $Id: OuterDocumentView.java,v 1.1 2004-09-02 18:26:29 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui.text.rules;
15
16 import java.util.Iterator;
17 import java.util.List;
18
19 import org.eclipse.jface.text.AbstractDocument;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.DefaultLineTracker;
22 import org.eclipse.jface.text.DocumentEvent;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.ITextStore;
25
26 /**
27  * Outer view to parent document.
28  * 
29  * @author Igor Malinin
30  */
31 public class OuterDocumentView extends AbstractDocument implements
32                 IDocumentView {
33
34         /**
35          * Implements ITextStore based on IDocument.
36          */
37         class TextStore implements ITextStore {
38
39                 /*
40                  * @see ITextStore#set
41                  */
42                 public void set(String txt) {
43                         try {
44                                 parent.replace(0, parent.getLength(), txt);
45                         } catch (BadLocationException x) {
46                                 // cannot happen
47                         }
48                 }
49
50                 /*
51                  * @see ITextStore#replace
52                  */
53                 public void replace(int offset, int length, String txt) {
54                         try {
55                                 int start = getParentOffset(offset);
56                                 int end = getParentOffset(offset + length - 1) + 1;
57
58                                 parent.replace(start, end - start, txt);
59                         } catch (BadLocationException x) {
60                                 // ignored as surrounding document should have handled this
61                         }
62                 }
63
64                 /*
65                  * @see ITextStore#getLength
66                  */
67                 public int getLength() {
68                         int length = parent.getLength();
69
70                         Iterator i = ranges.iterator();
71                         while (i.hasNext()) {
72                                 length -= ((FlatNode) i.next()).length;
73                         }
74
75                         return length;
76                 }
77
78                 /*
79                  * @see ITextStore#get
80                  */
81                 public String get(int offset, int length) {
82                         StringBuffer buf = new StringBuffer(length);
83
84                         try {
85                                 FlatNode range = null;
86
87                                 Iterator i = ranges.iterator();
88                                 while (i.hasNext()) {
89                                         range = (FlatNode) i.next();
90
91                                         if (offset < range.offset) {
92                                                 break;
93                                         }
94
95                                         offset += range.length;
96
97                                         range = null;
98                                 }
99
100                                 int remainder = length - buf.length();
101                                 while (remainder > 0) {
102                                         if (range == null || offset + remainder < range.offset) {
103                                                 buf.append(parent.get(offset, remainder));
104                                                 break;
105                                         }
106
107                                         buf.append(parent.get(offset, range.offset - offset));
108                                         offset = range.offset + range.length;
109                                         range = i.hasNext() ? (FlatNode) i.next() : null;
110
111                                         remainder = length - buf.length();
112                                 }
113                         } catch (BadLocationException x) {
114                                 return null;
115                         }
116
117                         return buf.toString();
118                 }
119
120                 /*
121                  * @see ITextStore#get
122                  */
123                 public char get(int offset) {
124                         try {
125                                 return parent.getChar(getParentOffset(offset));
126                         } catch (BadLocationException x) {
127                         }
128
129                         return (char) 0;
130                 }
131         }
132
133         /** The parent document */
134         IDocument parent;
135
136         /** The section inside the parent document */
137         List ranges;
138
139         /**
140          * Constructs outer view to parent document.
141          * 
142          * @param parent
143          *            parent document
144          */
145         public OuterDocumentView(IDocument parent, List ranges) {
146                 this.parent = parent;
147                 this.ranges = ranges;
148
149                 setTextStore(new TextStore());
150                 setLineTracker(new DefaultLineTracker());
151                 getTracker().set(getStore().get(0, getLength()));
152
153                 completeInitialization();
154         }
155
156 //      public void addRange(Position range) {
157 //              DocumentEvent event = new DocumentEvent(this,
158 //                      getLocalOffset(range.offset), range.length, "");
159 //              fireDocumentAboutToBeChanged(event);
160 //              ranges.add(-getIndex(range) - 1, range);
161 //              fireDocumentChanged(event);
162 //      }
163 //
164 //      public void removeRange(Position range) {
165 //              String text;
166 //              try {
167 //                      text = parent.get(range.offset, range.length);
168 //              } catch (BadLocationException e) {
169 //                      return;
170 //              }
171 //              DocumentEvent event = new DocumentEvent(this,
172 //                      getLocalOffset(range.offset), 0, text);
173 //              fireDocumentAboutToBeChanged(event);
174 //              deleteRange(range);
175 //              fireDocumentChanged(event);
176 //      }
177 //
178 //      public void deleteRange(Position range) {
179 //              ranges.remove(getIndex(range));
180 //      }
181 //
182 //      private int getIndex(Position range) {
183 //              return Collections.binarySearch(ranges, range, new Comparator() {
184 //                      public int compare(Object o1, Object o2) {
185 //                              int offset1 = ((Position) o1).offset;
186 //                              int offset2 = ((Position) o2).offset;
187 //
188 //                              if (offset1 < offset2) return -1;
189 //                              if (offset1 > offset2) return 1;
190 //                              return 0;
191 //                      }
192 //              });
193 //      }
194
195         /*
196          * @see org.eclipse.jface.text.AbstractDocument#fireDocumentAboutToBeChanged(DocumentEvent)
197          */
198         protected void fireDocumentAboutToBeChanged(DocumentEvent event) {
199                 super.fireDocumentAboutToBeChanged(event);
200         }
201
202         /*
203          * @see org.eclipse.jface.text.AbstractDocument#fireDocumentChanged(DocumentEvent)
204          */
205         protected void fireDocumentChanged(DocumentEvent event) {
206                 try {
207                         // TODO: move to a better place
208                         getTracker().replace(event.getOffset(), event.getLength(),
209                                         event.getText());
210                 } catch (BadLocationException x) {
211                 }
212
213                 super.fireDocumentChanged(event);
214         }
215
216         /*
217          * @see net.sourceforge.phpeclipse.text.rules.IDocumentView#getParentDocument()
218          */
219         public IDocument getParentDocument() {
220                 return parent;
221         }
222
223         /*
224          * @see net.sourceforge.phpeclipse.text.rules.IDocumentView#getParentOffset(int)
225          */
226         public int getParentOffset(int localOffset) {
227                 int offset = localOffset;
228
229                 Iterator i = ranges.iterator();
230                 while (i.hasNext()) {
231                         FlatNode range = (FlatNode) i.next();
232
233                         if (offset < range.offset) {
234                                 break;
235                         }
236
237                         offset += range.length;
238                 }
239
240                 return offset;
241         }
242
243         /*
244          * @see net.sf.wdte.text.rules.IDocumentView#getLocalOffset(int)
245          */
246         public int getLocalOffset(int parentOffset) {
247 //          Assert.isTrue(parentOffset>=0);
248                 int localOffset = parentOffset;
249
250                 Iterator i = ranges.iterator();
251                 while (i.hasNext()) {
252                         FlatNode range = (FlatNode) i.next();
253
254                         if (parentOffset <= range.offset) {
255                                 break;
256                         }
257
258                         if (parentOffset <= range.offset + range.length) {
259                                 localOffset -= parentOffset - range.offset;
260                                 break;
261                         }
262
263                         localOffset -= range.length;
264                 }
265                 // TODO jsurfer change start - check this
266 //        if (localOffset<0) {
267 //          return 0;
268 //        }
269         // jsurfer change end
270                 return localOffset;
271         }
272 }