1 4 package org.terracotta.dso.editors.xml; 5 6 import org.eclipse.jface.text.BadLocationException; 7 import org.eclipse.jface.text.DocumentEvent; 8 import org.eclipse.jface.text.IDocument; 9 import org.eclipse.jface.text.IRegion; 10 import org.eclipse.jface.text.ITypedRegion; 11 import org.eclipse.jface.text.Region; 12 import org.eclipse.jface.text.TextAttribute; 13 import org.eclipse.jface.text.TextPresentation; 14 import org.eclipse.jface.text.presentation.IPresentationDamager; 15 import org.eclipse.jface.text.presentation.IPresentationRepairer; 16 import org.eclipse.jface.util.Assert; 17 import org.eclipse.swt.custom.StyleRange; 18 19 public class NonRuleBasedDamagerRepairer 20 implements IPresentationDamager, IPresentationRepairer { 21 22 23 protected IDocument fDocument; 24 25 protected TextAttribute fDefaultTextAttribute; 26 27 30 public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) { 31 Assert.isNotNull(defaultTextAttribute); 32 33 fDefaultTextAttribute = defaultTextAttribute; 34 } 35 36 39 public void setDocument(IDocument document) { 40 fDocument = document; 41 } 42 43 51 protected int endOfLineOf(int offset) throws BadLocationException { 52 53 IRegion info = fDocument.getLineInformationOfOffset(offset); 54 if (offset <= info.getOffset() + info.getLength()) 55 return info.getOffset() + info.getLength(); 56 57 int line = fDocument.getLineOfOffset(offset); 58 try { 59 info = fDocument.getLineInformation(line + 1); 60 return info.getOffset() + info.getLength(); 61 } catch (BadLocationException x) { 62 return fDocument.getLength(); 63 } 64 } 65 66 69 public IRegion getDamageRegion( 70 ITypedRegion partition, 71 DocumentEvent event, 72 boolean documentPartitioningChanged) { 73 if (!documentPartitioningChanged) { 74 try { 75 76 IRegion info = 77 fDocument.getLineInformationOfOffset(event.getOffset()); 78 int start = Math.max(partition.getOffset(), info.getOffset()); 79 80 int end = 81 event.getOffset() 82 + (event.getText() == null 83 ? event.getLength() 84 : event.getText().length()); 85 86 if (info.getOffset() <= end 87 && end <= info.getOffset() + info.getLength()) { 88 end = info.getOffset() + info.getLength(); 90 } else 91 end = endOfLineOf(end); 92 93 end = 94 Math.min( 95 partition.getOffset() + partition.getLength(), 96 end); 97 return new Region(start, end - start); 98 99 } catch (BadLocationException x) {} 100 } 101 102 return partition; 103 } 104 105 108 public void createPresentation( 109 TextPresentation presentation, 110 ITypedRegion region) { 111 addRange( 112 presentation, 113 region.getOffset(), 114 region.getLength(), 115 fDefaultTextAttribute); 116 } 117 118 126 protected void addRange( 127 TextPresentation presentation, 128 int offset, 129 int length, 130 TextAttribute attr) { 131 if (attr != null) 132 presentation.addStyleRange( 133 new StyleRange( 134 offset, 135 length, 136 attr.getForeground(), 137 attr.getBackground(), 138 attr.getStyle())); 139 } 140 } | Popular Tags |