From 2b36b355e4069b076adf400c97351b204e82ad34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Ha=C3=9Flinger?= Date: Tue, 2 Nov 2021 16:43:13 +0100 Subject: [PATCH] introduce new event system bus --- .../java/de/marhali/easyi18n/DataBus.java | 55 +++++++++++++++++++ .../marhali/easyi18n/model/BusListener.java | 29 ++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/de/marhali/easyi18n/DataBus.java create mode 100644 src/main/java/de/marhali/easyi18n/model/BusListener.java diff --git a/src/main/java/de/marhali/easyi18n/DataBus.java b/src/main/java/de/marhali/easyi18n/DataBus.java new file mode 100644 index 0000000..56dba9b --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/DataBus.java @@ -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 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)); + } + }; + } +} \ No newline at end of file diff --git a/src/main/java/de/marhali/easyi18n/model/BusListener.java b/src/main/java/de/marhali/easyi18n/model/BusListener.java new file mode 100644 index 0000000..752dde0 --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/model/BusListener.java @@ -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); +} \ No newline at end of file