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.GridBagConstraints;
|
||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.event.InputMethodEvent;
|
import java.awt.event.KeyAdapter;
|
||||||
import java.awt.event.InputMethodListener;
|
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
@ -56,7 +55,7 @@ public class ChatWindow extends JFrame {
|
|||||||
private JList<User> userList = new JList<>();
|
private JList<User> userList = new JList<>();
|
||||||
private Chat currentChat;
|
private Chat currentChat;
|
||||||
|
|
||||||
private String messageText;
|
private JTextArea messageEnterTextArea;
|
||||||
|
|
||||||
public ChatWindow(Client client, LocalDB localDB) {
|
public ChatWindow(Client client, LocalDB localDB) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
@ -118,38 +117,29 @@ public class ChatWindow extends JFrame {
|
|||||||
contentPane.add(scrollPane, gbc_scrollPane);
|
contentPane.add(scrollPane, gbc_scrollPane);
|
||||||
|
|
||||||
// Message enter field
|
// Message enter field
|
||||||
JTextArea messageEnterTextfield = new JTextArea();
|
messageEnterTextArea = new JTextArea();
|
||||||
// checks for changed Message
|
messageEnterTextArea.addKeyListener(new KeyAdapter() {
|
||||||
messageEnterTextfield.addInputMethodListener(new InputMethodListener() {
|
|
||||||
|
|
||||||
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));
|
// checks for changed Message
|
||||||
messageEnterTextfield.setForeground(new Color(255, 255, 255));
|
messageEnterTextArea.setWrapStyleWord(true);
|
||||||
messageEnterTextfield.setBackground(new Color(51, 51, 51));
|
messageEnterTextArea.setCaretColor(new Color(255, 255, 255));
|
||||||
messageEnterTextfield.setLineWrap(true);
|
messageEnterTextArea.setForeground(new Color(255, 255, 255));
|
||||||
messageEnterTextfield.setBorder(null);
|
messageEnterTextArea.setBackground(new Color(51, 51, 51));
|
||||||
messageEnterTextfield.setFont(new Font("Arial", Font.PLAIN, 17));
|
messageEnterTextArea.setLineWrap(true);
|
||||||
messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5));
|
messageEnterTextArea.setBorder(null);
|
||||||
|
messageEnterTextArea.setFont(new Font("Arial", Font.PLAIN, 17));
|
||||||
|
messageEnterTextArea.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
|
|
||||||
GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints();
|
GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints();
|
||||||
gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH;
|
gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH;
|
||||||
@ -158,13 +148,10 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
gbc_messageEnterTextfield.insets = new Insets(10, 10, 10, 10);
|
gbc_messageEnterTextfield.insets = new Insets(10, 10, 10, 10);
|
||||||
|
|
||||||
contentPane.add(messageEnterTextfield, gbc_messageEnterTextfield);
|
contentPane.add(messageEnterTextArea, gbc_messageEnterTextfield);
|
||||||
|
|
||||||
// Post Button
|
// Post Button
|
||||||
JButton postButton = new JButton("Post");
|
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.setForeground(new Color(255, 255, 255));
|
||||||
postButton.setBackground(new Color(102, 51, 153));
|
postButton.setBackground(new Color(102, 51, 153));
|
||||||
postButton.setBorderPainted(false);
|
postButton.setBorderPainted(false);
|
||||||
@ -177,36 +164,7 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
|
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
|
||||||
|
|
||||||
postButton.addActionListener((evt) -> {
|
postButton.addActionListener((evt) -> { postMessage(client, messageList); });
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
||||||
|
|
||||||
@ -229,7 +187,7 @@ public class ChatWindow extends JFrame {
|
|||||||
SettingsScreen.open(localDB.getUser().getName());
|
SettingsScreen.open(localDB.getUser().getName());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
SettingsScreen.open();
|
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();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -244,7 +202,6 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
GridBagConstraints gbc_partnerName = new GridBagConstraints();
|
GridBagConstraints gbc_partnerName = new GridBagConstraints();
|
||||||
gbc_partnerName.fill = GridBagConstraints.HORIZONTAL;
|
gbc_partnerName.fill = GridBagConstraints.HORIZONTAL;
|
||||||
gbc_partnerName.gridwidth = 2;
|
|
||||||
gbc_partnerName.gridx = 1;
|
gbc_partnerName.gridx = 1;
|
||||||
gbc_partnerName.gridy = 0;
|
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 client the client who wants to send a {@link Message}
|
||||||
* @param wantedLine the line at which we currently are positioned
|
* @param messageList the chat in which this {@link Message} belongs
|
||||||
* @param lineSize the amount of chars per line
|
|
||||||
* @return the transformed message
|
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
private String transformLastSpace(String message, int wantedLine, int lineSize) {
|
private void postMessage(Client client, JList<Message> messageList) {
|
||||||
int index = wantedLine * lineSize;
|
if (!client.hasRecipient()) {
|
||||||
int lastSpace = message.lastIndexOf(" ", index);
|
JOptionPane.showMessageDialog(this,
|
||||||
if (index - lastSpace > lineSize) {// Fall Wort länger als Zeile
|
"Please select a recipient!",
|
||||||
return message.substring(0, index) + System.getProperty("line.separator") + message.substring(index);
|
"Cannot send message",
|
||||||
} else {
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
return message.substring(0, lastSpace - 1) + System.getProperty("line.separator")
|
return;
|
||||||
+ message.substring(lastSpace + 1);
|
}
|
||||||
|
|
||||||
|
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 static final long serialVersionUID = -4476913491263077107L;
|
||||||
private final JPanel contentPanel = new JPanel();
|
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:
|
// TODO: Add a JPanel with all the Information necessary:
|
||||||
// change (Picture,Username, Email, Password) and toggle(light/dark mode,
|
// 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
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public static boolean isEnterToSend() { return EnterToSend; }
|
public static boolean isEnterToSend() { return enterToSend; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param enterToSend <br>
|
* @param enterToSend <br>
|
||||||
@ -151,6 +151,6 @@ public class SettingsScreen extends JDialog {
|
|||||||
* buttonpress "enter" or "ctrl"+"enter"
|
* buttonpress "enter" or "ctrl"+"enter"
|
||||||
* @since Envoy v0.1-alpha
|
* @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
|
// TODO: Should be changed to private, but later to avoid warnings
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user