Added PrimaryScrollPane class with default scroll pane UI settings

This commit is contained in:
Kai S. K. Engelbart 2019-12-15 17:44:13 +01:00
parent adb5c417c5
commit acc7424503
3 changed files with 64 additions and 44 deletions

View File

@ -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<User> userList = new JList<>();
private Chat currentChat;
private JList<Message> 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<User> userList = new JList<>();
private Chat currentChat;
private JList<Message> 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());

View File

@ -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();

View File

@ -0,0 +1,48 @@
package envoy.client.ui;
import javax.swing.JScrollPane;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>PrimaryScrollPane.java</strong><br>
* Created: <strong>15 Dec 2019</strong><br>
*
* @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; }
}