| |
1 package de.unixwork.im; |
| |
2 |
| |
3 import javax.swing.*; |
| |
4 import java.awt.*; |
| |
5 import java.awt.event.MouseAdapter; |
| |
6 import java.awt.event.MouseEvent; |
| |
7 import java.util.List; |
| |
8 import org.jivesoftware.smack.roster.RosterEntry; |
| |
9 |
| |
10 // Main class for the XMPP contact list window |
| |
11 public class ContactListFrame extends JFrame { |
| |
12 |
| |
13 private DefaultListModel<RosterEntry> contactListModel; |
| |
14 private JList<RosterEntry> contactList; |
| |
15 private ContactClickListener contactClickListener; |
| |
16 |
| |
17 public ContactListFrame() { |
| |
18 setTitle("Contact List"); |
| |
19 setSize(200, 300); |
| |
20 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| |
21 setLayout(new BorderLayout()); |
| |
22 |
| |
23 // Create the list model and list view |
| |
24 contactListModel = new DefaultListModel<>(); |
| |
25 contactList = new JList<>(contactListModel); |
| |
26 contactList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| |
27 contactList.setCellRenderer(new DefaultListCellRenderer() { |
| |
28 @Override |
| |
29 public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
| |
30 Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
| |
31 if (value instanceof RosterEntry) { |
| |
32 setText(((RosterEntry) value).toString()); |
| |
33 } |
| |
34 return c; |
| |
35 } |
| |
36 }); |
| |
37 |
| |
38 // Add mouse listener for click events |
| |
39 contactList.addMouseListener(new MouseAdapter() { |
| |
40 @Override |
| |
41 public void mouseClicked(MouseEvent e) { |
| |
42 if (e.getClickCount() == 2) { // Double-click detected |
| |
43 int index = contactList.locationToIndex(e.getPoint()); |
| |
44 if (index >= 0 && contactClickListener != null) { |
| |
45 contactClickListener.onContactClicked(contactListModel.getElementAt(index)); |
| |
46 } |
| |
47 } |
| |
48 } |
| |
49 }); |
| |
50 |
| |
51 // Add the list to a scroll pane |
| |
52 JScrollPane scrollPane = new JScrollPane(contactList); |
| |
53 add(scrollPane, BorderLayout.CENTER); |
| |
54 } |
| |
55 |
| |
56 // Method to set the contact list data |
| |
57 public void setContacts(List<RosterEntry> contacts) { |
| |
58 contactListModel.clear(); |
| |
59 for (RosterEntry contact : contacts) { |
| |
60 contactListModel.addElement(contact); |
| |
61 } |
| |
62 } |
| |
63 |
| |
64 // Interface for click callback |
| |
65 public interface ContactClickListener { |
| |
66 void onContactClicked(RosterEntry contact); |
| |
67 } |
| |
68 |
| |
69 // Method to set the click listener |
| |
70 public void setContactClickListener(ContactClickListener listener) { |
| |
71 this.contactClickListener = listener; |
| |
72 } |
| |
73 } |