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