1 19 20 package org.netbeans.modules.editor.lib2.highlighting; 21 22 import java.util.logging.Level ; 23 import java.util.logging.Logger ; 24 import javax.swing.text.AttributeSet ; 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.Document ; 27 import javax.swing.text.JTextComponent ; 28 import javax.swing.text.SimpleAttributeSet ; 29 import org.netbeans.api.editor.mimelookup.MimeLookup; 30 import org.netbeans.api.editor.mimelookup.MimePath; 31 import org.netbeans.api.editor.settings.FontColorSettings; 32 import org.netbeans.modules.editor.lib2.search.EditorFindSupport; 33 import org.netbeans.spi.editor.highlighting.HighlightsChangeEvent; 34 import org.netbeans.spi.editor.highlighting.HighlightsChangeListener; 35 import org.netbeans.spi.editor.highlighting.support.AbstractHighlightsContainer; 36 import org.netbeans.spi.editor.highlighting.support.PositionsBag; 37 import org.netbeans.spi.editor.highlighting.HighlightsSequence; 38 39 43 public class BlockHighlighting extends AbstractHighlightsContainer implements HighlightsChangeListener { 44 45 private static final Logger LOG = Logger.getLogger(BlockHighlighting.class.getName()); 46 47 private String layerId; 48 private JTextComponent component; 49 private Document document; 50 private PositionsBag bag; 51 52 public BlockHighlighting(String layerId, JTextComponent component) { 53 this.layerId = layerId; 54 this.component = component; 55 this.document = component.getDocument(); 56 57 this.bag = new PositionsBag(document); 58 this.bag.addHighlightsChangeListener(this); 59 60 EditorFindSupport.getInstance().hookLayer(this, component); 61 } 62 63 public String getLayerTypeId() { 64 return layerId; 65 } 66 67 public HighlightsSequence getHighlights(int startOffset, int endOffset) { 68 return bag.getHighlights(startOffset, endOffset); 69 } 70 71 public void highlightChanged(HighlightsChangeEvent event) { 72 fireHighlightsChange(event.getStartOffset(), event.getEndOffset()); 73 } 74 75 public void highlightBlock(final int startOffset, final int endOffset, final String coloringName) { 76 document.render(new Runnable () { 77 public void run() { 78 if (startOffset < endOffset) { 79 if (LOG.isLoggable(Level.FINE)) { 80 LOG.fine("Highlighting block: [" + startOffset + ", " + endOffset + "]; " + getLayerTypeId()); 81 } 82 83 try { 84 PositionsBag newBag = new PositionsBag(document); 85 newBag.addHighlight( 86 document.createPosition(startOffset), 87 document.createPosition(endOffset), 88 getAttribs(coloringName) 89 ); 90 bag.setHighlights(newBag); 91 } catch (BadLocationException e) { 92 LOG.log(Level.FINE, "Can't add highlight <" + startOffset + 93 ", " + endOffset + ", " + coloringName + ">", e); 94 } 95 } else { 96 if (LOG.isLoggable(Level.FINE)) { 97 LOG.fine("Reseting block highlighs; " + getLayerTypeId()); 98 } 99 100 bag.clear(); 101 } 102 } 103 }); 104 } 105 106 public int [] gethighlightedBlock() { 107 HighlightsSequence sequence = bag.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE); 108 if (sequence.moveNext()) { 109 return new int [] { sequence.getStartOffset(), sequence.getEndOffset() }; 110 } else { 111 return null; 112 } 113 } 114 115 private AttributeSet getAttribs(String coloringName) { 116 FontColorSettings fcs = MimeLookup.getLookup( 117 MimePath.parse(getMimeType())).lookup(FontColorSettings.class); 118 AttributeSet attribs = fcs.getFontColors(coloringName); 119 return attribs == null ? SimpleAttributeSet.EMPTY : attribs; 120 } 121 122 private String getMimeType() { 123 return component.getUI().getEditorKit(component).getContentType(); 124 } 125 } 126 | Popular Tags |