From 4a2d6f913bfe2e6b64fa154fdd0f8e74fb558543 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 14 Dec 2019 19:10:45 +0100 Subject: [PATCH 01/10] Custom scroll bar * Added PrimaryScrollBar class * Implemented PrimaryScrollBar in ChatWindow for the ScrollPanes vertical scroll bar --- src/main/java/envoy/client/ui/ChatWindow.java | 11 +++ .../envoy/client/ui/PrimaryScrollBar.java | 95 +++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/main/java/envoy/client/ui/PrimaryScrollBar.java diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 1c562b6..a6510ec 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -1,5 +1,6 @@ package envoy.client.ui; +import java.awt.Color; import java.awt.ComponentOrientation; import java.awt.Font; import java.awt.GridBagConstraints; @@ -252,6 +253,16 @@ public class ChatWindow extends JFrame { // scrollPane scrollPane.setForeground(theme.getBackgroundColor()); scrollPane.setBackground(theme.getCellColor()); + // scrollPane.getVerticalScrollBar() + // .setBackground( + // new Color(theme.getBackgroundColor().getRed() + 50, + // theme.getBackgroundColor().getGreen() + 50, + // theme.getBackgroundColor().getBlue() + 50)); + scrollPane.getVerticalScrollBar().setBackground(theme.getCellColor()); + scrollPane.getVerticalScrollBar() + .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), + new Color(theme.getInteractableBackgroundColor().getRGB() - 50), + new Color(theme.getInteractableBackgroundColor().getRGB() + 170))); // messageEnterTextArea messageEnterTextArea.setCaretColor(theme.getTypingMessageColor()); messageEnterTextArea.setForeground(theme.getTypingMessageColor()); diff --git a/src/main/java/envoy/client/ui/PrimaryScrollBar.java b/src/main/java/envoy/client/ui/PrimaryScrollBar.java new file mode 100644 index 0000000..aaaa0a0 --- /dev/null +++ b/src/main/java/envoy/client/ui/PrimaryScrollBar.java @@ -0,0 +1,95 @@ +package envoy.client.ui; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.RenderingHints; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JScrollBar; +import javax.swing.plaf.basic.BasicScrollBarUI; + +import envoy.client.Settings; + +/** + * Project: envoy-client
+ * File: PrimaryScrollBar.javaEvent.java
+ * Created: 14.12.2019
+ * + * @author Maximilian Käfer + * @since Envoy v0.2-alpha + */ +public class PrimaryScrollBar extends BasicScrollBarUI{ + + private final Dimension d = new Dimension(); + private int arcSize; + private Color scrollBarColor; + private Color hoverColor; + private Color draggingColor; + + public PrimaryScrollBar(int arcSize, Color scrollBarColor, Color hoverColor, Color draggingColor) { + this.arcSize = arcSize; + this.scrollBarColor = scrollBarColor; + this.hoverColor = hoverColor; + this.draggingColor = draggingColor; + } + + @Override + protected JButton createDecreaseButton(int orientation) { + return new JButton() { + private static final long serialVersionUID = 1032443171070235890L; + + @Override + public Dimension getPreferredSize() { + return d; + } + }; + } + + @Override + protected JButton createIncreaseButton (int orientation) { + return new JButton() { + + private static final long serialVersionUID = 7575774542623215803L; + + @Override + public Dimension getPreferredSize() { + return d; + } + }; + } + @Override + protected void paintTrack(Graphics g, JComponent c, Rectangle r) { + } + + @Override + protected void paintThumb(Graphics g, JComponent c, Rectangle r) { + Graphics2D g2 = (Graphics2D) g.create(); + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + Color color = null; + JScrollBar sb = (JScrollBar) c; + if (!sb.isEnabled() || r.width > r.height) { + return; + } else if (isDragging) { + color = draggingColor; + } else if (isThumbRollover()) { + color = hoverColor; + } else { + color = scrollBarColor; + } + g2.setPaint(color); + g2.fillRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); + g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); + g2.drawRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); + g2.dispose(); + } + + @Override + protected void setThumbBounds(int x, int y, int width, int height) { + super.setThumbBounds(x, y, width, height); + scrollbar.repaint(); + } +} From 6bfa3c2b79426c01f5f85c418a5c77ce4eb51ffb Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 15 Dec 2019 00:34:44 +0100 Subject: [PATCH 02/10] Horizontal Scroll Bar Implemented primaryScrollBar support for horizontal scroll bar as well. --- src/main/java/envoy/client/ui/ChatWindow.java | 10 ++++++- .../envoy/client/ui/PrimaryScrollBar.java | 26 ++++++++++++++----- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index a6510ec..6968f5c 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -262,7 +262,15 @@ public class ChatWindow extends JFrame { scrollPane.getVerticalScrollBar() .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), - new Color(theme.getInteractableBackgroundColor().getRGB() + 170))); + new Color(theme.getInteractableBackgroundColor().getRGB() + 170), true)); + scrollPane.getHorizontalScrollBar().setBackground(theme.getCellColor()); + scrollPane.getHorizontalScrollBar() + .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), + new Color(theme.getInteractableBackgroundColor().getRGB() + 170), false)); + // int currentVerticalScrollBarValue = + // scrollPane.getVerticalScrollBar().getValue() + + // scrollPane.getVerticalScrollBar().getVisibleAmount(); Work in Progress for + // autoscroll // messageEnterTextArea messageEnterTextArea.setCaretColor(theme.getTypingMessageColor()); messageEnterTextArea.setForeground(theme.getTypingMessageColor()); diff --git a/src/main/java/envoy/client/ui/PrimaryScrollBar.java b/src/main/java/envoy/client/ui/PrimaryScrollBar.java index aaaa0a0..546119b 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollBar.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollBar.java @@ -29,12 +29,14 @@ public class PrimaryScrollBar extends BasicScrollBarUI{ private Color scrollBarColor; private Color hoverColor; private Color draggingColor; + private boolean isVertical; - public PrimaryScrollBar(int arcSize, Color scrollBarColor, Color hoverColor, Color draggingColor) { + public PrimaryScrollBar(int arcSize, Color scrollBarColor, Color hoverColor, Color draggingColor, boolean isVertical) { this.arcSize = arcSize; this.scrollBarColor = scrollBarColor; this.hoverColor = hoverColor; this.draggingColor = draggingColor; + this.isVertical = isVertical; } @Override @@ -71,7 +73,7 @@ public class PrimaryScrollBar extends BasicScrollBarUI{ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Color color = null; JScrollBar sb = (JScrollBar) c; - if (!sb.isEnabled() || r.width > r.height) { + if (!sb.isEnabled() || (isVertical == true && r.width > r.height) || (isVertical == false && r.width < r.height)) { return; } else if (isDragging) { color = draggingColor; @@ -80,11 +82,21 @@ public class PrimaryScrollBar extends BasicScrollBarUI{ } else { color = scrollBarColor; } - g2.setPaint(color); - g2.fillRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); - g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); - g2.drawRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); - g2.dispose(); + + if (isVertical == true) { + g2.setPaint(color); + g2.fillRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); + g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); + g2.drawRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); + g2.dispose(); + } + if (isVertical == false) { + g2.setPaint(color); + g2.fillRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); + g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); + g2.drawRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); + g2.dispose(); + } } @Override From 3fefeb1082fef1091bf899c1ef2cddd5cc3e392b Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 15 Dec 2019 12:48:40 +0100 Subject: [PATCH 03/10] Autoscroll * Implemented functionality to automatically scroll down when user is on the bottom of the chat and then there are new messages added. * When chat is opened, the vertical scroll bar starts at the bottom. * When rereading messages, the chat doesn't scroll down if new messages are added. (Besides see first point) --- src/main/java/envoy/client/ui/ChatWindow.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 6968f5c..134734f 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -63,6 +63,8 @@ public class ChatWindow extends JFrame { // private JCheckBox jCbChangeMode; private PrimaryButton postButton = new PrimaryButton("Post"); private PrimaryButton settingsButton = new PrimaryButton("Settings"); + private int verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); + private boolean chatOpened = false; private static int space = 4; @@ -208,6 +210,7 @@ public class ChatWindow extends JFrame { textPane.setText(currentChat.getRecipient().getName()); messageList.setModel(currentChat.getModel()); + chatOpened = true; contentPane.revalidate(); } }); @@ -253,11 +256,8 @@ public class ChatWindow extends JFrame { // scrollPane scrollPane.setForeground(theme.getBackgroundColor()); scrollPane.setBackground(theme.getCellColor()); - // scrollPane.getVerticalScrollBar() - // .setBackground( - // new Color(theme.getBackgroundColor().getRed() + 50, - // theme.getBackgroundColor().getGreen() + 50, - // theme.getBackgroundColor().getBlue() + 50)); + + // Scroll Bar Styling scrollPane.getVerticalScrollBar().setBackground(theme.getCellColor()); scrollPane.getVerticalScrollBar() .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), @@ -267,10 +267,26 @@ public class ChatWindow extends JFrame { scrollPane.getHorizontalScrollBar() .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), new Color(theme.getInteractableBackgroundColor().getRGB() + 170), false)); - // int currentVerticalScrollBarValue = - // scrollPane.getVerticalScrollBar().getValue() + - // scrollPane.getVerticalScrollBar().getVisibleAmount(); Work in Progress for - // autoscroll + + // Autoscroll + scrollPane.getVerticalScrollBar().addAdjustmentListener(e -> { + if ((verticalScrollBarMaximumValue - e.getAdjustable().getMaximum()) == 0) { + return; + } + + if (chatOpened == true) { + e.getAdjustable().setValue(e.getAdjustable().getMaximum()); + verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); + chatOpened = false; + return; + } + if (scrollPane.getVerticalScrollBar().getValue() + + scrollPane.getVerticalScrollBar().getVisibleAmount() + 100 >= scrollPane.getVerticalScrollBar().getMaximum()) { + e.getAdjustable().setValue(e.getAdjustable().getMaximum()); + verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); + } + }); + // messageEnterTextArea messageEnterTextArea.setCaretColor(theme.getTypingMessageColor()); messageEnterTextArea.setForeground(theme.getTypingMessageColor()); From 4f777412f38c3bea6f742ed480b24ff1cee9d519 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 15 Dec 2019 12:53:01 +0100 Subject: [PATCH 04/10] Cold style improvement Changed if query with isVertical in PrimaryScrollBar to short form. --- src/main/java/envoy/client/ui/PrimaryScrollBar.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/envoy/client/ui/PrimaryScrollBar.java b/src/main/java/envoy/client/ui/PrimaryScrollBar.java index 546119b..a7dc073 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollBar.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollBar.java @@ -83,14 +83,14 @@ public class PrimaryScrollBar extends BasicScrollBarUI{ color = scrollBarColor; } - if (isVertical == true) { + if (isVertical) { g2.setPaint(color); g2.fillRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); g2.drawRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); g2.dispose(); } - if (isVertical == false) { + if (!isVertical) { g2.setPaint(color); g2.fillRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); From adb5c417c541ed85bdce5b6d1583faed96127d97 Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 15 Dec 2019 16:26:11 +0100 Subject: [PATCH 05/10] Improved code style and formatting --- src/main/java/envoy/client/ui/ChatWindow.java | 33 ++++---- .../envoy/client/ui/PrimaryScrollBar.java | 83 ++++++++----------- 2 files changed, 48 insertions(+), 68 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 134734f..2e263da 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -49,22 +49,22 @@ public class ChatWindow extends JFrame { private static final long serialVersionUID = 6865098428255463649L; - // user specific objects + // User specific objects private Client client; private LocalDB localDB; + // GUI components - private JPanel contentPane = new JPanel(); - private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space); - private JList userList = new JList<>(); + private JPanel contentPane = new JPanel(); + private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space); + private JList userList = new JList<>(); private Chat currentChat; - private JList messageList = new JList<>(); - private JScrollPane scrollPane = new JScrollPane(); - private JTextPane textPane = new JTextPane(); - // private JCheckBox jCbChangeMode; - private PrimaryButton postButton = new PrimaryButton("Post"); - private PrimaryButton settingsButton = new PrimaryButton("Settings"); + private JList messageList = new JList<>(); + private JScrollPane scrollPane = new JScrollPane(); private int verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); private boolean chatOpened = false; + private JTextPane textPane = new JTextPane(); + private PrimaryButton postButton = new PrimaryButton("Post"); + private PrimaryButton settingsButton = new PrimaryButton("Settings"); private static int space = 4; @@ -239,7 +239,7 @@ public class ChatWindow extends JFrame { /** * Used to immediately reload the ChatWindow when settings were changed. - * + * * @since Envoy v0.1-alpha */ public void changeChatWindowColors(String key) { @@ -260,8 +260,7 @@ public class ChatWindow extends JFrame { // Scroll Bar Styling scrollPane.getVerticalScrollBar().setBackground(theme.getCellColor()); scrollPane.getVerticalScrollBar() - .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), - new Color(theme.getInteractableBackgroundColor().getRGB() - 50), + .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), new Color(theme.getInteractableBackgroundColor().getRGB() + 170), true)); scrollPane.getHorizontalScrollBar().setBackground(theme.getCellColor()); scrollPane.getHorizontalScrollBar() @@ -270,9 +269,7 @@ public class ChatWindow extends JFrame { // Autoscroll scrollPane.getVerticalScrollBar().addAdjustmentListener(e -> { - if ((verticalScrollBarMaximumValue - e.getAdjustable().getMaximum()) == 0) { - return; - } + if ((verticalScrollBarMaximumValue - e.getAdjustable().getMaximum()) == 0) { return; } if (chatOpened == true) { e.getAdjustable().setValue(e.getAdjustable().getMaximum()); @@ -280,8 +277,8 @@ public class ChatWindow extends JFrame { chatOpened = false; return; } - if (scrollPane.getVerticalScrollBar().getValue() - + scrollPane.getVerticalScrollBar().getVisibleAmount() + 100 >= scrollPane.getVerticalScrollBar().getMaximum()) { + if (scrollPane.getVerticalScrollBar().getValue() + scrollPane.getVerticalScrollBar().getVisibleAmount() + + 100 >= scrollPane.getVerticalScrollBar().getMaximum()) { e.getAdjustable().setValue(e.getAdjustable().getMaximum()); verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); } diff --git a/src/main/java/envoy/client/ui/PrimaryScrollBar.java b/src/main/java/envoy/client/ui/PrimaryScrollBar.java index a7dc073..845448c 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollBar.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollBar.java @@ -16,23 +16,23 @@ import envoy.client.Settings; /** * Project: envoy-client
- * File: PrimaryScrollBar.javaEvent.java
+ * File: PrimaryScrollBar.java
* Created: 14.12.2019
- * + * * @author Maximilian Käfer * @since Envoy v0.2-alpha */ -public class PrimaryScrollBar extends BasicScrollBarUI{ - +public class PrimaryScrollBar extends BasicScrollBarUI { + private final Dimension d = new Dimension(); - private int arcSize; - private Color scrollBarColor; - private Color hoverColor; - private Color draggingColor; - private boolean isVertical; - + private final int arcSize; + private final Color scrollBarColor; + private final Color hoverColor; + private final Color draggingColor; + private final boolean isVertical; + public PrimaryScrollBar(int arcSize, Color scrollBarColor, Color hoverColor, Color draggingColor, boolean isVertical) { - this.arcSize = arcSize; + this.arcSize = arcSize; this.scrollBarColor = scrollBarColor; this.hoverColor = hoverColor; this.draggingColor = draggingColor; @@ -41,62 +41,45 @@ public class PrimaryScrollBar extends BasicScrollBarUI{ @Override protected JButton createDecreaseButton(int orientation) { - return new JButton() { - private static final long serialVersionUID = 1032443171070235890L; - - @Override - public Dimension getPreferredSize() { - return d; - } - }; + JButton button = new JButton(); + button.setPreferredSize(d); + return button; } - + @Override - protected JButton createIncreaseButton (int orientation) { - return new JButton() { - - private static final long serialVersionUID = 7575774542623215803L; - - @Override - public Dimension getPreferredSize() { - return d; - } - }; + protected JButton createIncreaseButton(int orientation) { + JButton button = new JButton(); + button.setPreferredSize(d); + return button; } + @Override - protected void paintTrack(Graphics g, JComponent c, Rectangle r) { - } - + protected void paintTrack(Graphics g, JComponent c, Rectangle r) {} + @Override protected void paintThumb(Graphics g, JComponent c, Rectangle r) { Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - Color color = null; - JScrollBar sb = (JScrollBar) c; - if (!sb.isEnabled() || (isVertical == true && r.width > r.height) || (isVertical == false && r.width < r.height)) { - return; - } else if (isDragging) { - color = draggingColor; - } else if (isThumbRollover()) { - color = hoverColor; - } else { - color = scrollBarColor; - } + Color color; + JScrollBar sb = (JScrollBar) c; + if (!sb.isEnabled() || (isVertical && r.width > r.height) || (!isVertical && r.width < r.height)) return; + + if (isDragging) color = draggingColor; + else if (isThumbRollover()) color = hoverColor; + else color = scrollBarColor; + + g2.setPaint(color); if (isVertical) { - g2.setPaint(color); g2.fillRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); g2.drawRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); - g2.dispose(); - } - if (!isVertical) { - g2.setPaint(color); + } else { g2.fillRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); g2.drawRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); - g2.dispose(); } + g2.dispose(); } @Override From acc7424503740bf054b21d3ef39099674ec3b454 Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 15 Dec 2019 17:44:13 +0100 Subject: [PATCH 06/10] Added PrimaryScrollPane class with default scroll pane UI settings --- src/main/java/envoy/client/ui/ChatWindow.java | 55 ++++--------------- .../envoy/client/ui/PrimaryScrollBar.java | 5 ++ .../envoy/client/ui/PrimaryScrollPane.java | 48 ++++++++++++++++ 3 files changed, 64 insertions(+), 44 deletions(-) create mode 100644 src/main/java/envoy/client/ui/PrimaryScrollPane.java diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 2e263da..3a28754 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -1,6 +1,5 @@ package envoy.client.ui; -import java.awt.Color; import java.awt.ComponentOrientation; import java.awt.Font; import java.awt.GridBagConstraints; @@ -20,7 +19,6 @@ import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; @@ -54,17 +52,15 @@ public class ChatWindow extends JFrame { private LocalDB localDB; // GUI components - private JPanel contentPane = new JPanel(); - private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space); - private JList userList = new JList<>(); - private Chat currentChat; - private JList messageList = new JList<>(); - private JScrollPane scrollPane = new JScrollPane(); - private int verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); - private boolean chatOpened = false; - private JTextPane textPane = new JTextPane(); - private PrimaryButton postButton = new PrimaryButton("Post"); - private PrimaryButton settingsButton = new PrimaryButton("Settings"); + private JPanel contentPane = new JPanel(); + private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space); + private JList userList = new JList<>(); + private Chat currentChat; + private JList messageList = new JList<>(); + private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); + private JTextPane textPane = new JTextPane(); + private PrimaryButton postButton = new PrimaryButton("Post"); + private PrimaryButton settingsButton = new PrimaryButton("Settings"); private static int space = 4; @@ -115,7 +111,6 @@ public class ChatWindow extends JFrame { messageList.setBorder(new EmptyBorder(space, space, space, space)); scrollPane.setViewportView(messageList); - scrollPane.setBorder(null); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); gbc_scrollPane.fill = GridBagConstraints.BOTH; @@ -210,7 +205,7 @@ public class ChatWindow extends JFrame { textPane.setText(currentChat.getRecipient().getName()); messageList.setModel(currentChat.getModel()); - chatOpened = true; + scrollPane.setChatOpened(true); contentPane.revalidate(); } }); @@ -254,35 +249,7 @@ public class ChatWindow extends JFrame { messageList.setForeground(theme.getMessageColorChat()); messageList.setBackground(theme.getCellColor()); // scrollPane - scrollPane.setForeground(theme.getBackgroundColor()); - scrollPane.setBackground(theme.getCellColor()); - - // Scroll Bar Styling - scrollPane.getVerticalScrollBar().setBackground(theme.getCellColor()); - scrollPane.getVerticalScrollBar() - .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), - new Color(theme.getInteractableBackgroundColor().getRGB() + 170), true)); - scrollPane.getHorizontalScrollBar().setBackground(theme.getCellColor()); - scrollPane.getHorizontalScrollBar() - .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), - new Color(theme.getInteractableBackgroundColor().getRGB() + 170), false)); - - // Autoscroll - scrollPane.getVerticalScrollBar().addAdjustmentListener(e -> { - if ((verticalScrollBarMaximumValue - e.getAdjustable().getMaximum()) == 0) { return; } - - if (chatOpened == true) { - e.getAdjustable().setValue(e.getAdjustable().getMaximum()); - verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); - chatOpened = false; - return; - } - if (scrollPane.getVerticalScrollBar().getValue() + scrollPane.getVerticalScrollBar().getVisibleAmount() - + 100 >= scrollPane.getVerticalScrollBar().getMaximum()) { - e.getAdjustable().setValue(e.getAdjustable().getMaximum()); - verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); - } - }); + scrollPane.applyTheme(theme); // messageEnterTextArea messageEnterTextArea.setCaretColor(theme.getTypingMessageColor()); diff --git a/src/main/java/envoy/client/ui/PrimaryScrollBar.java b/src/main/java/envoy/client/ui/PrimaryScrollBar.java index 845448c..6853d8f 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollBar.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollBar.java @@ -39,6 +39,11 @@ public class PrimaryScrollBar extends BasicScrollBarUI { this.isVertical = isVertical; } + public PrimaryScrollBar(Theme theme, boolean isVertical) { + this(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), + new Color(theme.getInteractableBackgroundColor().getRGB() + 170), isVertical); + } + @Override protected JButton createDecreaseButton(int orientation) { JButton button = new JButton(); diff --git a/src/main/java/envoy/client/ui/PrimaryScrollPane.java b/src/main/java/envoy/client/ui/PrimaryScrollPane.java new file mode 100644 index 0000000..d4e0b28 --- /dev/null +++ b/src/main/java/envoy/client/ui/PrimaryScrollPane.java @@ -0,0 +1,48 @@ +package envoy.client.ui; + +import javax.swing.JScrollPane; + +/** + * Project: envoy-client
+ * File: PrimaryScrollPane.java
+ * Created: 15 Dec 2019
+ * + * @author Kai S. K. Engelbart + */ +public class PrimaryScrollPane extends JScrollPane { + + private static final long serialVersionUID = -4786837444056228439L; + + private int verticalScrollBarMaximum = getVerticalScrollBar().getMaximum(); + private boolean chatOpened = false; + + public PrimaryScrollPane() { setBorder(null); } + + public void applyTheme(Theme theme) { + setForeground(theme.getBackgroundColor()); + setBackground(theme.getCellColor()); + + getVerticalScrollBar().setBackground(theme.getCellColor()); + getVerticalScrollBar().setUI(new PrimaryScrollBar(theme, true)); + getHorizontalScrollBar().setBackground(theme.getCellColor()); + getHorizontalScrollBar().setUI(new PrimaryScrollBar(theme, false)); + + // Automatic scrolling to the bottom + getVerticalScrollBar().addAdjustmentListener(e -> { + if (verticalScrollBarMaximum == e.getAdjustable().getMaximum()) return; + + if (chatOpened) { + e.getAdjustable().setValue(e.getAdjustable().getMaximum()); + verticalScrollBarMaximum = getVerticalScrollBar().getMaximum(); + chatOpened = false; + return; + } + if (getVerticalScrollBar().getValue() + getVerticalScrollBar().getVisibleAmount() + 100 >= getVerticalScrollBar().getMaximum()) { + e.getAdjustable().setValue(e.getAdjustable().getMaximum()); + verticalScrollBarMaximum = getVerticalScrollBar().getMaximum(); + } + }); + } + + public void setChatOpened(boolean chatOpened) { this.chatOpened = chatOpened; } +} From f6bd6ab754310acd0ccdd23089c4e9a2dfa53a83 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 15 Dec 2019 20:18:43 +0100 Subject: [PATCH 07/10] Formatting * Split applyTheme method in applyTheme and autoscroll. * Added Javadoc --- src/main/java/envoy/client/ui/ChatWindow.java | 1 + .../envoy/client/ui/PrimaryScrollPane.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 3a28754..9595d43 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -250,6 +250,7 @@ public class ChatWindow extends JFrame { messageList.setBackground(theme.getCellColor()); // scrollPane scrollPane.applyTheme(theme); + scrollPane.autoscroll(); // messageEnterTextArea messageEnterTextArea.setCaretColor(theme.getTypingMessageColor()); diff --git a/src/main/java/envoy/client/ui/PrimaryScrollPane.java b/src/main/java/envoy/client/ui/PrimaryScrollPane.java index d4e0b28..904806a 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollPane.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollPane.java @@ -8,6 +8,7 @@ import javax.swing.JScrollPane; * Created: 15 Dec 2019
* * @author Kai S. K. Engelbart + * @author Maximilian Käfer */ public class PrimaryScrollPane extends JScrollPane { @@ -18,6 +19,12 @@ public class PrimaryScrollPane extends JScrollPane { public PrimaryScrollPane() { setBorder(null); } + /** + * Styles the vertical and horizontal scroll bars. + * + * @param theme + * @since Envoy v0.2-alpha + */ public void applyTheme(Theme theme) { setForeground(theme.getBackgroundColor()); setBackground(theme.getCellColor()); @@ -26,7 +33,22 @@ public class PrimaryScrollPane extends JScrollPane { getVerticalScrollBar().setUI(new PrimaryScrollBar(theme, true)); getHorizontalScrollBar().setBackground(theme.getCellColor()); getHorizontalScrollBar().setUI(new PrimaryScrollBar(theme, false)); + } + /** + * Implements autoscroll functionality for the vertical scroll bar.
+ *
+ * Functionality to automatically scroll down when user views
+ * the bottom of the chat while there are new messages added.
+ *
+ * When chat is opened, the vertical scroll bar starts at the bottom.
+ *
+ * When rereading messages, the chat doesn't scroll down if new messages
+ * are added. (Besides see first point) + * + * @since Envoy v0.2-alpha + */ + public void autoscroll() { // Automatic scrolling to the bottom getVerticalScrollBar().addAdjustmentListener(e -> { if (verticalScrollBarMaximum == e.getAdjustable().getMaximum()) return; From 396686bfdcf53e29e1739f87376c4612c814e266 Mon Sep 17 00:00:00 2001 From: kske Date: Mon, 16 Dec 2019 09:41:21 +0100 Subject: [PATCH 08/10] Added ThemeChangeEvent, improved EventHandler declaration --- src/main/java/envoy/client/event/Event.java | 6 +-- .../java/envoy/client/event/EventBus.java | 40 +++++++++------ .../java/envoy/client/event/EventHandler.java | 11 +--- .../envoy/client/event/ThemeChangeEvent.java | 20 ++++++++ src/main/java/envoy/client/ui/ChatWindow.java | 11 ++-- .../java/envoy/client/ui/SettingsScreen.java | 50 +++++++------------ .../java/envoy/client/ui/StatusTrayIcon.java | 38 ++------------ 7 files changed, 79 insertions(+), 97 deletions(-) create mode 100644 src/main/java/envoy/client/event/ThemeChangeEvent.java 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; - } } From 69153005cd661544a39005df4c2574b9c8ebe809 Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 17 Dec 2019 08:17:22 +0100 Subject: [PATCH 09/10] Fixed vertical scroll bar drawing on 4K monitors --- src/main/java/envoy/client/ui/ChatWindow.java | 1 - src/main/java/envoy/client/ui/PrimaryScrollBar.java | 6 +++--- src/main/java/envoy/client/ui/PrimaryScrollPane.java | 5 +++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 49c73df..910180c 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -195,7 +195,6 @@ public class ChatWindow extends JFrame { @SuppressWarnings("unchecked") final JList selectedUserList = (JList) listSelectionEvent.getSource(); final User user = selectedUserList.getSelectedValue(); - client.setRecipient(user); currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get(); diff --git a/src/main/java/envoy/client/ui/PrimaryScrollBar.java b/src/main/java/envoy/client/ui/PrimaryScrollBar.java index 6853d8f..20bed6c 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollBar.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollBar.java @@ -68,7 +68,7 @@ public class PrimaryScrollBar extends BasicScrollBarUI { Color color; JScrollBar sb = (JScrollBar) c; - if (!sb.isEnabled() || (isVertical && r.width > r.height) || (!isVertical && r.width < r.height)) return; + if (!sb.isEnabled()) return; if (isDragging) color = draggingColor; else if (isThumbRollover()) color = hoverColor; @@ -76,9 +76,9 @@ public class PrimaryScrollBar extends BasicScrollBarUI { g2.setPaint(color); if (isVertical) { - g2.fillRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); + g2.fillRoundRect(r.x - 9, r.y, r.width, r.height, arcSize, arcSize); g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); - g2.drawRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); + g2.drawRoundRect(r.x - 9, r.y, r.width, r.height, arcSize, arcSize); } else { g2.fillRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); diff --git a/src/main/java/envoy/client/ui/PrimaryScrollPane.java b/src/main/java/envoy/client/ui/PrimaryScrollPane.java index 904806a..61c348a 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollPane.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollPane.java @@ -21,7 +21,7 @@ public class PrimaryScrollPane extends JScrollPane { /** * Styles the vertical and horizontal scroll bars. - * + * * @param theme * @since Envoy v0.2-alpha */ @@ -31,6 +31,7 @@ public class PrimaryScrollPane extends JScrollPane { getVerticalScrollBar().setBackground(theme.getCellColor()); getVerticalScrollBar().setUI(new PrimaryScrollBar(theme, true)); + getHorizontalScrollBar().setBackground(theme.getCellColor()); getHorizontalScrollBar().setUI(new PrimaryScrollBar(theme, false)); } @@ -45,7 +46,7 @@ public class PrimaryScrollPane extends JScrollPane { *
* When rereading messages, the chat doesn't scroll down if new messages
* are added. (Besides see first point) - * + * * @since Envoy v0.2-alpha */ public void autoscroll() { From 500555c8dbfb6ebdc64a8221dd65731529a93328 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 18 Dec 2019 22:07:05 +0100 Subject: [PATCH 10/10] Added Javadoc requested by @delvh --- .../java/envoy/client/ui/PrimaryButton.java | 10 +- .../envoy/client/ui/PrimaryScrollBar.java | 210 ++++++++++-------- .../envoy/client/ui/PrimaryScrollPane.java | 12 + 3 files changed, 132 insertions(+), 100 deletions(-) diff --git a/src/main/java/envoy/client/ui/PrimaryButton.java b/src/main/java/envoy/client/ui/PrimaryButton.java index fe93571..65c52a0 100644 --- a/src/main/java/envoy/client/ui/PrimaryButton.java +++ b/src/main/java/envoy/client/ui/PrimaryButton.java @@ -8,7 +8,7 @@ import javax.swing.JButton; * Project: envoy-client
* File: PrimaryButton.javaEvent.java
* Created: 07.12.2019
- * + * * @author Kai S. K. Engelbart * @author Maximilian Käfer * @since Envoy v0.2-alpha @@ -21,7 +21,7 @@ public class PrimaryButton extends JButton { /** * Creates a primary button - * + * * @param title the title of the button * @since Envoy 0.2-alpha */ @@ -29,9 +29,9 @@ public class PrimaryButton extends JButton { /** * Creates a primary button - * - * @param title the title of the button - * @param the size of the arc used to draw the round button edges + * + * @param title the title of the button + * @param arcSize the size of the arc used to draw the round button edges * @since Envoy 0.2-alpha */ public PrimaryButton(String title, int arcSize) { diff --git a/src/main/java/envoy/client/ui/PrimaryScrollBar.java b/src/main/java/envoy/client/ui/PrimaryScrollBar.java index 20bed6c..a7cb8fc 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollBar.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollBar.java @@ -1,95 +1,115 @@ -package envoy.client.ui; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.Rectangle; -import java.awt.RenderingHints; - -import javax.swing.JButton; -import javax.swing.JComponent; -import javax.swing.JScrollBar; -import javax.swing.plaf.basic.BasicScrollBarUI; - -import envoy.client.Settings; - -/** - * Project: envoy-client
- * File: PrimaryScrollBar.java
- * Created: 14.12.2019
- * - * @author Maximilian Käfer - * @since Envoy v0.2-alpha - */ -public class PrimaryScrollBar extends BasicScrollBarUI { - - private final Dimension d = new Dimension(); - private final int arcSize; - private final Color scrollBarColor; - private final Color hoverColor; - private final Color draggingColor; - private final boolean isVertical; - - public PrimaryScrollBar(int arcSize, Color scrollBarColor, Color hoverColor, Color draggingColor, boolean isVertical) { - this.arcSize = arcSize; - this.scrollBarColor = scrollBarColor; - this.hoverColor = hoverColor; - this.draggingColor = draggingColor; - this.isVertical = isVertical; - } - - public PrimaryScrollBar(Theme theme, boolean isVertical) { - this(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), - new Color(theme.getInteractableBackgroundColor().getRGB() + 170), isVertical); - } - - @Override - protected JButton createDecreaseButton(int orientation) { - JButton button = new JButton(); - button.setPreferredSize(d); - return button; - } - - @Override - protected JButton createIncreaseButton(int orientation) { - JButton button = new JButton(); - button.setPreferredSize(d); - return button; - } - - @Override - protected void paintTrack(Graphics g, JComponent c, Rectangle r) {} - - @Override - protected void paintThumb(Graphics g, JComponent c, Rectangle r) { - Graphics2D g2 = (Graphics2D) g.create(); - g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - Color color; - JScrollBar sb = (JScrollBar) c; - - if (!sb.isEnabled()) return; - - if (isDragging) color = draggingColor; - else if (isThumbRollover()) color = hoverColor; - else color = scrollBarColor; - - g2.setPaint(color); - if (isVertical) { - g2.fillRoundRect(r.x - 9, r.y, r.width, r.height, arcSize, arcSize); - g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); - g2.drawRoundRect(r.x - 9, r.y, r.width, r.height, arcSize, arcSize); - } else { - g2.fillRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); - g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); - g2.drawRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); - } - g2.dispose(); - } - - @Override - protected void setThumbBounds(int x, int y, int width, int height) { - super.setThumbBounds(x, y, width, height); - scrollbar.repaint(); - } -} +package envoy.client.ui; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.RenderingHints; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JScrollBar; +import javax.swing.plaf.basic.BasicScrollBarUI; + +import envoy.client.Settings; + +/** + * Project: envoy-client
+ * File: PrimaryScrollBar.java
+ * Created: 14.12.2019
+ * + * @author Maximilian Käfer + * @since Envoy v0.2-alpha + */ +public class PrimaryScrollBar extends BasicScrollBarUI { + + private final Dimension d = new Dimension(); + private final int arcSize; + private final Color scrollBarColor; + private final Color hoverColor; + private final Color draggingColor; + private final boolean isVertical; + + /** + * Initializes a {@link PrimaryScrollBar} with a color scheme. + * + * @param arcSize the size of the arc used to draw the round scroll bar + * edges + * @param scrollBarColor the default color + * @param hoverColor the color while hovering + * @param draggingColor the color while dragging + * @param isVertical indicates whether this is a vertical + * {@link PrimaryScrollBar} + */ + public PrimaryScrollBar(int arcSize, Color scrollBarColor, Color hoverColor, Color draggingColor, boolean isVertical) { + this.arcSize = arcSize; + this.scrollBarColor = scrollBarColor; + this.hoverColor = hoverColor; + this.draggingColor = draggingColor; + this.isVertical = isVertical; + } + + /** + * Initializes a {@link PrimaryScrollBar} using a color scheme specified in a + * {@link Theme} + * + * @param theme the {@link Theme} to be applied to this + * {@link PrimaryScrollBar} + * @param isVertical indicates whether this is a vertical + * {@link PrimaryScrollBar} + */ + public PrimaryScrollBar(Theme theme, boolean isVertical) { + this(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), + new Color(theme.getInteractableBackgroundColor().getRGB() + 170), isVertical); + } + + @Override + protected JButton createDecreaseButton(int orientation) { + JButton button = new JButton(); + button.setPreferredSize(d); + return button; + } + + @Override + protected JButton createIncreaseButton(int orientation) { + JButton button = new JButton(); + button.setPreferredSize(d); + return button; + } + + @Override + protected void paintTrack(Graphics g, JComponent c, Rectangle r) {} + + @Override + protected void paintThumb(Graphics g, JComponent c, Rectangle r) { + Graphics2D g2 = (Graphics2D) g.create(); + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + Color color; + JScrollBar sb = (JScrollBar) c; + + if (!sb.isEnabled()) return; + + if (isDragging) color = draggingColor; + else if (isThumbRollover()) color = hoverColor; + else color = scrollBarColor; + + g2.setPaint(color); + if (isVertical) { + g2.fillRoundRect(r.x - 9, r.y, r.width, r.height, arcSize, arcSize); + g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); + g2.drawRoundRect(r.x - 9, r.y, r.width, r.height, arcSize, arcSize); + } else { + g2.fillRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); + g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); + g2.drawRoundRect(r.x, r.y + 9, r.width, r.height - 10, arcSize, arcSize); + } + g2.dispose(); + } + + @Override + protected void setThumbBounds(int x, int y, int width, int height) { + super.setThumbBounds(x, y, width, height); + scrollbar.repaint(); + } +} diff --git a/src/main/java/envoy/client/ui/PrimaryScrollPane.java b/src/main/java/envoy/client/ui/PrimaryScrollPane.java index 61c348a..8b78a99 100644 --- a/src/main/java/envoy/client/ui/PrimaryScrollPane.java +++ b/src/main/java/envoy/client/ui/PrimaryScrollPane.java @@ -17,6 +17,11 @@ public class PrimaryScrollPane extends JScrollPane { private int verticalScrollBarMaximum = getVerticalScrollBar().getMaximum(); private boolean chatOpened = false; + /** + * Initializes a {@link JScrollPane} with the primary Envoy design scheme + * + * @since Envoy v0.2-alpha + */ public PrimaryScrollPane() { setBorder(null); } /** @@ -67,5 +72,12 @@ public class PrimaryScrollPane extends JScrollPane { }); } + /** + * Indicates a chat being opened by the user to this {@link PrimaryScrollPane} + * triggering it to automatically scroll down. + * + * @param chatOpened indicates the chat opening status + * @since Envoy v0.2-alpha + */ public void setChatOpened(boolean chatOpened) { this.chatOpened = chatOpened; } }