From ceb50cd01cedc58fa72060155200e70c975e6fca Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 22 Mar 2020 17:20:05 +0100 Subject: [PATCH] Added disabling of postButton while String.isBlank() returns true --- .../envoy/client/ui/container/ChatWindow.java | 90 ++++++++++--------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/src/main/java/envoy/client/ui/container/ChatWindow.java b/src/main/java/envoy/client/ui/container/ChatWindow.java index b762ceb..a2c2f9a 100644 --- a/src/main/java/envoy/client/ui/container/ChatWindow.java +++ b/src/main/java/envoy/client/ui/container/ChatWindow.java @@ -4,9 +4,7 @@ import java.awt.*; import java.awt.datatransfer.StringSelection; import java.awt.event.*; import java.io.IOException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -118,39 +116,44 @@ public class ChatWindow extends JFrame { gbl_contentPane.columnWidths = new int[] { 1, 1, 1 }; gbl_contentPane.rowHeights = new int[] { 1, 1, 1, 1 }; gbl_contentPane.columnWeights = new double[] { 0.03, 1.0, 0.1 }; - gbl_contentPane.rowWeights = new double[] { 0.03, 0.001, 1.0, 0.005 }; + gbl_contentPane.rowWeights = new double[] { 0.03, 0.001, 1.0, 0.001 }; contentPane.setLayout(gbl_contentPane); - // ContextMenu - Map commands = new HashMap<>() { - - private static final long serialVersionUID = -2755235774946990126L; - - { - put("forward selected message", - evt -> forwardMessageToMultipleUsers(messageList.getSingleSelectedElement(), - ContactsChooserDialog - .showForwardingDialog("Forward selected message to", messageList.getSingleSelectedElement(), client))); - put("copy", evt -> { - // TODO should be enhanced to allow also copying of message attachments, - // especially pictures - StringSelection copy = new StringSelection(messageList.getSingleSelectedElement().getText()); - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(copy, copy); - }); - // TODO insert implementation to edit and delete messages - put("delete", evt -> {}); - put("edit", evt -> {}); - put("quote", evt -> {}); - } - }; - contextMenu = new ContextMenu(null, messageList, commands, null, null).build(); - messageList.setBorder(new EmptyBorder(space, space, space, space)); messageList.setSelectionMode(SelectionMode.SINGLE); messageList.setSelectionHandler((message, comp, isSelected) -> { final var theme = Settings.getInstance().getCurrentTheme(); comp.setBackground(isSelected ? theme.getSelectionColor() : theme.getCellColor()); - contextMenu.show(comp, 0, 0); + + // ContextMenu + Map commands = new HashMap<>() { + + private static final long serialVersionUID = -2755235774946990126L; + + { + put("forward selected message", evt -> { + final Message selectedMessage = messageList.getSingleSelectedElement(); + forwardMessage(selectedMessage, + ContactsChooserDialog + .showForwardingDialog("Forward selected message to", null, selectedMessage, localDb.getUsers().values()) + .toArray(new User[0])); + }); + put("copy", evt -> { + // TODO should be enhanced to allow also copying of message attachments, + // especially pictures + StringSelection copy = new StringSelection(messageList.getSingleSelectedElement().getText()); + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(copy, copy); + }); + // TODO insert implementation to edit and delete messages + put("delete", evt -> {}); + put("edit", evt -> {}); + put("quote", evt -> {}); + } + }; + if (isSelected) { + contextMenu = new ContextMenu(null, comp, commands, null, null).build(); + contextMenu.show(comp, 0, 0); + } }); scrollPane.setViewportView(messageList); @@ -180,7 +183,10 @@ public class ChatWindow extends JFrame { messageEnterTextArea.addInputMethodListener(new InputMethodListener() { @Override - public void inputMethodTextChanged(InputMethodEvent event) { checkMessageTextLength(); } + public void inputMethodTextChanged(InputMethodEvent event) { + checkMessageTextLength(); + checkPostButton(messageEnterTextArea.getText()); + } @Override public void caretPositionChanged(InputMethodEvent event) {} @@ -191,31 +197,30 @@ public class ChatWindow extends JFrame { @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER - && (Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0 || e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK)) + && (Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0 || e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK) + && postButton.isEnabled()) postMessage(); // Checking if text is too long checkMessageTextLength(); + checkPostButton(messageEnterTextArea.getText()); } }); - GridBagConstraints gbc_scrollPaneForTextInput = new GridBagConstraints(); - gbc_scrollPaneForTextInput.fill = GridBagConstraints.BOTH; - gbc_scrollPaneForTextInput.gridx = 1; - gbc_scrollPaneForTextInput.gridy = 3; - - gbc_scrollPaneForTextInput.insets = insets; - - contentPane.add(messageEnterTextArea, gbc_scrollPaneForTextInput); + GridBagConstraints gbc_messageEnterTextArea = new GridBagConstraints(); + gbc_messageEnterTextArea.fill = GridBagConstraints.BOTH; + gbc_messageEnterTextArea.gridx = 1; + gbc_messageEnterTextArea.gridy = 3; + gbc_messageEnterTextArea.insets = insets; + contentPane.add(messageEnterTextArea, gbc_messageEnterTextArea); // Post Button GridBagConstraints gbc_postButton = new GridBagConstraints(); - gbc_postButton.fill = GridBagConstraints.BOTH; gbc_postButton.gridx = 2; gbc_postButton.gridy = 3; gbc_postButton.insets = insets; - postButton.addActionListener((evt) -> { postMessage(); }); + postButton.setEnabled(false); contentPane.add(postButton, gbc_postButton); // Settings Button @@ -552,6 +557,7 @@ public class ChatWindow extends JFrame { sendMessage(message); // Clear text field messageEnterTextArea.setText(""); + postButton.setEnabled(false); } /** @@ -684,4 +690,6 @@ public class ChatWindow extends JFrame { JOptionPane.WARNING_MESSAGE); } } + + private void checkPostButton(String text) { postButton.setEnabled(!text.trim().isBlank()); } }