Implemented ContactRenderer and built a properScrollPane with list, etc.
This commit is contained in:
		| @@ -17,6 +17,7 @@ import envoy.client.event.MessageCreationEvent; | |||||||
| import envoy.client.event.ThemeChangeEvent; | 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.settings.SettingsScreen; | import envoy.client.ui.settings.SettingsScreen; | ||||||
| import envoy.client.util.EnvoyLog; | import envoy.client.util.EnvoyLog; | ||||||
| import envoy.data.Message; | import envoy.data.Message; | ||||||
| @@ -54,17 +55,18 @@ public class ChatWindow extends JFrame { | |||||||
| 	private PrimaryButton			settingsButton			= new PrimaryButton("Settings"); | 	private PrimaryButton			settingsButton			= new PrimaryButton("Settings"); | ||||||
|  |  | ||||||
| 	// Contacts Header | 	// Contacts Header | ||||||
| 	private JPanel		contactsHeader	= new JPanel(); | 	private JPanel			contactsHeader	= new JPanel(); | ||||||
| 	private JTextPane	contactsDisplay	= new JTextPane(); | 	private JTextPane		contactsDisplay	= new JTextPane(); | ||||||
| 	private PrimaryButton	addContact		= new PrimaryButton("+"); | 	private PrimaryButton	addContact		= new PrimaryButton("+"); | ||||||
|  |  | ||||||
| 	// Search Contacts | 	// Search Contacts | ||||||
| 	private JPanel searchPane = new JPanel(); | 	private final JPanel					searchPane			= new JPanel(); | ||||||
| 	private PrimaryButton	cancelButton	= new PrimaryButton("x"); | 	private final PrimaryButton				cancelButton		= new PrimaryButton("x"); | ||||||
| 	private PrimaryTextArea	searchField		= new PrimaryTextArea(space); | 	private final PrimaryTextArea			searchField			= new PrimaryTextArea(space); | ||||||
| 	private PrimaryScrollPane	possibleContacts	= new PrimaryScrollPane(); | 	private final PrimaryScrollPane			possibleContacts	= new PrimaryScrollPane(); | ||||||
| 	private ComponentList<User>	contactList;									// TODO Implement data rendering model as already done with the | 	private final ContactsSearchRenderer	contactRenderer		= new ContactsSearchRenderer(); | ||||||
| 																				// messages | 	private final ComponentListModel<User>	contactsSearchModel	= new ComponentListModel<>(); | ||||||
|  | 	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()); | ||||||
|  |  | ||||||
| @@ -215,12 +217,12 @@ public class ChatWindow extends JFrame { | |||||||
| 		userList.setBorder(new EmptyBorder(space, space, space, space)); | 		userList.setBorder(new EmptyBorder(space, space, space, space)); | ||||||
|  |  | ||||||
| 		GridBagConstraints gbc_userList = new GridBagConstraints(); | 		GridBagConstraints gbc_userList = new GridBagConstraints(); | ||||||
| 		gbc_userList.fill	= GridBagConstraints.VERTICAL; | 		gbc_userList.fill		= GridBagConstraints.VERTICAL; | ||||||
| 		gbc_userList.gridx	= 0; | 		gbc_userList.gridx		= 0; | ||||||
| 		gbc_userList.gridy	= 2; | 		gbc_userList.gridy		= 2; | ||||||
| 		gbc_userList.anchor	= GridBagConstraints.PAGE_START; | 		gbc_userList.gridheight	= 2; | ||||||
| 		gbc_userList.insets	= insets; | 		gbc_userList.anchor		= GridBagConstraints.PAGE_START; | ||||||
|  | 		gbc_userList.insets		= insets; | ||||||
|  |  | ||||||
| 		contentPane.add(userList, gbc_userList); | 		contentPane.add(userList, gbc_userList); | ||||||
| 		contentPane.revalidate(); | 		contentPane.revalidate(); | ||||||
| @@ -247,9 +249,9 @@ public class ChatWindow extends JFrame { | |||||||
| 		gbc_searchField.gridx	= 0; | 		gbc_searchField.gridx	= 0; | ||||||
| 		gbc_searchField.gridy	= 0; | 		gbc_searchField.gridy	= 0; | ||||||
| 		gbc_searchField.insets	= new Insets(7, 4, 4, 4); | 		gbc_searchField.insets	= new Insets(7, 4, 4, 4); | ||||||
| 		 |  | ||||||
| 		searchPane.add(searchField, gbc_searchField); | 		searchPane.add(searchField, gbc_searchField); | ||||||
| 		 |  | ||||||
| 		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; | ||||||
| @@ -260,6 +262,8 @@ public class ChatWindow extends JFrame { | |||||||
|  |  | ||||||
| 		searchPane.add(cancelButton, gbc_cancelButton); | 		searchPane.add(cancelButton, gbc_cancelButton); | ||||||
|  |  | ||||||
|  | 		contactList.setModel(contactsSearchModel); | ||||||
|  | 		possibleContacts.setBorder(new EmptyBorder(space, space, space, space)); | ||||||
| 		possibleContacts.setViewportView(contactList); | 		possibleContacts.setViewportView(contactList); | ||||||
|  |  | ||||||
| 		GridBagConstraints gbc_possibleContacts = new GridBagConstraints(); | 		GridBagConstraints gbc_possibleContacts = new GridBagConstraints(); | ||||||
| @@ -269,9 +273,10 @@ public class ChatWindow extends JFrame { | |||||||
| 		gbc_possibleContacts.gridy		= 1; | 		gbc_possibleContacts.gridy		= 1; | ||||||
|  |  | ||||||
| 		gbc_possibleContacts.insets = insets; | 		gbc_possibleContacts.insets = insets; | ||||||
| 		 |  | ||||||
| 		searchPane.add(possibleContacts, gbc_possibleContacts); | 		searchPane.add(possibleContacts, gbc_possibleContacts); | ||||||
|  |  | ||||||
|  |  | ||||||
| 		// Contacts Header | 		// Contacts Header | ||||||
|  |  | ||||||
| 		GridBagConstraints gbc_contactsHeader = new GridBagConstraints(); | 		GridBagConstraints gbc_contactsHeader = new GridBagConstraints(); | ||||||
| @@ -305,7 +310,7 @@ public class ChatWindow extends JFrame { | |||||||
| 		gbc_addContact.gridx	= 1; | 		gbc_addContact.gridx	= 1; | ||||||
| 		gbc_addContact.gridy	= 0; | 		gbc_addContact.gridy	= 0; | ||||||
| 		gbc_addContact.insets	= insets; | 		gbc_addContact.insets	= insets; | ||||||
| 		 |  | ||||||
| 		addContact.addActionListener((evt) -> { drawContactSearch(gbc_searchPane); }); | 		addContact.addActionListener((evt) -> { drawContactSearch(gbc_searchPane); }); | ||||||
|  |  | ||||||
| 		contactsHeader.add(addContact, gbc_addContact); | 		contactsHeader.add(addContact, gbc_addContact); | ||||||
| @@ -320,7 +325,7 @@ public class ChatWindow extends JFrame { | |||||||
|  |  | ||||||
| 		// Listen to received messages | 		// Listen to received messages | ||||||
| 		EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { | 		EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { | ||||||
| 			Message message = ((MessageCreationEvent) evt).get(); | 			Message	message	= ((MessageCreationEvent) evt).get(); | ||||||
| 			Chat	chat	= localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get(); | 			Chat	chat	= localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get(); | ||||||
| 			chat.appendMessage(message); | 			chat.appendMessage(message); | ||||||
|  |  | ||||||
| @@ -355,6 +360,7 @@ public class ChatWindow extends JFrame { | |||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		revalidate(); | 		revalidate(); | ||||||
|  | 		repaint(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| @@ -406,9 +412,8 @@ public class ChatWindow extends JFrame { | |||||||
| 		searchField.setForeground(theme.getUserNameColor()); | 		searchField.setForeground(theme.getUserNameColor()); | ||||||
| 		cancelButton.setBackground(theme.getInteractableBackgroundColor()); | 		cancelButton.setBackground(theme.getInteractableBackgroundColor()); | ||||||
| 		cancelButton.setForeground(theme.getInteractableForegroundColor()); | 		cancelButton.setForeground(theme.getInteractableForegroundColor()); | ||||||
| 		// TODO: Uncomment if renderer is implemented | 		contactList.setForeground(theme.getMessageColorChat()); | ||||||
| 		// contactList.setForeground(theme.getMessageColorChat()); | 		contactList.setBackground(theme.getCellColor()); | ||||||
| 		// contactList.setBackground(theme.getCellColor()); |  | ||||||
| 		possibleContacts.applyTheme(theme); | 		possibleContacts.applyTheme(theme); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -465,6 +470,9 @@ public class ChatWindow extends JFrame { | |||||||
| 					localDb.getChats().add(new Chat(user)); | 					localDb.getChats().add(new Chat(user)); | ||||||
| 			}); | 			}); | ||||||
| 			SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); | 			SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); | ||||||
|  |  | ||||||
|  | 			revalidate(); | ||||||
|  | 			repaint(); | ||||||
| 		}).start(); | 		}).start(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -477,15 +485,14 @@ public class ChatWindow extends JFrame { | |||||||
| 			logger.log(Level.WARNING, "Couldn't notify server about message status change", e); | 			logger.log(Level.WARNING, "Couldn't notify server about message status change", e); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	 |  | ||||||
| 	 |  | ||||||
| 	private void drawChatBox(GridBagConstraints gbc_scrollPane) { | 	private void drawChatBox(GridBagConstraints gbc_scrollPane) { | ||||||
| 		contentPane.remove(searchPane); | 		contentPane.remove(searchPane); | ||||||
| 		contentPane.add(scrollPane, gbc_scrollPane); | 		contentPane.add(scrollPane, gbc_scrollPane); | ||||||
| 		contentPane.revalidate(); | 		contentPane.revalidate(); | ||||||
| 		contentPane.repaint(); | 		contentPane.repaint(); | ||||||
| 	} | 	} | ||||||
| 	 |  | ||||||
| 	private void drawContactSearch(GridBagConstraints gbc_searchPane) { | 	private void drawContactSearch(GridBagConstraints gbc_searchPane) { | ||||||
| 		currentChat = null; | 		currentChat = null; | ||||||
| 		userList.removeSelectionInterval(0, userList.getModel().getSize() - 1); | 		userList.removeSelectionInterval(0, userList.getModel().getSize() - 1); | ||||||
| @@ -494,6 +501,7 @@ public class ChatWindow extends JFrame { | |||||||
| 		contentPane.remove(scrollPane); | 		contentPane.remove(scrollPane); | ||||||
| 		contentPane.add(searchPane, gbc_searchPane); | 		contentPane.add(searchPane, gbc_searchPane); | ||||||
| 		contentPane.revalidate(); | 		contentPane.revalidate(); | ||||||
|  | 		contactRenderer.setScrollPane(possibleContacts); | ||||||
| 		contentPane.repaint(); | 		contentPane.repaint(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										83
									
								
								src/main/java/envoy/client/ui/ContactsSearchRenderer.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								src/main/java/envoy/client/ui/ContactsSearchRenderer.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,83 @@ | |||||||
|  | package envoy.client.ui; | ||||||
|  |  | ||||||
|  | import java.awt.Component; | ||||||
|  | import java.awt.Dimension; | ||||||
|  | import java.awt.Font; | ||||||
|  |  | ||||||
|  | import javax.swing.*; | ||||||
|  |  | ||||||
|  | import envoy.client.Settings; | ||||||
|  | import envoy.client.ui.list.ComponentList; | ||||||
|  | import envoy.client.ui.list.ComponentListCellRenderer; | ||||||
|  | import envoy.data.User; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Defines how a contact is displayed.<br> | ||||||
|  |  * <br> | ||||||
|  |  * Project: <strong>envoy-client</strong><br> | ||||||
|  |  * File: <strong>ContactsSearchRenderer.java</strong><br> | ||||||
|  |  * Created: <strong>08.02.2020</strong><br> | ||||||
|  |  * | ||||||
|  |  * @author Maximilian Käfer | ||||||
|  |  * @author Kai S. K. Engelbart | ||||||
|  |  * @since Envoy v0.3-alpha | ||||||
|  |  */ | ||||||
|  | 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(); | ||||||
|  | 		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); | ||||||
|  | 		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()); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		// TODO: Handle message attachments | ||||||
|  |  | ||||||
|  | 		final String text = value.getName(); | ||||||
|  |  | ||||||
|  | 		// Getting the UserColor in the Chat of the current theme | ||||||
|  | 		String textColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat().toHex(); | ||||||
|  |  | ||||||
|  | 		JLabel display = new JLabel(String.format("<html><p style=\"color:%s\">%s</html>", textColor, text)); | ||||||
|  | 		display.setAlignmentX(Component.LEFT_ALIGNMENT); | ||||||
|  | 		display.setAlignmentY(Component.CENTER_ALIGNMENT); | ||||||
|  | 		display.setFont(new Font("Arial", Font.PLAIN, 16)); | ||||||
|  | 		panel.add(display); | ||||||
|  |  | ||||||
|  | 		PrimaryButton add = new PrimaryButton("+"); | ||||||
|  | 		add.setFont(new Font("Arial", Font.PLAIN, 19)); | ||||||
|  | 		add.setPreferredSize(new Dimension(45, 45)); | ||||||
|  | 		add.setMinimumSize(new Dimension(45, 45)); | ||||||
|  | 		add.setMaximumSize(new Dimension(45, 45)); | ||||||
|  |  | ||||||
|  | 		add.setBackground(list.getBackground()); | ||||||
|  | 		add.setForeground(list.getForeground()); | ||||||
|  |  | ||||||
|  | 		panel.add(add); | ||||||
|  |  | ||||||
|  | 		// Define some space to the messages below | ||||||
|  | 		panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, 0, 15, 0), BorderFactory.createEtchedBorder())); | ||||||
|  |  | ||||||
|  | 		// Define a maximum height of 50px | ||||||
|  | 		Dimension size = new Dimension(435, 50); | ||||||
|  | 		panel.setMaximumSize(size); | ||||||
|  | 		panel.setMinimumSize(size); | ||||||
|  | 		panel.setPreferredSize(size); | ||||||
|  |  | ||||||
|  | 		return panel; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// TODO: Use this method properly | ||||||
|  | 	public void setScrollPane(PrimaryScrollPane scrollPane) { this.scrollPane = scrollPane; } | ||||||
|  | } | ||||||
| @@ -14,7 +14,7 @@ import envoy.data.Message; | |||||||
|  * Defines how a message is displayed.<br> |  * Defines how a message is displayed.<br> | ||||||
|  * <br> |  * <br> | ||||||
|  * Project: <strong>envoy-client</strong><br> |  * Project: <strong>envoy-client</strong><br> | ||||||
|  * File: <strong>UserListRenderer.java</strong><br> |  * File: <strong>MessageListRenderer.java</strong><br> | ||||||
|  * Created: <strong>19 Oct 2019</strong><br> |  * Created: <strong>19 Oct 2019</strong><br> | ||||||
|  * |  * | ||||||
|  * @author Kai S. K. Engelbart |  * @author Kai S. K. Engelbart | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 DieGurke
					DieGurke