Search in sources :

Example 1 with UserSearchManager

use of org.jivesoftware.smackx.search.UserSearchManager in project Spark by igniterealtime.

the class RosterDialog method searchForContact.

/**
 * Creates a Popupdialog above the Search Button displaying matching
 * Contacts
 *
 * @param byname
 *            , the Searchname, atleast 5 Chars long
 * @param event
 *            , the MouseEvent which triggered it
 * @throws XMPPException
 * @throws InterruptedException
 */
public void searchForContact(String byname, MouseEvent event) throws XMPPException, SmackException.NotConnectedException, SmackException.NoResponseException, InterruptedException {
    UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
    if (byname.contains("@")) {
        byname = byname.substring(0, byname.indexOf("@"));
    }
    if (byname.length() <= 1) {
        JOptionPane.showMessageDialog(jidField, Res.getString("message.search.input.short"), Res.getString("title.notification"), JOptionPane.ERROR_MESSAGE);
    } else {
        JPopupMenu popup = new JPopupMenu();
        JMenuItem header = new JMenuItem(Res.getString("group.search.results") + ":");
        header.setBackground(UIManager.getColor("List.selectionBackground"));
        header.setForeground(Color.red);
        popup.add(header);
        for (DomainBareJid search : _usersearchservice) {
            ReportedData data;
            UserSearchManager usersearchManager = new UserSearchManager(SparkManager.getConnection());
            DataForm f = usersearchManager.getSearchForm(search);
            FillableForm answer = new FillableForm(f);
            answer.setAnswer("Name", true);
            answer.setAnswer("Email", true);
            answer.setAnswer("Username", true);
            answer.setAnswer("search", byname);
            data = usersearchManager.getSearchResults(answer.getDataFormToSubmit(), search);
            ArrayList<String> columnnames = new ArrayList<>();
            for (ReportedData.Column column : data.getColumns()) {
                String label = column.getLabel();
                columnnames.add(label);
            }
            for (ReportedData.Row row : data.getRows()) {
                if (!row.getValues(columnnames.get(0)).isEmpty()) {
                    String s = row.getValues(columnnames.get(0)).get(0).toString();
                    final JMenuItem item = new JMenuItem(s);
                    popup.add(item);
                    item.addActionListener(e -> {
                        jidField.setText(item.getText());
                        nicknameField.setText(XmppStringUtils.parseLocalpart(item.getText()));
                    });
                }
            }
        }
        if (popup.getComponentCount() > 2) {
            popup.setVisible(true);
            popup.show(_searchForName, event.getX(), event.getY());
        } else if (popup.getComponentCount() == 2) {
            jidField.setText(((JMenuItem) popup.getComponent(1)).getText());
            nicknameField.setText(XmppStringUtils.parseLocalpart(((JMenuItem) popup.getComponent(1)).getText()));
        } else {
            JOptionPane.showMessageDialog(jidField, Res.getString("message.no.results.found"), Res.getString("title.notification"), JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : UserSearchManager(org.jivesoftware.smackx.search.UserSearchManager) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) ArrayList(java.util.ArrayList) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) JMenuItem(javax.swing.JMenuItem) DomainBareJid(org.jxmpp.jid.DomainBareJid) JPopupMenu(javax.swing.JPopupMenu) ReportedData(org.jivesoftware.smackx.search.ReportedData)

Aggregations

ArrayList (java.util.ArrayList)1 JMenuItem (javax.swing.JMenuItem)1 JPopupMenu (javax.swing.JPopupMenu)1 ReportedData (org.jivesoftware.smackx.search.ReportedData)1 UserSearchManager (org.jivesoftware.smackx.search.UserSearchManager)1 FillableForm (org.jivesoftware.smackx.xdata.form.FillableForm)1 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)1 DomainBareJid (org.jxmpp.jid.DomainBareJid)1