Added user list (abstractListModel), setting the recipientID (in message

object) by selecting user from the list.
This commit is contained in:
Maxi 2019-10-09 21:54:31 +02:00
parent 5c495e5bd8
commit 6390be5e8b
3 changed files with 97 additions and 13 deletions

View File

@ -85,6 +85,7 @@ public class EnvoyClient {
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/message/send",
serverProps.getProperty("server"),
serverProps.getProperty("port")));
Response response = target.request().post(Entity.entity(messages, "application/xml"));
System.out.println("Response code: " + response.getStatus());
response.close();
@ -100,7 +101,7 @@ public class EnvoyClient {
* @param textContent The content (text) of the message
* @return Prepared {@link Message} object
*/
public Message createMessage(String senderID, String recipientID, String textContent) {
public Message createMessage(String senderID, long recipientID, String textContent) {
Message.MetaData metaData = objectFactory.createMessageMetaData();
metaData.setSender(senderID);
metaData.setRecipient(recipientID);
@ -123,7 +124,7 @@ public class EnvoyClient {
wrapper.getMessage().addAll(Arrays.asList(messages));
return wrapper;
}
public Users getUsersListXml() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/user",

View File

@ -7,8 +7,10 @@ import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.List;
import java.io.IOException;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
@ -17,8 +19,11 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.client.ClientProtocolException;
@ -37,10 +42,13 @@ import envoy.schema.Users;
*/
public class ChatWindow extends JFrame {
private static final long serialVersionUID = 6865098428255463649L;
public long recipientID = 0;
private JPanel contentPane = new JPanel();
private static EnvoyClient envoyClient = new EnvoyClient();
private static UserJListModel AuserJListModel = new UserJListModel();
public DefaultListModel<String> listModel = new DefaultListModel<>();
@ -115,6 +123,7 @@ public class ChatWindow extends JFrame {
postButton.setForeground(new Color(255, 255, 255));
postButton.setBackground(new Color(102, 51, 153));
postButton.setBorderPainted(false);
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
@ -127,8 +136,11 @@ public class ChatWindow extends JFrame {
contentPane.add(postButton, gbc_moveSelectionPostButton);
postButton.addActionListener((evt) -> {
if (!messageEnterTextfield.getText().isEmpty()) try {
final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText());
if(recipientID == 0)
System.out.println("Please select recipient");
if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try {
final Message message = envoyClient.createMessage("Kai", recipientID, messageEnterTextfield.getText());
envoyClient.sendMessage(message);
appendMessageToChat(message);
messageEnterTextfield.setText("");
@ -140,29 +152,62 @@ public class ChatWindow extends JFrame {
e.printStackTrace();
}
});
new Thread(() -> loadUserJList()).start();
}
public void loadUserJList() {
Users users = envoyClient.getUsersListXml();
//Users users = envoyClient.getUsersListXml();
SwingUtilities.invokeLater(() -> {
// User List
JList<User> userJList = new JList<>();
DefaultListModel<User> userJListModel = new DefaultListModel<>();
users.getUser().forEach(user -> userJListModel.addElement(user));
userJList.setModel(userJListModel);
//System.out.println(userJListModel.getElementAt(0).getName());
userJList.setModel(AuserJListModel);
userJList.setSelectionForeground(new Color(255, 255, 255));
userJList.setSelectionBackground(new Color(102, 0, 153));
userJList.setForeground(new Color(255, 255, 255));
userJList.setBackground(new Color(51, 51, 51));
userJList.setFont(new Font("Arial", Font.PLAIN, 17));
//userJList.setFixedCellHeight(60);
userJList.setBorder(new EmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc_userList = new GridBagConstraints();
gbc_userList.fill = GridBagConstraints.BOTH;
//gbc_userList.fill = GridBagConstraints.BOTH;
gbc_userList.gridx = 0;
gbc_userList.gridy = 1;
gbc_userList.anchor = GridBagConstraints.PAGE_START;
// gbc_userList.insets = new Insets(10, 10, 10, 10);
System.out.println(userJListModel.getSize());
System.out.println("test");
gbc_userList.insets = new Insets(10, 0, 10, 0);
//System.out.println(userJListModel.getSize());
//System.out.println("test");
userJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionListener listSelectionListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent listSelectionEvent) {;
boolean adjust = listSelectionEvent.getValueIsAdjusting();
if (!adjust) {
JList selectedUserList = (JList) listSelectionEvent.getSource();
int selection = selectedUserList.getSelectedIndex();
//System.out.println(AuserJListModel.getElementID(selection));
recipientID = AuserJListModel.getElementID(selection);
}
}
};
userJList.addListSelectionListener(listSelectionListener);
contentPane.add(userJList, gbc_userList);
contentPane.revalidate();
});
}

View File

@ -0,0 +1,38 @@
package envoy.client.ui;
import java.util.*;
import java.util.LinkedList;
import javax.swing.AbstractListModel;
import envoy.client.EnvoyClient;
import envoy.schema.User;
import envoy.schema.Users;
public class UserJListModel extends AbstractListModel {
public EnvoyClient envoyClient = new EnvoyClient();
Users users = envoyClient.getUsersListXml();
private List<User> userList = new LinkedList<>();
public UserJListModel() {
users.getUser().forEach(user -> userList.add(user));
}
@Override
public int getSize() {
return userList.size();
}
@Override
public Object getElementAt(int index) {
return userList.get(index).getName();
}
public long getElementID(int index) {
return userList.get(index).getID();
}
}