KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > projection > ProjectionTextStore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text.projection;
12
13
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextStore;
18 import org.eclipse.jface.text.Region;
19
20
21 /**
22  * A text store representing the projection defined by the given document
23  * information mapping.
24  *
25  * @since 3.0
26  */

27 class ProjectionTextStore implements ITextStore {
28
29     /**
30      * Implementation of {@link IRegion} that can be reused
31      * by setting the offset and the length.
32      */

33     private static class ReusableRegion implements IRegion {
34
35         private int fOffset;
36         private int fLength;
37
38         /*
39          * @see org.eclipse.jface.text.IRegion#getLength()
40          */

41         public int getLength() {
42             return fLength;
43         }
44
45         /*
46          * @see org.eclipse.jface.text.IRegion#getOffset()
47          */

48         public int getOffset() {
49             return fOffset;
50         }
51
52         /**
53          * Updates this region.
54          *
55          * @param offset the new offset
56          * @param length the new length
57          */

58         public void update(int offset, int length) {
59             fOffset= offset;
60             fLength= length;
61         }
62     }
63
64     /** The master document */
65     private IDocument fMasterDocument;
66     /** The document information mapping */
67     private IMinimalMapping fMapping;
68     /** Internal region used for querying the mapping. */
69     private ReusableRegion fReusableRegion= new ReusableRegion();
70
71
72     /**
73      * Creates a new projection text store for the given master document and
74      * the given document information mapping.
75      *
76      * @param masterDocument the master document
77      * @param mapping the document information mapping
78      */

79     public ProjectionTextStore(IDocument masterDocument, IMinimalMapping mapping) {
80         fMasterDocument= masterDocument;
81         fMapping= mapping;
82     }
83
84     private void internalError() {
85         throw new IllegalStateException JavaDoc();
86     }
87
88     /*
89      * @see org.eclipse.jface.text.ITextStore#set(java.lang.String)
90      */

91     public void set(String JavaDoc contents) {
92
93         IRegion masterRegion= fMapping.getCoverage();
94         if (masterRegion == null)
95             internalError();
96
97         try {
98             fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), contents);
99         } catch (BadLocationException e) {
100             internalError();
101         }
102     }
103
104     /*
105      * @see org.eclipse.jface.text.ITextStore#replace(int, int, java.lang.String)
106      */

107     public void replace(int offset, int length, String JavaDoc text) {
108         fReusableRegion.update(offset, length);
109         try {
110             IRegion masterRegion= fMapping.toOriginRegion(fReusableRegion);
111             fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), text);
112         } catch (BadLocationException e) {
113             internalError();
114         }
115     }
116
117     /*
118      * @see org.eclipse.jface.text.ITextStore#getLength()
119      */

120     public int getLength() {
121         return fMapping.getImageLength();
122     }
123
124     /*
125      * @see org.eclipse.jface.text.ITextStore#get(int)
126      */

127     public char get(int offset) {
128         try {
129             int originOffset= fMapping.toOriginOffset(offset);
130             return fMasterDocument.getChar(originOffset);
131         } catch (BadLocationException e) {
132             internalError();
133         }
134
135         // unreachable
136
return (char) 0;
137     }
138
139     /*
140      * @see ITextStore#get(int, int)
141      */

142     public String JavaDoc get(int offset, int length) {
143         try {
144             IRegion[] fragments= fMapping.toExactOriginRegions(new Region(offset, length));
145             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
146             for (int i= 0; i < fragments.length; i++) {
147                 IRegion fragment= fragments[i];
148                 buffer.append(fMasterDocument.get(fragment.getOffset(), fragment.getLength()));
149             }
150             return buffer.toString();
151         } catch (BadLocationException e) {
152             internalError();
153         }
154
155         // unreachable
156
return null;
157     }
158 }
159
Popular Tags