KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > highlighting > TextSearchHighlighting


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.lib2.highlighting;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import javax.swing.text.AttributeSet JavaDoc;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.EditorKit JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31 import javax.swing.text.SimpleAttributeSet JavaDoc;
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 /**
45  *
46  * @author Vita Stejskal
47  */

48 public class TextSearchHighlighting extends AbstractHighlightsContainer implements PropertyChangeListener JavaDoc, HighlightsChangeListener {
49
50     private static final Logger JavaDoc LOG = Logger.getLogger(TextSearchHighlighting.class.getName());
51     
52     public static final String JavaDoc LAYER_TYPE_ID = "org.netbeans.modules.editor.lib2.highlighting.TextSearchHighlighting"; //NOI18N
53

54     private final MimePath mimePath;
55     private final JTextComponent JavaDoc component;
56     private final Document JavaDoc document;
57     private final OffsetsBag bag;
58     
59     /** Creates a new instance of TextSearchHighlighting */
60     public TextSearchHighlighting(JTextComponent JavaDoc component) {
61         // Determine the mime type
62
EditorKit JavaDoc kit = component.getUI().getEditorKit(component);
63         String JavaDoc 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 JavaDoc 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 JavaDoc() {
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 JavaDoc 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 JavaDoc 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 JavaDoc prop = EditorFindSupport.getInstance().getFindProperty(
128             EditorFindSupport.FIND_HIGHLIGHT_SEARCH);
129         return (prop instanceof Boolean JavaDoc) && ((Boolean JavaDoc) prop).booleanValue();
130     }
131     
132     private AttributeSet JavaDoc getAttribs() {
133         FontColorSettings fcs = MimeLookup.getLookup(mimePath).lookup(FontColorSettings.class);
134         AttributeSet JavaDoc attribs = fcs.getFontColors(FontColorNames.HIGHLIGHT_SEARCH_COLORING);
135         return attribs == null ? SimpleAttributeSet.EMPTY : attribs;
136     }
137 }
138
Popular Tags