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", WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/message/send",
serverProps.getProperty("server"), serverProps.getProperty("server"),
serverProps.getProperty("port"))); serverProps.getProperty("port")));
Response response = target.request().post(Entity.entity(messages, "application/xml")); Response response = target.request().post(Entity.entity(messages, "application/xml"));
System.out.println("Response code: " + response.getStatus()); System.out.println("Response code: " + response.getStatus());
response.close(); response.close();
@ -100,7 +101,7 @@ public class EnvoyClient {
* @param textContent The content (text) of the message * @param textContent The content (text) of the message
* @return Prepared {@link Message} object * @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(); Message.MetaData metaData = objectFactory.createMessageMetaData();
metaData.setSender(senderID); metaData.setSender(senderID);
metaData.setRecipient(recipientID); metaData.setRecipient(recipientID);

View File

@ -7,8 +7,10 @@ import java.awt.Font;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.Insets; import java.awt.Insets;
import java.awt.List;
import java.io.IOException; import java.io.IOException;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
@ -17,8 +19,11 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
@ -37,10 +42,13 @@ import envoy.schema.Users;
*/ */
public class ChatWindow extends JFrame { public class ChatWindow extends JFrame {
private static final long serialVersionUID = 6865098428255463649L; private static final long serialVersionUID = 6865098428255463649L;
public long recipientID = 0;
private JPanel contentPane = new JPanel(); private JPanel contentPane = new JPanel();
private static EnvoyClient envoyClient = new EnvoyClient(); private static EnvoyClient envoyClient = new EnvoyClient();
private static UserJListModel AuserJListModel = new UserJListModel();
public DefaultListModel<String> listModel = new DefaultListModel<>(); public DefaultListModel<String> listModel = new DefaultListModel<>();
@ -116,6 +124,7 @@ public class ChatWindow extends JFrame {
postButton.setBackground(new Color(102, 51, 153)); postButton.setBackground(new Color(102, 51, 153));
postButton.setBorderPainted(false); postButton.setBorderPainted(false);
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints(); GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH; gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH;
@ -127,8 +136,11 @@ public class ChatWindow extends JFrame {
contentPane.add(postButton, gbc_moveSelectionPostButton); contentPane.add(postButton, gbc_moveSelectionPostButton);
postButton.addActionListener((evt) -> { postButton.addActionListener((evt) -> {
if (!messageEnterTextfield.getText().isEmpty()) try { if(recipientID == 0)
final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText()); 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); envoyClient.sendMessage(message);
appendMessageToChat(message); appendMessageToChat(message);
messageEnterTextfield.setText(""); messageEnterTextfield.setText("");
@ -140,29 +152,62 @@ public class ChatWindow extends JFrame {
e.printStackTrace(); e.printStackTrace();
} }
}); });
new Thread(() -> loadUserJList()).start(); new Thread(() -> loadUserJList()).start();
} }
public void loadUserJList() { public void loadUserJList() {
Users users = envoyClient.getUsersListXml(); //Users users = envoyClient.getUsersListXml();
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
// User List // User List
JList<User> userJList = new JList<>(); JList<User> userJList = new JList<>();
DefaultListModel<User> userJListModel = new DefaultListModel<>();
users.getUser().forEach(user -> userJListModel.addElement(user)); //System.out.println(userJListModel.getElementAt(0).getName());
userJList.setModel(userJListModel); 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(); GridBagConstraints gbc_userList = new GridBagConstraints();
gbc_userList.fill = GridBagConstraints.BOTH; //gbc_userList.fill = GridBagConstraints.BOTH;
gbc_userList.gridx = 0; gbc_userList.gridx = 0;
gbc_userList.gridy = 1; gbc_userList.gridy = 1;
gbc_userList.anchor = GridBagConstraints.PAGE_START;
// gbc_userList.insets = new Insets(10, 10, 10, 10); gbc_userList.insets = new Insets(10, 0, 10, 0);
System.out.println(userJListModel.getSize()); //System.out.println(userJListModel.getSize());
System.out.println("test"); //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.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();
}
}