Adding contacts technical aspects

* Contact SearchResult from server is now getting processed and
correctly displayed.
* Sending a AddContact event to server, if button is pressed.
* Added several interface objects
This commit is contained in:
DieGurke 2020-02-09 16:26:36 +01:00
parent 79da5e6979
commit 7a2544bab6
6 changed files with 166 additions and 17 deletions

View File

@ -28,7 +28,7 @@
<dependency> <dependency>
<groupId>com.github.informatik-ag-ngl</groupId> <groupId>com.github.informatik-ag-ngl</groupId>
<artifactId>envoy-common</artifactId> <artifactId>envoy-common</artifactId>
<version>develop-SNAPSHOT</version> <version>f~contacts-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,42 @@
package envoy.client.event;
import envoy.data.User;
import envoy.event.ContactOperation.Operation;
import envoy.event.Event;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>AddContactEvent.java</strong><br>
* Created: <strong>09.02.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy v0.3-alpha
*/
public class AddContactEvent implements Event<User> {
private User contact;
private Operation operation;
private static final long serialVersionUID = 7855669140917046709L;
/**
* Initializes a {@link AddContactEvent}
*
* @param contact the user to be added to the contacts
* @param operation the operation, which should be executed
* @since Envoy v0.3-alpha
*/
public AddContactEvent(User contact, Operation operation) {
this.contact = contact;
this.operation = operation;
}
@Override
public User get() { return contact; }
/**
* @return the operation, which should be executed
*/
public Operation getOperation() { return operation; }
}

View File

@ -0,0 +1,32 @@
package envoy.client.event;
import java.util.List;
import envoy.data.User;
import envoy.event.Event;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>SearchResultEvent.java</strong><br>
* Created: <strong>08.02.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy v0.3-alpha
*/
public class SearchResultEvent implements Event<List<User>> {
private final List<User> resultList;
private static final long serialVersionUID = 2540321329192201277L;
/**
* Initializes a {@link SearchResultEvent}
*
* @param resultList the List containing the contacts sent from the server
* @since Envoy v0.3-alpha
*/
public SearchResultEvent(List<User> resultList) { this.resultList = resultList; }
@Override
public List<User> get() { return resultList; }
}

View File

