/*
* 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 net.sphene.goim.rcp.beans.GOIMAccount;
import net.sphene.goim.rcp.xmpp.util.JabberErrorConditionMapping;
import org.eclipse.swt.SWT;
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.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.packet.VCard;
public class vCardGUI {
private VCard vCard = new VCard();
private Shell parent = null;
private Shell vCShell = null;
private Composite compositeTop = null;
private Composite compositeMiddle = null;
private Composite compositeBottom = null;
Label messageField = null;
private Label labelFirstName = null;
private Label labelMiddleName = null;
private Label labelLastName = null;
private Label labelEmailHome = null;
private Label labelEmailWork = null;
private Label labelJabberId = null;
private Label labelOrganization = null;
private Label labelOrganizationUnit = null;
private Label labelNickname = null;
private Text textFirstName = null;
private Text textMiddleName = null;
private Text textLastName = null;
private Text textNickname = null;
private Text textOrganization = null;
private Text textOrganizationUnit = null;
private Text textEmailHome = null;
private Text textEmailWork = null;
private Text textJabberId = null;
private Button buttonOK = null;
private Button buttonCancel = null;
private Button buttonRetrieve = null;
private GOIMAccount account = null;
private String targetJID;
private Label labelBirthday;
private Text textBirthday;
private Label labelHomepage;
private Text textHomepage;
private Text textAbout;
private Text textPhoneNumberHome;
private Label labelPhoneNumberHome;
private Label labelFullName;
private Text textFullName;
//View and/or Edit your VCard
public vCardGUI(GOIMAccount gAccount) {
createGUI(gAccount, gAccount.jid);
setFieldsEditable(true);
buttonCancel.setVisible(true);
buttonOK.setText("Save");
buttonOK.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
saveVCard();
//vCShell.dispose();
}
});
buttonCancel.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
vCShell.dispose();
}
});
}
public vCardGUI(GOIMAccount gAccount, String user, Shell parent) {
this.parent = parent;
createGUI(gAccount, user);
vCShell.setText("View " + user + "'s vCard");
buttonOK.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
vCShell.dispose();
}
});
}
public vCardGUI(GOIMAccount gAccount, String user) {
this(gAccount,user,null);
}
//View the VCard of another jabber user
private void createGUI(GOIMAccount gAccount, String user) {
account = gAccount;
targetJID = user;
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.marginLeft = 20;
layout.marginRight = 20;
layout.marginTop = 20;
layout.marginBottom = 20;
layout.horizontalSpacing = 0;
if(parent==null)
vCShell = new Shell(SWT.DIALOG_TRIM | SWT.RESIZE);
else
vCShell = new Shell(parent,SWT.DIALOG_TRIM | SWT.RESIZE);
vCShell.setText("Edit VCard Information");
vCShell.setLayout(layout);
//vCShell.setSize(new Point(400,440));
layout = new GridLayout();
layout.numColumns = 1;
//Top composite contains text message
compositeTop = new Composite(vCShell, SWT.NONE);
compositeTop.setLayout(layout);
layout = new GridLayout();
layout.numColumns = 2;
//Middle composite contains labels and fields
compositeMiddle = new Composite(vCShell, SWT.NONE);
compositeMiddle.setLayout(layout);
layout = new GridLayout();
layout.numColumns = 3;
//bottom composite contains buttons
compositeBottom = new Composite(vCShell, SWT.NONE);
compositeBottom.setLayout(layout);
messageField = new Label(compositeTop, SWT.CENTER);
messageField.setText("test field");
GridData data = new GridData();
data.widthHint = 145;
// Full Name
labelFullName = new Label(compositeMiddle, SWT.NONE);
labelFullName.setText("Full Name (Not Editable)");
labelFullName.setLayoutData(data);
textFullName = new Text(compositeMiddle, SWT.BORDER | SWT.READ_ONLY);
//First Name
labelFirstName = new Label(compositeMiddle, SWT.NONE);
labelFirstName.setText("First Name");
labelFirstName.setLayoutData(data);
textFirstName = new Text(compositeMiddle, SWT.BORDER);
//Middle Name
labelMiddleName = new Label(compositeMiddle, SWT.NONE);
labelMiddleName.setText("Middle Name");
labelMiddleName.setLayoutData(data);
textMiddleName = new Text(compositeMiddle, SWT.BORDER);
//Last Name
labelLastName = new Label(compositeMiddle, SWT.NONE);
labelLastName.setText("Last Name");
labelLastName.setLayoutData(data);
textLastName = new Text(compositeMiddle, SWT.BORDER);
//Nickname
labelNickname = new Label(compositeMiddle, SWT.NONE);
labelNickname.setText("Nickname");
labelNickname.setLayoutData(data);
textNickname = new Text(compositeMiddle, SWT.BORDER);
// Birthday
labelBirthday = new Label(compositeMiddle, SWT.NONE);
labelBirthday.setText("Birthday");
labelBirthday.setLayoutData(data);
textBirthday = new Text(compositeMiddle, SWT.BORDER);
//eMail
labelEmailHome = new Label(compositeMiddle, SWT.NONE);
labelEmailHome.setText("Email Address (Home)");
labelEmailHome.setLayoutData(data);
textEmailHome = new Text(compositeMiddle, SWT.BORDER);
// Homepage
labelHomepage = new Label(compositeMiddle, SWT.NONE);
labelHomepage.setText("Homepage URL");
labelHomepage.setLayoutData(data);
textHomepage = new Text(compositeMiddle, SWT.BORDER);
// Phone Number (Home)
labelPhoneNumberHome = new Label(compositeMiddle, SWT.NONE);
labelPhoneNumberHome.setText("Phone # (Home)");
labelPhoneNumberHome.setLayoutData(data);
textPhoneNumberHome = new Text(compositeMiddle,SWT.BORDER);
//Organization
labelOrganization = new Label(compositeMiddle, SWT.NONE);
labelOrganization.setText("Organization");
labelOrganization.setLayoutData(data);
textOrganization = new Text(compositeMiddle, SWT.BORDER);
//Organization
labelOrganizationUnit = new Label(compositeMiddle, SWT.NONE);
labelOrganizationUnit.setText("Organization Unit");
labelOrganizationUnit.setLayoutData(data);
textOrganizationUnit = new Text(compositeMiddle, SWT.BORDER);
//eMail
labelEmailWork = new Label(compositeMiddle, SWT.NONE);
labelEmailWork.setText("Email Address (Work)");
labelEmailWork.setLayoutData(data);
textEmailWork = new Text(compositeMiddle, SWT.BORDER);
//JabberId
labelJabberId = new Label(compositeMiddle, SWT.NONE);
labelJabberId.setText("Jabber ID");
labelJabberId.setLayoutData(data);
textJabberId = new Text(compositeMiddle, SWT.BORDER);
textJabberId.setText(account.jid);
// About Field
Group groupAbout = new Group(compositeMiddle,SWT.SHADOW_IN);
groupAbout.setText("About");
groupAbout.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,1));
groupAbout.setLayout(new GridLayout(1,true));
textAbout = new Text(groupAbout,SWT.MULTI | SWT.BORDER);
GridData textAboutData = new GridData(SWT.FILL,SWT.FILL,true,true);
textAboutData.minimumHeight = 80;
textAbout.setLayoutData(textAboutData);
//Make the text fields stretch to the
//right side of the window
data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = 170;
textFirstName.setLayoutData(data);
textMiddleName.setLayoutData(data);
textLastName.setLayoutData(data);
textNickname.setLayoutData(data);
textOrganization.setLayoutData(data);
textOrganizationUnit.setLayoutData(data);
textEmailHome.setLayoutData(data);
textEmailWork.setLayoutData(data);
textJabberId.setLayoutData(data);
textBirthday.setLayoutData(data);
textHomepage.setLayoutData(data);
textPhoneNumberHome.setLayoutData(data);
textFullName.setLayoutData(data);
//Get the existing VCard from server if there is one
loadVCard(user);
setFieldsEditable(false);
//Buttons
data = new GridData(GridData.CENTER);
data.widthHint = 105;
buttonOK = new Button(compositeBottom, SWT.PUSH);
buttonOK.setText("Done");
buttonOK.setLayoutData(data);
buttonCancel = new Button(compositeBottom, SWT.PUSH);
buttonCancel.setText("Close");
buttonCancel.setLayoutData(data);
buttonCancel.setVisible(false);
buttonRetrieve = new Button(compositeBottom, SWT.PUSH);
buttonRetrieve.setText("Re-Retrieve");
buttonRetrieve.setLayoutData(data);
buttonRetrieve.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
loadVCard(targetJID);
}
});
vCShell.pack();
vCShell.setVisible(true);
}
// //Get the existing VCard from server if there is one
// private void loadVCard() {
// loadVCard(account.jid);
// }
//Get the existing VCard from server if there is one
private void loadVCard(final String user) {
messageField.setText("Retrieving vCard information ...");
new Thread() {
public void run() {
try{
VCard newVCard = new VCard();
newVCard.load(account.xmpp.getConnection(), user);
vCard = newVCard;
System.out.println("vCard Loaded Successfully");
vCShell.getDisplay().asyncExec(new Runnable() {
public void run() {
messageField.setText("vCard Retrieved Successfully");
fillVCardData();
}
});
} catch (final XMPPException e) {
System.out.println(e.getMessage());
e.printStackTrace();
vCShell.getDisplay().asyncExec(new Runnable() {
public void run() {
messageField.setText("Error Retrieving vCard: " + e.toString() + (e.getXMPPError() == null ? "" : (" - " + JabberErrorConditionMapping.getMeaning(e.getXMPPError().getCode()))));
messageField.getParent().getParent().layout(true);
}
});
}
}
}.start();
}
private void fillVCardData() {
if (vCard.getFirstName() != null)
textFirstName.setText(vCard.getFirstName());
else
textFirstName.setText("");
if (vCard.getMiddleName() != null)
textMiddleName.setText(vCard.getMiddleName());
else
textMiddleName.setText("");
if (vCard.getLastName() != null)
textLastName.setText(vCard.getLastName());
else
textLastName.setText("");
if (vCard.getNickName() != null)
textNickname.setText(vCard.getNickName());
else
textNickname.setText("");
if (vCard.getOrganization() != null)
textOrganization.setText(vCard.getOrganization());
else
textOrganization.setText("");
if (vCard.getOrganizationUnit() != null)
textOrganizationUnit.setText(vCard.getOrganizationUnit());
else
textOrganizationUnit.setText("");
if (vCard.getEmailHome() != null)
textEmailHome.setText(vCard.getEmailHome());
else
textEmailHome.setText("");
if (vCard.getEmailWork() != null)
textEmailWork.setText(vCard.getEmailWork());
else
textEmailWork.setText("");
if (vCard.getJabberId() != null)
textJabberId.setText(vCard.getJabberId());
else
textJabberId.setText("");
if (vCard.getField("BDAY") != null)
textBirthday.setText(vCard.getField("BDAY"));
else
textBirthday.setText("");
if (vCard.getField("BDAY") != null)
textHomepage.setText(vCard.getField("URL"));
else
textHomepage.setText("");
if(vCard.getPhoneHome("VOICE") != null)
textPhoneNumberHome.setText(vCard.getPhoneHome("VOICE"));
else
textPhoneNumberHome.setText("");
if(vCard.getField("DESC") != null)
textAbout.setText(vCard.getField("DESC"));
else
textAbout.setText("");
if(vCard.getField("FN") != null)
textFullName.setText(vCard.getField("FN"));
else
textFullName.setText(buildFullName(vCard.getFirstName(),vCard.getMiddleName(),vCard.getLastName()));
}
private String buildFullName(String firstName, String middleName, String lastName) {
StringBuffer buf = new StringBuffer();
if(firstName != null && !firstName.equals(""))
buf.append(firstName);
if(middleName != null && !middleName.equals("")) {
if(buf.length() > 0)
buf.append(' ');
buf.append(middleName);
}
if(lastName != null && !lastName.equals("")) {
if(buf.length() > 0)
buf.append(' ');
buf.append(lastName);
}
return buf.toString();
}
//Send current vCard to server
private void saveVCard() {
vCard = new VCard();
vCard.setFirstName(textFirstName.getText());
vCard.setMiddleName(textMiddleName.getText());
vCard.setLastName(textLastName.getText());
vCard.setNickName(textNickname.getText());
vCard.setOrganization(textOrganization.getText());
vCard.setOrganizationUnit(textOrganizationUnit.getText());
vCard.setEmailHome(textEmailHome.getText());
vCard.setEmailWork(textEmailWork.getText());
vCard.setJabberId(textJabberId.getText());
vCard.setField("BDAY",textBirthday.getText());
vCard.setField("URL",textHomepage.getText());
vCard.setPhoneHome("VOICE",textPhoneNumberHome.getText());
vCard.setField("DESC",textAbout.getText());
vCard.setField("FN",buildFullName(vCard.getFirstName(),vCard.getMiddleName(),vCard.getLastName()));
messageField.setText("Saving vCard information.");
new Thread() {
public void run() {
try {
vCard.save(account.xmpp.getConnection());
} catch (XMPPException e) {
// TODO add error dialog
e.printStackTrace();
}
System.out.println("VCard Transmitted Successfully");
vCShell.getDisplay().asyncExec(new Runnable() {
public void run() {
messageField.setText("Successfully saved vCard.");
}
});
}
}.start();
}
private void setFieldsEditable(boolean b) {
if (b == false) {
textFirstName.setEditable(false);
textMiddleName.setEditable(false);
textLastName.setEditable(false);
textNickname.setEditable(false);
textOrganization.setEditable(false);
textOrganizationUnit.setEditable(false);
textEmailHome.setEditable(false);
textEmailWork.setEditable(false);
textJabberId.setEditable(false);
textBirthday.setEditable(false);
textHomepage.setEditable(false);
textPhoneNumberHome.setEditable(false);
textAbout.setEditable(false);
} else {
textFirstName.setEditable(true);
textMiddleName.setEditable(true);
textLastName.setEditable(true);
textNickname.setEditable(true);
textOrganization.setEditable(true);
textOrganizationUnit.setEditable(true);
textEmailHome.setEditable(true);
textEmailWork.setEditable(true);
textJabberId.setEditable(true);
textBirthday.setEditable(true);
textHomepage.setEditable(true);
textPhoneNumberHome.setEditable(true);
textAbout.setEditable(true);
}
}
}