1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-11 03:45:06 -05:00
filebot/source/net/sourceforge/tuned/MessageBus.java
Reinhard Pointner 87e8d830ce * full support for multiple checksum types (SFV, MD5, SHA-1)
notes:
* updated to MigLayout 3.6.3
* better exception handling in *TransferablePolicy
* added checksum toggle button and artwork
* poperly cancel computation tasks on reset
* better "Total Progress" visibility behaviour
* improved checksum table model classes, better update/repaint behaviour
2009-02-15 12:20:43 +00:00

87 lines
1.9 KiB
Java

package net.sourceforge.tuned;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
public class MessageBus {
private static final MessageBus instance = new MessageBus();
public static MessageBus getDefault() {
return instance;
}
private final Map<String, List<MessageHandler>> handlers = new HashMap<String, List<MessageHandler>>();
public synchronized void addMessageHandler(String topic, MessageHandler handler) {
if (handler == null)
return;
List<MessageHandler> list = handlers.get(topic.toLowerCase());
if (list == null) {
list = new ArrayList<MessageHandler>(3);
handlers.put(topic.toLowerCase(), list);
}
list.add(handler);
}
public synchronized void removeMessageHandler(String topic, MessageHandler handler) {
List<MessageHandler> list = handlers.get(topic.toLowerCase());
if (list != null) {
list.remove(handler);
}
}
public synchronized MessageHandler[] getHandlers(String topic) {
List<MessageHandler> list = handlers.get(topic.toLowerCase());
if (list == null)
return new MessageHandler[0];
return list.toArray(new MessageHandler[0]);
}
public void publish(final String topic, final Object... messages) {
if (SwingUtilities.isEventDispatchThread()) {
publishDirect(topic, messages);
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
publishDirect(topic, messages);
}
});
}
}
private void publishDirect(String topic, Object... messages) {
for (MessageHandler handler : getHandlers(topic.toLowerCase())) {
try {
handler.handle(topic.toLowerCase(), messages);
} catch (Exception e) {
Logger.getLogger("global").log(Level.SEVERE, e.getMessage(), e);
}
}
}
}