filebot/source/net/sourceforge/tuned/MessageBus.java
Reinhard Pointner 0c674849d8 * refactored and simplified transfer api
* use more GlazedLists stuff (EventList, AutoCompleteSupport) and remove obsolete classes (SimpleListModel, TextCompletion)
* don't use SearchResultCache in EpisodeListClient (was only done for better ui interactions)
* removed caching from ResourceManager
* some improvements based on FindBugs warnings
* use args4j for improved argument parsing
* updated ant build script
* more general MessageBus/Handler (use Object as message type instead of string)
* ChecksumComputationService is not a singleton anymore
* TemporaryFolder is always recreated if it is deleted by the user, or another instance shutting down
* Notifications flicker less when one window is removed and the others are layouted
* lots of other refactoring
2008-07-30 22:37:01 +00:00

74 lines
1.5 KiB
Java

package net.sourceforge.tuned;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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>>();
private MessageBus() {
}
public synchronized void addMessageHandler(String topic, MessageHandler handler) {
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) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
for (MessageHandler handler : getHandlers(topic.toLowerCase())) {
handler.handle(topic.toLowerCase(), messages);
}
}
});
}
}