1 11 package org.eclipse.pde.internal.ui.editor.text; 12 13 import org.eclipse.jface.text.BadLocationException; 14 import org.eclipse.jface.text.DocumentEvent; 15 import org.eclipse.jface.text.IDocument; 16 import org.eclipse.jface.text.IRegion; 17 import org.eclipse.jface.text.ITypedRegion; 18 import org.eclipse.jface.text.Region; 19 import org.eclipse.jface.text.TextAttribute; 20 import org.eclipse.jface.text.TextPresentation; 21 import org.eclipse.jface.text.presentation.IPresentationDamager; 22 import org.eclipse.jface.text.presentation.IPresentationRepairer; 23 import org.eclipse.jface.util.Assert; 24 import org.eclipse.swt.custom.StyleRange; 25 26 public class NonRuleBasedDamagerRepairer 27 implements IPresentationDamager, IPresentationRepairer { 28 29 30 protected IDocument fDocument; 31 32 protected TextAttribute fDefaultTextAttribute; 33 34 37 public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) { 38 Assert.isNotNull(defaultTextAttribute); 39 40 fDefaultTextAttribute = defaultTextAttribute; 41 } 42 43 46 public void setDocument(IDocument document) { 47 fDocument = document; 48 } 49 50 58 protected int endOfLineOf(int offset) throws BadLocationException { 59 60 IRegion info = fDocument.getLineInformationOfOffset(offset); 61 if (offset <= info.getOffset() + info.getLength()) 62 return info.getOffset() + info.getLength(); 63 64 int line = fDocument.getLineOfOffset(offset); 65 try { 66 info = fDocument.getLineInformation(line + 1); 67 return info.getOffset() + info.getLength(); 68 } catch (BadLocationException x) { 69 return fDocument.getLength(); 70 } 71 } 72 73 76 public IRegion getDamageRegion( 77 ITypedRegion partition, 78 DocumentEvent event, 79 boolean documentPartitioningChanged) { 80 if (!documentPartitioningChanged) { 81 try { 82 83 IRegion info = fDocument.getLineInformationOfOffset(event.getOffset()); 84 int start = Math.max(partition.getOffset(), info.getOffset()); 85 86 int end = 87 event.getOffset() 88 + (event.getText() == null ? event.getLength() : event.getText().length()); 89 90 if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) { 91 end = info.getOffset() + info.getLength(); 93 } else 94 end = endOfLineOf(end); 95 96 end = Math.min(partition.getOffset() + partition.getLength(), end); 97 return new Region(start, end - start); 98 99 } catch (BadLocationException x) { 100 } 101 } 102 103 return partition; 104 } 105 106 109 public void createPresentation( 110 TextPresentation presentation, 111 ITypedRegion region) { 112 addRange( 113 presentation, 114 region.getOffset(), 115 region.getLength(), 116 fDefaultTextAttribute); 117 } 118 119 127 protected void addRange( 128 TextPresentation presentation, 129 int offset, 130 int length, 131 TextAttribute attr) { 132 if (attr != null) 133 presentation.addStyleRange( 134 new StyleRange( 135 offset, 136 length, 137 attr.getForeground(), 138 attr.getBackground(), 139 attr.getStyle())); 140 } 141 142 146 public void setDefaultTextAttribute(TextAttribute defaultTextAttribute) { 147 fDefaultTextAttribute= defaultTextAttribute; 148 } 149 } 150 | Popular Tags |