introduce new event system bus

This commit is contained in:
Marcel Haßlinger 2021-11-02 16:43:13 +01:00
parent 91c9ab1496
commit 2b36b355e4
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,55 @@
package de.marhali.easyi18n;
import de.marhali.easyi18n.model.BusListener;
import de.marhali.easyi18n.model.TranslationData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Set;
/**
* Data-bus which is used to distribute changes regarding translations or ui tools to the participating components.
* @author marhali
*/
public class DataBus {
private final Set<BusListener> listener;
protected DataBus() {
this.listener = new HashSet<>();
}
/**
* Adds a participant to the event bus. Every participant needs to be added manually.
* @param listener Bus listener
*/
public void addListener(BusListener listener) {
this.listener.add(listener);
}
/**
* Fires the called events on the returned prototype.
* The event will be distributed to all participants which were registered at execution time.
* @return Listener prototype
*/
public BusListener propagate() {
return new BusListener() {
@Override
public void onUpdateData(@NotNull TranslationData data) {
listener.forEach(li -> li.onUpdateData(data));
}
@Override
public void onFocusKey(@Nullable String key) {
listener.forEach(li -> li.onFocusKey(key));
}
@Override
public void onSearchQuery(@Nullable String query) {
listener.forEach(li -> li.onSearchQuery(query));
}
};
}
}

View File

@ -0,0 +1,29 @@
package de.marhali.easyi18n.model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Interface for communication of changes for participants of the data bus.
* @author marhali
*/
public interface BusListener {
/**
* Update the translations based on the supplied data.
* @param data Updated translations
*/
void onUpdateData(@NotNull TranslationData data);
/**
* Move the specified translation key (full-key) into focus.
* @param key Absolute translation key
*/
void onFocusKey(@Nullable String key);
/**
* Filter the displayed data according to the search query. Supply 'null' to return to the normal state.
* The keys and the content itself should be considered.
* @param query Filter key or content
*/
void onSearchQuery(@Nullable String query);
}