KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.logging.Level JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24 import javax.swing.text.AttributeSet JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28 import javax.swing.text.SimpleAttributeSet JavaDoc;
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 /**
40  *
41  * @author vita
42  */

43 public class BlockHighlighting extends AbstractHighlightsContainer implements HighlightsChangeListener {
44
45     private static final Logger JavaDoc LOG = Logger.getLogger(BlockHighlighting.class.getName());
46
47     private String JavaDoc layerId;
48     private JTextComponent JavaDoc component;
49     private Document JavaDoc document;
50     private PositionsBag bag;
51     
52     public BlockHighlighting(String JavaDoc layerId, JTextComponent JavaDoc 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 JavaDoc 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 JavaDoc coloringName) {
76         document.render(new Runnable JavaDoc() {
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 JavaDoc 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 JavaDoc getAttribs(String JavaDoc coloringName) {
116         FontColorSettings fcs = MimeLookup.getLookup(
117             MimePath.parse(getMimeType())).lookup(FontColorSettings.class);
118         AttributeSet JavaDoc attribs = fcs.getFontColors(coloringName);
119         return attribs == null ? SimpleAttributeSet.EMPTY : attribs;
120     }
121     
122     private String JavaDoc getMimeType() {
123         return component.getUI().getEditorKit(component).getContentType();
124     }
125 }
126
Popular Tags