Mercurial > projects > dwt-addons
comparison dwtx/jface/text/CopyOnWriteTextStore.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) 2005, 2008 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 * Anton Leherbauer (anton.leherbauer@windriver.com) - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.CopyOnWriteTextStore; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import dwtx.core.runtime.Assert; | |
18 | |
19 | |
20 /** | |
21 * Copy-on-write <code>ITextStore</code> wrapper. | |
22 * <p> | |
23 * This implementation uses an unmodifiable text store for the initial content. | |
24 * Upon first modification attempt, the unmodifiable store is replaced with | |
25 * a modifiable instance which must be supplied in the constructor.</p> | |
26 * <p> | |
27 * This class is not intended to be subclassed. | |
28 * </p> | |
29 * | |
30 * @since 3.2 | |
31 * @noextend This class is not intended to be subclassed by clients. | |
32 */ | |
33 public class CopyOnWriteTextStore : ITextStore { | |
34 | |
35 /** | |
36 * An unmodifiable String based text store. It is not possible to modify the content | |
37 * other than using {@link #set}. Trying to {@link #replace} a text range will | |
38 * throw an <code>UnsupportedOperationException</code>. | |
39 */ | |
40 private static class StringTextStore : ITextStore { | |
41 | |
42 /** Represents the content of this text store. */ | |
43 private String fText= ""; //$NON-NLS-1$ | |
44 | |
45 /** | |
46 * Create an empty text store. | |
47 */ | |
48 private StringTextStore() { | |
49 super(); | |
50 } | |
51 | |
52 /** | |
53 * Create a text store with initial content. | |
54 * @param text the initial content | |
55 */ | |
56 private StringTextStore(String text) { | |
57 super(); | |
58 set(text); | |
59 } | |
60 | |
61 /* | |
62 * @see dwtx.jface.text.ITextStore#get(int) | |
63 */ | |
64 public char get(int offset) { | |
65 return fText.charAt(offset); | |
66 } | |
67 | |
68 /* | |
69 * @see dwtx.jface.text.ITextStore#get(int, int) | |
70 */ | |
71 public String get(int offset, int length) { | |
72 return fText.substring(offset, offset + length); | |
73 } | |
74 | |
75 /* | |
76 * @see dwtx.jface.text.ITextStore#getLength() | |
77 */ | |
78 public int getLength() { | |
79 return fText.length(); | |
80 } | |
81 | |
82 /* | |
83 * @see dwtx.jface.text.ITextStore#replace(int, int, java.lang.String) | |
84 */ | |
85 public void replace(int offset, int length, String text) { | |
86 // modification not supported | |
87 throw new UnsupportedOperationException(); | |
88 } | |
89 | |
90 /* | |
91 * @see dwtx.jface.text.ITextStore#set(java.lang.String) | |
92 */ | |
93 public void set(String text) { | |
94 fText= text !is null ? text : ""; //$NON-NLS-1$ | |
95 } | |
96 | |
97 } | |
98 | |
99 /** The underlying "real" text store */ | |
100 protected ITextStore fTextStore= new StringTextStore(); | |
101 | |
102 /** A modifiable <code>ITextStore</code> instance */ | |
103 private final ITextStore fModifiableTextStore; | |
104 | |
105 /** | |
106 * Creates an empty text store. The given text store will be used upon first | |
107 * modification attempt. | |
108 * | |
109 * @param modifiableTextStore | |
110 * a modifiable <code>ITextStore</code> instance, may not be | |
111 * <code>null</code> | |
112 */ | |
113 public CopyOnWriteTextStore(ITextStore modifiableTextStore) { | |
114 Assert.isNotNull(modifiableTextStore); | |
115 fTextStore= new StringTextStore(); | |
116 fModifiableTextStore= modifiableTextStore; | |
117 } | |
118 | |
119 /* | |
120 * @see dwtx.jface.text.ITextStore#get(int) | |
121 */ | |
122 public char get(int offset) { | |
123 return fTextStore.get(offset); | |
124 } | |
125 | |
126 /* | |
127 * @see dwtx.jface.text.ITextStore#get(int, int) | |
128 */ | |
129 public String get(int offset, int length) { | |
130 return fTextStore.get(offset, length); | |
131 } | |
132 | |
133 /* | |
134 * @see dwtx.jface.text.ITextStore#getLength() | |
135 */ | |
136 public int getLength() { | |
137 return fTextStore.getLength(); | |
138 } | |
139 | |
140 /* | |
141 * @see dwtx.jface.text.ITextStore#replace(int, int, java.lang.String) | |
142 */ | |
143 public void replace(int offset, int length, String text) { | |
144 if (fTextStore !is fModifiableTextStore) { | |
145 String content= fTextStore.get(0, fTextStore.getLength()); | |
146 fTextStore= fModifiableTextStore; | |
147 fTextStore.set(content); | |
148 } | |
149 fTextStore.replace(offset, length, text); | |
150 } | |
151 | |
152 /* | |
153 * @see dwtx.jface.text.ITextStore#set(java.lang.String) | |
154 */ | |
155 public void set(String text) { | |
156 fTextStore= new StringTextStore(text); | |
157 fModifiableTextStore.set(""); //$NON-NLS-1$ | |
158 } | |
159 | |
160 } |