KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > editor > highlighting > support > AbstractHighlightsContainer


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.spi.editor.highlighting.support;
21
22 import java.util.List JavaDoc;
23 import org.netbeans.lib.editor.util.ListenerList;
24 import org.netbeans.spi.editor.highlighting.HighlightsChangeEvent;
25 import org.netbeans.spi.editor.highlighting.HighlightsChangeListener;
26 import org.netbeans.spi.editor.highlighting.HighlightsContainer;
27 import org.netbeans.spi.editor.highlighting.HighlightsSequence;
28
29 /**
30  * The default implementation of the <code>HighlightsContainer</code> interface.
31  * It provides standard implementation of the methods for adding and removing
32  * <code>HighlightsChangeListener</code>s and allows subclasses to notify listeners
33  * by calling the <code>fireHighlightsChange</code> method.
34  *
35  * @author Vita Stejskal
36  */

37 public abstract class AbstractHighlightsContainer implements HighlightsContainer {
38     
39     private ListenerList<HighlightsChangeListener> listeners = new ListenerList<HighlightsChangeListener>();
40     
41     /** Creates a new instance of AbstractHighlightsContainer */
42     protected AbstractHighlightsContainer() {
43     }
44
45     public abstract HighlightsSequence getHighlights(int startOffset, int endOffset);
46
47     /**
48      * Adds <code>HighlightsChangeListener</code> to this container.
49      *
50      * @param listener The listener to add.
51      */

52     public final void addHighlightsChangeListener(HighlightsChangeListener listener) {
53         synchronized (listeners) {
54             listeners.add(listener);
55         }
56     }
57
58     /**
59      * Removes <code>HighlightsChangeListener</code> to this container.
60      *
61      * @param listener The listener to remove.
62      */

63     public final void removeHighlightsChangeListener(HighlightsChangeListener listener) {
64         synchronized (listeners) {
65             listeners.remove(listener);
66         }
67     }
68     
69     /**
70      * Notifies all registered listeners about a change in this container. The
71      * area of a document where highlights changed is specified by the
72      * <code>changeStartOffset</code> and <code>changeEndOffset</code> parameters.
73      *
74      * @param changeStartOffset The starting offset of the changed area.
75      * @param changeEndOffset The ending offset of the changed area.
76      */

77     protected final void fireHighlightsChange(int changeStartOffset, int changeEndOffset) {
78         List JavaDoc<HighlightsChangeListener> targets;
79         
80         synchronized (listeners) {
81             targets = listeners.getListeners();
82         }
83         
84         if (targets.size() > 0) {
85             HighlightsChangeEvent evt = new HighlightsChangeEvent(this, changeStartOffset, changeEndOffset);
86             
87             for(HighlightsChangeListener l : targets) {
88                 l.highlightChanged(evt);
89             }
90         }
91     }
92 }
93
Popular Tags