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
9 * Igor Malinin - initial contribution
11 * $Id: OuterDocumentView.java,v 1.3 2004-09-25 22:42:52 jsurfer Exp $
14 package net.sourceforge.phpeclipse.ui.text.rules;
16 import java.util.Iterator;
17 import java.util.List;
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;
27 * Outer view to parent document.
29 * @author Igor Malinin
31 public class OuterDocumentView extends AbstractDocument implements
35 * Implements ITextStore based on IDocument.
37 class TextStore implements ITextStore {
42 public void set(String txt) {
44 parent.replace(0, parent.getLength(), txt);
45 } catch (BadLocationException x) {
51 * @see ITextStore#replace
53 public void replace(int offset, int length, String txt) {
55 int start = getParentOffset(offset);
56 int end = getParentOffset(offset + length - 1) + 1;
58 parent.replace(start, end - start, txt);
59 } catch (BadLocationException x) {
60 // ignored as surrounding document should have handled this
65 * @see ITextStore#getLength
67 public int getLength() {
68 int length = parent.getLength();
70 Iterator i = ranges.iterator();
72 length -= ((FlatNode) i.next()).length;
81 public String get(int offset, int length) {
82 StringBuffer buf = new StringBuffer(length);
85 FlatNode range = null;
87 Iterator i = ranges.iterator();
89 range = (FlatNode) i.next();
91 if (offset < range.offset) {
95 offset += range.length;
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));
107 buf.append(parent.get(offset, range.offset - offset));
108 offset = range.offset + range.length;
109 range = i.hasNext() ? (FlatNode) i.next() : null;
111 remainder = length - buf.length();
113 } catch (BadLocationException x) {
117 return buf.toString();
121 * @see ITextStore#get
123 public char get(int offset) {
125 return parent.getChar(getParentOffset(offset));
126 } catch (BadLocationException x) {
133 /** The parent document */
136 /** The section inside the parent document */
140 * Constructs outer view to parent document.
145 public OuterDocumentView(IDocument parent, List ranges) {
146 this.parent = parent;
147 this.ranges = ranges;
149 setTextStore(new TextStore());
150 setLineTracker(new DefaultLineTracker());
151 int length = getLength();
155 getTracker().set(getStore().get(0, length));
157 completeInitialization();
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);
168 // public void removeRange(Position range) {
171 // text = parent.get(range.offset, range.length);
172 // } catch (BadLocationException e) {
175 // DocumentEvent event = new DocumentEvent(this,
176 // getLocalOffset(range.offset), 0, text);
177 // fireDocumentAboutToBeChanged(event);
178 // deleteRange(range);
179 // fireDocumentChanged(event);
182 // public void deleteRange(Position range) {
183 // ranges.remove(getIndex(range));
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;
192 // if (offset1 < offset2) return -1;
193 // if (offset1 > offset2) return 1;
200 * @see org.eclipse.jface.text.AbstractDocument#fireDocumentAboutToBeChanged(DocumentEvent)
202 protected void fireDocumentAboutToBeChanged(DocumentEvent event) {
203 super.fireDocumentAboutToBeChanged(event);
207 * @see org.eclipse.jface.text.AbstractDocument#fireDocumentChanged(DocumentEvent)
209 protected void fireDocumentChanged(DocumentEvent event) {
211 // TODO: move to a better place
212 getTracker().replace(event.getOffset(), event.getLength(),
214 } catch (IndexOutOfBoundsException x) {
216 } catch (BadLocationException x) {
219 super.fireDocumentChanged(event);
223 * @see net.sourceforge.phpeclipse.text.rules.IDocumentView#getParentDocument()
225 public IDocument getParentDocument() {
230 * @see net.sourceforge.phpeclipse.text.rules.IDocumentView#getParentOffset(int)
232 public int getParentOffset(int localOffset) {
233 int offset = localOffset;
235 Iterator i = ranges.iterator();
236 while (i.hasNext()) {
237 FlatNode range = (FlatNode) i.next();
239 if (offset < range.offset) {
243 offset += range.length;
250 * @see net.sf.wdte.text.rules.IDocumentView#getLocalOffset(int)
252 public int getLocalOffset(int parentOffset) {
253 // Assert.isTrue(parentOffset>=0);
254 int localOffset = parentOffset;
256 Iterator i = ranges.iterator();
257 while (i.hasNext()) {
258 FlatNode range = (FlatNode) i.next();
260 if (parentOffset <= range.offset) {
264 if (parentOffset <= range.offset + range.length) {
265 localOffset -= parentOffset - range.offset;
269 localOffset -= range.length;
271 // TODO jsurfer change start - check this
272 // if (localOffset<0) {
275 // jsurfer change end