src/main/java/de/unixwork/im/App.java

Wed, 25 Dec 2024 21:49:48 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 25 Dec 2024 21:49:48 +0100
changeset 0
f3095cda599e
child 2
94c6a715fa44
permissions
-rw-r--r--

add initial code with minimal working contact list and conversations

0
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 package de.unixwork.im;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 import javax.swing.*;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 import java.util.HashMap;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 import java.util.List;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 import java.util.Map;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 import org.jivesoftware.smack.roster.RosterEntry;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 import org.jxmpp.jid.Jid;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 public class App {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 private static App instance;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 private final ContactListFrame contactListFrame;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 private final Map<String, ConversationFrame> conversations;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 private final Xmpp xmpp;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 public App(Xmpp xmpp) throws Exception {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 if(instance != null) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 throw new Exception("App already initilized");
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 App.instance = this;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 conversations = new HashMap<>();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 this.xmpp = xmpp;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 // Create the contact list window
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 contactListFrame = new ContactListFrame();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 contactListFrame.setContactClickListener(contact -> {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 openConversation(contact.getJid().asUnescapedString());
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 });
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 contactListFrame.setVisible(true);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 public static App getInstance() {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 return instance;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 public Xmpp getXmpp() {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 return xmpp;
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 // Method to open a conversation window
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 public void openConversation(String xid) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 SwingUtilities.invokeLater(() -> {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 if (!conversations.containsKey(xid)) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 ConversationFrame conversationFrame = new ConversationFrame(xid);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 conversations.put(xid, conversationFrame);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 conversationFrame.setVisible(true);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 } else {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 conversations.get(xid).toFront();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 });
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 public void dispatchMessage(Jid from, String msg, boolean secure) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 SwingUtilities.invokeLater(() -> {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 // add message to the correct conversation
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 // if no conversation exists yet, create a new window
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 String xid = from.asBareJid().toString();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 //String resource = from.getResourceOrNull();
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 ConversationFrame conversation = conversations.get(xid);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 if(conversation == null) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 conversation = new ConversationFrame(xid);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 conversations.put(xid, conversation);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 conversation.addToLog(msg, true, false);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 conversation.setVisible(true);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 });
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 public void setContacts(List<RosterEntry> contacts) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 SwingUtilities.invokeLater(() -> {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 contactListFrame.setContacts(contacts);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 });
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 // Method to perform actions in the GUI thread from other threads
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 public void runOnUiThread(Runnable action) {
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 SwingUtilities.invokeLater(action);
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 }
f3095cda599e add initial code with minimal working contact list and conversations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 }

mercurial