/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.rcp.ui;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import net.sphene.goim.rcp.beans.GOIMAccount;
import net.sphene.goim.rcp.xmpp.GOIMPacketListener;
import net.sphene.goim.rcp.xmpp.TaskObserver;
import net.sphene.goim.rcp.xmpp.smackextensions.SearchIQ;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.Form;
public class SearchUI {
private Shell shell;
private Text txtFirstName, txtLastName, txtEmail, txtNickName;
private Button btnCancel, btnSearch;
private Table searchResults;
private GOIMAccount account;
private String searchservice;
public SearchUI(GOIMAccount account, String searchservice) {
this.account = account;
this.searchservice = searchservice;
shell = new Shell();
sendSearchIQ();
//createSearchForm();
}
private void createSearchShell() {
shell = new Shell();
shell.setText("Search " + searchservice);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
shell.setLayout(layout);
shell.setSize(new org.eclipse.swt.graphics.Point(483, 280));
}
String[] colsNames = { "Jabber ID", "Nick Name", "First Name", "Last Name"};
private void createSearchUI() {
Composite parent = new Composite(shell, SWT.NONE);
GridLayout gLayout = new GridLayout();
gLayout.numColumns = 1;
parent.setLayout(gLayout);
searchResults = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION);
searchResults.setLayout(new GridLayout());
for (int i = 0; i < colsNames.length; i++) {
TableColumn col = new TableColumn(searchResults, SWT.CENTER);
col.setText(colsNames[i]);
col.setWidth(80);
}
searchResults.setHeaderVisible(true);
searchResults.setLinesVisible(true);
new PopupMenu( searchResults, account );
int listHeight = searchResults.getItemHeight() * 12;
Rectangle trim = searchResults.computeTrim(0, 0, 0, listHeight);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
| GridData.VERTICAL_ALIGN_FILL);
gridData.heightHint = trim.height;
gridData.grabExcessHorizontalSpace = true;
searchResults.setLayoutData(gridData);
new Label(parent, SWT.NONE).setText("First Name");
txtFirstName = new Text(parent, SWT.SINGLE | SWT.BORDER);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
txtFirstName.setLayoutData(gridData);
new Label(parent, SWT.NONE).setText("Nick Name");
txtNickName = new Text(parent, SWT.NONE | SWT.BORDER);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
txtNickName.setLayoutData(gridData);
new Label(parent, SWT.NONE).setText("Last Name");
txtLastName = new Text(parent, SWT.NONE | SWT.BORDER);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
txtLastName.setLayoutData(gridData);
new Label(parent, SWT.NONE).setText("Email");
txtEmail = new Text(parent, SWT.NONE | SWT.BORDER);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
txtEmail.setLayoutData(gridData);
Composite btnComposite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
btnComposite.setLayout(layout);
btnSearch = new Button(btnComposite, SWT.PUSH);
btnSearch.setText("Search");
btnSearch.setToolTipText( "Search" );
btnSearch.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.setCursor( new Cursor( shell.getDisplay(), SWT.CURSOR_WAIT ));
sendSearch();
clearFields();
}
});
btnCancel = new Button(btnComposite, SWT.PUSH);
btnCancel.setText("Cancel");
btnCancel.setToolTipText( "Cancel" );
btnCancel.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
shell.dispose();
}
});
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
btnComposite.setLayoutData(gridData);
}
private void sendSearch(){
try {
SearchIQ iq = new SearchIQ();
iq.setType(IQ.Type.SET);
// iq.setType(IQ.Type.GET );
iq.setFrom(account.xmpp.getConnection().getUser());
iq.setTo(searchservice);
iq.addField("first");
iq.setField("first", txtFirstName.getText());
iq.addField("last");
iq.setField("last", txtLastName.getText());
iq.addField("nick");
iq.setField("nick", txtNickName.getText());
iq.addField("email");
iq.setField("email", txtEmail.getText());
new GOIMPacketListener(account.xmpp.getConnection(),
new PacketIDFilter(iq.getPacketID()), new TaskObserver()) {
public void processPacket(final Packet packet) {
final SearchIQ result = (SearchIQ) packet;
shell.getDisplay().asyncExec(new Runnable() { public void run() {
shell.setCursor( new Cursor( shell.getDisplay(), SWT.CURSOR_ARROW ));
if(result==null)
{
MessageDialog.openError( shell, "Error" , "No response from server." );
return;
}
if(result.getNumberOfItems()==0){
MessageDialog.openError( shell, "Error", "No result available for this search criteria" );
return;
}
}});
showResults(result);
}
}.registerListener();
account.xmpp.getConnection().sendPacket(iq);
} catch (Exception se) {
se.printStackTrace();
}
}
private void clearFields() {
txtFirstName.setText("");
txtLastName.setText("");
txtNickName.setText("");
txtEmail.setText("");
}
private void createSearchForm() {
createSearchShell();
createSearchUI();
shell.open();
}
private void sendSearchIQ() {
// SearchIQProvider.install();
try {
SearchIQ iq = new SearchIQ();
// iq.setType(IQ.Type.SET);
iq.setType(IQ.Type.GET );
iq.setFrom(account.xmpp.getConnection().getUser());
iq.setTo(searchservice);
//iq.setTo( "vjud.sphene.net" );
// iq.setTo( "users.netlab.cz" );
/*iq.addField("first");
iq.setField("first", txtFirstName.getText());
iq.addField("last");
iq.setField("last", txtLastName.getText());
iq.addField("nick");
iq.setField("nick", txtNickName.getText());
iq.addField("email");
iq.setField("email", txtEmail.getText());
*/
System.out.println(iq.toXML());
new GOIMPacketListener(account.xmpp.getConnection(),
new PacketIDFilter(iq.getPacketID()), new TaskObserver()) {
public void processPacket(final Packet packet) {
final SearchIQ result = (SearchIQ) packet;
// shell.getDisplay().asyncExec(new Runnable() { public void run() {
//// shell.setCursor( new Cursor( shell.getDisplay(), SWT.CURSOR_ARROW ));
// if(result==null)
// {
// MessageDialog.openError( shell, "Error" , "No response from server." );
// return;
// }
// if(result.getNumberOfItems()==0){
// MessageDialog.openError( shell, "Error", "No result available for this search criteria" );
// return;
// }
// }});
test( result );
//showResults(result);
}
}.registerListener();
account.xmpp.getConnection().sendPacket(iq);
} catch (Exception se) {
se.printStackTrace();
}
}
private void test( final SearchIQ result )
{
System.out.println( "RESULT ::::: " + result.getChildElementXML() );
final Form form = Form.getFormFrom(result);
if( form != null){
shell.getDisplay().asyncExec(new Runnable() { public void run() {
//SearchForm sForm = new SearchForm( shell, form, account, searchservice, result );
new SearchForm( shell, form, account, result.getFrom(), result );
// sForm.open();
}});
}
else {
shell.getDisplay().asyncExec(new Runnable() { public void run() {
createSearchForm();
}});
}
}
private void showResults(SearchIQ result) {
Enumeration e = result.getJids();
DefaultPacketExtension dpe = null;
Hashtable<String,String> columns = new Hashtable<String,String>();
// TableColumn tColumn = new TableColumn( searchResults, SWT.CENTER );
// tColumn.setText( "Jabber ID" );
while (e.hasMoreElements()) {
dpe = result.getItem((String) e.nextElement());
Iterator i = dpe.getNames();
while (i.hasNext()) {
// TableColumn col = new TableColumn( searchResults, SWT.CENTER
// );
String colName = i.next().toString();
// col.setText( colName );
columns.put(colName, colName);
}
}
e = result.getJids();
Enumeration columnNames = columns.keys();
String name = null;
while (columnNames.hasMoreElements()) {
name = (String) columnNames.nextElement();
name = "form_" + name.trim().toLowerCase();
getContents(name);
}
// searchResults.setHeaderVisible( true );
// data now....
String data = "";
String jid = "";
Vector<String> row = null;
Vector<Object> rowData = new Vector<Object>();
while (e.hasMoreElements()) {
columnNames = columns.keys();
jid = (String) e.nextElement();
dpe = result.getItem(jid);
row = new Vector<String>();
String[] strArray = new String[4];
row.add(jid);
int i = 0;
while (columnNames.hasMoreElements()) {
try {
data = dpe.getValue((String) columnNames.nextElement());
} catch (Exception ex) {
data = "";
}
System.out.println("+++" + data);
row.add(data);
// item.setText( i, data );
strArray[i] = data;
i++;
}
rowData.add(strArray);
System.out.println(row);
}
fillTableData(rowData);
}
private void fillTableData(final Vector rowData) {
shell.getDisplay().asyncExec(new Runnable() {
public void run() {
searchResults.removeAll();
for (int i = 0; i < rowData.size(); i++) {
TableItem item = new TableItem(searchResults, SWT.SINGLE);
item.setText((String[]) rowData.elementAt(i));
}
}
});
}
private String getContents(String value) {
String returnVal = "";
for (int i = 0; i < contents.length; i++) {
if (contents[i].equals(value)) {
returnVal = (String) contents[i][i];
System.out.println("asdasdasdsadasdas " + returnVal);
}
}
return null;
}
private static final Object[][] contents = { { "form_name", "Name:" }, // the
// user's
// name.
{ "form_nick", "Nickname:" }, // nickname
{ "form_nickname", "Nickname:" }, // nick name 2
{ "form_username", "UserName:" }, // username
{ "form_password", "Password:" },// a password
{ "form_first", "First Name:" }, // the user's first name.
{ "form_last", "Last Name:" }, // the user's last name.
{ "form_email", "Email:" }, // the user's email address.
};
}