KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > client > StaticSuggestions


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.modules.tasklist.client;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import javax.swing.event.EventListenerList JavaDoc;
27 import javax.swing.event.ListDataEvent JavaDoc;
28 import javax.swing.event.ListDataListener JavaDoc;
29
30 /**
31  * A list of "static" suggestions.
32  * Those suggestions normally will be updated after a file was
33  * written to the hard disk.
34  * The class is thread-safe.
35  *
36  * @author tl
37  */

38 public class StaticSuggestions {
39     private static StaticSuggestions instance = new StaticSuggestions();
40     
41     /**
42      * Returns the default registry.
43      *
44      * @return registry with statis suggestions
45      */

46     public static StaticSuggestions getDefault() {
47         return instance;
48     }
49             
50     private List JavaDoc all = new ArrayList JavaDoc();
51     private EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
52     
53     /**
54      * Creates a new instance of StaticSuggestions
55      */

56     private StaticSuggestions() {
57     }
58     
59     /**
60      * Registers a suggestion
61      *
62      * @param s a suggestion
63      */

64     public synchronized void add(Suggestion s) {
65         all.add(s);
66         fireIntervalAdded(all.size() - 1, all.size() - 1);
67     }
68     
69     /**
70      * Removes a suggestion.
71      *
72      * @param s suggestion to be removed
73      */

74     public synchronized void remove(Suggestion s) {
75         int index = all.indexOf(s);
76         if (index >= 0) {
77             all.remove(s);
78             fireIntervalRemoved(index, index);
79         }
80     }
81     
82     /**
83      * Returns all registered suggestions.
84      *
85      * @return all registered suggestions.
86      */

87     public synchronized Suggestion[] getAll() {
88         return (Suggestion[]) all.toArray(new Suggestion[all.size()]);
89     }
90     
91     /**
92      * Fires a ListDataEvent
93      *
94      * @param index0 the one end of the intervall
95      * @param index1 the other end of the intervall
96      */

97     private void fireIntervalAdded(int index0, int index1) {
98         // Guaranteed to return a non-null array
99
Object JavaDoc[] listeners = listenerList.getListenerList();
100         
101         // Process the listeners last to first, notifying
102
// those that are interested in this event
103
ChangeEvent JavaDoc changeEvent = null;
104         for (int i = listeners.length - 2; i >= 0; i -= 2) {
105             if (listeners[i] == ListDataListener JavaDoc.class) {
106                 // Lazily create the event:
107
if (changeEvent == null)
108                     changeEvent = new ChangeEvent JavaDoc(this);
109                 ((ListDataListener JavaDoc)listeners[i+1]).intervalAdded(
110                         new ListDataEvent JavaDoc(this, ListDataEvent.INTERVAL_ADDED,
111                         index0, index1));
112             }
113         }
114     }
115     
116     /**
117      * Fires a ListDataEvent
118      *
119      * @param index0 the one end of the intervall
120      * @param index1 the other end of the intervall
121      */

122     private void fireIntervalRemoved(int index0, int index1) {
123         // Guaranteed to return a non-null array
124
Object JavaDoc[] listeners = listenerList.getListenerList();
125         
126         // Process the listeners last to first, notifying
127
// those that are interested in this event
128
ChangeEvent JavaDoc changeEvent = null;
129         for (int i = listeners.length - 2; i >= 0; i -= 2) {
130             if (listeners[i] == ListDataListener JavaDoc.class) {
131                 // Lazily create the event:
132
if (changeEvent == null)
133                     changeEvent = new ChangeEvent JavaDoc(this);
134                 ((ListDataListener JavaDoc)listeners[i+1]).intervalAdded(
135                         new ListDataEvent JavaDoc(this, ListDataEvent.INTERVAL_REMOVED,
136                         index0, index1));
137             }
138         }
139     }
140     
141     /**
142      * Removes a listener.
143      *
144      * @param l the listener that will be removed
145      */

146     public void removeListener(ListDataListener JavaDoc l) {
147         this.listenerList.remove(ListDataListener JavaDoc.class, l);
148     }
149     
150     /**
151      * Adds a listener. The listener will be notified each time
152      * new suggestions were registered or removed from the registry.
153      *
154      * @param l a listener
155      */

156     public void addListener(ListDataListener JavaDoc l) {
157         this.listenerList.add(ListDataListener JavaDoc.class, l);
158     }
159 }
160
Popular Tags