package cz.cuni.mff.inetpaint.dialog;
import cz.cuni.mff.inetpaint.ConnectionManager;
import cz.cuni.mff.inetpaint.Program;
import cz.cuni.mff.inetpaint.UserPreferences;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.*;
import org.jivesoftware.smackx.packet.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import org.jivesoftware.smack.packet.Presence.Mode;
/**
* Dialog zobrazující formulář pro vytvoření nového účtu
* @author Jindřich Helcl
*/
public class CreateAccountDialog extends JDialog {
private static final Dimension SIZE = new Dimension(500, 600);
private JTextField serverAddressTF;
private JTextField usernameTF;
private JTextField firstNameTF;
private JTextField lastNameTF;
private JTextField emailTF;
private JTextField cityTF;
private JTextField countryTF;
private JTextField zipTF;
private JTextField phoneTF;
private JTextField urlTF;
private JPasswordField passwordTF;
private JPasswordField passwordCheckTF;
private JTextField usernameExistingTF;
private JPasswordField passwordExistingTF;
private JCheckBox savePasswordCB;
private JCheckBox savePasswordExistingCB;
private JCheckBox loginCB;
private JCheckBox loginExistingCB;
private JLabel lastNameLB;
private JLabel firstNameLB;
private JLabel emailLB;
private JLabel cityLB;
private JLabel countryLB;
private JLabel zipLB;
private JLabel phoneLB;
private JLabel urlLB;
private JLabel badPass;
private void initComponents() {
final ResourceBundle rb = Program.getStringsBundle();
setTitle(rb.getString("createAccTitle"));
setIconImage(Program.getImage("people_icon.png"));
setSize(SIZE);
setResizable(false);
serverAddressTF = new JTextField();
usernameTF = new JTextField();
lastNameTF = new JTextField();
firstNameTF = new JTextField();
emailTF = new JTextField();
cityTF = new JTextField();
countryTF = new JTextField();
zipTF = new JTextField();
phoneTF = new JTextField();
urlTF = new JTextField();
usernameExistingTF = new JTextField();
passwordTF = new JPasswordField();
passwordCheckTF = new JPasswordField();
passwordExistingTF = new JPasswordField();
savePasswordCB = new JCheckBox(rb.getString("createAccSavePass"));
savePasswordExistingCB = new JCheckBox(rb.getString("createAccSavePassEx"));
loginCB = new JCheckBox(rb.getString("createAccLogin"));
loginExistingCB = new JCheckBox(rb.getString("createAccLoginEx"));
lastNameLB = new JLabel(rb.getString("createAccLastName"));
firstNameLB = new JLabel(rb.getString("createAccFirstName"));
emailLB = new JLabel(rb.getString("createAccEMail"));
cityLB = new JLabel(rb.getString("createAccCity"));
countryLB = new JLabel(rb.getString("createAccCountry"));
zipLB = new JLabel(rb.getString("createAccZip"));
phoneLB = new JLabel(rb.getString("createAccPhone"));
urlLB = new JLabel(rb.getString("createAccURL"));
JLabel serverAddressLB = new JLabel(rb.getString("createAccServer"));
JLabel usernameLB = new JLabel(rb.getString("createAccUsername"));
JLabel passwordLB = new JLabel(rb.getString("createAccPassword"));
JLabel passwordCheckLB = new JLabel(rb.getString("createAccPasswordCheck"));
JLabel usernameExLB = new JLabel(rb.getString("createAccUsernameEx"));
JLabel passwordExLB = new JLabel(rb.getString("createAccPasswordEx"));
JLabel newAccountTitleText = new JLabel("<html>" + rb.getString("createAccNewTitleText") + "</html>");
JLabel newAccountCredentialsTitleText = new JLabel("<html>" + rb.getString("createAccCredTitleText") + "</html>");
JLabel newAccountDetailsTitleText = new JLabel("<html>" + rb.getString("createAccDetailsTitleText") + "</html>");
JLabel exAccountTitleText = new JLabel("<html>" + rb.getString("createAccExTitleText") + "</html>");
JButton useServerButton = new JButton(rb.getString("createAccSelectServerButton"));
useServerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectServer();
}
});
JButton cancelNewButton = new JButton(rb.getString("cancelButton"));
cancelNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
processWindowEvent(new WindowEvent(CreateAccountDialog.this, WindowEvent.WINDOW_CLOSING));
}
});
JButton createNewButton = new JButton(rb.getString("createAccCreateButton"));
createNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createAccount();
}
});
JButton cancelExButton = new JButton(rb.getString("cancelButton"));
cancelExButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
processWindowEvent(new WindowEvent(CreateAccountDialog.this, WindowEvent.WINDOW_CLOSING));
}
});
JButton createExButton = new JButton(rb.getString("createAccAddExAccount"));
createExButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createExistingAccount();
}
});
badPass = new JLabel(rb.getString("createAccPassNotMatch"));
badPass.setForeground(Color.red);
badPass.setVisible(false);
final Dimension sepSize = new Dimension(Short.MAX_VALUE, 5);
JSeparator sep0 = new JSeparator();
JSeparator sep1 = new JSeparator();
JSeparator sep2 = new JSeparator();
JSeparator sep3 = new JSeparator();
JSeparator sep4 = new JSeparator();
JSeparator sep5 = new JSeparator();
sep0.setMaximumSize(sepSize);
sep1.setMaximumSize(sepSize);
sep2.setMaximumSize(sepSize);
sep3.setMaximumSize(sepSize);
sep4.setMaximumSize(sepSize);
sep5.setMaximumSize(sepSize);
JPanel newAccount = new JPanel();
JPanel existingAccount = new JPanel();
GroupLayout layNew = new GroupLayout(newAccount);
GroupLayout layEx = new GroupLayout(existingAccount);
newAccount.setLayout(layNew);
existingAccount.setLayout(layEx);
layNew.setAutoCreateContainerGaps(true);
layNew.setAutoCreateGaps(true);
layEx.setAutoCreateContainerGaps(true);
layEx.setAutoCreateGaps(true);
layNew.setHorizontalGroup(layNew.createParallelGroup()
.addComponent(newAccountTitleText)
.addComponent(sep0)
.addComponent(serverAddressLB)
.addGroup(layNew.createSequentialGroup()
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(serverAddressTF)
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(useServerButton)
)
.addComponent(sep1)
.addComponent(newAccountCredentialsTitleText)
.addGroup(layNew.createSequentialGroup()
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(usernameLB)
.addComponent(passwordLB)
.addComponent(passwordCheckLB)
)
.addGap(13)
.addGroup(layNew.createParallelGroup()
.addComponent(usernameTF)
.addComponent(passwordTF)
.addComponent(passwordCheckTF)
)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layNew.createParallelGroup()
.addComponent(savePasswordCB)
.addComponent(loginCB)
.addComponent(badPass)
)
)
.addComponent(sep2)
.addComponent(newAccountDetailsTitleText)
.addGroup(layNew.createSequentialGroup()
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(firstNameLB)
.addComponent(lastNameLB)
.addComponent(emailLB)
.addComponent(cityLB)
.addComponent(countryLB)
.addComponent(zipLB)
.addComponent(phoneLB)
.addComponent(urlLB)
)
.addGap(20)
.addGroup(layNew.createParallelGroup()
.addComponent(firstNameTF)
.addComponent(lastNameTF)
.addComponent(emailTF)
.addComponent(cityTF)
.addComponent(countryTF)
.addComponent(zipTF)
.addComponent(phoneTF)
.addComponent(urlTF)
)
)
.addComponent(sep3)
.addGroup(layNew.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(cancelNewButton)
.addComponent(createNewButton)
)
);
layNew.setVerticalGroup(layNew.createSequentialGroup()
.addComponent(newAccountTitleText)
.addComponent(sep0)
.addComponent(serverAddressLB)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(serverAddressTF)
.addComponent(useServerButton)
)
.addComponent(sep1)
.addComponent(newAccountCredentialsTitleText)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(usernameLB)
.addComponent(usernameTF)
.addComponent(savePasswordCB)
)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(passwordLB)
.addComponent(passwordTF)
.addComponent(loginCB)
)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(passwordCheckLB)
.addComponent(passwordCheckTF)
.addComponent(badPass)
)
.addComponent(sep2)
.addComponent(newAccountDetailsTitleText)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(firstNameLB)
.addComponent(firstNameTF)
)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lastNameLB)
.addComponent(lastNameTF)
)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(emailLB)
.addComponent(emailTF)
)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(cityLB)
.addComponent(cityTF)
)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(countryLB)
.addComponent(countryTF)
)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(zipLB)
.addComponent(zipTF)
)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(phoneLB)
.addComponent(phoneTF)
)
.addGroup(layNew.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(urlLB)
.addComponent(urlTF)
)
.addComponent(sep3)
.addGroup(layNew.createParallelGroup()
.addComponent(cancelNewButton)
.addComponent(createNewButton)
)
);
layEx.setHorizontalGroup(layEx.createParallelGroup()
.addComponent(exAccountTitleText)
.addComponent(sep4)
.addGroup(layEx.createSequentialGroup()
.addGap(10)
.addGroup(layEx.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(usernameExLB)
.addComponent(passwordExLB)
)
.addGap(10)
.addGroup(layEx.createParallelGroup()
.addComponent(usernameExistingTF)
.addComponent(passwordExistingTF)
.addComponent(savePasswordExistingCB)
.addComponent(loginExistingCB)
)
)
.addComponent(sep5)
.addGroup(layEx.createSequentialGroup()
.addGap(0,0,Short.MAX_VALUE)
.addComponent(cancelExButton)
.addComponent(createExButton)
)
);
layEx.setVerticalGroup(layEx.createSequentialGroup()
.addComponent(exAccountTitleText)
.addComponent(sep4)
.addGroup(layEx.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(usernameExLB)
.addComponent(usernameExistingTF)
)
.addGroup(layEx.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(passwordExLB)
.addComponent(passwordExistingTF)
)
.addComponent(savePasswordExistingCB)
.addComponent(loginExistingCB)
.addComponent(sep5)
.addGroup(layEx.createParallelGroup()
.addComponent(cancelExButton)
.addComponent(createExButton)
)
);
JTabbedPane tabs = new JTabbedPane();
tabs.insertTab(rb.getString("createAccTabsNew"), null, newAccount, rb.getString("createAccTabsNewTT"), 0);
tabs.insertTab(rb.getString("createAccTabsEx"), null, existingAccount, rb.getString("createAccTabsExTT"), 1);
add(tabs);
}
/**
* Provede připojení k serveru a zjištění potřebných údajů pro vytvoření účtu.
*/
private void selectServer() {
final ResourceBundle rb = Program.getStringsBundle();
String server = serverAddressTF.getText();
XMPPConnection con = new XMPPConnection(server);
try {
con.connect();
}
catch(XMPPException e) {
e.printStackTrace(System.err);
JOptionPane.showMessageDialog(rootPane, rb.getString("errorNoConnectionOrBadServerName") , rb.getString("errorTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
AccountManager am = new AccountManager(con);
String str = am.getAccountInstructions();
if(str != null)
JOptionPane.showMessageDialog(rootPane, str, rb.getString("serverInfoTitle"), JOptionPane.INFORMATION_MESSAGE);
boolean jdeTo = am.supportsAccountCreation();
if(!jdeTo) {
JOptionPane.showMessageDialog(rootPane, rb.getString("warningCannotCreateAccountOnServer"), rb.getString("warningTitle"), JOptionPane.WARNING_MESSAGE);
}
for(String attr : am.getAccountAttributes()) {
firstNameLB.setForeground(attr.equalsIgnoreCase("first") ? Color.red : Color.black);
lastNameLB.setForeground(attr.equalsIgnoreCase("last") ? Color.red : Color.black);
cityLB.setForeground(attr.equalsIgnoreCase("city") ? Color.red : Color.black);
countryLB.setForeground(attr.equalsIgnoreCase("state") ? Color.red : Color.black);
phoneLB.setForeground(attr.equalsIgnoreCase("phone") ? Color.red : Color.black);
zipLB.setForeground(attr.equalsIgnoreCase("zip") ? Color.red : Color.black);
emailLB.setForeground(attr.equalsIgnoreCase("email") ? Color.red : Color.black);
urlLB.setForeground(attr.equalsIgnoreCase("url") ? Color.red : Color.black);
if(attr.equalsIgnoreCase("name")) {
firstNameLB.setForeground(Color.red);
lastNameLB.setForeground(Color.red);
}
//este neco udelat kdyz bude neco jinyho povinny
}
con.disconnect();
}
/**
* Provádí vytváření uživatelského účtu z dat formuláře.
*/
private void createAccount() {
final ResourceBundle rb = Program.getStringsBundle();
String server = serverAddressTF.getText();
String username = usernameTF.getText();
String password = new String(passwordTF.getPassword());
XMPPConnection con = new XMPPConnection(server);
try {
con.connect();
}
catch(XMPPException e) {
e.printStackTrace(System.err);
JOptionPane.showMessageDialog(rootPane, rb.getString("errorNoConnectionOrBadServerName") , rb.getString("errorTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
AccountManager am = new AccountManager(con);
Map<String, String> attributes = new HashMap<String, String>(20);
String first = firstNameTF.getText();
String last = lastNameTF.getText();
String email = emailTF.getText();
String city = cityTF.getText();
String country = countryTF.getText();
String zip = zipTF.getText();
String phone = phoneTF.getText();
String url = urlTF.getText();
String name = first + " " + last;
attributes.put("first", first);
attributes.put("last", last);
attributes.put("email", email);
attributes.put("city", city);
attributes.put("state", country);
attributes.put("zip", zip);
attributes.put("phone", phone);
attributes.put("url", url);
attributes.put("name", name);
try {
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
am.createAccount(username, password, attributes);
}
catch(XMPPException xm) {
xm.printStackTrace(System.err);
JOptionPane.showMessageDialog(rootPane, rb.getString("errorCreatingAccount"), rb.getString("errorTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
JOptionPane.showMessageDialog(rootPane, rb.getString("createAccAccountCreated"), rb.getString("successfulOperation"), JOptionPane.INFORMATION_MESSAGE);
try {
con.login(username, password);
// vytvořit vizitku..
VCard vc = new VCard();
vc.setFirstName(first);
vc.setLastName(last);
vc.setEmailHome(email);
vc.setAddressFieldHome("LOCALITY", city);
vc.setAddressFieldHome("POSTAL", zip);
vc.setAddressFieldHome("PCODE", zip);
vc.setAddressFieldHome("CTRY", country);
vc.setPhoneHome("VOICE", phone);
vc.setField("URL", url);
vc.setJabberId(username + "@" + server);
vc.save(con);
}
catch (XMPPException x) {
System.err.println("Nepodarilo se ulozit vizitku");
JOptionPane.showMessageDialog(rootPane, rb.getString("errorMessage"), rb.getString("errorTitle"), JOptionPane.ERROR_MESSAGE);
x.printStackTrace(System.err);
}
// podarilo se. ted jen zalozit lokalni profil a treba logout + login, a zavrit okno.
addExistingAccount(username, server, password, savePasswordCB.isSelected());
if(loginCB.isSelected()) {
ConnectionManager man = ConnectionManager.getInstance();
if(man.isLoggedIn()) {
man.disconnect();
}
man.login(username, server, password, "", Mode.available);
}
processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
/**
* Provádí vytvoření nového uživatelského profilu v aplikaci
* z existujícího účtu na serveru.
*/
private void createExistingAccount() {
final ResourceBundle rb = Program.getStringsBundle();
String JID = usernameExistingTF.getText();
String password = new String(passwordExistingTF.getPassword());
// kontrola udaju:
if(!JID.matches(Program.REGEX_VALID_JID)) {
JOptionPane.showMessageDialog(rootPane, rb.getString("errorInvalidJID"),rb.getString("errorTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
String username = StringUtils.parseName(JID);
String server = StringUtils.parseServer(JID);
XMPPConnection con = new XMPPConnection(server);
try {
con.connect();
}
catch(XMPPException x) {
x.printStackTrace(System.err);
JOptionPane.showMessageDialog(rootPane, rb.getString("errorNoConnection"), rb.getString("errorTitle"), JOptionPane.ERROR_MESSAGE);
}
try {
con.login(username, password);
}
catch(XMPPException x) {
x.printStackTrace(System.err);
JOptionPane.showMessageDialog(rootPane, rb.getString("errorAddingAccountPass"), rb.getString("errorTitle"), JOptionPane.ERROR_MESSAGE);
}
con.disconnect();
addExistingAccount(username, server, password, savePasswordExistingCB.isSelected());
processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
if(loginExistingCB.isSelected()) {
ConnectionManager man = ConnectionManager.getInstance();
if(man.isLoggedIn()) {
man.disconnect();
}
man.login(username, server, password, "", Mode.available);
}
}
private void addExistingAccount(String username, String server, String password, boolean savePass) {
String JID = username + "@" + server;
UserPreferences.setUserNode(JID, true);
UserPreferences.setSavePassword(savePass);
UserPreferences.setAutoLogin(false);
UserPreferences.setPassword(savePass ? password : "");
UserPreferences.setLastAcconunt(UserPreferences.userNode.name());
}
/**
* Vytvoří novou instanci dialogu.
* @param owner Rodičovské okno
*/
public CreateAccountDialog(Frame owner) {
super(owner, true);
initComponents();
}
}