Package JabberClient

Source Code of JabberClient.SearchWindow

package JabberClient;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.ArrayList;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.ReportedData;
import org.jivesoftware.smackx.ReportedData.Row;
import org.jivesoftware.smackx.search.UserSearchManager;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.ReportedData.Column;


public class SearchWindow extends JFrame {
    private JButton jbtnSearch;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem4;
    private ArrayList arlSearchFields = new ArrayList();
    private ArrayList<String> arlTextFieldValues = new ArrayList<String>();
    private ArrayList<String> arlSearchResultHeader = new ArrayList<String>();
    private XMPPConnection xmppconnection;
    private String strService;
    private Form searchForm;
    private Form answerForm;
    private ReportedData reporteddata;
    private UserSearchManager usersearchmanager;
    private JButton jbtnAdd;
    private JTable jtable;
    private DefaultTableModel defaulttablemodel;
    private Container container;
    private int intSelectedRow;
    private Vector<String> vecColumnNames;
    private Vector<Vector> vecRowData;

   
    public SearchWindow(XMPPConnection xmppconnection){
        this.xmppconnection = xmppconnection;
        intSelectedRow = -1;
        getSearchFields();
        initComponents();
    }


    private void initComponents() {
        jMenuBar1 = new JMenuBar();
        jMenu1 = new JMenu();
        jMenuItem4 = new JMenuItem();
        jMenu3 = new JMenu();
        jMenuItem2 = new JMenuItem();

        jMenu1.setText("Hauptmenü");
        jMenuItem4.setText("Beenden");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);
        jMenuBar1.add(jMenu1);
        jMenu3.setText("Hilfe");
        jMenuItem2.setText("Messengerhilfe");
        jMenu3.add(jMenuItem2);
        jMenuBar1.add(jMenu3);
        setJMenuBar(jMenuBar1);

        defaulttablemodel = new DefaultTableModel();
        jtable = new JTable(defaulttablemodel){
            public boolean isCellEditable(int x, int y) {
                return false;
            }
        };
        jtable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
       
        container = this.getContentPane();
        BorderLayout bLayout = new BorderLayout();
        GridLayout gLayoutFields = new GridLayout(arlSearchFields.size(),1,10,10);
        GridLayout gLayoutButtons = new GridLayout(1,2);

        JPanel jpFields = new JPanel();
        jpFields.setLayout(gLayoutFields);

        JPanel jpButtons = new JPanel();
        jpButtons.setLayout(gLayoutButtons);

        container.setLayout(bLayout);
        container.add(jpFields,BorderLayout.NORTH);
        container.add( new JScrollPane(jtable),BorderLayout.CENTER );
        container.add(jpButtons,BorderLayout.SOUTH);