@ -11,11 +11,10 @@ import javax.naming.TimeLimitExceededException;
import envoy.client.Config; import envoy.client.Config;
import envoy.client.data.LocalDb; import envoy.client.data.LocalDb;
import envoy.client.event.SearchResultEvent;
import envoy.client.util.EnvoyLog; import envoy.client.util.EnvoyLog;
import envoy.data.*; import envoy.data.*;
import envoy.event.Event; import envoy.event.*;
import envoy.event.IdGeneratorRequest;
import envoy.event.MessageStatusChangeEvent;
import envoy.util.SerializationUtils; import envoy.util.SerializationUtils;
/** /**
@ -111,6 +110,9 @@ public class Client implements Closeable {
// Process message ID generation // Process message ID generation
receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator); receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator);
// Process contact searches
receiver.registerProcessor(Contacts.class, contacts -> EventBus.getInstance().dispatch(new SearchResultEvent(contacts.getContacts())));
// Request a generator if none is present or the existing one is consumed // Request a generator if none is present or the existing one is consumed
if (!localDb.hasIdGenerator() || !localDb.getIdGenerator().hasNext()) requestIdGenerator(); if (!localDb.hasIdGenerator() || !localDb.getIdGenerator().hasNext()) requestIdGenerator();

View File

@ -9,12 +9,13 @@ import java.util.logging.Logger;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import envoy.client.Settings; import envoy.client.Settings;
import envoy.client.data.Chat; import envoy.client.data.Chat;
import envoy.client.data.LocalDb; import envoy.client.data.LocalDb;
import envoy.client.event.MessageCreationEvent; import envoy.client.event.*;
import envoy.client.event.ThemeChangeEvent;
import envoy.client.net.Client; import envoy.client.net.Client;
import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentList;
import envoy.client.ui.list.ComponentListModel; import envoy.client.ui.list.ComponentListModel;
@ -24,8 +25,8 @@ import envoy.data.Message;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
import envoy.data.MessageBuilder; import envoy.data.MessageBuilder;
import envoy.data.User; import envoy.data.User;
import envoy.event.EventBus; import envoy.event.*;
import envoy.event.MessageStatusChangeEvent; import envoy.event.ContactOperation.Operation;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -47,6 +48,7 @@ public class ChatWindow extends JFrame {
private JPanel contentPane = new JPanel(); private JPanel contentPane = new JPanel();
private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space); private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space);
private JList<User> userList = new JList<>(); private JList<User> userList = new JList<>();
private DefaultListModel<User> userListModel = new DefaultListModel<>();
private Chat currentChat; private Chat currentChat;
private ComponentList<Message> messageList = new ComponentList<>(new MessageListRenderer()); private ComponentList<Message> messageList = new ComponentList<>(new MessageListRenderer());
private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); private PrimaryScrollPane scrollPane = new PrimaryScrollPane();
@ -65,7 +67,7 @@ public class ChatWindow extends JFrame {
private final PrimaryTextArea searchField = new PrimaryTextArea(space); private final PrimaryTextArea searchField = new PrimaryTextArea(space);
private final PrimaryScrollPane possibleContacts = new PrimaryScrollPane(); private final PrimaryScrollPane possibleContacts = new PrimaryScrollPane();
private final ContactsSearchRenderer contactRenderer = new ContactsSearchRenderer(); private final ContactsSearchRenderer contactRenderer = new ContactsSearchRenderer();
private final ComponentListModel<User> contactsSearchModel = new ComponentListModel<>(); private final ComponentListModel<User> contactsModel = new ComponentListModel<>();
private final ComponentList<User> contactList = new ComponentList<>(contactRenderer); private final ComponentList<User> contactList = new ComponentList<>(contactRenderer);
private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName());
@ -252,6 +254,43 @@ public class ChatWindow extends JFrame {
searchPane.add(searchField, gbc_searchField); searchPane.add(searchField, gbc_searchField);
// Sends event to server, if input has changed
searchField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
if (client.isOnline()) {
try {
if(!searchField.getText().isEmpty()) {
client.sendEvent(new ContactsRequest(searchField.getText()));
} else {
contactsModel.clear();
revalidate();
repaint();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
@Override
public void insertUpdate(DocumentEvent e) {
if (client.isOnline()) {
try {
if(!searchField.getText().isEmpty()) {
client.sendEvent(new ContactsRequest(searchField.getText()));
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
@Override
public void changedUpdate(DocumentEvent e) {}
});
GridBagConstraints gbc_cancelButton = new GridBagConstraints(); GridBagConstraints gbc_cancelButton = new GridBagConstraints();
gbc_cancelButton.fill = GridBagConstraints.BOTH; gbc_cancelButton.fill = GridBagConstraints.BOTH;
gbc_cancelButton.gridx = 1; gbc_cancelButton.gridx = 1;
@ -262,7 +301,7 @@ public class ChatWindow extends JFrame {
searchPane.add(cancelButton, gbc_cancelButton); searchPane.add(cancelButton, gbc_cancelButton);
contactList.setModel(contactsSearchModel); contactList.setModel(contactsModel);
possibleContacts.setBorder(new EmptyBorder(space, space, space, space)); possibleContacts.setBorder(new EmptyBorder(space, space, space, space));
possibleContacts.setViewportView(contactList); possibleContacts.setViewportView(contactList);
@ -276,7 +315,6 @@ public class ChatWindow extends JFrame {
searchPane.add(possibleContacts, gbc_possibleContacts); searchPane.add(possibleContacts, gbc_possibleContacts);
// Contacts Header // Contacts Header
GridBagConstraints gbc_contactsHeader = new GridBagConstraints(); GridBagConstraints gbc_contactsHeader = new GridBagConstraints();
@ -359,6 +397,36 @@ public class ChatWindow extends JFrame {
repaint(); repaint();
}); });
EventBus.getInstance().register(SearchResultEvent.class, (evt) -> {
contactsModel.clear();
final java.util.List<User> contacts = ((SearchResultEvent) evt).get();
logger.info("Received contact search result " + contacts);
contacts.forEach(contactsModel::add);
revalidate();
repaint();
});
EventBus.getInstance().register(AddContactEvent.class, (evt) -> {
User contact = ((AddContactEvent) evt).get();
Operation operation = ((AddContactEvent) evt).getOperation();
try {
client.sendEvent(new ContactOperation(contact, operation));
} catch (IOException e) {
e.printStackTrace();
}
// TODO: Not finished LocalDB update of contact list
// Add this user to the chats
// userListModel.addElement(contact);
// Check if user exists in local DB
// if (localDb.getChats().stream().filter(c -> c.getRecipient().getId() ==
// contact.getId()).count() == 0)
// localDb.getChats().add(new Chat(contact));
revalidate();
repaint();
});
revalidate(); revalidate();
repaint(); repaint();
} }
@ -461,7 +529,6 @@ public class ChatWindow extends JFrame {
*/ */
private void loadUsersAndChats() { private void loadUsersAndChats() {
new Thread(() -> { new Thread(() -> {
DefaultListModel<User> userListModel = new DefaultListModel<>();
localDb.getUsers().values().forEach(user -> { localDb.getUsers().values().forEach(user -> {
userListModel.addElement(user); userListModel.addElement(user);
@ -511,7 +578,9 @@ public class ChatWindow extends JFrame {
* @param client the {@link Client} used to send and receive messages * @param client the {@link Client} used to send and receive messages
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public void setClient(Client client) { this.client = client; } public void setClient(Client client) {
this.client = client;
}
/** /**
* Sets the {@link LocalDb} used by this {@link ChatWindow}. After * Sets the {@link LocalDb} used by this {@link ChatWindow}. After

View File

@ -7,9 +7,12 @@ import java.awt.Font;
import javax.swing.*; import javax.swing.*;
import envoy.client.Settings; import envoy.client.Settings;
import envoy.client.event.AddContactEvent;
import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentList;
import envoy.client.ui.list.ComponentListCellRenderer; import envoy.client.ui.list.ComponentListCellRenderer;
import envoy.data.User; import envoy.data.User;
import envoy.event.ContactOperation.Operation;
import envoy.event.EventBus;
/** /**
* Defines how a contact is displayed.<br> * Defines how a contact is displayed.<br>
@ -26,7 +29,6 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer<User> {
private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); private PrimaryScrollPane scrollPane = new PrimaryScrollPane();
@Override @Override
public JComponent getListCellComponent(ComponentList<? extends User> list, User value, boolean isSelected) { public JComponent getListCellComponent(ComponentList<? extends User> list, User value, boolean isSelected) {
final JPanel panel = new JPanel(); final JPanel panel = new JPanel();
@ -34,9 +36,7 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer<User> {
if (isSelected) { if (isSelected) {
panel.setBackground(Color.DARK_GRAY); panel.setBackground(Color.DARK_GRAY);
panel.setForeground(Color.RED); panel.setForeground(Color.RED);
// TODO: Selection
// setBackground(list.getSelectionBackground());
// setForeground(list.getSelectionForeground());
} else { } else {
panel.setBackground(list.getBackground()); panel.setBackground(list.getBackground());
panel.setForeground(list.getForeground()); panel.setForeground(list.getForeground());
@ -64,6 +64,10 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer<User> {
add.setBackground(list.getBackground()); add.setBackground(list.getBackground());
add.setForeground(list.getForeground()); add.setForeground(list.getForeground());
add.addActionListener((evt) -> {
EventBus.getInstance().dispatch(new AddContactEvent(value, Operation.ADD));
});
panel.add(add); panel.add(add);
// Define some space to the messages below // Define some space to the messages below