Search in sources :

Example 6 with SwingWorker

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

the class RoomBrowser method displayRoomInformation.

public void displayRoomInformation(final String roomJID) {
    SwingWorker worker = new SwingWorker() {

        RoomInfo roomInfo = null;

        DiscoverItems items = null;

        public Object construct() {
            try {
                roomInfo = MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).getRoomInfo(roomJID);
                ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(SparkManager.getConnection());
                items = manager.discoverItems(roomJID);
            } catch (XMPPException | SmackException e) {
                Log.error(e);
            }
            return "ok";
        }

        public void finished() {
            setupRoomInformationUI(roomJID, roomInfo, items);
        }
    };
    worker.start();
}
Also used : SmackException(org.jivesoftware.smack.SmackException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) RoomInfo(org.jivesoftware.smackx.muc.RoomInfo) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) XMPPException(org.jivesoftware.smack.XMPPException) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager)

Example 7 with SwingWorker

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

the class BookmarkPlugin method initialize.

@Override
public void initialize() {
    final SwingWorker bookmarkThreadWorker = new SwingWorker() {

        @Override
        public Object construct() {
            return this;
        }

        /**
         * Installing menu into spark menu and adding events to bookmarks
         */
        @Override
        public void finished() {
            try {
                initialize();
            } catch (Exception e) {
                Log.error(e);
            }
        }

        /**
         */
        public void initialize() {
            final JMenu bookmarkMenu = new JMenu(Res.getString("menuitem.bookmarks"));
            createMenu(bookmarkMenu);
            if (bookmarkMenu.getMenuComponentCount() > 0) {
                SparkManager.getMainWindow().getMenu().add(bookmarkMenu, 3);
            }
            BookmarksUI bookmarksUi = ConferenceServices.getBookmarkedConferences();
            bookmarksUi.addBookmarksListener(new BookmarksListener() {

                @Override
                public void bookmarkAdded(String roomJID) {
                    rescan(bookmarkMenu);
                }

                @Override
                public void bookmarkRemoved(String roomJID) {
                    rescan(bookmarkMenu);
                }
            });
        }

        /**
         * Rescaning our bookmarks and remaking menu items
         *
         * @param bookmarkMenu menu Jmenu
         */
        public void rescan(JMenu bookmarkMenu) {
            // removing old menus
            bookmarkMenu.removeAll();
            try {
                // making new
                setBookmarks(bookmarkMenu);
                int onPanel = SparkManager.getMainWindow().getMenu().getComponentIndex(bookmarkMenu);
                if (onPanel < 0) {
                    if (bookmarkMenu.getMenuComponentCount() > 0) {
                        int menuCount = SparkManager.getMainWindow().getMenu().getMenuCount();
                        SparkManager.getMainWindow().getMenu().add(bookmarkMenu, menuCount - 2);
                    }
                }
                if (onPanel >= 0) {
                    if (bookmarkMenu.getMenuComponentCount() <= 0) {
                        SparkManager.getMainWindow().getMenu().remove(bookmarkMenu);
                    }
                }
                SparkManager.getMainWindow().getMenu().invalidate();
                SparkManager.getMainWindow().getMenu().validate();
                SparkManager.getMainWindow().getMenu().repaint();
            } catch (XMPPException | SmackException ex) {
                Log.error(ex);
            }
        }

        /**
         * Updating statusbar and generating menu items
         *
         * @param bookmarkMenu menu Jmenu
         */
        public void createMenu(JMenu bookmarkMenu) {
            try {
                setBookmarks(bookmarkMenu);
            } catch (XMPPException | SmackException ex) {
                Log.error(ex);
            }
        }

        /**
         * loading menu items and setting bookmarks listeners
         *
         * @param bookmarkMenu menu Jmenu
         */
        public void setBookmarks(JMenu bookmarkMenu) throws XMPPException, SmackException {
            BookmarkManager manager = BookmarkManager.getBookmarkManager(SparkManager.getConnection());
            if (manager != null) {
                Collection<BookmarkedConference> bookmarkedConferences = manager.getBookmarkedConferences();
                final Collection<BookmarkedURL> bookmarkedLinks = manager.getBookmarkedURLs();
                for (Object bookmarkedLink : bookmarkedLinks) {
                    final BookmarkedURL link = (BookmarkedURL) bookmarkedLink;
                    Action urlAction = new AbstractAction() {

                        private static final long serialVersionUID = 4246574779205966917L;

                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            try {
                                BrowserLauncher.openURL(link.getURL());
                            } catch (Exception e) {
                                Log.error(e);
                            }
                        }
                    };
                    urlAction.putValue(Action.NAME, link.getName());
                    urlAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.LINK_16x16));
                    bookmarkMenu.add(urlAction);
                }
                for (Object bookmarkedConference : bookmarkedConferences) {
                    final BookmarkedConference conferences = (BookmarkedConference) bookmarkedConference;
                    Action conferenceAction = new AbstractAction() {

                        private static final long serialVersionUID = 5964584172262968704L;

                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            final TimerTask task = new SwingTimerTask() {

                                @Override
                                public void doRun() {
                                    ConferenceUtils.joinConferenceOnSeperateThread(conferences.getName(), conferences.getJid(), conferences.getPassword());
                                }
                            };
                            TaskEngine.getInstance().schedule(task, 10);
                        }
                    };
                    conferenceAction.putValue(Action.NAME, conferences.getName());
                    conferenceAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.CONFERENCE_IMAGE_16x16));
                    bookmarkMenu.add(conferenceAction);
                }
            }
        }
    };
    bookmarkThreadWorker.start();
}
Also used : BookmarkedURL(org.jivesoftware.smackx.bookmarks.BookmarkedURL) Action(javax.swing.Action) AbstractAction(javax.swing.AbstractAction) ActionEvent(java.awt.event.ActionEvent) SmackException(org.jivesoftware.smack.SmackException) BookmarksListener(org.jivesoftware.spark.ui.conferences.BookmarksListener) BookmarkedConference(org.jivesoftware.smackx.bookmarks.BookmarkedConference) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) BookmarksUI(org.jivesoftware.spark.ui.conferences.BookmarksUI) BookmarkManager(org.jivesoftware.smackx.bookmarks.BookmarkManager) SwingTimerTask(org.jivesoftware.spark.util.SwingTimerTask) SwingTimerTask(org.jivesoftware.spark.util.SwingTimerTask) TimerTask(java.util.TimerTask) SwingWorker(org.jivesoftware.spark.util.SwingWorker) XMPPException(org.jivesoftware.smack.XMPPException) AbstractAction(javax.swing.AbstractAction) JMenu(javax.swing.JMenu)