        ListSelectionModel rowSM = jtable.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();
              if (!lsm.isSelectionEmpty()) {
                  intSelectedRow = lsm.getMinSelectionIndex();
              }
            }
        });

        for(int i=0; i<arlSearchFields.size();i++){
            JTextField tmp = (JTextField) arlSearchFields.get(i);
            Border etched = BorderFactory.createEtchedBorder();
            tmp.setBorder(BorderFactory.createTitledBorder(etched,tmp.getName()));
            jpFields.add(tmp);
        }

         jbtnSearch = new JButton("Suchen");
         jbtnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSearchActionPerformed(evt);
            }
         });
         jpButtons.add(jbtnSearch);

         jbtnAdd = new JButton("Hinzufügen");
         jbtnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAddActionPerformed(evt);
            }
         });
         jpButtons.add(jbtnAdd);
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
       
        pack();
    }

    private void jbtnSearchActionPerformed(java.awt.event.ActionEvent evt) {
        intSelectedRow = -1;
        getSearchResults();
    }

    private void jbtnAddActionPerformed(java.awt.event.ActionEvent evt) {
        if(intSelectedRow>=0){
        int intPosJID = 0;
        int intPosNickname = 0;
        Roster roster = xmppconnection.getRoster();
        System.out.println("Hinzufügen-Schalter ausgewählter Eintrag: " + intSelectedRow);
        Vector vSelectedRow = (Vector) defaulttablemodel.getDataVector().elementAt(intSelectedRow);

        for(int i=0; i<vecColumnNames.size();i++){
            if(vecColumnNames.get(i).equals("jid")){
                intPosJID = i;
            }
            if(vecColumnNames.get(i).equals("Name")){
                intPosNickname = i;
            }
        }

        System.out.println(vecColumnNames);
        System.out.println(vSelectedRow);
            try {
                if(roster.contains((String) vSelectedRow.elementAt(intPosJID))){
                    JOptionPane.showMessageDialog(null,"Der Benutzer >>" + vSelectedRow.elementAt(intPosJID) + "<< ist bereits in der Kontaktliste vorhanden!","Information",1);
                }
                else{
                    roster.createEntry((String) vSelectedRow.elementAt(intPosJID), (String) vSelectedRow.elementAt(intPosNickname), null);
                    //Neuer eintrag zu contacts
                    //contacts.addContact(new Contact((String) vSelectedRow.elementAt(posJID), (String) vSelectedRow.elementAt(posJID),(String) vSelectedRow.elementAt(posNickname), ));
                }
            } catch (XMPPException ex) {
                ex.printStackTrace();
            }
        }
        else{
            System.out.println("Kein Wert aus der Tabelle ausgewählt");
        }
    }

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
        this.dispose();
}

    private void formWindowClosing(java.awt.event.WindowEvent evt) {
        this.dispose();
    }


    private void getSearchFields(){
        try {
            usersearchmanager = new UserSearchManager(xmppconnection);

            // wenn am Server im Searchservice ein Feld geändert wird, und gleich der client gestartet wird, erscheint hier ein Nullpointer
            //liefert alle Suchservices des Servers, der erste wird hier für die Suche verwendet
            strService = (String) usersearchmanager.getSearchServices().iterator().next();
            //strService = "search." + xmppconnection.getHost();
            searchForm = usersearchmanager.getSearchForm(strService);

            Iterator<FormField> fields = searchForm.getFields();
            while (fields.hasNext()) {
                FormField field = fields.next();
                if(field.getType().equals("boolean")){
                    JTextField tmp = new JTextField();
                    tmp.setName(field.getVariable());
                    arlSearchFields.add(tmp);
                }
            }
        } catch (XMPPException ex) {
            ex.printStackTrace();
        }
   }


    private void getSearchResults(){
        try {
            arlTextFieldValues.clear();
            arlSearchResultHeader.clear();
            vecColumnNames = new Vector<String>();
            vecRowData = new Vector<Vector>();
            answerForm = searchForm.createAnswerForm();

            for(int i=0; i<arlSearchFields.size();i++){
                 JTextField tmp = (JTextField) arlSearchFields.get(i);
                 if(!tmp.getText().equals("")){
                    arlTextFieldValues.add(tmp.getText());
                 }
            }

            answerForm.getField("search").setType("list-multi");
            answerForm.setAnswer("search", arlTextFieldValues);

            for(int i=0; i<arlSearchFields.size();i++){
                JTextField tmp = (JTextField) arlSearchFields.get(i);
                if(!tmp.getText().equals("")){
                    answerForm.setAnswer(tmp.getName(), true);
                }
            }

            reporteddata = usersearchmanager.getSearchResults(answerForm, strService);

            Iterator<Column> columns = reporteddata.getColumns();
            while (columns.hasNext()) {
                vecColumnNames.addElement(columns.next().getVariable());
            }

            Iterator<Row> rows = reporteddata.getRows();
            while (rows.hasNext()) {
                Row row = rows.next();
                Vector<String> vRow = new Vector<String>();

                for(int i=0;i<vecColumnNames.size();i++){
                    Iterator<String>  strValue = row.getValues((String)vecColumnNames.get(i));
                    while (strValue.hasNext()) {
                        vRow.add(strValue.next());
                    }
                }
                vecRowData.addElement(vRow);
            }
        defaulttablemodel.setDataVector(vecRowData, vecColumnNames);

        }
        catch (XMPPException ex) {
            ex.printStackTrace();
        }
    }
}
TOP

Related Classes of JabberClient.SearchWindow

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.