1 19 20 package org.netbeans.modules.editor.lib2.highlighting; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.swing.text.AttributeSet ; 27 import javax.swing.text.BadLocationException ; 28 import javax.swing.text.Document ; 29 import javax.swing.text.EditorKit ; 30 import javax.swing.text.JTextComponent ; 31 import javax.swing.text.SimpleAttributeSet ; 32 import org.netbeans.api.editor.mimelookup.MimeLookup; 33 import org.netbeans.api.editor.mimelookup.MimePath; 34 import org.netbeans.api.editor.settings.FontColorNames; 35 import org.netbeans.api.editor.settings.FontColorSettings; 36 import org.netbeans.modules.editor.lib2.search.EditorFindSupport; 37 import org.netbeans.spi.editor.highlighting.HighlightsChangeEvent; 38 import org.netbeans.spi.editor.highlighting.HighlightsChangeListener; 39 import org.netbeans.spi.editor.highlighting.HighlightsSequence; 40 import org.netbeans.spi.editor.highlighting.support.AbstractHighlightsContainer; 41 import org.netbeans.spi.editor.highlighting.support.OffsetsBag; 42 import org.openide.util.WeakListeners; 43 44 48 public class TextSearchHighlighting extends AbstractHighlightsContainer implements PropertyChangeListener , HighlightsChangeListener { 49 50 private static final Logger LOG = Logger.getLogger(TextSearchHighlighting.class.getName()); 51 52 public static final String LAYER_TYPE_ID = "org.netbeans.modules.editor.lib2.highlighting.TextSearchHighlighting"; 54 private final MimePath mimePath; 55 private final JTextComponent component; 56 private final Document document; 57 private final OffsetsBag bag; 58 59 60 public TextSearchHighlighting(JTextComponent component) { 61 EditorKit kit = component.getUI().getEditorKit(component); 63 String mimeType = kit == null ? null : kit.getContentType(); 64 this.mimePath = mimeType == null ? MimePath.EMPTY : MimePath.parse(mimeType); 65 66 this.component = component; 67 this.document = component.getDocument(); 68 69 this.bag = new OffsetsBag(document); 70 this.bag.addHighlightsChangeListener(this); 71 72 EditorFindSupport.getInstance().addPropertyChangeListener( 73 WeakListeners.propertyChange(this, EditorFindSupport.getInstance()) 74 ); 75 76 fillInTheBag(); 77 } 78 79 public HighlightsSequence getHighlights(int startOffset, int endOffset) { 80 return bag.getHighlights(startOffset, endOffset); 81 } 82 83 public void highlightChanged(HighlightsChangeEvent event) { 84 fireHighlightsChange(event.getStartOffset(), event.getEndOffset()); 85 } 86 87 public void propertyChange(PropertyChangeEvent evt) { 88 if (evt.getPropertyName() == null || 89 EditorFindSupport.FIND_WHAT.equals(evt.getPropertyName()) || 90 EditorFindSupport.FIND_HIGHLIGHT_SEARCH.equals(evt.getPropertyName())) 91 { 92 fillInTheBag(); 93 } 94 } 95 96 private void fillInTheBag() { 97 document.render(new Runnable () { 98 public void run() { 99 OffsetsBag newBag = new OffsetsBag(document); 100 101 if (LOG.isLoggable(Level.FINE)) { 102 LOG.fine("TSH: filling the bag; enabled = " + isEnabled()); 103 } 104 105 if (isEnabled()) { 106 try { 107 int [] blocks = EditorFindSupport.getInstance().getBlocks( 108 new int [] {-1, -1}, document, 0, document.getLength()); 109 110 assert blocks.length % 2 == 0 : "Wrong number of block offsets"; 111 112 AttributeSet attribs = getAttribs(); 113 for (int i = 0; i < blocks.length / 2; i++) { 114 newBag.addHighlight(blocks[2 * i], blocks[2 * i + 1], attribs); 115 } 116 } catch (BadLocationException e) { 117 LOG.log(Level.WARNING, e.getMessage(), e); 118 } 119 } 120 121 bag.setHighlights(newBag); 122 } 123 }); 124 } 125 126 private boolean isEnabled() { 127 Object prop = EditorFindSupport.getInstance().getFindProperty( 128 EditorFindSupport.FIND_HIGHLIGHT_SEARCH); 129 return (prop instanceof Boolean ) && ((Boolean ) prop).booleanValue(); 130 } 131 132 private AttributeSet getAttribs() { 133 FontColorSettings fcs = MimeLookup.getLookup(mimePath).lookup(FontColorSettings.class); 134 AttributeSet attribs = fcs.getFontColors(FontColorNames.HIGHLIGHT_SEARCH_COLORING); 135 return attribs == null ? SimpleAttributeSet.EMPTY : attribs; 136 } 137 } 138 | Popular Tags |