KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > CopyOnWriteTextStore


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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  *******************************************************************************/

11
12 package org.eclipse.jface.text;
13
14 import org.eclipse.core.runtime.Assert;
15
16 /**
17  * Copy-on-write <code>ITextStore</code> wrapper.
18  * <p>
19  * This implementation uses an unmodifiable text store for the initial content.
20  * Upon first modification attempt, the unmodifiable store is replaced with
21  * a modifiable instance which must be supplied in the constructor.</p>
22  * <p>
23  * This class is not intended to be subclassed.
24  * </p>
25  *
26  * @since 3.2
27  */

28 public class CopyOnWriteTextStore implements ITextStore {
29
30     /**
31      * An unmodifiable String based text store. It is not possible to modify the content
32      * other than using {@link #set}. Trying to {@link #replace} a text range will
33      * throw an <code>UnsupportedOperationException</code>.
34      */

35     private static class StringTextStore implements ITextStore {
36
37         /** Represents the content of this text store. */
38         private String JavaDoc fText= ""; //$NON-NLS-1$
39

40         /**
41          * Create an empty text store.
42          */

43         private StringTextStore() {
44             super();
45         }
46
47         /**
48          * Create a text store with initial content.
49          * @param text the initial content
50          */

51         private StringTextStore(String JavaDoc text) {
52             super();
53             set(text);
54         }
55
56         /*
57          * @see org.eclipse.jface.text.ITextStore#get(int)
58          */

59         public char get(int offset) {
60             return fText.charAt(offset);
61         }
62
63         /*
64          * @see org.eclipse.jface.text.ITextStore#get(int, int)
65          */

66         public String JavaDoc get(int offset, int length) {
67             return fText.substring(offset, offset + length);
68         }
69
70         /*
71          * @see org.eclipse.jface.text.ITextStore#getLength()
72          */

73         public int getLength() {
74             return fText.length();
75         }
76
77         /*
78          * @see org.eclipse.jface.text.ITextStore#replace(int, int, java.lang.String)
79          */

80         public void replace(int offset, int length, String JavaDoc text) {
81             // modification not supported
82
throw new UnsupportedOperationException JavaDoc();
83         }
84
85         /*
86          * @see org.eclipse.jface.text.ITextStore#set(java.lang.String)
87          */

88         public void set(String JavaDoc text) {
89             fText= text != null ? text : ""; //$NON-NLS-1$
90
}
91
92     }
93
94     /** The underlying "real" text store */
95     protected ITextStore fTextStore= new StringTextStore();
96
97     /** A modifiable <code>ITextStore</code> instance */
98     private final ITextStore fModifiableTextStore;
99
100     /**
101      * Creates an empty text store. The given text store will be used upon first
102      * modification attempt.
103      *
104      * @param modifiableTextStore
105      * a modifiable <code>ITextStore</code> instance, may not be
106      * <code>null</code>
107      */

108     public CopyOnWriteTextStore(ITextStore modifiableTextStore) {
109         Assert.isNotNull(modifiableTextStore);
110         fTextStore= new StringTextStore();
111         fModifiableTextStore= modifiableTextStore;
112     }
113
114     /*
115      * @see org.eclipse.jface.text.ITextStore#get(int)
116      */

117     public char get(int offset) {
118         return fTextStore.get(offset);
119     }
120
121     /*
122      * @see org.eclipse.jface.text.ITextStore#get(int, int)
123      */

124     public String JavaDoc get(int offset, int length) {
125         return fTextStore.get(offset, length);
126     }
127
128     /*
129      * @see org.eclipse.jface.text.ITextStore#getLength()
130      */

131     public int getLength() {
132         return fTextStore.getLength();
133     }
134
135     /*
136      * @see org.eclipse.jface.text.ITextStore#replace(int, int, java.lang.String)
137      */

138     public void replace(int offset, int length, String JavaDoc text) {
139         if (fTextStore != fModifiableTextStore) {
140             String JavaDoc content= fTextStore.get(0, fTextStore.getLength());
141             fTextStore= fModifiableTextStore;
142             fTextStore.set(content);
143         }
144         fTextStore.replace(offset, length, text);
145     }
146
147     /*
148      * @see org.eclipse.jface.text.ITextStore#set(java.lang.String)
149      */

150     public void set(String JavaDoc text) {
151         fTextStore= new StringTextStore(text);
152         fModifiableTextStore.set(""); //$NON-NLS-1$
153
}
154
155 }
156
Popular Tags