1 7 8 package org.jdesktop.swing.event; 9 10 import javax.swing.event.EventListenerList ; 11 12 import java.util.logging.Level ; 13 14 26 public class MessageSourceSupport implements MessageSource, 27 ProgressSource { 28 29 private EventListenerList listeners; 30 private Object source; 31 32 36 public MessageSourceSupport(Object source) { 37 if (source == null) { 38 throw new IllegalArgumentException ("source Object cannot be null"); 39 } 40 this.source = source; 41 this.listeners = new EventListenerList (); 42 } 43 44 public void addMessageListener(MessageListener l) { 45 if (l != null) { 46 listeners.add(MessageListener.class, l); 47 } 48 } 49 50 public void removeMessageListener(MessageListener l) { 51 if (l != null) { 52 listeners.remove(MessageListener.class, l); 53 } 54 } 55 56 public MessageListener[] getMessageListeners() { 57 return (MessageListener[])listeners.getListeners(MessageListener.class); 58 } 59 60 62 public void addProgressListener(ProgressListener l) { 63 if (l != null) { 64 listeners.add(ProgressListener.class, l); 65 } 66 } 67 68 public void removeProgressListener(ProgressListener l) { 69 if (l != null) { 70 listeners.remove(ProgressListener.class, l); 71 } 72 } 73 74 public ProgressListener[] getProgressListeners() { 75 return (ProgressListener[])listeners.getListeners(ProgressListener.class); 76 } 77 78 86 public void fireProgressStarted(int minimum, int maximum) { 87 fireProgressStarted(new ProgressEvent(source, minimum, maximum)); 88 } 89 90 95 public void fireProgressIncremented(int progress) { 96 fireProgressIncremented(new ProgressEvent(source, progress)); 97 } 98 99 100 103 public void fireProgressEnded() { 104 fireProgressEnded(new ProgressEvent(source)); 105 } 106 107 114 public void fireException(Throwable t) { 115 fireMessage(new MessageEvent(source, t, Level.SEVERE)); 116 } 117 118 123 public void fireMessage(String message) { 124 fireMessage(message, Level.INFO); 125 } 126 127 133 public void fireMessage(Object value, Level level) { 134 fireMessage(value, level, 0L); 135 } 136 137 144 public void fireMessage(Object value, Level level, long when) { 145 fireMessage(new MessageEvent(source, value, level, when)); 146 } 147 148 153 public void fireMessage(MessageEvent evt) { 154 if (evt == null) { 155 throw new IllegalArgumentException ("the event should not be null"); 156 } 157 158 MessageListener[] ls = getMessageListeners(); 159 for (int i = 0; i < ls.length; i++) { 160 ls[i].message(evt); 161 } 162 } 163 164 169 public void fireProgressIncremented(ProgressEvent evt) { 170 if (evt == null) { 171 throw new IllegalArgumentException ("the event should not be null"); 172 } 173 174 ProgressListener[] ls = getProgressListeners(); 175 for (int i = 0; i < ls.length; i++) { 176 ls[i].progressIncremented(evt); 177 } 178 } 179 180 185 public void fireProgressStarted(ProgressEvent evt) { 186 if (evt == null) { 187 throw new IllegalArgumentException ("the event should not be null"); 188 } 189 190 ProgressListener[] ls = getProgressListeners(); 191 for (int i = 0; i < ls.length; i++) { 192 ls[i].progressStarted(evt); 193 } 194 } 195 196 201 public void fireProgressEnded(ProgressEvent evt) { 202 if (evt == null) { 203 throw new IllegalArgumentException ("the event should not be null"); 204 } 205 206 ProgressListener[] ls = getProgressListeners(); 207 for (int i = 0; i < ls.length; i++) { 208 ls[i].progressEnded(evt); 209 } 210 } 211 } 212 | Popular Tags |