/*
* 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.preferences;
import java.net.MalformedURLException;
import java.net.URL;
import net.sphene.goim.rcp.GOIMPlugin;
import net.sphene.goim.rcp.beans.GOIMAccount;
import net.sphene.goim.rcp.beans.GOIMAccountList;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.RowLayout;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
public class GOIMPreferencePageCreateUser {
private Shell sShell = null; // @jve:decl-index=0:visual-constraint="10,10"
private Shell parent = null;
private Label labelAccountName = null;
private GOIMAccount account = null;
private Label lblJabberId = null;
private Text jabberId = null;
private Label lblJabberDescription = null;
private Button lblJabberDescriptionURL = null;
private Label lblPassword = null;
private Text password = null;
private Label lblRepassword = null;
private Text repassword = null;
private Composite composite = null;
private Button buttonCancel = null;
private Button buttonRegister = null;
private Label lblStatus = null;
private Button useCustomHost = null;
private Text customHost = null;
private Composite customComposite = null;
private Text customPort = null;
private Label labelCustomPort = null;
private Button useSSL = null;
private Composite compositeParent;
public GOIMPreferencePageCreateUser() {
}
public GOIMPreferencePageCreateUser(Shell parent, GOIMAccount account) {
this.parent = parent;
this.account = account;
createSShell();
labelAccountName.setText(account.name);
labelAccountName.getParent().layout(true);
useCustomHost.setSelection(false);
customHost.setEnabled(false);
customPort.setEnabled(false);
sShell.open();
jabberId.setFocus();
}
public GOIMPreferencePageCreateUser(Composite parent, GOIMAccount account) {
this.compositeParent = parent;
this.account = account;
createCreateUser();
labelAccountName.setText(account.name);
labelAccountName.getParent().layout(true);
useCustomHost.setSelection(false);
customHost.setEnabled(false);
customPort.setEnabled(false);
jabberId.setFocus();
}
/**
* This method initializes sShell
*/
private void createSShell() {
if(parent == null) {
sShell = new Shell(SWT.APPLICATION_MODAL | SWT.SHELL_TRIM);
} else {
sShell = new Shell(parent,SWT.APPLICATION_MODAL | SWT.SHELL_TRIM);
}
sShell.setText("GOIM - Create New User");
compositeParent = sShell;
createCreateUser();
}
private void createCreateUser() {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
compositeParent.setLayout(gridLayout);
GridData gridData12 = new org.eclipse.swt.layout.GridData();
gridData12.grabExcessHorizontalSpace = true;
gridData12.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData12.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
GridData gridData23 = new org.eclipse.swt.layout.GridData();
gridData23.horizontalSpan = 2;
gridData23.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData23.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData11 = new org.eclipse.swt.layout.GridData();
gridData11.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData11.grabExcessHorizontalSpace = true;
gridData11.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData32 = new org.eclipse.swt.layout.GridData();
gridData32.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData32.grabExcessHorizontalSpace = true;
gridData32.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData22 = new org.eclipse.swt.layout.GridData();
gridData22.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData22.grabExcessHorizontalSpace = true;
gridData22.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData61 = new org.eclipse.swt.layout.GridData();
gridData61.horizontalSpan = 2;
gridData61.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData61.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData61.grabExcessHorizontalSpace = true;
GridData gridData5 = new org.eclipse.swt.layout.GridData();
gridData5.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData5.grabExcessHorizontalSpace = true;
gridData5.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData41 = new org.eclipse.swt.layout.GridData();
gridData41.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData41.grabExcessHorizontalSpace = true;
gridData41.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData31 = new org.eclipse.swt.layout.GridData();
gridData31.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData31.grabExcessHorizontalSpace = true;
gridData31.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData21 = new org.eclipse.swt.layout.GridData();
gridData21.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData21.grabExcessHorizontalSpace = true;
gridData21.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData1 = new org.eclipse.swt.layout.GridData();
gridData1.horizontalSpan = 2;
gridData1.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData1.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData1.grabExcessHorizontalSpace = true;
GridData gridData4 = new org.eclipse.swt.layout.GridData();
gridData4.horizontalSpan = 2;
gridData4.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData4.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData4.grabExcessHorizontalSpace = true;
GridData gridData3 = new org.eclipse.swt.layout.GridData();
gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData3.grabExcessHorizontalSpace = true;
gridData3.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData2 = new org.eclipse.swt.layout.GridData();
gridData2.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData2.grabExcessHorizontalSpace = true;
gridData2.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData = new org.eclipse.swt.layout.GridData();
gridData.horizontalSpan = 2;
gridData.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
labelAccountName = new Label(sShell, SWT.NONE);
labelAccountName.setText("Label");
lblJabberDescription = new Label(sShell, SWT.NONE);
lblJabberDescription.setText("Note: JabberId consists of username@server.com\nPublic servers include: jabber.org / amessage.info / etc.");
lblJabberDescription.setLayoutData(gridData4);
lblJabberDescriptionURL = new Button(sShell, SWT.FLAT);
lblJabberDescriptionURL.setText("View list of public servers at http://www.jabber.org");
lblJabberDescriptionURL.setLayoutData(gridData1);
lblJabberId = new Label(sShell, SWT.NONE);
lblJabberId.setText("Jabber Id");
lblJabberId.setLayoutData(gridData2);
jabberId = new Text(sShell, SWT.BORDER);
jabberId.setLayoutData(gridData3);
useSSL = new Button(sShell, SWT.CHECK);
useSSL.setText("Use SSL Connection");
useSSL.setLayoutData(gridData23);
useSSL.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
if(useSSL.getSelection())
customPort.setText(Integer.toString(GOIMAccount.DEFAULT_PORT_SSL));
else
customPort.setText(Integer.toString(GOIMAccount.DEFAULT_PORT));
}
});
jabberId.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
if(!useCustomHost.getSelection()) {
customHost.setText(StringUtils.parseServer(jabberId.getText()));
if(useSSL.getSelection())
customPort.setText(Integer.toString(GOIMAccount.DEFAULT_PORT_SSL));
else
customPort.setText(Integer.toString(GOIMAccount.DEFAULT_PORT));
}
}
});
useCustomHost = new Button(sShell, SWT.CHECK);
useCustomHost.setText("Connect To Custom Host");
useCustomHost.setLayoutData(gridData22);
useCustomHost
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
customHost.setEnabled(useCustomHost.getSelection());
customPort.setEnabled(useCustomHost.getSelection());
if(!useCustomHost.getSelection()) {
customHost.setText(StringUtils.parseServer(jabberId.getText()));
if(useSSL.getSelection())
customPort.setText(Integer.toString(GOIMAccount.DEFAULT_PORT_SSL));
else
customPort.setText(Integer.toString(GOIMAccount.DEFAULT_PORT));
}
}
});
GridLayout gridLayout2 = new GridLayout(2,false);
gridLayout2.numColumns = 3;
gridLayout2.horizontalSpacing = 5;
gridLayout2.marginHeight = 0;
gridLayout2.marginWidth = 0;
customComposite = new Composite(sShell, SWT.NULL);
customComposite.setLayout(gridLayout2);
customComposite.setLayoutData(gridData11);
customHost = new Text(customComposite, SWT.BORDER);
customHost.setLayoutData(gridData32);
labelCustomPort = new Label(customComposite, SWT.NONE);
labelCustomPort.setText(":");
customPort = new Text(customComposite, SWT.BORDER);
customPort.setLayoutData(gridData12);
lblPassword = new Label(sShell, SWT.NONE);
lblPassword.setText("Password");
lblPassword.setLayoutData(gridData21);
password = new Text(sShell, SWT.BORDER);
password.setEchoChar('*');
password.setLayoutData(gridData31);
lblRepassword = new Label(sShell, SWT.NONE);
lblRepassword.setText("Retype Password");
lblRepassword.setLayoutData(gridData41);
repassword = new Text(sShell, SWT.BORDER);
repassword.setEchoChar('*');
repassword.setLayoutData(gridData5);
lblJabberDescriptionURL
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
try {
PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL("http://www.jabber.org/network/"));
} catch (PartInitException e1) {
e1.printStackTrace();
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
});
labelAccountName.setLayoutData(gridData);
sShell.setText("Shell");
sShell.setMinimumSize(new org.eclipse.swt.graphics.Point(380,300));
sShell.pack();
createComposite();
lblStatus = new Label(sShell, SWT.NONE);
lblStatus.setText("Enter all Fields and click Register");
lblStatus.setLayoutData(gridData61);
}
/**
* This method initializes composite
*
*/
private void createComposite() {
RowLayout rowLayout = new RowLayout();
rowLayout.spacing = 14;
GridData gridData6 = new org.eclipse.swt.layout.GridData();
gridData6.horizontalSpan = 2;
gridData6.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData6.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
composite = new Composite(sShell, SWT.NONE);
composite.setLayoutData(gridData6);
composite.setLayout(rowLayout);
buttonRegister = new Button(composite, SWT.NONE);
buttonRegister.setText("Register");
buttonRegister
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
if(!password.getText().equals(repassword.getText())) {
MessageDialog.openWarning(sShell,"Passwords don't match","The two entered passwords don't match.");
return;
}
account.jid = jabberId.getText();
account.password = password.getText();
account.useSSL = useSSL.getSelection();
account.customPort = Integer.parseInt(customPort.getText());
if(useCustomHost.getSelection()) {
account.customHost = customHost.getText();
} else
account.customHost = null;
buttonRegister.setEnabled(false);
buttonCancel.setEnabled(false);
lblStatus.setText("Trying to register ...");
try {
account.xmpp.tryRegister();
MessageDialog.openInformation(sShell,"Successfully registered account.","Successfully registered user account with specified server.");
GOIMAccountList accountList = GOIMPlugin.getAccountList();
accountList.add(account);
sShell.close();
} catch (XMPPException e1) {
e1.printStackTrace();
ErrorDialog.openError(sShell,"Error while trying to register","An error occured while trying to create a user account on the specified server: " + e1.getLocalizedMessage(),new Status(IStatus.ERROR,GOIMPlugin.ID,IStatus.OK,"Error while trying to register on server.",e1));
lblStatus.setText("Error while trying to register.");
}
}
});
buttonCancel = new Button(composite, SWT.NONE);
buttonCancel.setText("Cancel");
buttonCancel
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
sShell.close();
}
});
}
}