Revised code according to reviews by @delvh and @CyB3RC0nN0R

This commit is contained in:
DieGurke 2019-12-14 13:46:19 +01:00
parent 3c8a4c445f
commit 507e249512
3 changed files with 24 additions and 28 deletions

View File

@ -53,16 +53,16 @@ public class ChatWindow extends JFrame {
private Client client;
private LocalDB localDB;
// GUI components
private JPanel contentPane = new JPanel();
private PrimaryTextArea messageEnterTextArea;
private JList<User> userList = new JList<>();
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 JTextPane textPane = new JTextPane();
private JList<Message> messageList = new JList<>();
private JScrollPane scrollPane = new JScrollPane();
private JTextPane textPane = new JTextPane();
// private JCheckBox jCbChangeMode;
private PrimaryButton postButton;
private PrimaryButton settingsButton;
private PrimaryButton postButton = new PrimaryButton("Post");
private PrimaryButton settingsButton = new PrimaryButton("Settings");
private static int space = 4;
@ -76,7 +76,8 @@ public class ChatWindow extends JFrame {
setBounds(100, 100, 600, 800);
setTitle("Envoy");
setLocationRelativeTo(null);
setIconImage(Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("envoy_logo.png")));
setIconImage(
Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("envoy_logo.png")));
// Save chats when window closes
addWindowListener(new WindowAdapter() {
@ -124,9 +125,6 @@ public class ChatWindow extends JFrame {
gbc_scrollPane.insets = new Insets(space, space, space, space);
contentPane.add(scrollPane, gbc_scrollPane);
// Checks for changed Message
messageEnterTextArea = new PrimaryTextArea(space);
// Message enter field
messageEnterTextArea.addKeyListener(new KeyAdapter() {
@ -140,7 +138,6 @@ public class ChatWindow extends JFrame {
}
});
GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints();
gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH;
gbc_messageEnterTextfield.gridx = 1;
@ -151,7 +148,6 @@ public class ChatWindow extends JFrame {
contentPane.add(messageEnterTextArea, gbc_messageEnterTextfield);
// Post Button
postButton = new PrimaryButton("Post");
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH;
@ -164,8 +160,6 @@ public class ChatWindow extends JFrame {
contentPane.add(postButton, gbc_moveSelectionPostButton);
// Settings Button
settingsButton = new PrimaryButton("Settings");
GridBagConstraints gbc_moveSelectionSettingsButton = new GridBagConstraints();
gbc_moveSelectionSettingsButton.fill = GridBagConstraints.BOTH;
@ -177,8 +171,8 @@ public class ChatWindow extends JFrame {
settingsButton.addActionListener((evt) -> {
try {
SettingsScreen.open();
changeChatWindowColors(Settings.getInstance().getCurrentTheme());
} catch (Exception e) {
changeChatWindowColors(Settings.getInstance().getCurrentTheme());
} catch (Exception e) {
SettingsScreen.open();
logger.log(Level.WARNING, "An error occured while opening the settings screen", e);
e.printStackTrace();
@ -207,7 +201,11 @@ public class ChatWindow extends JFrame {
final User user = selectedUserList.getSelectedValue();
client.setRecipient(user);
currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get();
currentChat = localDB.getChats()
.stream()
.filter(chat -> chat.getRecipient().getID() == user.getID())
.findFirst()
.get();
// Set all unread messages in the chat to read
readCurrentChat();
@ -231,7 +229,7 @@ public class ChatWindow extends JFrame {
gbc_userList.insets = new Insets(space, space, space, space);
changeChatWindowColors(Settings.getInstance().getCurrentTheme());
contentPane.add(userList, gbc_userList);
contentPane.revalidate();
@ -240,7 +238,6 @@ public class ChatWindow extends JFrame {
contentPane.revalidate();
}
/**
* Used to immediately reload the ChatWindow when settings were changed.
@ -373,4 +370,3 @@ public class ChatWindow extends JFrame {
*/
private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } }
}

View File

@ -11,6 +11,7 @@ import javax.swing.JButton;
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy v0.2-alpha
*/
public class PrimaryButton extends JButton {
@ -35,8 +36,6 @@ public class PrimaryButton extends JButton {
*/
public PrimaryButton(String title, int arcSize) {
super(title);
// setForeground(new Color(255, 255, 255));
// setBackground(new Color(102, 51, 153));
setBorderPainted(false);
setFocusPainted(false);
setContentAreaFilled(false);

View File

@ -12,6 +12,7 @@ import javax.swing.border.EmptyBorder;
* Created: <strong>07.12.2019</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy v0.2-alpha
*/
public class PrimaryTextArea extends JTextArea {
@ -20,7 +21,7 @@ public class PrimaryTextArea extends JTextArea {
private int arcSize;
/**
* Creates TextArea
* Creates the text area
*
* @param borderSpace
* @since Envoy 0.2-alpha
@ -28,10 +29,10 @@ public class PrimaryTextArea extends JTextArea {
public PrimaryTextArea(int borderSpace) { this(6, borderSpace); }
/**
* Creates TextArea
* Creates the text area
*
* @param arcSize
* @param borderSpace
* @param borderSpace - the insets of the border on all four sides
* @since Envoy 0.2-alpha
*/
public PrimaryTextArea(int arcSize, int borderSpace) {
@ -54,7 +55,7 @@ public class PrimaryTextArea extends JTextArea {
}
/**
* @return the arcSize
* @return the arcSize - the diameter of the arc at the four corners.
* @since Envoy 0.2-alpha
*/
public int getArcSize() { return arcSize; }