introduce new event system bus
This commit is contained in:
parent
91c9ab1496
commit
2b36b355e4
55
src/main/java/de/marhali/easyi18n/DataBus.java
Normal file
55
src/main/java/de/marhali/easyi18n/DataBus.java
Normal 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));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
29
src/main/java/de/marhali/easyi18n/model/BusListener.java
Normal file
29
src/main/java/de/marhali/easyi18n/model/BusListener.java
Normal 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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user