KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > 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.retouche.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  * This file is originally from Retouche, the Java Support
36  * infrastructure in NetBeans. I have modified the file as little
37  * as possible to make merging Retouche fixes back as simple as
38  * possible.
39  *
40  *
41  * @author Jan Lahoda
42  */

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