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

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

mercurial