74abf9689633dad51dc2849c5b8dc5cf7579ace1
[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.3 2004-09-25 22:42:52 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                 int length = getLength();
152                 if (length<0) {
153                   length = 0;
154                 }
155                 getTracker().set(getStore().get(0, length));
156  
157                 completeInitialization();
158         }
159
160 //      public void addRange(Position range) {
161 //              DocumentEvent event = new DocumentEvent(this,
162 //                      getLocalOffset(range.offset), range.length, "");
163 //              fireDocumentAboutToBeChanged(event);
164 //              ranges.add(-getIndex(range) - 1, range);
165 //              fireDocumentChanged(event);
166 //      }
167 //
168 //      public void removeRange(Position range) {
169 //              String text;
170 //              try {
171 //                      text = parent.get(range.offset, range.length);
172 //              } catch (BadLocationException e) {
173 //                      return;
174 //              }
175 //              DocumentEvent event = new DocumentEvent(this,
176 //                      getLocalOffset(range.offset), 0, text);
177 //              fireDocumentAboutToBeChanged(event);
178 //              deleteRange(range);
179 //              fireDocumentChanged(event);
180 //      }
181 //
182 //      public void deleteRange(Position range) {
183 //              ranges.remove(getIndex(range));
184 //      }
185 //
186 //      private int getIndex(Position range) {
187 //              return Collections.binarySearch(ranges, range, new Comparator() {
188 //                      public int compare(Object o1, Object o2) {
189 //                              int offset1 = ((Position) o1).offset;
190 //                              int offset2 = ((Position) o2).offset;
191 //
192 //                              if (offset1 < offset2) return -1;
193 //                              if (offset1 > offset2) return 1;
194 //                              return 0;
195 //                      }
196 //              });
197 //      }
198
199         /*
200          * @see org.eclipse.jface.text.AbstractDocument#fireDocumentAboutToBeChanged(DocumentEvent)
201          */
202         protected void fireDocumentAboutToBeChanged(DocumentEvent event) {
203                 super.fireDocumentAboutToBeChanged(event);
204         }
205
206         /*
207          * @see org.eclipse.jface.text.AbstractDocument#fireDocumentChanged(DocumentEvent)
208          */
209         protected void fireDocumentChanged(DocumentEvent event) {
210                 try {
211                         // TODO: move to a better place
212                         getTracker().replace(event.getOffset(), event.getLength(),
213                                         event.getText());
214                 } catch (IndexOutOfBoundsException x) {
215                   
216                 } catch (BadLocationException x) {
217                 }
218
219                 super.fireDocumentChanged(event);
220         }
221
222         /*
223          * @see net.sourceforge.phpeclipse.text.rules.IDocumentView#getParentDocument()
224          */
225         public IDocument getParentDocument() {
226                 return parent;
227         }
228
229         /*
230          * @see net.sourceforge.phpeclipse.text.rules.IDocumentView#getParentOffset(int)
231          */
232         public int getParentOffset(int localOffset) {
233                 int offset = localOffset;
234
235                 Iterator i = ranges.iterator();
236                 while (i.hasNext()) {
237                         FlatNode range = (FlatNode) i.next();
238
239                         if (offset < range.offset) {
240                                 break;
241                         }
242
243                         offset += range.length;
244                 }
245
246                 return offset;
247         }
248
249         /*
250          * @see net.sf.wdte.text.rules.IDocumentView#getLocalOffset(int)
251          */
252         public int getLocalOffset(int parentOffset) {
253 //          Assert.isTrue(parentOffset>=0);
254                 int localOffset = parentOffset;
255
256                 Iterator i = ranges.iterator();
257                 while (i.hasNext()) {
258                         FlatNode range = (FlatNode) i.next();
259
260                         if (parentOffset <= range.offset) {
261                                 break;
262                         }
263
264                         if (parentOffset <= range.offset + range.length) {
265                                 localOffset -= parentOffset - range.offset;
266                                 break;
267                         }
268
269                         localOffset -= range.length;
270                 }
271                 // TODO jsurfer change start - check this
272 //        if (localOffset<0) {
273 //          return 0;
274 //        }
275         // jsurfer change end
276                 return localOffset;
277         }
278 }