1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.util.Collections ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import javax.swing.event.ChangeEvent ; 26 import javax.swing.event.ChangeListener ; 27 28 34 public class ChangeSupport { 35 36 38 private final Object source; 39 private List <ChangeListener > listeners = Collections.emptyList(); 40 41 public ChangeSupport(Object source) { 42 this.source = source; 43 } 44 45 public synchronized void addChangeListener(ChangeListener listener) { 46 List <ChangeListener > newListeners = new LinkedList <ChangeListener >(listeners); 47 newListeners.add(listener); 48 listeners = newListeners; 49 } 50 51 public synchronized void removeChangeListener(ChangeListener listener) { 52 List <ChangeListener > newListeners = new LinkedList <ChangeListener >(listeners); 53 newListeners.remove(listener); 54 listeners = newListeners; 55 } 56 57 public void fireChange() { 58 fireChange(new ChangeEvent (source)); 59 } 60 61 public void fireChange(ChangeEvent event) { 62 List <ChangeListener > listenersCopy; 63 synchronized (this) { 64 listenersCopy = listeners; 65 } 66 for (ChangeListener listener : listenersCopy) { 67 listener.stateChanged(event); 68 } 69 } 70 71 public synchronized int getListenerCount() { 72 return listeners.size(); 73 } 74 } 75 | Popular Tags |