Added automatic line separation and Keylistener for posting
Improvements yet to be made (help wanted!): * automatic line separation does not work * automatic line separation needs a case for insertion of a String * Keylistener for Posting does not work * Method for listening to multiple keys needed (ctrl+enter) * A template Settings screen was added, has yet to be polished
This commit is contained in:
@ -6,6 +6,9 @@ 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.KeyEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
@ -39,7 +42,7 @@ import envoy.schema.Users;
|
||||
* @author Kai S. K. Engelbart
|
||||
* @author Maximilian Käfer
|
||||
* @author Leon Hofmeister
|
||||
* @since Envoy 0.1
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public class ChatWindow extends JFrame {
|
||||
|
||||
@ -53,6 +56,8 @@ public class ChatWindow extends JFrame {
|
||||
private JList<User> userList = new JList<>();
|
||||
private Chat currentChat;
|
||||
|
||||
private String messageText;
|
||||
|
||||
public ChatWindow(Client client, LocalDB localDB) {
|
||||
this.client = client;
|
||||
this.localDB = localDB;
|
||||
@ -77,7 +82,7 @@ public class ChatWindow extends JFrame {
|
||||
gbl_contentPane.columnWidths = new int[] { 1, 1, 1 };
|
||||
gbl_contentPane.rowHeights = new int[] { 1, 1, 1 };
|
||||
gbl_contentPane.columnWeights = new double[] { 0.3, 1.0, 0.1 };
|
||||
gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 };
|
||||
gbl_contentPane.rowWeights = new double[] { 0.05, 1.0, 0.07 };
|
||||
contentPane.setLayout(gbl_contentPane);
|
||||
|
||||
JList<Message> messageList = new JList<>();
|
||||
@ -114,6 +119,30 @@ public class ChatWindow extends JFrame {
|
||||
|
||||
// Message enter field
|
||||
JTextArea messageEnterTextfield = new JTextArea();
|
||||
// checks for changed Message
|
||||
messageEnterTextfield.addInputMethodListener(new InputMethodListener() {
|
||||
|
||||
public void caretPositionChanged(InputMethodEvent arg0) {}
|
||||
|
||||
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));
|
||||
@ -122,17 +151,20 @@ public class ChatWindow extends JFrame {
|
||||
messageEnterTextfield.setFont(new Font("Arial", Font.PLAIN, 17));
|
||||
messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
|
||||
GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints();
|
||||
gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH;
|
||||
gbc_moveSelectionMessageEnterTextfield.gridx = 1;
|
||||
gbc_moveSelectionMessageEnterTextfield.gridy = 2;
|
||||
GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints();
|
||||
gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH;
|
||||
gbc_messageEnterTextfield.gridx = 1;
|
||||
gbc_messageEnterTextfield.gridy = 2;
|
||||
|
||||
gbc_moveSelectionMessageEnterTextfield.insets = new Insets(10, 10, 10, 10);
|
||||
gbc_messageEnterTextfield.insets = new Insets(10, 10, 10, 10);
|
||||
|
||||
contentPane.add(messageEnterTextfield, gbc_moveSelectionMessageEnterTextfield);
|
||||
contentPane.add(messageEnterTextfield, 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);
|
||||
@ -147,7 +179,10 @@ public class ChatWindow extends JFrame {
|
||||
|
||||
postButton.addActionListener((evt) -> {
|
||||
if (!client.hasRecipient()) {
|
||||
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"Please select a recipient!",
|
||||
"Cannot send message",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -175,6 +210,31 @@ public class ChatWindow extends JFrame {
|
||||
|
||||
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
||||
|
||||
// Settings Button
|
||||
JButton settingsButton = new JButton("Settings");
|
||||
settingsButton.setForeground(new Color(255, 255, 255));
|
||||
settingsButton.setBackground(new Color(102, 51, 153));
|
||||
settingsButton.setBorderPainted(false);
|
||||
|
||||
GridBagConstraints gbc_moveSelectionSettingsButton = new GridBagConstraints();
|
||||
|
||||
gbc_moveSelectionSettingsButton.fill = GridBagConstraints.BOTH;
|
||||
gbc_moveSelectionSettingsButton.gridx = 2;
|
||||
gbc_moveSelectionSettingsButton.gridy = 0;
|
||||
|
||||
gbc_moveSelectionSettingsButton.insets = new Insets(10, 10, 10, 10);
|
||||
|
||||
settingsButton.addActionListener((evt) -> {
|
||||
try {
|
||||
SettingsScreen.open(localDB.getUser().getName());
|
||||
} catch (Exception e) {
|
||||
SettingsScreen.open();
|
||||
System.err.println("An Error occured while opening the Settings screen");
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
contentPane.add(settingsButton, gbc_moveSelectionSettingsButton);
|
||||
|
||||
// Partner name display
|
||||
JTextPane textPane = new JTextPane();
|
||||
textPane.setBackground(new Color(0, 0, 0));
|
||||
@ -200,7 +260,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();
|
||||
|
||||
client.setRecipient(user);
|
||||
|
||||
@ -233,9 +297,31 @@ public class ChatWindow extends JFrame {
|
||||
contentPane.revalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* takes care of too long or not needed lines in the message.
|
||||
*
|
||||
* @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
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the elements of the user list by downloading them from the
|
||||
* server.
|
||||
*
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
private void loadUsersAndChats() {
|
||||
new Thread(() -> {
|
||||
@ -257,14 +343,17 @@ public class ChatWindow extends JFrame {
|
||||
*
|
||||
* @param timeout the amount of time that passes between two requests sent to
|
||||
* the server
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
private void startReceiverThread(int timeout) {
|
||||
new Timer(timeout, (evt) -> {
|
||||
Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
|
||||
for (int i = 0; i < unreadMessages.getMessage().size(); i++)
|
||||
for (int j = 0; j < localDB.getChats().size(); j++)
|
||||
if (localDB.getChats().get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender())
|
||||
localDB.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i));
|
||||
if (localDB.getChats().get(j).getRecipient().getID() == unreadMessages.getMessage()
|
||||
.get(i)
|
||||
.getMetaData()
|
||||
.getSender()) localDB.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i));
|
||||
}).start();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user