Added KeyListeners for sending the message and an automatic line break
This commit is contained in:
parent
0edd9d998f
commit
d9870f9b22
@ -6,8 +6,7 @@ import java.awt.Font;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.InputMethodEvent;
|
||||
import java.awt.event.InputMethodListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
@ -56,7 +55,7 @@ public class ChatWindow extends JFrame {
|
||||
private JList<User> userList = new JList<>();
|
||||
private Chat currentChat;
|
||||
|
||||
private String messageText;
|
||||
private JTextArea messageEnterTextArea;
|
||||
|
||||
public ChatWindow(Client client, LocalDB localDB) {
|
||||
this.client = client;
|
||||
@ -118,38 +117,29 @@ public class ChatWindow extends JFrame {
|
||||
contentPane.add(scrollPane, gbc_scrollPane);
|
||||
|
||||
// Message enter field
|
||||
JTextArea messageEnterTextfield = new JTextArea();
|
||||
// checks for changed Message
|
||||
messageEnterTextfield.addInputMethodListener(new InputMethodListener() {
|
||||
messageEnterTextArea = new JTextArea();
|
||||
messageEnterTextArea.addKeyListener(new KeyAdapter() {
|
||||
|
||||
public void caretPositionChanged(InputMethodEvent arg0) {}
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_ENTER && ((SettingsScreen.enterToSend && e.getModifiersEx() == 0)
|
||||
|| (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) {
|
||||
postMessage(client, messageList);
|
||||
|
||||
public void inputMethodTextChanged(InputMethodEvent arg0) {
|
||||
String message = messageEnterTextfield.getText();
|
||||
int messageSize = message.length();
|
||||
int lineSize = 45;
|
||||
String[] tempString = message.split(System.getProperty("line.separator"));
|
||||
int currentLineAmount = tempString.length;
|
||||
int wantedLineAmount = Math.floorDiv(messageSize, lineSize);
|
||||
if (currentLineAmount != wantedLineAmount) {
|
||||
if (Math.abs(messageText.length() - messageSize) == 1
|
||||
&& currentLineAmount == wantedLineAmount - 1) {// Check for "normal" Keystroke/Backspace
|
||||
messageEnterTextfield.setText(transformLastSpace(message, wantedLineAmount, lineSize));
|
||||
}
|
||||
} else {// That's the case if a group of chars was a)inserted or b)deleted
|
||||
// TODO
|
||||
}
|
||||
messageText = message;
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
messageEnterTextfield.setCaretColor(new Color(255, 255, 255));
|
||||
messageEnterTextfield.setForeground(new Color(255, 255, 255));
|
||||
messageEnterTextfield.setBackground(new Color(51, 51, 51));
|
||||
messageEnterTextfield.setLineWrap(true);
|
||||
messageEnterTextfield.setBorder(null);
|
||||
messageEnterTextfield.setFont(new Font("Arial", Font.PLAIN, 17));
|
||||
messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
// checks for changed Message
|
||||
messageEnterTextArea.setWrapStyleWord(true);
|
||||
messageEnterTextArea.setCaretColor(new Color(255, 255, 255));
|
||||
messageEnterTextArea.setForeground(new Color(255, 255, 255));
|
||||
messageEnterTextArea.setBackground(new Color(51, 51, 51));
|
||||
messageEnterTextArea.setLineWrap(true);
|
||||
messageEnterTextArea.setBorder(null);
|
||||
messageEnterTextArea.setFont(new Font("Arial", Font.PLAIN, 17));
|
||||
messageEnterTextArea.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
|
||||
GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints();
|
||||
gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH;
|
||||
@ -158,13 +148,10 @@ public class ChatWindow extends JFrame {
|
||||
|
||||
gbc_messageEnterTextfield.insets = new Insets(10, 10, 10, 10);
|
||||
|
||||
contentPane.add(messageEnterTextfield, gbc_messageEnterTextfield);
|
||||
contentPane.add(messageEnterTextArea, gbc_messageEnterTextfield);
|
||||
|
||||
// Post Button
|
||||
JButton postButton = new JButton("Post");
|
||||
if (SettingsScreen.isEnterToSend() == true) { postButton.setMnemonic(KeyEvent.VK_ENTER); }
|
||||
// TODO: Other option to send only on "ctrl"+"enter" needs to be implemented.
|
||||
// TODO: Difficult. Also above statement doesn't work!
|
||||
postButton.setForeground(new Color(255, 255, 255));
|
||||
postButton.setBackground(new Color(102, 51, 153));
|
||||
postButton.setBorderPainted(false);
|
||||
@ -177,36 +164,7 @@ public class ChatWindow extends JFrame {
|
||||
|
||||
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
|
||||
|
||||
postButton.addActionListener((evt) -> {
|
||||
if (!client.hasRecipient()) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"Please select a recipient!",
|
||||
"Cannot send message",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!messageEnterTextfield.getText().isEmpty()) try {
|
||||
|
||||
// Create and send message object
|
||||
final Message message = client.createMessage(messageEnterTextfield.getText());
|
||||
client.sendMessage(message);
|
||||
|
||||
// Append message object to chat
|
||||
currentChat.appendMessage(message);
|
||||
messageList.setModel(currentChat.getModel());
|
||||
|
||||
// Clear text field
|
||||
messageEnterTextfield.setText("");
|
||||
contentPane.revalidate();
|
||||
} catch (Exception e) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"An exception occured while sending a message. See the log for more details.",
|
||||
"Exception occured",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
postButton.addActionListener((evt) -> { postMessage(client, messageList); });
|
||||
|
||||
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
||||
|
||||
@ -229,7 +187,7 @@ public class ChatWindow extends JFrame {
|
||||
SettingsScreen.open(localDB.getUser().getName());
|
||||
} catch (Exception e) {
|
||||
SettingsScreen.open();
|
||||
System.err.println("An Error occured while opening the Settings screen");
|
||||
System.err.println("An Error occured while opening the Settings screen: " + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
@ -244,7 +202,6 @@ public class ChatWindow extends JFrame {
|
||||
|
||||
GridBagConstraints gbc_partnerName = new GridBagConstraints();
|
||||
gbc_partnerName.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_partnerName.gridwidth = 2;
|
||||
gbc_partnerName.gridx = 1;
|
||||
gbc_partnerName.gridy = 0;
|
||||
|
||||
@ -298,22 +255,41 @@ public class ChatWindow extends JFrame {
|
||||
}
|
||||
|
||||
/**
|
||||
* takes care of too long or not needed lines in the message.
|
||||
* Posts a {@link Message}. Is used only twice: Once for clicking on the {@code postButton}<br>
|
||||
* and once for pressing the KeyStroke(s) to send a message ( (ctrl+)enter)
|
||||
*
|
||||
* @param message the message that is currently written in the textfield
|
||||
* @param wantedLine the line at which we currently are positioned
|
||||
* @param lineSize the amount of chars per line
|
||||
* @return the transformed message
|
||||
* @param client the client who wants to send a {@link Message}
|
||||
* @param messageList the chat in which this {@link Message} belongs
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
private String transformLastSpace(String message, int wantedLine, int lineSize) {
|
||||
int index = wantedLine * lineSize;
|
||||
int lastSpace = message.lastIndexOf(" ", index);
|
||||
if (index - lastSpace > lineSize) {// Fall Wort länger als Zeile
|
||||
return message.substring(0, index) + System.getProperty("line.separator") + message.substring(index);
|
||||
} else {
|
||||
return message.substring(0, lastSpace - 1) + System.getProperty("line.separator")
|
||||
+ message.substring(lastSpace + 1);
|
||||
private void postMessage(Client client, JList<Message> messageList) {
|
||||
if (!client.hasRecipient()) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"Please select a recipient!",
|
||||
"Cannot send message",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!messageEnterTextArea.getText().isEmpty()) try {
|
||||
|
||||
// Create and send message object
|
||||
final Message message = client.createMessage(messageEnterTextArea.getText());
|
||||
client.sendMessage(message);
|
||||
|
||||
// Append message object to chat
|
||||
currentChat.appendMessage(message);
|
||||
messageList.setModel(currentChat.getModel());
|
||||
|
||||
// Clear text field
|
||||
messageEnterTextArea.setText("");
|
||||
contentPane.revalidate();
|
||||
} catch (Exception e) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"An exception occured while sending a message. See the log for more details.",
|
||||
"Exception occured",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ public class SettingsScreen extends JDialog {
|
||||
|
||||
private static final long serialVersionUID = -4476913491263077107L;
|
||||
private final JPanel contentPanel = new JPanel();
|
||||
public static boolean EnterToSend = true;
|
||||
public static boolean enterToSend = true;
|
||||
|
||||
// TODO: Add a JPanel with all the Information necessary:
|
||||
// change (Picture,Username, Email, Password) and toggle(light/dark mode,
|
||||
@ -139,10 +139,10 @@ public class SettingsScreen extends JDialog {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if Enter should be used to send a message
|
||||
* @return true if Enter should be used to send a message instead of ctrl+enter
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public static boolean isEnterToSend() { return EnterToSend; }
|
||||
public static boolean isEnterToSend() { return enterToSend; }
|
||||
|
||||
/**
|
||||
* @param enterToSend <br>
|
||||
@ -151,6 +151,6 @@ public class SettingsScreen extends JDialog {
|
||||
* buttonpress "enter" or "ctrl"+"enter"
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public static void setEnterToSend(boolean enterToSend) { EnterToSend = enterToSend; }
|
||||
public static void setEnterToSend(boolean enterForSend) { enterToSend = enterForSend; }
|
||||
// TODO: Should be changed to private, but later to avoid warnings
|
||||
}
|
||||
|
Reference in New Issue
Block a user