001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.gsfret.editor.semantic;
042:
043: import java.lang.ref.Reference;
044: import java.lang.ref.WeakReference;
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.Collections;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.WeakHashMap;
051: import javax.swing.text.Document;
052: import org.netbeans.modules.editor.errorstripe.privatespi.Mark;
053: import org.netbeans.modules.editor.errorstripe.privatespi.MarkProvider;
054: import org.netbeans.modules.editor.highlights.spi.Highlight;
055:
056: /**
057: * This file is originally from Retouche, the Java Support
058: * infrastructure in NetBeans. I have modified the file as little
059: * as possible to make merging Retouche fixes back as simple as
060: * possible.
061: *
062: *
063: * @author Jan Lahoda
064: */
065: public class OccurrencesMarkProvider extends MarkProvider {
066:
067: private static Map<Document, Reference<OccurrencesMarkProvider>> providers = new WeakHashMap<Document, Reference<OccurrencesMarkProvider>>();
068:
069: public static synchronized OccurrencesMarkProvider get(Document doc) {
070: Reference<OccurrencesMarkProvider> ref = providers.get(doc);
071: OccurrencesMarkProvider p = ref != null ? ref.get() : null;
072:
073: if (p == null) {
074: providers.put(doc, new WeakReference(
075: p = new OccurrencesMarkProvider()));
076: }
077:
078: return p;
079: }
080:
081: private List<Mark> semantic;
082: private List<Mark> occurrences;
083: private List<Mark> joint;
084:
085: /** Creates a new instance of OccurrencesMarkProvider */
086: private OccurrencesMarkProvider() {
087: semantic = Collections.emptyList();
088: occurrences = Collections.emptyList();
089: joint = Collections.emptyList();
090: }
091:
092: public synchronized List getMarks() {
093: return joint;
094: }
095:
096: public synchronized void setSematic(Collection<Highlight> s) {
097: semantic = new ArrayList<Mark>();
098:
099: for (Highlight h : s) {
100: if (h != null && h instanceof Mark
101: && ((Mark) h).getEnhancedColor() != null)
102: semantic.add((Mark) h);
103: }
104:
105: List<Mark> old = joint;
106:
107: joint = new ArrayList<Mark>();
108:
109: joint.addAll(semantic);
110: joint.addAll(occurrences);
111:
112: firePropertyChange(PROP_MARKS, old, joint);
113: }
114:
115: public synchronized void setOccurrences(Collection<Highlight> s) {
116: occurrences = new ArrayList<Mark>();
117:
118: for (Highlight h : s) {
119: if (h != null && h instanceof Mark
120: && ((Mark) h).getEnhancedColor() != null)
121: occurrences.add((Mark) h);
122: }
123:
124: List<Mark> old = joint;
125:
126: joint = new ArrayList<Mark>();
127:
128: joint.addAll(semantic);
129: joint.addAll(occurrences);
130:
131: firePropertyChange(PROP_MARKS, old, joint);
132: }
133:
134: }
|