Example 8 with SwingWorker

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

the class ContactListAssistantPlugin method addContactItem.

/**
 * Copies or moves a new <code>ContactItem</code> into the <code>ContactGroup</code>.
 *
 * @param contactGroup the ContactGroup.
 * @param item         the ContactItem to move.
 * @param move         true if the ContactItem should be moved, otherwise false.
 */
private void addContactItem(final ContactGroup contactGroup, final ContactItem item, final boolean move) {
    ContactItem newContact = UIComponentRegistry.createContactItem(item.getAlias(), item.getNickname(), item.getJID());
    newContact.setPresence(item.getPresence());
    newContact.setIcon(item.getIcon());
    newContact.getNicknameLabel().setFont(item.getNicknameLabel().getFont());
    boolean groupHadAvailableContacts = false;
    // Do not copy/move a contact item only if it is not already in the Group.
    if (contactGroup.getContactItemByJID(item.getJID(), true) != null) {
        return;
    }
    if (!PresenceManager.isOnline(item.getJID())) {
        contactGroup.addOfflineContactItem(item.getAlias(), item.getNickname(), item.getJID(), null);
    } else {
        groupHadAvailableContacts = contactGroup.hasAvailableContacts();
        contactGroup.addContactItem(newContact);
    }
    contactGroup.clearSelection();
    // Updating group title
    contactGroup.fireContactGroupUpdated();
    final ContactGroup oldGroup = getContactGroup(item.getGroupName());
    final boolean groupAvailableContacts = groupHadAvailableContacts;
    SwingWorker worker = new SwingWorker() {

        @Override
        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;
                        if (!groupAvailableContacts) {
                            SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
                        }
                        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);
                    if (!groupAvailableContacts) {
                        SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
                    }
                } catch (XMPPException | SmackException e) {
                    Log.error(e);
                }
            }
            return true;
        }

        @Override
        public void finished() {
            if ((Boolean) get()) {
                // Now try and remove the group from the old one.
                if (move) {
                    removeContactItem(oldGroup, item);
                    if (!localPreferences.isEmptyGroupsShown() && !oldGroup.hasAvailableContacts()) {
                        SparkManager.getContactList().toggleGroupVisibility(oldGroup.getGroupName(), false);
                    }
                }
            }
        }
    };
    worker.start();
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) ContactItem(org.jivesoftware.spark.ui.ContactItem) SmackException(org.jivesoftware.smack.SmackException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) ContactGroup(org.jivesoftware.spark.ui.ContactGroup) XMPPException(org.jivesoftware.smack.XMPPException) RosterGroup(org.jivesoftware.smack.roster.RosterGroup)

