fixed "replace all" bug
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / text / rules / InnerDocumentView.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: InnerDocumentView.java,v 1.2 2004-09-22 18:51:51 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui.text.rules;
15
16 import org.eclipse.jface.text.AbstractDocument;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.DefaultLineTracker;
19 import org.eclipse.jface.text.DocumentEvent;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.ITextStore;
22
23 /**
24  * Inner view to parent document.
25  * 
26  * @author Igor Malinin
27  */
28 public class InnerDocumentView extends AbstractDocument implements IDocumentView {
29
30   /**
31    * Implements ITextStore based on IDocument.
32    */
33   class TextStore implements ITextStore {
34
35     /*
36      * @see ITextStore#set
37      */
38     public void set(String txt) {
39       try {
40         parent.replace(range.offset, range.length, txt);
41       } catch (BadLocationException x) {
42       }
43     }
44
45     /*
46      * @see ITextStore#replace
47      */
48     public void replace(int offset, int length, String txt) {
49       try {
50         parent.replace(range.offset + offset, length, txt);
51       } catch (BadLocationException x) {
52       }
53     }
54
55     /*
56      * @see ITextStore#getLength
57      */
58     public int getLength() {
59       return range.length;
60     }
61
62     /*
63      * @see ITextStore#get
64      */
65     public String get(int offset, int length) {
66       try {
67         return parent.get(range.offset + offset, length);
68       } catch (BadLocationException x) {
69       }
70
71       return null;
72     }
73
74     /*
75      * @see ITextStore#get
76      */
77     public char get(int offset) {
78       try {
79         return parent.getChar(range.offset + offset);
80       } catch (BadLocationException x) {
81       }
82
83       return (char) 0;
84     }
85   }
86
87   /** The parent document */
88   IDocument parent;
89
90   /** The section inside the parent document */
91   ViewNode range;
92
93   /**
94    * Constructs inner view to parent document.
95    * 
96    * @param parent
97    *          parent document
98    * @param range
99    */
100   public InnerDocumentView(IDocument parent, ViewNode range) {
101     this.parent = parent;
102     this.range = range;
103
104     setTextStore(new TextStore());
105     setLineTracker(new DefaultLineTracker());
106     getTracker().set(getStore().get(0, getLength()));
107     completeInitialization();
108   }
109
110   /*
111    * @see net.sourceforge.phpeclipse.text.rules.IDocumentView#getParentDocument()
112    */
113   public IDocument getParentDocument() {
114     return parent;
115   }
116
117   /*
118    * @see org.eclipse.jface.text.AbstractDocument#fireDocumentAboutToBeChanged(DocumentEvent)
119    */
120   protected void fireDocumentAboutToBeChanged(DocumentEvent event) {
121     super.fireDocumentAboutToBeChanged(event);
122   }
123
124   /*
125    * @see org.eclipse.jface.text.AbstractDocument#fireDocumentChanged(DocumentEvent)
126    */
127   protected void fireDocumentChanged(DocumentEvent event) {
128     try {
129       // TODO: move to a better place
130       getTracker().replace(event.getOffset(), event.getLength(), event.getText());
131     } catch (BadLocationException x) {
132     }
133
134     super.fireDocumentChanged(event);
135   }
136
137   /*
138    * @see net.sf.wdte.text.rules.IDocumentView#getParentOffset(int)
139    */
140   public int getParentOffset(int localOffset) {
141     return localOffset + range.offset;
142   }
143
144   /*
145    * @see net.sf.wdte.text.rules.IDocumentView#getLocalOffset(int)
146    */
147   public int getLocalOffset(int parentOffset) {
148     return parentOffset - range.offset;
149   }
150 }