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:
		
							
								
								
									
										42
									
								
								src/main/java/envoy/client/event/AddContactEvent.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/main/java/envoy/client/event/AddContactEvent.java
									
									
									
									
									
										Normal 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ä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; } | ||||
|  | ||||
| } | ||||
							
								
								
									
										32
									
								
								src/main/java/envoy/client/event/SearchResultEvent.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/main/java/envoy/client/event/SearchResultEvent.java
									
									
									
									
									
										Normal 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ä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; } | ||||
| } | ||||
| @@ -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(); | ||||
|  | ||||
|   | ||||
| @@ -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: <strong>envoy-client</strong><br> | ||||
| @@ -47,6 +48,7 @@ public class ChatWindow extends JFrame { | ||||
| 	private JPanel					contentPane				= new JPanel(); | ||||
| 	private PrimaryTextArea			messageEnterTextArea	= new PrimaryTextArea(space); | ||||
| 	private JList<User>				userList				= new JList<>(); | ||||
| 	private DefaultListModel<User>	userListModel			= new DefaultListModel<>(); | ||||
| 	private Chat					currentChat; | ||||
| 	private ComponentList<Message>	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<User>	contactsSearchModel	= new ComponentListModel<>(); | ||||
| 	private final ComponentListModel<User>	contactsModel		= new ComponentListModel<>(); | ||||
| 	private final ComponentList<User>		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<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(); | ||||
| 		repaint(); | ||||
| 	} | ||||
| @@ -461,7 +529,6 @@ public class ChatWindow extends JFrame { | ||||
| 	 */ | ||||
| 	private void loadUsersAndChats() { | ||||
| 		new Thread(() -> { | ||||
| 			DefaultListModel<User> 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 | ||||
|   | ||||
| @@ -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.<br> | ||||
| @@ -26,7 +29,6 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer<User> { | ||||
|  | ||||
| 	private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); | ||||
|  | ||||
|  | ||||
| 	@Override | ||||
| 	public JComponent getListCellComponent(ComponentList<? extends User> list, User value, boolean isSelected) { | ||||
| 		final JPanel panel = new JPanel(); | ||||
| @@ -34,9 +36,7 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer<User> { | ||||
| 		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<User> { | ||||
| 		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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 DieGurke
					DieGurke