Revised code according to reviews by @delvh and @CyB3RC0nN0R

This commit is contained in:
DieGurke
2019-12-14 13:46:19 +01:00
parent ecf2566431
commit 4ba1f6360c
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); } }
}