Example 9 with SwingWorker

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

the class GatewayPlugin method registerPresenceListener.

private void registerPresenceListener() {
    StanzaFilter orFilter = new OrFilter(new StanzaTypeFilter(Presence.class), new StanzaTypeFilter(Message.class));
    SparkManager.getConnection().addAsyncStanzaListener(stanza -> {
        if (stanza instanceof Presence) {
            Presence presence = (Presence) stanza;
            Transport transport = TransportUtils.getTransport(stanza.getFrom());
            if (transport != null) {
                boolean registered = true;
                if (presence.getType() == Presence.Type.unavailable) {
                    registered = false;
                }
                GatewayItem button = uiMap.get(transport);
                button.signedIn(registered);
                SwingWorker worker = new SwingWorker() {

                    @Override
                    public Object construct() {
                        transferTab.revalidate();
                        transferTab.repaint();
                        return 41;
                    }
                };
                worker.start();
            }
        } else if (stanza instanceof Message) {
            Message message = (Message) stanza;
            String from = message.getFrom();
            boolean hasError = message.getType() == Message.Type.error;
            String body = message.getBody();
            if (from != null && hasError) {
                Transport transport = TransportUtils.getTransport(from);
                if (transport != null) {
                    String title = "Alert from " + transport.getName();
                    // Show error
                    MessageDialog.showAlert(body, title, "Information", SparkRes.getImageIcon(SparkRes.INFORMATION_IMAGE));
                }
            }
        }
    }, orFilter);
    ChatManager chatManager = SparkManager.getChatManager();
    chatManager.addContactItemHandler(this);
    // Iterate through Contacts and check for
    final ContactList contactList = SparkManager.getWorkspace().getContactList();
    for (ContactGroup contactGroup : contactList.getContactGroups()) {
        for (ContactItem contactItem : contactGroup.getContactItems()) {
            Presence presence = contactItem.getPresence();
            if (presence.isAvailable()) {
                String domain = XmppStringUtils.parseDomain(presence.getFrom());
                Transport transport = TransportUtils.getTransport(domain);
                if (transport != null) {
                    handlePresence(contactItem, presence);
                    contactGroup.fireContactGroupUpdated();
                }
            }
        }
    }
    SparkManager.getSessionManager().addPresenceListener(presence -> {
        for (Transport transport : TransportUtils.getTransports()) {
            GatewayItem button = uiMap.get(transport);
            if (button.isLoggedIn()) {
                if (!presence.isAvailable()) {
                    return;
                }
                // Create new presence
                Presence p = new Presence(presence.getType(), presence.getStatus(), presence.getPriority(), presence.getMode());
                p.setTo(transport.getServiceName());
                try {
                    SparkManager.getConnection().sendStanza(p);
                } catch (SmackException.NotConnectedException e) {
                    Log.warning("Unable to forward presence change to transport.", e);
                }
            }
        }
    });
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Message(org.jivesoftware.smack.packet.Message) SmackException(org.jivesoftware.smack.SmackException) OrFilter(org.jivesoftware.smack.filter.OrFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) Presence(org.jivesoftware.smack.packet.Presence) SwingWorker(org.jivesoftware.spark.util.SwingWorker) ChatManager(org.jivesoftware.spark.ChatManager)

Example 10 with SwingWorker

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

the class BookmarksUI method getServicePanel.

private JPanel getServicePanel() {
    final JPanel servicePanel = new JPanel();
    servicePanel.setOpaque(false);
    servicePanel.setLayout(new GridBagLayout());
    final JLabel serviceLabel = new JLabel();
    final RolloverButton addButton = new RolloverButton(SparkRes.getImageIcon(SparkRes.SMALL_ADD_IMAGE));
    addButton.setToolTipText(Res.getString("message.add.conference.service"));
    final JTextField serviceField = new JTextField();
    servicePanel.add(serviceLabel, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 2, 5), 0, 0));
    servicePanel.add(serviceField, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 2, 5), 0, 0));
    servicePanel.add(addButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 2, 5), 0, 0));
    // Add resource utils
    ResourceUtils.resLabel(serviceLabel, serviceField, Res.getString("label.add.conference.service"));
    final Action conferenceAction = new AbstractAction() {

        private static final long serialVersionUID = 7973928300442518496L;

        @Override
        public void actionPerformed(ActionEvent e) {
            final String conferenceService = serviceField.getText();
            if (hasService(conferenceService)) {
                UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                JOptionPane.showMessageDialog(null, Res.getString("message.service.already.exists"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                serviceField.setText("");
            } else {
                final List<String> serviceList = new ArrayList<>();
                serviceField.setText(Res.getString("message.searching.please.wait"));
                serviceField.setEnabled(false);
                addButton.setEnabled(false);
                SwingWorker worker = new SwingWorker() {

                    DiscoverInfo discoInfo;

                    @Override
                    public Object construct() {
                        ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(SparkManager.getConnection());
                        try {
                            discoInfo = discoManager.discoverInfo(conferenceService);
                            for (DiscoverInfo.Identity identity : discoInfo.getIdentities()) {
                                if ("conference".equals(identity.getCategory())) {
                                    serviceList.add(conferenceService);
                                    break;
                                } else if ("server".equals(identity.getCategory())) {
                                    try {
                                        Collection<String> services = getConferenceServices(conferenceService);
                                        for (String service : services) {
                                            serviceList.add(service);
                                        }
                                    } catch (Exception e1) {
                                        Log.error("Unable to load conference services in server.", e1);
                                    }
                                }
                            }
                        } catch (XMPPException | SmackException e1) {
                            Log.error("Error in disco discovery.", e1);
                        }
                        return true;
                    }

                    @Override
                    public void finished() {
                        if (discoInfo != null) {
                            for (String aServiceList : serviceList) {
                                if (!hasService(aServiceList)) {
                                    addServiceToList(aServiceList);
                                }
                            }
                            serviceField.setText("");
                            serviceField.setEnabled(true);
                            addButton.setEnabled(true);
                        } else {
                            UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                            JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.conference.service.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                            serviceField.setText("");
                            serviceField.setEnabled(true);
                            addButton.setEnabled(true);
                        }
                    }
                };
                worker.start();
            }
        }
    };
    addButton.addActionListener(conferenceAction);
    serviceField.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                conferenceAction.actionPerformed(null);
            }
        }
    });
    return servicePanel;
}
Also used : RolloverButton(org.jivesoftware.spark.component.RolloverButton) DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) SmackException(org.jivesoftware.smack.SmackException) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager) SwingWorker(org.jivesoftware.spark.util.SwingWorker) XMPPException(org.jivesoftware.smack.XMPPException)

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