129
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.projection.ProjectionTextStore;
|
|
14
|
131
|
15 import dwtx.jface.text.projection.ProjectionMapping; // packageimport
|
|
16 import dwtx.jface.text.projection.ChildDocumentManager; // packageimport
|
|
17 import dwtx.jface.text.projection.SegmentUpdater; // packageimport
|
|
18 import dwtx.jface.text.projection.Segment; // packageimport
|
|
19 import dwtx.jface.text.projection.ProjectionDocument; // packageimport
|
|
20 import dwtx.jface.text.projection.FragmentUpdater; // packageimport
|
|
21 import dwtx.jface.text.projection.ProjectionDocumentEvent; // packageimport
|
|
22 import dwtx.jface.text.projection.ChildDocument; // packageimport
|
|
23 import dwtx.jface.text.projection.IMinimalMapping; // packageimport
|
|
24 import dwtx.jface.text.projection.Fragment; // packageimport
|
|
25 import dwtx.jface.text.projection.ProjectionDocumentManager; // packageimport
|
|
26
|
|
27
|
129
|
28 import dwt.dwthelper.utils;
|
|
29
|
|
30
|
|
31 import dwtx.jface.text.BadLocationException;
|
|
32 import dwtx.jface.text.IDocument;
|
|
33 import dwtx.jface.text.IRegion;
|
|
34 import dwtx.jface.text.ITextStore;
|
|
35 import dwtx.jface.text.Region;
|
|
36
|
|
37
|
|
38 /**
|
|
39 * A text store representing the projection defined by the given document
|
|
40 * information mapping.
|
|
41 *
|
|
42 * @since 3.0
|
|
43 */
|
|
44 class ProjectionTextStore : ITextStore {
|
|
45
|
|
46 /**
|
|
47 * Implementation of {@link IRegion} that can be reused
|
|
48 * by setting the offset and the length.
|
|
49 */
|
|
50 private static class ReusableRegion : IRegion {
|
|
51
|
|
52 private int fOffset;
|
|
53 private int fLength;
|
|
54
|
|
55 /*
|
|
56 * @see dwtx.jface.text.IRegion#getLength()
|
|
57 */
|
|
58 public int getLength() {
|
|
59 return fLength;
|
|
60 }
|
|
61
|
|
62 /*
|
|
63 * @see dwtx.jface.text.IRegion#getOffset()
|
|
64 */
|
|
65 public int getOffset() {
|
|
66 return fOffset;
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * Updates this region.
|
|
71 *
|
|
72 * @param offset the new offset
|
|
73 * @param length the new length
|
|
74 */
|
|
75 public void update(int offset, int length) {
|
|
76 fOffset= offset;
|
|
77 fLength= length;
|
|
78 }
|
|
79 }
|
|
80
|
|
81 /** The master document */
|
|
82 private IDocument fMasterDocument;
|
|
83 /** The document information mapping */
|
|
84 private IMinimalMapping fMapping;
|
|
85 /** Internal region used for querying the mapping. */
|
|
86 private ReusableRegion fReusableRegion= new ReusableRegion();
|
|
87
|
|
88
|
|
89 /**
|
|
90 * Creates a new projection text store for the given master document and
|
|
91 * the given document information mapping.
|
|
92 *
|
|
93 * @param masterDocument the master document
|
|
94 * @param mapping the document information mapping
|
|
95 */
|
|
96 public ProjectionTextStore(IDocument masterDocument, IMinimalMapping mapping) {
|
|
97 fMasterDocument= masterDocument;
|
|
98 fMapping= mapping;
|
|
99 }
|
|
100
|
|
101 private void internalError() {
|
|
102 throw new IllegalStateException();
|
|
103 }
|
|
104
|
|
105 /*
|
|
106 * @see dwtx.jface.text.ITextStore#set(java.lang.String)
|
|
107 */
|
|
108 public void set(String contents) {
|
|
109
|
|
110 IRegion masterRegion= fMapping.getCoverage();
|
|
111 if (masterRegion is null)
|
|
112 internalError();
|
|
113
|
|
114 try {
|
|
115 fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), contents);
|
|
116 } catch (BadLocationException e) {
|
|
117 internalError();
|
|
118 }
|
|
119 }
|
|
120
|
|
121 /*
|
|
122 * @see dwtx.jface.text.ITextStore#replace(int, int, java.lang.String)
|
|
123 */
|
|
124 public void replace(int offset, int length, String text) {
|
|
125 fReusableRegion.update(offset, length);
|
|
126 try {
|
|
127 IRegion masterRegion= fMapping.toOriginRegion(fReusableRegion);
|
|
128 fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), text);
|
|
129 } catch (BadLocationException e) {
|
|
130 internalError();
|
|
131 }
|
|
132 }
|
|
133
|
|
134 /*
|
|
135 * @see dwtx.jface.text.ITextStore#getLength()
|
|
136 */
|
|
137 public int getLength() {
|
|
138 return fMapping.getImageLength();
|
|
139 }
|
|
140
|
|
141 /*
|
|
142 * @see dwtx.jface.text.ITextStore#get(int)
|
|
143 */
|
|
144 public char get(int offset) {
|
|
145 try {
|
|
146 int originOffset= fMapping.toOriginOffset(offset);
|
|
147 return fMasterDocument.getChar(originOffset);
|
|
148 } catch (BadLocationException e) {
|
|
149 internalError();
|
|
150 }
|
|
151
|
|
152 // unreachable
|
|
153 return (char) 0;
|
|
154 }
|
|
155
|
|
156 /*
|
|
157 * @see ITextStore#get(int, int)
|
|
158 */
|
|
159 public String get(int offset, int length) {
|
|
160 try {
|
|
161 IRegion[] fragments= fMapping.toExactOriginRegions(new Region(offset, length));
|
|
162 StringBuffer buffer= new StringBuffer();
|
|
163 for (int i= 0; i < fragments.length; i++) {
|
|
164 IRegion fragment= fragments[i];
|
|
165 buffer.append(fMasterDocument.get(fragment.getOffset(), fragment.getLength()));
|
|
166 }
|
|
167 return buffer.toString();
|
|
168 } catch (BadLocationException e) {
|
|
169 internalError();
|
|
170 }
|
|
171
|
|
172 // unreachable
|
|
173 return null;
|
|
174 }
|
|
175 }
|