diff --git a/pom.xml b/pom.xml index 0b5487d..c7abb5f 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + f~contacts-SNAPSHOT diff --git a/src/main/java/envoy/client/event/AddContactEvent.java b/src/main/java/envoy/client/event/AddContactEvent.java new file mode 100644 index 0000000..8f356fa --- /dev/null +++ b/src/main/java/envoy/client/event/AddContactEvent.java @@ -0,0 +1,42 @@ +package envoy.client.event; + +import envoy.data.User; +import envoy.event.ContactOperation.Operation; +import envoy.event.Event; + +/** + * Project: envoy-client
+ * File: AddContactEvent.java
+ * Created: 09.02.2020
+ * + * @author Maximilian Käfer + * @since Envoy v0.3-alpha + */ +public class AddContactEvent implements Event { + + 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; } + +} diff --git a/src/main/java/envoy/client/event/SearchResultEvent.java b/src/main/java/envoy/client/event/SearchResultEvent.java new file mode 100644 index 0000000..dc834d3 --- /dev/null +++ b/src/main/java/envoy/client/event/SearchResultEvent.java @@ -0,0 +1,32 @@ +package envoy.client.event; + +import java.util.List; + +import envoy.data.User; +import envoy.event.Event; + +/** + * Project: envoy-client
+ * File: SearchResultEvent.java
+ * Created: 08.02.2020
+ * + * @author Maximilian Käfer + * @since Envoy v0.3-alpha + */ +public class SearchResultEvent implements Event> { + + private final List 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 resultList) { this.resultList = resultList; } + + @Override + public List get() { return resultList; } +} diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 91e9040..c99ef13 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -11,11 +11,10 @@ import javax.naming.TimeLimitExceededException; import envoy.client.Config; import envoy.client.data.LocalDb; +import envoy.client.event.SearchResultEvent; import envoy.client.util.EnvoyLog; import envoy.data.*; -import envoy.event.Event; -import envoy.event.IdGeneratorRequest; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.*; import envoy.util.SerializationUtils; /** @@ -111,6 +110,9 @@ public class Client implements Closeable { // Process message ID generation 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 if (!localDb.hasIdGenerator() || !localDb.getIdGenerator().hasNext()) requestIdGenerator(); diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 2366556..206f670 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -9,12 +9,13 @@ import java.util.logging.Logger; import javax.swing.*; import javax.swing.border.EmptyBorder; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; import envoy.client.Settings; import envoy.client.data.Chat; import envoy.client.data.LocalDb; -import envoy.client.event.MessageCreationEvent; -import envoy.client.event.ThemeChangeEvent; +import envoy.client.event.*; import envoy.client.net.Client; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListModel; @@ -24,8 +25,8 @@ import envoy.data.Message; import envoy.data.Message.MessageStatus; import envoy.data.MessageBuilder; import envoy.data.User; -import envoy.event.EventBus; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.*; +import envoy.event.ContactOperation.Operation; /** * Project: envoy-client
@@ -47,6 +48,7 @@ public class ChatWindow extends JFrame { private JPanel contentPane = new JPanel(); private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space); private JList userList = new JList<>(); + private DefaultListModel userListModel = new DefaultListModel<>(); private Chat currentChat; private ComponentList messageList = new ComponentList<>(new MessageListRenderer()); private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); @@ -65,7 +67,7 @@ public class ChatWindow extends JFrame { private final PrimaryTextArea searchField = new PrimaryTextArea(space); private final PrimaryScrollPane possibleContacts = new PrimaryScrollPane(); private final ContactsSearchRenderer contactRenderer = new ContactsSearchRenderer(); - private final ComponentListModel contactsSearchModel = new ComponentListModel<>(); + private final ComponentListModel contactsModel = new ComponentListModel<>(); private final ComponentList contactList = new ComponentList<>(contactRenderer); private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName()); @@ -252,6 +254,43 @@ public class ChatWindow extends JFrame { 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(); gbc_cancelButton.fill = GridBagConstraints.BOTH; gbc_cancelButton.gridx = 1; @@ -262,7 +301,7 @@ public class ChatWindow extends JFrame { searchPane.add(cancelButton, gbc_cancelButton); - contactList.setModel(contactsSearchModel); + contactList.setModel(contactsModel); possibleContacts.setBorder(new EmptyBorder(space, space, space, space)); possibleContacts.setViewportView(contactList); @@ -276,7 +315,6 @@ public class ChatWindow extends JFrame { searchPane.add(possibleContacts, gbc_possibleContacts); - // Contacts Header GridBagConstraints gbc_contactsHeader = new GridBagConstraints(); @@ -359,6 +397,36 @@ public class ChatWindow extends JFrame { repaint(); }); + EventBus.getInstance().register(SearchResultEvent.class, (evt) -> { + contactsModel.clear(); + final java.util.List 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(); repaint(); } @@ -461,7 +529,6 @@ public class ChatWindow extends JFrame { */ private void loadUsersAndChats() { new Thread(() -> { - DefaultListModel userListModel = new DefaultListModel<>(); localDb.getUsers().values().forEach(user -> { userListModel.addElement(user); @@ -511,7 +578,9 @@ public class ChatWindow extends JFrame { * @param client the {@link Client} used to send and receive messages * @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 diff --git a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java index 8f4e0e0..b435def 100644 --- a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java +++ b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java @@ -7,9 +7,12 @@ import java.awt.Font; import javax.swing.*; import envoy.client.Settings; +import envoy.client.event.AddContactEvent; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListCellRenderer; import envoy.data.User; +import envoy.event.ContactOperation.Operation; +import envoy.event.EventBus; /** * Defines how a contact is displayed.
@@ -26,7 +29,6 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); - @Override public JComponent getListCellComponent(ComponentList list, User value, boolean isSelected) { final JPanel panel = new JPanel(); @@ -34,9 +36,7 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { if (isSelected) { panel.setBackground(Color.DARK_GRAY); panel.setForeground(Color.RED); - // TODO: Selection - // setBackground(list.getSelectionBackground()); - // setForeground(list.getSelectionForeground()); + } else { panel.setBackground(list.getBackground()); panel.setForeground(list.getForeground()); @@ -64,6 +64,10 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { add.setBackground(list.getBackground()); add.setForeground(list.getForeground()); + add.addActionListener((evt) -> { + EventBus.getInstance().dispatch(new AddContactEvent(value, Operation.ADD)); + }); + panel.add(add); // Define some space to the messages below