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 Client client;
private LocalDB localDB; private LocalDB localDB;
// GUI components // GUI components
private JPanel contentPane = new JPanel(); private JPanel contentPane = new JPanel();
private PrimaryTextArea messageEnterTextArea; private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space);
private JList<User> userList = new JList<>(); private JList<User> userList = new JList<>();
private Chat currentChat; private Chat currentChat;
private JList<Message> messageList = new JList<>(); private JList<Message> messageList = new JList<>();
private JScrollPane scrollPane = new JScrollPane(); private JScrollPane scrollPane = new JScrollPane();
private JTextPane textPane = new JTextPane(); private JTextPane textPane = new JTextPane();
// private JCheckBox jCbChangeMode; // private JCheckBox jCbChangeMode;
private PrimaryButton postButton; private PrimaryButton postButton = new PrimaryButton("Post");
private PrimaryButton settingsButton; private PrimaryButton settingsButton = new PrimaryButton("Settings");
private static int space = 4; private static int space = 4;
@ -76,7 +76,8 @@ public class ChatWindow extends JFrame {
setBounds(100, 100, 600, 800); setBounds(100, 100, 600, 800);
setTitle("Envoy"); setTitle("Envoy");
setLocationRelativeTo(null); 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 // Save chats when window closes
addWindowListener(new WindowAdapter() { addWindowListener(new WindowAdapter() {
@ -124,9 +125,6 @@ public class ChatWindow extends JFrame {
gbc_scrollPane.insets = new Insets(space, space, space, space); gbc_scrollPane.insets = new Insets(space, space, space, space);
contentPane.add(scrollPane, gbc_scrollPane); contentPane.add(scrollPane, gbc_scrollPane);
// Checks for changed Message
messageEnterTextArea = new PrimaryTextArea(space);
// Message enter field // Message enter field
messageEnterTextArea.addKeyListener(new KeyAdapter() { messageEnterTextArea.addKeyListener(new KeyAdapter() {
@ -140,7 +138,6 @@ public class ChatWindow extends JFrame {
} }
}); });
GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints(); GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints();
gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH; gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH;
gbc_messageEnterTextfield.gridx = 1; gbc_messageEnterTextfield.gridx = 1;
@ -151,7 +148,6 @@ public class ChatWindow extends JFrame {
contentPane.add(messageEnterTextArea, gbc_messageEnterTextfield); contentPane.add(messageEnterTextArea, gbc_messageEnterTextfield);
// Post Button // Post Button
postButton = new PrimaryButton("Post");
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints(); GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH; gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH;
@ -164,8 +160,6 @@ public class ChatWindow extends JFrame {
contentPane.add(postButton, gbc_moveSelectionPostButton); contentPane.add(postButton, gbc_moveSelectionPostButton);
// Settings Button // Settings Button
settingsButton = new PrimaryButton("Settings");
GridBagConstraints gbc_moveSelectionSettingsButton = new GridBagConstraints(); GridBagConstraints gbc_moveSelectionSettingsButton = new GridBagConstraints();
gbc_moveSelectionSettingsButton.fill = GridBagConstraints.BOTH; gbc_moveSelectionSettingsButton.fill = GridBagConstraints.BOTH;
@ -177,8 +171,8 @@ public class ChatWindow extends JFrame {
settingsButton.addActionListener((evt) -> { settingsButton.addActionListener((evt) -> {
try { try {
SettingsScreen.open(); SettingsScreen.open();
changeChatWindowColors(Settings.getInstance().getCurrentTheme()); changeChatWindowColors(Settings.getInstance().getCurrentTheme());
} catch (Exception e) { } catch (Exception e) {
SettingsScreen.open(); SettingsScreen.open();
logger.log(Level.WARNING, "An error occured while opening the settings screen", e); logger.log(Level.WARNING, "An error occured while opening the settings screen", e);
e.printStackTrace(); e.printStackTrace();
@ -207,7 +201,11 @@ public class ChatWindow extends JFrame {
final User user = selectedUserList.getSelectedValue(); final User user = selectedUserList.getSelectedValue();
client.setRecipient(user); 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 // Set all unread messages in the chat to read
readCurrentChat(); readCurrentChat();
@ -231,7 +229,7 @@ public class ChatWindow extends JFrame {
gbc_userList.insets = new Insets(space, space, space, space); gbc_userList.insets = new Insets(space, space, space, space);
changeChatWindowColors(Settings.getInstance().getCurrentTheme()); changeChatWindowColors(Settings.getInstance().getCurrentTheme());
contentPane.add(userList, gbc_userList); contentPane.add(userList, gbc_userList);
contentPane.revalidate(); contentPane.revalidate();
@ -240,7 +238,6 @@ public class ChatWindow extends JFrame {
contentPane.revalidate(); contentPane.revalidate();
} }
/** /**
* Used to immediately reload the ChatWindow when settings were changed. * 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); } } 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 Kai S. K. Engelbart
* @author Maximilian K&auml;fer * @author Maximilian K&auml;fer
* @since Envoy v0.2-alpha
*/ */
public class PrimaryButton extends JButton { public class PrimaryButton extends JButton {
@ -35,8 +36,6 @@ public class PrimaryButton extends JButton {
*/ */
public PrimaryButton(String title, int arcSize) { public PrimaryButton(String title, int arcSize) {
super(title); super(title);
// setForeground(new Color(255, 255, 255));
// setBackground(new Color(102, 51, 153));
setBorderPainted(false); setBorderPainted(false);
setFocusPainted(false); setFocusPainted(false);
setContentAreaFilled(false); setContentAreaFilled(false);

View File

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