comparison dwtx/jface/text/projection/ProjectionTextStore.d @ 129:eb30df5ca28b

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