diff --git a/src/main/java/envoy/client/event/Event.java b/src/main/java/envoy/client/event/Event.java index 9db2477..4a3264d 100644 --- a/src/main/java/envoy/client/event/Event.java +++ b/src/main/java/envoy/client/event/Event.java @@ -1,10 +1,10 @@ package envoy.client.event; /** - * Project: envoy-clientChess
- * File: Event.javaEvent.java
+ * Project: envoy-client
+ * File: Event.java
* Created: 04.12.2019
- * + * * @author Kai S. K. Engelbart * @since Envoy v0.2-alpha */ diff --git a/src/main/java/envoy/client/event/EventBus.java b/src/main/java/envoy/client/event/EventBus.java index f6da3f5..06b385f 100644 --- a/src/main/java/envoy/client/event/EventBus.java +++ b/src/main/java/envoy/client/event/EventBus.java @@ -1,8 +1,9 @@ package envoy.client.event; import java.util.ArrayList; -import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * This class handles events by allowing {@link EventHandler} object to register @@ -10,11 +11,12 @@ import java.util.List; * bus.
*
* The event bus is a singleton and can be used across the entire application to - * guarantee the propagation of events. + * guarantee the propagation of events.
+ * * Project: envoy-client
* File: EventBus.java
* Created: 04.12.2019
- * + * * @author Kai S. K. Engelbart * @since Envoy v0.2-alpha */ @@ -22,9 +24,10 @@ public class EventBus { /** * Contains all {@link EventHandler} instances registered at this - * {@link EventBus}. + * {@link EventBus} as values mapped to by their supported {@link Event} + * classes. */ - private List handlers = new ArrayList<>(); + private Map>, List> handlers = new HashMap<>(); /** * The singleton instance of this {@link EventBus} that is used across the @@ -46,26 +49,33 @@ public class EventBus { public static EventBus getInstance() { return eventBus; } /** - * Registers a list of {@link EventHandler} objects to be notified when a - * {@link Event} is dispatched that they are subscribed to. - * - * @param handlers the {@link EventHandler} objects to register + * Registers an {@link EventHandler} to be notified when a + * {@link Event} of a certain type is dispatched. + * + * @param eventClass the class which the {@link EventHandler} is subscribed to + * @param handler the {@link EventHandler} to register * @since Envoy v0.2-alpha */ - public void register(EventHandler... handlers) { this.handlers.addAll(Arrays.asList(handlers)); } + public void register(Class> eventClass, EventHandler handler) { + if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>()); + handlers.get(eventClass).add(handler); + } /** * Dispatches a {@link Event} to every {@link EventHandler} subscribed to it. - * + * * @param event the {@link Event} to dispatch * @since Envoy v0.2-alpha */ - public void dispatch(Event event) { handlers.stream().filter(h -> h.supports().contains(event.getClass())).forEach(h -> h.handle(event)); } + public void dispatch(Event event) { + handlers.keySet().stream().filter(event.getClass()::isAssignableFrom).map(handlers::get).flatMap(List::stream).forEach(h -> h.handle(event)); + } /** - * @return a list of all {@link EventHandler} instances currently registered at - * this {@link EventBus} + * @return a map of all {@link EventHandler} instances currently registered at + * this {@link EventBus} with the {@link Event} classes they are + * subscribed to as keys * @since Envoy v0.2-alpha */ - public List getHandlers() { return handlers; } + public Map>, List> getHandlers() { return handlers; } } diff --git a/src/main/java/envoy/client/event/EventHandler.java b/src/main/java/envoy/client/event/EventHandler.java index a6e5b81..ef3daea 100644 --- a/src/main/java/envoy/client/event/EventHandler.java +++ b/src/main/java/envoy/client/event/EventHandler.java @@ -1,25 +1,18 @@ package envoy.client.event; -import java.util.Set; - /** * Project: envoy-clientChess
* File: EventHandler.javaEvent.java
* Created: 04.12.2019
- * + * * @author Kai S. K. Engelbart */ public interface EventHandler { /** * Consumes an event dispatched by the event bus. - * + * * @param event The event dispatched by the event bus, only of supported type */ void handle(Event event); - - /** - * @return A set of classes this class is supposed to handle in events - */ - Set>> supports(); } diff --git a/src/main/java/envoy/client/event/ThemeChangeEvent.java b/src/main/java/envoy/client/event/ThemeChangeEvent.java new file mode 100644 index 0000000..d3ba96b --- /dev/null +++ b/src/main/java/envoy/client/event/ThemeChangeEvent.java @@ -0,0 +1,20 @@ +package envoy.client.event; + +import envoy.client.ui.Theme; + +/** + * Project: envoy-client
+ * File: ThemeChangeEvent.java
+ * Created: 15 Dec 2019
+ * + * @author Kai S. K. Engelbart + */ +public class ThemeChangeEvent implements Event { + + private final Theme theme; + + public ThemeChangeEvent(Theme theme) { this.theme = theme; } + + @Override + public Theme get() { return theme; } +} diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 9595d43..49c73df 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -30,6 +30,8 @@ import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; import envoy.client.Settings; +import envoy.client.event.EventBus; +import envoy.client.event.ThemeChangeEvent; import envoy.schema.Message; import envoy.schema.User; @@ -166,7 +168,6 @@ public class ChatWindow extends JFrame { settingsButton.addActionListener((evt) -> { try { SettingsScreen.open(); - changeChatWindowColors(Settings.getInstance().getCurrentTheme()); } catch (Exception e) { SettingsScreen.open(); logger.log(Level.WARNING, "An error occured while opening the settings screen", e); @@ -220,11 +221,13 @@ public class ChatWindow extends JFrame { gbc_userList.anchor = GridBagConstraints.PAGE_START; gbc_userList.insets = new Insets(space, space, space, space); - changeChatWindowColors(Settings.getInstance().getCurrentTheme()); + changeChatWindowColors(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); contentPane.add(userList, gbc_userList); contentPane.revalidate(); + EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> changeChatWindowColors((Theme) evt.get())); + loadUsersAndChats(); if (client.isOnline()) startSyncThread(Config.getInstance().getSyncTimeout()); @@ -237,9 +240,7 @@ public class ChatWindow extends JFrame { * * @since Envoy v0.1-alpha */ - public void changeChatWindowColors(String key) { - Theme theme = Settings.getInstance().getThemes().get(key); - + private void changeChatWindowColors(Theme theme) { // contentPane contentPane.setBackground(theme.getBackgroundColor()); contentPane.setForeground(theme.getUserNameColor()); diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index 161365d..fc0ebda 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -27,14 +27,16 @@ import javax.swing.ListSelectionModel; import envoy.client.LocalDB; import envoy.client.Settings; +import envoy.client.event.EventBus; +import envoy.client.event.ThemeChangeEvent; /** * This class provides the GUI to change the user specific settings. - * + * * Project: envoy-client
* File: SettingsScreen.java
* Created: 31 Oct 2019
- * + * * @author Leon Hofmeister * @author Maximilian Käfer * @author Kai S. K. Engelbart @@ -45,12 +47,12 @@ public class SettingsScreen extends JDialog { private final JPanel contentPanel = new JPanel(); private DefaultListModel optionsListModel = new DefaultListModel<>(); - private final JList options = new JList(); + private final JList options = new JList<>(); private JPanel buttonPane = new JPanel(); private JPanel themeContent = new JPanel(); private String[] themeArray = Settings.getInstance().getThemes().keySet().toArray(new String[0]); - private JComboBox themes = new JComboBox(themeArray); + private JComboBox themes = new JComboBox<>(themeArray); private GridBagConstraints gbc_themeContent = new GridBagConstraints(); @@ -63,8 +65,7 @@ public class SettingsScreen extends JDialog { private JButton cancelButton = new JButton("Cancel"); private static int space = 5; - private boolean colorChanged = false; - private Theme temporaryTheme; + private Theme temporaryTheme; private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); @@ -77,7 +78,7 @@ public class SettingsScreen extends JDialog { /** * Opens the settings screen.
- * + * * @since Envoy v0.1-alpha */ public static void open() { @@ -89,7 +90,7 @@ public class SettingsScreen extends JDialog { /** * Builds the settings screen. - * + * * @since Envoy v0.1-alpha */ private SettingsScreen() { @@ -296,9 +297,10 @@ public class SettingsScreen extends JDialog { Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName()); System.out.println(selectedTheme.getThemeName()); - changeSettingsScreenColors(Settings.getInstance().getCurrentTheme()); - updateColorVariables(Settings.getInstance().getCurrentTheme()); - + final Theme currentTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); + changeSettingsScreenColors(currentTheme); + updateColorVariables(currentTheme); + EventBus.getInstance().dispatch(new ThemeChangeEvent(currentTheme)); Settings.getInstance().save(); revalidate(); @@ -310,11 +312,10 @@ public class SettingsScreen extends JDialog { }); } } - changeSettingsScreenColors(Settings.getInstance().getCurrentTheme()); + changeSettingsScreenColors(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); } - private void changeSettingsScreenColors(String key) { - Theme theme = Settings.getInstance().getThemes().get(key); + private void changeSettingsScreenColors(Theme theme) { // whole JDialog setBackground(theme.getBackgroundColor()); // contentPanel @@ -345,19 +346,8 @@ public class SettingsScreen extends JDialog { } - private void updateColorVariables(String key) { - Theme theme = Settings.getInstance().getThemes().get(key); - - temporaryTheme = new Theme("temporaryTheme", - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getBackgroundColor(), - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor(), - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableForegroundColor(), - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor(), - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat(), - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat(), - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getSelectionColor(), - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getTypingMessageColor(), - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor()); + private void updateColorVariables(Theme theme) { + temporaryTheme = new Theme("temporaryTheme", theme); colorsPanel.removeAll(); @@ -462,7 +452,6 @@ public class SettingsScreen extends JDialog { // TODO: When Theme changed in same settings screen, color variable doesnt // update. temporaryTheme.setColor(yIndex, newColor); - colorChanged = true; createNewThemeButton.setEnabled(true); } button.setBackground(newColor); @@ -481,8 +470,5 @@ public class SettingsScreen extends JDialog { colorsPanel.add(panel); } - private Color getInvertedColor(Color color) { - return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue()); - } - + private Color getInvertedColor(Color color) { return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue()); } } diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java index 03380c2..4670377 100644 --- a/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -11,12 +11,8 @@ import java.awt.TrayIcon.MessageType; import java.awt.Window; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.util.HashSet; -import java.util.Set; -import envoy.client.event.Event; import envoy.client.event.EventBus; -import envoy.client.event.EventHandler; import envoy.client.event.MessageCreationEvent; import envoy.exception.EnvoyException; import envoy.schema.Message; @@ -29,7 +25,7 @@ import envoy.schema.Message; * @author Kai S. K. Engelbart * @since Envoy v0.2-alpha */ -public class StatusTrayIcon implements EventHandler { +public class StatusTrayIcon { /** * The {@link TrayIcon} provided by the System Tray API for controlling the @@ -85,7 +81,10 @@ public class StatusTrayIcon implements EventHandler { trayIcon.addActionListener((evt) -> { focusTarget.setVisible(true); focusTarget.requestFocus(); }); // Start processing message events - EventBus.getInstance().register(this); + EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { + if (displayMessages) + trayIcon.displayMessage("New message received", ((MessageCreationEvent) evt).get().getContent().get(0).getText(), MessageType.INFO); + }); } /** @@ -102,31 +101,4 @@ public class StatusTrayIcon implements EventHandler { throw new EnvoyException("Could not attach Envoy tray icon to system tray.", e); } } - - /** - * Notifies the user of a message by displaying a pop-up every time a new - * message is received. - * - * @since Envoy v0.2-alpha - */ - @Override - public void handle(Event event) { - System.out.println("Message received. Displaying message: " + displayMessages); - if (displayMessages) - trayIcon.displayMessage("New message received", ((MessageCreationEvent) event).get().getContent().get(0).getText(), MessageType.INFO); - } - - /** - * The {@link StatusTrayIcon} only reacts to {@link MessageCreationEvent} - * instances which signify newly received messages. - * - * @return A set with the single element {@code MessageCreationEvent.class} - * @since Envoy v0.2-alpha - */ - @Override - public Set>> supports() { - Set>> supportedEvents = new HashSet<>(); - supportedEvents.add(MessageCreationEvent.class); - return supportedEvents; - } }