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.2 2004-09-22 18:51:51 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 (BadLocationException x) {
217 super.fireDocumentChanged(event);
221 * @see net.sourceforge.phpeclipse.text.rules.IDocumentView#getParentDocument()
223 public IDocument getParentDocument() {
228 * @see net.sourceforge.phpeclipse.text.rules.IDocumentView#getParentOffset(int)
230 public int getParentOffset(int localOffset) {
231 int offset = localOffset;
233 Iterator i = ranges.iterator();
234 while (i.hasNext()) {
235 FlatNode range = (FlatNode) i.next();
237 if (offset < range.offset) {
241 offset += range.length;
248 * @see net.sf.wdte.text.rules.IDocumentView#getLocalOffset(int)
250 public int getLocalOffset(int parentOffset) {
251 // Assert.isTrue(parentOffset>=0);
252 int localOffset = parentOffset;
254 Iterator i = ranges.iterator();
255 while (i.hasNext()) {
256 FlatNode range = (FlatNode) i.next();
258 if (parentOffset <= range.offset) {
262 if (parentOffset <= range.offset + range.length) {
263 localOffset -= parentOffset - range.offset;
267 localOffset -= range.length;
269 // TODO jsurfer change start - check this
270 // if (localOffset<0) {
273 // jsurfer change end