1 11 12 package org.eclipse.jface.text; 13 14 import org.eclipse.core.runtime.Assert; 15 16 28 public class CopyOnWriteTextStore implements ITextStore { 29 30 35 private static class StringTextStore implements ITextStore { 36 37 38 private String fText= ""; 40 43 private StringTextStore() { 44 super(); 45 } 46 47 51 private StringTextStore(String text) { 52 super(); 53 set(text); 54 } 55 56 59 public char get(int offset) { 60 return fText.charAt(offset); 61 } 62 63 66 public String get(int offset, int length) { 67 return fText.substring(offset, offset + length); 68 } 69 70 73 public int getLength() { 74 return fText.length(); 75 } 76 77 80 public void replace(int offset, int length, String text) { 81 throw new UnsupportedOperationException (); 83 } 84 85 88 public void set(String text) { 89 fText= text != null ? text : ""; } 91 92 } 93 94 95 protected ITextStore fTextStore= new StringTextStore(); 96 97 98 private final ITextStore fModifiableTextStore; 99 100 108 public CopyOnWriteTextStore(ITextStore modifiableTextStore) { 109 Assert.isNotNull(modifiableTextStore); 110 fTextStore= new StringTextStore(); 111 fModifiableTextStore= modifiableTextStore; 112 } 113 114 117 public char get(int offset) { 118 return fTextStore.get(offset); 119 } 120 121 124 public String get(int offset, int length) { 125 return fTextStore.get(offset, length); 126 } 127 128 131 public int getLength() { 132 return fTextStore.getLength(); 133 } 134 135 138 public void replace(int offset, int length, String text) { 139 if (fTextStore != fModifiableTextStore) { 140 String content= fTextStore.get(0, fTextStore.getLength()); 141 fTextStore= fModifiableTextStore; 142 fTextStore.set(content); 143 } 144 fTextStore.replace(offset, length, text); 145 } 146 147 150 public void set(String text) { 151 fTextStore= new StringTextStore(text); 152 fModifiableTextStore.set(""); } 154 155 } 156 | Popular Tags |