/*
* 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.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.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Control;
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.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.packet.DataForm;
import org.jivesoftware.smackx.packet.DataForm.ReportedData;
public class SearchForm {//extends Dialog {
private Form form;
private Composite dialogArea;
private GOIMAccount account;
private Form replyValue;
private Shell parent;
private SearchIQ searchIQ;
private Vector<String> columnNames;
private Table searchResults;
private Button btnSearch, btnCancel;
public SearchForm(Shell parentShell, Form form, GOIMAccount account,
String searchservice, SearchIQ searchIQ) {
System.out.println( "Here also" );
//super(parentShell);
//this.parent = parentShell;
parent = new Shell( SWT.TITLE | SWT.MIN | SWT.CLOSE);
parent.setText( "Search " + searchservice);
this.form = form;
this.account = account;
this.searchIQ = searchIQ;
columnNames = new Vector<String>();
createSearchUI( parent );
parent.setSize(new org.eclipse.swt.graphics.Point(800, 450));
//
// parent.setVisible( true );
parent.open();
}
// @Override
// protected Control createDialogArea(Composite parent) {
private void createSearchUI( final Composite parent ){
// dialogArea = (Composite)super.createDialogArea(parent);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
parent.setLayout( layout);
Label instructions = new Label(parent, SWT.NULL);
instructions.setText(form.getInstructions());
instructions.setLayoutData(new GridData(SWT.NULL, SWT.NULL, false,
false));
// Composite parent1 = (Composite) super.createDialogArea(parent);
Composite parent1 = new Composite( parent, SWT.NONE );
GridLayout gLayout = new GridLayout();
gLayout.numColumns = 2;
parent1.setLayout(gLayout);
GridData gData = new GridData();
gData.grabExcessHorizontalSpace = true;
gData.grabExcessVerticalSpace = true;
parent1.setLayoutData( gData );
dialogArea = new Composite(parent1, SWT.NONE);
dialogArea.setLayout(new GridLayout());
searchResults = new Table( parent1, SWT.SINGLE
| SWT.FULL_SELECTION );
searchResults.setLayout( new GridLayout() );
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);
// Label instructions = new Label(dialogArea,SWT.NULL);
// instructions.setText( form.getInstructions() );
// instructions.setLayoutData(new
// GridData(SWT.NULL,SWT.NULL,false,false));
Composite composite = new Composite(dialogArea, SWT.NULL);
composite.setLayout(new GridLayout(2, false));
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
if (form != null) {
Iterator i = form.getFields();
while (i.hasNext()) {
FormField field = (FormField) i.next();
if (field.getType().equals(FormField.TYPE_HIDDEN))
continue;
GridData defaultLayoutData = new GridData(SWT.FILL, SWT.NULL,
false, false);
Object toAdd = null;
if (field.getType().equals(FormField.TYPE_TEXT_SINGLE)) {
createFieldLabel(composite, field);
Text inputText = new Text(composite, SWT.BORDER);
inputText.setLayoutData(defaultLayoutData);
String val = getFirstValueOfField(field);
if (val != null)
inputText.setText(val);
toAdd = inputText;
} else if (field.getType().equals(FormField.TYPE_TEXT_PRIVATE)) {
createFieldLabel(composite, field);
Text inputText = new Text(composite, SWT.BORDER);
inputText.setLayoutData(defaultLayoutData);
toAdd = inputText;
inputText.setEchoChar('*');
String val = getFirstValueOfField(field);
if (val != null)
inputText.setText(val);
} else if (field.getType().equals(FormField.TYPE_BOOLEAN)) {
Button button = new Button(composite, SWT.CHECK);
button.setText(field.getLabel());
button.setToolTipText(field.getDescription());
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
false, 2, 1));
toAdd = button;
String val = getFirstValueOfField(field);
if (val != null)
button.setSelection(val.equals("1"));
} else {
System.out.println("Field not supported: "
+ field.getLabel() + " (" + field.getType() + ")");
}
dialogArea.setData(field.getVariable(), toAdd);
}
}
createTable( searchResults );
Composite btnComposite = new Composite(parent, SWT.NONE);
GridLayout layout1 = new GridLayout();
layout1.numColumns = 2;
btnComposite.setLayout(layout1);
btnSearch = new Button(btnComposite, SWT.PUSH);
btnSearch.setText("Search");
btnSearch.setToolTipText( "Search" );
btnSearch.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
//parent.setCursor( new Cursor( parent.getDisplay(), SWT.CURSOR_WAIT ));
click();
}
});
btnCancel = new Button(btnComposite, SWT.PUSH);
btnCancel.setText("Cancel");
btnCancel.setToolTipText( "Cancel" );
btnCancel.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
parent.dispose();
}
});
//btnComposite.setLayoutData(gridData);
//return dialogArea;
}
private void createTable( Table searchResults){
searchResults.setLayout(new GridLayout());
for (int i = 0; i < columnNames.size(); i++) {
TableColumn col = new TableColumn(searchResults, SWT.CENTER);
col.setText( columnNames.elementAt( i ) );
col.setWidth(80);
}
}
private String getFirstValueOfField(FormField field) {
Iterator vals = field.getValues();
while (vals.hasNext()) {
return (String) vals.next();
}
return null;
}
private void createFieldLabel(Composite parent, FormField field) {
Label fieldLabel = new Label(parent, SWT.NULL);
fieldLabel.setText(field.getLabel() + (field.isRequired() ? " *" : ""));
columnNames.add( field.getLabel() );
fieldLabel.setToolTipText(field.getDescription());
}
protected void click(){
SearchIQ iq = new SearchIQ();
iq.setType(IQ.Type.SET);
iq.setFrom(account.xmpp.getConnection().getUser());
//iq.setTo(searchservice);
//iq.setTo( "users.netlab.cz" );
if (searchIQ != null)
iq.setTo(searchIQ.getFrom());
Form reply = form.createAnswerForm();
Iterator i = reply.getFields();
while (i.hasNext()) {
FormField field = (FormField) i.next();
if (field.getType().equals(FormField.TYPE_HIDDEN))
continue;
Object obj = (Control) dialogArea.getData(field.getVariable());
if (obj instanceof Text) {
reply.setAnswer(field.getVariable(), ((Text) obj)
.getText());
} else if (obj instanceof Button) {
reply.setAnswer(field.getVariable(), ((Button) obj)
.getSelection());
}
}
replyValue = reply;
if (searchIQ != null)
iq.addExtension(reply.getDataFormToSend());
if (searchIQ == null) {
// close();
return;
}
//System.out.println("SEND DATA " + iq.getChildElementXML());
// result = (SearchIQ)account.xmpp.sendIQPacketAndWaitForReply( iq);
new GOIMPacketListener(account.xmpp.getConnection(),
new PacketIDFilter(iq.getPacketID()), new TaskObserver()) {
public void processPacket(final Packet packet) {
final SearchIQ result = (SearchIQ) packet;
showResults( result );
}
}.registerListener();
account.xmpp.getConnection().sendPacket(iq);
}
private void showResults( SearchIQ results ){
Hashtable<String,String> columns = new Hashtable<String,String>();
Vector<Object> rowData = new Vector<Object>();
Iterator extensions = results.getExtensions();
while( extensions.hasNext() ){
DataForm dataForm = (DataForm)extensions.next();
ReportedData rData = dataForm.getReportedData();
Iterator fields = rData.getFields();
while( fields.hasNext() ){
FormField fField = (FormField)fields.next();
columns.put( fField.getVariable(), fField.getLabel() );
}
Iterator items = dataForm.getItems();
while( items.hasNext() ){
DataForm.Item item = (DataForm.Item)items.next();
int a = 0;
String [] strArray = new String[ columns.size() ];
Iterator field = item.getFields();
while( field.hasNext() ){
FormField formField = (FormField)field.next();
Iterator i = formField.getValues();
while( i.hasNext() ){
strArray[ a ] = i.next().toString();
a++;
}
}
rowData.add( strArray);
}
}
fillTableData( rowData );
}
private void fillTableData(final Vector rowData) {
parent.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));
}
}
});
}
public Form getResultForm() {
return replyValue;
}
}