KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > semantic > OccurrencesMarkProvider


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.java.editor.semantic;
20
21 import java.lang.ref.Reference JavaDoc;
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.WeakHashMap JavaDoc;
29 import javax.swing.text.Document JavaDoc;
30 import org.netbeans.modules.editor.errorstripe.privatespi.Mark;
31 import org.netbeans.modules.editor.errorstripe.privatespi.MarkProvider;
32 import org.netbeans.modules.editor.highlights.spi.Highlight;
33
34 /**
35  *
36  * @author Jan Lahoda
37  */

38 public class OccurrencesMarkProvider extends MarkProvider {
39     
40     private static Map JavaDoc<Document JavaDoc, Reference JavaDoc<OccurrencesMarkProvider>> providers = new WeakHashMap JavaDoc<Document JavaDoc, Reference JavaDoc<OccurrencesMarkProvider>>();
41     
42     public static synchronized OccurrencesMarkProvider get(Document JavaDoc doc) {
43         Reference JavaDoc<OccurrencesMarkProvider> ref = providers.get(doc);
44         OccurrencesMarkProvider p = ref != null ? ref.get() : null;
45         
46         if (p == null) {
47             providers.put(doc, new WeakReference JavaDoc(p = new OccurrencesMarkProvider()));
48         }
49         
50         return p;
51     }
52     
53     private List JavaDoc<Mark> semantic;
54     private List JavaDoc<Mark> occurrences;
55     private List JavaDoc<Mark> joint;
56     
57     /** Creates a new instance of OccurrencesMarkProvider */
58     private OccurrencesMarkProvider() {
59         semantic = Collections.emptyList();
60         occurrences = Collections.emptyList();
61         joint = Collections.emptyList();
62     }
63     
64     public synchronized List JavaDoc getMarks() {
65         return joint;
66     }
67     
68     public void setSematic(Collection JavaDoc<Highlight> s) {
69         List JavaDoc<Mark> old;
70         List JavaDoc<Mark> nue;
71         
72         synchronized (this) {
73             semantic = new ArrayList JavaDoc<Mark>();
74             
75             for (Highlight h : s) {
76                 if (h != null && h instanceof Mark && ((Mark) h).getEnhancedColor() != null)
77                     semantic.add((Mark) h);
78             }
79             
80             old = joint;
81             
82             nue = joint = new ArrayList JavaDoc<Mark>();
83             
84             joint.addAll(semantic);
85             joint.addAll(occurrences);
86         }
87         
88         //#85919: fire outside the lock:
89
firePropertyChange(PROP_MARKS, old, nue);
90     }
91     
92     public void setOccurrences(Collection JavaDoc<Highlight> s) {
93         List JavaDoc<Mark> old;
94         List JavaDoc<Mark> nue;
95         
96         synchronized (this) {
97             occurrences = new ArrayList JavaDoc<Mark>();
98             
99             for (Highlight h : s) {
100                 if (h != null && h instanceof Mark && ((Mark) h).getEnhancedColor() != null)
101                     occurrences.add((Mark) h);
102             }
103             
104             old = joint;
105             
106             nue = joint = new ArrayList JavaDoc<Mark>();
107             
108             joint.addAll(semantic);
109             joint.addAll(occurrences);
110         }
111         
112         //#85919: fire outside the lock:
113
firePropertyChange(PROP_MARKS, old, nue);
114     }
115     
116 }
117
Popular Tags