Search in sources :

Example 26 with SwingWorker

use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.

the class OnlineAgents method activateChat.

/**
 * Activate a chat room with the selected user.
 */
private void activateChat(final String userJID, final String nickname) {
    if (!ModelUtil.hasLength(userJID)) {
        return;
    }
    SwingWorker worker = new SwingWorker() {

        final ChatManager chatManager = SparkManager.getChatManager();

        ChatRoom chatRoom;

        public Object construct() {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                Log.error("Error in activate chat.", e);
            }
            ChatContainer chatRooms = chatManager.getChatContainer();
            try {
                chatRoom = chatRooms.getChatRoom(userJID);
            } catch (ChatRoomNotFoundException e) {
                Log.warning("Room not found for jid: " + userJID, e);
            }
            return chatRoom;
        }

        public void finished() {
            if (chatRoom == null) {
                chatRoom = new ChatRoomImpl(userJID, nickname, nickname);
                chatManager.getChatContainer().addChatRoom(chatRoom);
            }
            chatManager.getChatContainer().activateChatRoom(chatRoom);
        }
    };
    worker.start();
}
Also used : ChatContainer(org.jivesoftware.spark.ui.ChatContainer) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) ChatRoomNotFoundException(org.jivesoftware.spark.ui.ChatRoomNotFoundException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) ChatManager(org.jivesoftware.spark.ChatManager) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl)

Example 27 with SwingWorker

use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.

the class FastpathPlugin method initialize.

public void initialize() {
    new WorkgroupInitializer().initialize();
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            container = new FastpathContainer();
            workgroupLabel = new JLabel(FpRes.getString("workgroup"));
            comboBox = new JComboBox();
            joinButton = new JButton(FpRes.getString("join"), null);
            logoutButton = new RolloverButton(FpRes.getString("logout"), null);
            // Initialize tab handler for Fastpath chats.
            fastpathTabHandler = new FastpathTabHandler();
            mainPanel = new BackgroundPane();
        }
    });
    try {
        DiscoverItems items = SparkManager.getSessionManager().getDiscoveredItems();
        for (DiscoverItems.Item item : items.getItems()) {
            String entityID = item.getEntityID() != null ? item.getEntityID() : "";
            if (entityID.startsWith("workgroup")) {
                // Log into workgroup
                final String workgroupService = "workgroup." + SparkManager.getSessionManager().getServerAddress();
                final String jid = SparkManager.getSessionManager().getJID();
                SwingWorker worker = new SwingWorker() {

                    public Object construct() {
                        try {
                            return Agent.getWorkgroups(workgroupService, jid, SparkManager.getConnection());
                        } catch (XMPPException | SmackException e1) {
                            return Collections.emptyList();
                        }
                    }

                    public void finished() {
                        Collection<String> agents = (Collection<String>) get();
                        if (agents.size() == 0) {
                            return;
                        }
                        showSelection(agents);
                    }
                };
                worker.start();
            }
        }
    } catch (Exception e) {
        Log.error(e);
    }
    SparkManager.getConnection().addConnectionListener(this);
}
Also used : SmackException(org.jivesoftware.smack.SmackException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) BackgroundPane(org.jivesoftware.fastpath.workspace.panes.BackgroundPane) RolloverButton(org.jivesoftware.spark.component.RolloverButton) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) Collection(java.util.Collection) XMPPException(org.jivesoftware.smack.XMPPException)

Example 28 with SwingWorker

use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.

the class FastpathPlugin method joinWorkgroup.

private void joinWorkgroup() {
    wasConnected = false;
    joinButton.setEnabled(false);
    SwingWorker worker = new SwingWorker() {

        public Object construct() {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                Log.error(e);
            }
            return null;
        }

        public void finished() {
            XMPPConnection con = SparkManager.getConnection();
            String workgroupName = org.jivesoftware.spark.util.StringUtils.makeFirstWordCaptial((String) comboBox.getSelectedItem());
            workgroupLabel.setText(FpRes.getString("message.workgroup.logged.into", workgroupName));
            logoutButton.setVisible(true);
            joinButton.setVisible(false);
            comboBox.setVisible(false);
            String workgroup = comboBox.getSelectedItem() + "@" + getComponentAddress();
            if (agentSession != null && agentSession.isOnline()) {
                try {
                    agentSession.setOnline(false);
                    agentSession.setOnline(true);
                } catch (XMPPException | SmackException e) {
                    Log.error(e);
                    leaveWorkgroup();
                    joinButton.setEnabled(true);
                    return;
                }
            } else {
                agentSession = new AgentSession(workgroup, con);
                try {
                    agentSession.setOnline(true);
                } catch (XMPPException | SmackException e1) {
                    Log.error(e1);
                    leaveWorkgroup();
                    joinButton.setEnabled(true);
                    return;
                }
            }
            // Send actual presence to workgroup.
            final Presence actualPresence = SparkManager.getWorkspace().getStatusBar().getPresence();
            Presence toWorkgroupPresence = new Presence(actualPresence.getType(), actualPresence.getStatus(), actualPresence.getPriority(), actualPresence.getMode());
            toWorkgroupPresence.setTo(workgroup);
            try {
                con.sendStanza(toWorkgroupPresence);
                wgroup = new Workgroup(workgroup, con);
            } catch (Exception e) {
                e.printStackTrace();
                joinButton.setEnabled(true);
                return;
            }
            // Initialize Workspace
            litWorkspace = new Workpane();
            litWorkspace.listenForOffers();
            joinButton.setEnabled(true);
            // Register tab handler
            SparkManager.getChatManager().addSparkTabHandler(fastpathTabHandler);
        }
    };
    worker.start();
}
Also used : AgentSession(org.jivesoftware.smackx.workgroup.agent.AgentSession) Workpane(org.jivesoftware.fastpath.workspace.Workpane) SmackException(org.jivesoftware.smack.SmackException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException) Workgroup(org.jivesoftware.smackx.workgroup.user.Workgroup) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 29 with SwingWorker

