1 11 package org.eclipse.jface.text.projection; 12 13 14 import org.eclipse.jface.text.BadLocationException; 15 import org.eclipse.jface.text.IDocument; 16 import org.eclipse.jface.text.IRegion; 17 import org.eclipse.jface.text.ITextStore; 18 import org.eclipse.jface.text.Region; 19 20 21 27 class ProjectionTextStore implements ITextStore { 28 29 33 private static class ReusableRegion implements IRegion { 34 35 private int fOffset; 36 private int fLength; 37 38 41 public int getLength() { 42 return fLength; 43 } 44 45 48 public int getOffset() { 49 return fOffset; 50 } 51 52 58 public void update(int offset, int length) { 59 fOffset= offset; 60 fLength= length; 61 } 62 } 63 64 65 private IDocument fMasterDocument; 66 67 private IMinimalMapping fMapping; 68 69 private ReusableRegion fReusableRegion= new ReusableRegion(); 70 71 72 79 public ProjectionTextStore(IDocument masterDocument, IMinimalMapping mapping) { 80 fMasterDocument= masterDocument; 81 fMapping= mapping; 82 } 83 84 private void internalError() { 85 throw new IllegalStateException (); 86 } 87 88 91 public void set(String contents) { 92 93 IRegion masterRegion= fMapping.getCoverage(); 94 if (masterRegion == null) 95 internalError(); 96 97 try { 98 fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), contents); 99 } catch (BadLocationException e) { 100 internalError(); 101 } 102 } 103 104 107 public void replace(int offset, int length, String text) { 108 fReusableRegion.update(offset, length); 109 try { 110 IRegion masterRegion= fMapping.toOriginRegion(fReusableRegion); 111 fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), text); 112 } catch (BadLocationException e) { 113 internalError(); 114 } 115 } 116 117 120 public int getLength() { 121 return fMapping.getImageLength(); 122 } 123 124 127 public char get(int offset) { 128 try { 129 int originOffset= fMapping.toOriginOffset(offset); 130 return fMasterDocument.getChar(originOffset); 131 } catch (BadLocationException e) { 132 internalError(); 133 } 134 135 return (char) 0; 137 } 138 139 142 public String get(int offset, int length) { 143 try { 144 IRegion[] fragments= fMapping.toExactOriginRegions(new Region(offset, length)); 145 StringBuffer buffer= new StringBuffer (); 146 for (int i= 0; i < fragments.length; i++) { 147 IRegion fragment= fragments[i]; 148 buffer.append(fMasterDocument.get(fragment.getOffset(), fragment.getLength())); 149 } 150 return buffer.toString(); 151 } catch (BadLocationException e) { 152 internalError(); 153 } 154 155 return null; 157 } 158 } 159 | Popular Tags |