1 19 20 package org.netbeans.modules.tasklist.client; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import javax.swing.event.ChangeEvent ; 25 import javax.swing.event.ChangeListener ; 26 import javax.swing.event.EventListenerList ; 27 import javax.swing.event.ListDataEvent ; 28 import javax.swing.event.ListDataListener ; 29 30 38 public class StaticSuggestions { 39 private static StaticSuggestions instance = new StaticSuggestions(); 40 41 46 public static StaticSuggestions getDefault() { 47 return instance; 48 } 49 50 private List all = new ArrayList (); 51 private EventListenerList listenerList = new EventListenerList (); 52 53 56 private StaticSuggestions() { 57 } 58 59 64 public synchronized void add(Suggestion s) { 65 all.add(s); 66 fireIntervalAdded(all.size() - 1, all.size() - 1); 67 } 68 69 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 87 public synchronized Suggestion[] getAll() { 88 return (Suggestion[]) all.toArray(new Suggestion[all.size()]); 89 } 90 91 97 private void fireIntervalAdded(int index0, int index1) { 98 Object [] listeners = listenerList.getListenerList(); 100 101 ChangeEvent changeEvent = null; 104 for (int i = listeners.length - 2; i >= 0; i -= 2) { 105 if (listeners[i] == ListDataListener .class) { 106 if (changeEvent == null) 108 changeEvent = new ChangeEvent (this); 109 ((ListDataListener )listeners[i+1]).intervalAdded( 110 new ListDataEvent (this, ListDataEvent.INTERVAL_ADDED, 111 index0, index1)); 112 } 113 } 114 } 115 116 122 private void fireIntervalRemoved(int index0, int index1) { 123 Object [] listeners = listenerList.getListenerList(); 125 126 ChangeEvent changeEvent = null; 129 for (int i = listeners.length - 2; i >= 0; i -= 2) { 130 if (listeners[i] == ListDataListener .class) { 131 if (changeEvent == null) 133 changeEvent = new ChangeEvent (this); 134 ((ListDataListener )listeners[i+1]).intervalAdded( 135 new ListDataEvent (this, ListDataEvent.INTERVAL_REMOVED, 136 index0, index1)); 137 } 138 } 139 } 140 141 146 public void removeListener(ListDataListener l) { 147 this.listenerList.remove(ListDataListener .class, l); 148 } 149 150 156 public void addListener(ListDataListener l) { 157 this.listenerList.add(ListDataListener .class, l); 158 } 159 } 160 | Popular Tags |