use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.

the class ContactList method switchAllUserOffline.

/**
 * Switches all users to Offline and Creates a Reconnection Group
 */
private synchronized void switchAllUserOffline(final boolean onError) {
    SwingWorker worker = new SwingWorker() {

        @Override
        public Object construct() {
            mainPanel.add(_reconnectpanelsmall, 0);
            _reconnectpanelsmall.setClosedOnError(onError);
            final Collection<RosterEntry> roster = Roster.getInstanceFor(SparkManager.getConnection()).getEntries();
            for (RosterEntry r : roster) {
                Presence p = new Presence(Presence.Type.unavailable);
                moveToOfflineGroup(p, r.getUser());
            }
            return true;
        }
    };
    worker.start();
}
Also used : SwingWorker(org.jivesoftware.spark.util.SwingWorker) Presence(org.jivesoftware.smack.packet.Presence) RosterEntry(org.jivesoftware.smack.roster.RosterEntry)

Example 30 with SwingWorker

use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.

the class ContactGroupTransferHandler method addContactItem.

private void addContactItem(final ContactGroup contactGroup, final ContactItem item) {
    ContactItem newContact = UIComponentRegistry.createContactItem(Res.getString("group.empty"), null, null);
    newContact.setPresence(item.getPresence());
    newContact.setIcon(item.getIcon());
    newContact.getNicknameLabel().setFont(item.getNicknameLabel().getFont());
    if (!PresenceManager.isOnline(item.getJID())) {
        contactGroup.addOfflineContactItem(item.getAlias(), item.getNickname(), item.getJID(), null);
    } else {
        contactGroup.addContactItem(newContact);
    }
    contactGroup.clearSelection();
    // Updating group title
    contactGroup.fireContactGroupUpdated();
    final ContactGroup oldGroup = getContactGroup(item.getGroupName());
    SwingWorker worker = new SwingWorker() {

        public Object construct() {
            Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
            RosterEntry entry = roster.getEntry(item.getJID());
            RosterGroup groupFound = null;
            for (RosterGroup group : roster.getGroups()) {
                if (group.getName().equals(contactGroup.getGroupName())) {
                    try {
                        groupFound = group;
                        group.addEntry(entry);
                    } catch (XMPPException | SmackException e1) {
                        Log.error(e1);
                        return false;
                    }
                }
            }
            // This is a new group
            if (groupFound == null) {
                groupFound = roster.createGroup(contactGroup.getGroupName());
                try {
                    groupFound.addEntry(entry);
                } catch (XMPPException | SmackException e) {
                    Log.error(e);
                }
            }
            return true;
        }

        public void finished() {
            if ((Boolean) get()) {
                // Now try and remove the group from the old one.
                removeContactItem(oldGroup, item);
            }
        }
    };
    worker.start();
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) SmackException(org.jivesoftware.smack.SmackException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) XMPPException(org.jivesoftware.smack.XMPPException) RosterGroup(org.jivesoftware.smack.roster.RosterGroup)

Aggregations

SwingWorker (org.jivesoftware.spark.util.SwingWorker)65 SmackException (org.jivesoftware.smack.SmackException)22 XMPPException (org.jivesoftware.smack.XMPPException)15 Presence (org.jivesoftware.smack.packet.Presence)11 MouseAdapter (java.awt.event.MouseAdapter)8 MouseEvent (java.awt.event.MouseEvent)8 LocalPreferences (org.jivesoftware.sparkimpl.settings.local.LocalPreferences)8 ActionEvent (java.awt.event.ActionEvent)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 GridBagConstraints (java.awt.GridBagConstraints)6 Insets (java.awt.Insets)6 Collection (java.util.Collection)6 JLabel (javax.swing.JLabel)6 JPanel (javax.swing.JPanel)5 ContactItem (org.jivesoftware.spark.ui.ContactItem)5 Action (javax.swing.Action)4 Cursor (java.awt.Cursor)3 ActionListener (java.awt.event.ActionListener)3 File (java.io.File)3