86 lines
2.7 KiB
Java
86 lines
2.7 KiB
Java
package envoy.client.ui.primary;
|
|
|
|
import javax.swing.JScrollPane;
|
|
|
|
import envoy.client.ui.Theme;
|
|
|
|
/**
|
|
* Project: <strong>envoy-client</strong><br>
|
|
* File: <strong>PrimaryScrollPane.java</strong><br>
|
|
* Created: <strong>15 Dec 2019</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @author Maximilian Käfer
|
|
*/
|
|
public class PrimaryScrollPane extends JScrollPane {
|
|
|
|
private static final long serialVersionUID = -4786837444056228439L;
|
|
|
|
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); }
|
|
|
|
/**
|
|
* Styles the vertical and horizontal scroll bars.
|
|
*
|
|
* @param theme the color set used to color the component
|
|
* @since Envoy v0.2-alpha
|
|
*/
|
|
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));
|
|
}
|
|
|
|
/**
|
|
* Implements <b>autoscroll functionality</b> for the vertical scroll bar. </br>
|
|
* </br>
|
|
* Functionality to automatically scroll down when user views </br>
|
|
* the bottom of the chat while there are new messages added. </br>
|
|
* </br>
|
|
* When chat is opened, the vertical scroll bar starts at the bottom. </br>
|
|
* </br>
|
|
* When rereading messages, the chat doesn't scroll down if new messages </br>
|
|
* 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;
|
|
|
|
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();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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; }
|
|
}
|