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; } +}