KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > highlights > HighlighterImpl


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 package org.netbeans.modules.editor.highlights;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.WeakHashMap JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import javax.swing.text.Document JavaDoc;
34 import javax.swing.text.JTextComponent JavaDoc;
35 import org.netbeans.editor.EditorUI;
36 import org.netbeans.editor.Registry;
37 import org.netbeans.editor.Utilities;
38 import org.netbeans.modules.editor.highlights.spi.*;
39 import org.openide.filesystems.FileObject;
40 import org.openide.loaders.DataObject;
41 import org.openide.util.WeakSet;
42
43 /**
44  *
45  * @author Jan Lahoda
46  */

47 public final class HighlighterImpl implements PropertyChangeListener JavaDoc, ChangeListener JavaDoc {
48     
49     private static HighlighterImpl INSTANCE = new HighlighterImpl();
50     
51     public static HighlighterImpl getDefault() {
52         return INSTANCE;
53     }
54     
55     private Map JavaDoc/*<JTextComponent, FileObject>*/ comp2FO;
56     private Map JavaDoc/*<FileObject, Collection<JTextComponent>>*/ fo2Comp;
57     
58     private Map JavaDoc/*<JTextComponent, Reference<HighlightLayer>>*/ comp2Layer;
59
60      public HighlighterImpl() {
61         comp2FO = new WeakHashMap JavaDoc/*<JTextComponent, FileObject>*/();
62         fo2Comp = new WeakHashMap JavaDoc/*<FileObject, Collection<JTextComponent>>*/();
63         comp2Layer = new WeakHashMap JavaDoc/*<JTextComponent, Reference<HighlightLayer>>*/();
64     }
65     
66     private HighlightLayer getLayer(JTextComponent JavaDoc c) {
67         Reference JavaDoc r = (Reference JavaDoc) comp2Layer.get(c);
68         
69         if (r == null)
70             return null;
71         
72         return (HighlightLayer) r.get();
73     }
74     
75     synchronized void assureRegistered(JTextComponent JavaDoc c) {
76         if (c == null || getLayer(c) != null)
77             return ;
78         
79         comp2FO.put(c, null);
80         c.addPropertyChangeListener(this);
81         updateFileObjectMapping(c);
82         
83         HighlightLayer layer = new HighlightLayer();
84         
85         comp2Layer.put(c, new WeakReference JavaDoc(layer));
86         
87         EditorUI eui = Utilities.getEditorUI(c);
88         
89         if (eui != null)
90             eui.addLayer(layer, HighlightLayer.VISIBILITY);
91     }
92     
93     private synchronized void updateFileObjectMapping(JTextComponent JavaDoc c) {
94         Document JavaDoc doc = c.getDocument();
95         Object JavaDoc stream = doc.getProperty(Document.StreamDescriptionProperty);
96         
97         FileObject old = (FileObject) comp2FO.put(c, null);
98         
99         if (old != null) {
100             Collection JavaDoc/*<JTextComponent>*/ components = (Collection JavaDoc) fo2Comp.get(old);
101             
102             if (components != null) {
103                 components.remove(c);
104             }
105         }
106                 
107         if (stream != null && stream instanceof DataObject) {
108             FileObject fo = ((DataObject) stream).getPrimaryFile();
109             
110             comp2FO.put(c, fo);
111             getComponents(fo).add(c);
112         }
113         
114     }
115     
116     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
117         if ("document".equals(evt.getPropertyName())) { // NOI18N
118
updateFileObjectMapping((JTextComponent JavaDoc) evt.getSource());
119         }
120     }
121     
122     private Collection JavaDoc/*<JTextComponent>*/ getComponents(FileObject fo) {
123         Collection JavaDoc/*<JTextComponent>*/ components = (Collection JavaDoc) fo2Comp.get(fo);
124         
125         if (components == null) {
126             fo2Comp.put(fo, components = new WeakSet/*<JTextComponent>*/());
127         }
128         
129         return components;
130     }
131     
132     public synchronized void setHighlights(FileObject fo, String JavaDoc type, Collection JavaDoc/*<Highlight>*/ highlights) {
133 // for (JTextComponent c : getComponents(fo)) {
134
for (Iterator JavaDoc i = getComponents(fo).iterator(); i.hasNext(); ) {
135             JTextComponent JavaDoc c = (JTextComponent JavaDoc) i.next();
136             HighlightLayer layer = getLayer(c);
137             
138             if (layer == null)
139                 continue;
140             
141             layer.setHighlights(type, highlights);
142             
143             c.repaint(); //TODO: not very efficient.
144
}
145     }
146
147     public void stateChanged(ChangeEvent JavaDoc e) {
148         assureRegistered(Registry.getMostActiveComponent());
149     }
150     
151 }
152
Popular Tags