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