/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.plugin.gibberishaccregwizz;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.protocol.*;
/**
* The FirstWizardPage is the page, where user could enter the user ID
* and the password of the account.
*
* @author Emil Ivov
*/
public class FirstWizardPage
extends JPanel implements WizardPage, DocumentListener
{
public static final String FIRST_PAGE_IDENTIFIER = "FirstPageIdentifier";
private JPanel userPassPanel = new JPanel(new BorderLayout(10, 10));
private JPanel labelsPanel = new JPanel();
private JPanel valuesPanel = new JPanel();
private JLabel userID = new JLabel(Resources.getString("userID"));
private JLabel passLabel = new JLabel(Resources.getString("password"));
private JLabel existingAccountLabel
= new JLabel(Resources.getString("existingAccount"));
private JPanel emptyPanel = new JPanel();
private JLabel userIDExampleLabel = new JLabel("Ex: random.user.name");
private JTextField userIDField = new JTextField();
private JPasswordField passField = new JPasswordField();
private JCheckBox rememberPassBox = new JCheckBox(
Resources.getString("rememberPassword"));
private JPanel mainPanel = new JPanel();
private Object nextPageIdentifier = WizardPage.SUMMARY_PAGE_IDENTIFIER;
private GibberishAccountRegistrationWizard wizard;
/**
* Creates an instance of FirstWizardPage.
* @param registration the GibberishAccountRegistration, where
* all data through the wizard are stored
* @param wizard the parent wizard
*/
public FirstWizardPage(GibberishAccountRegistrationWizard wizard)
{
super(new BorderLayout());
this.wizard = wizard;
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
this.init();
this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.labelsPanel.setLayout(new BoxLayout(labelsPanel, BoxLayout.Y_AXIS));
this.valuesPanel.setLayout(new BoxLayout(valuesPanel, BoxLayout.Y_AXIS));
}
/**
* Initializes all panels, buttons, etc.
*/
private void init()
{
this.userIDField.getDocument().addDocumentListener(this);
this.rememberPassBox.setSelected(true);
this.existingAccountLabel.setForeground(Color.RED);
this.userIDExampleLabel.setForeground(Color.GRAY);
this.userIDExampleLabel.setFont(
userIDExampleLabel.getFont().deriveFont(8));
this.emptyPanel.setMaximumSize(new Dimension(40, 35));
this.userIDExampleLabel.setBorder(
BorderFactory.createEmptyBorder(0, 0, 8,0));
labelsPanel.add(userID);
labelsPanel.add(emptyPanel);
labelsPanel.add(passLabel);
valuesPanel.add(userIDField);
valuesPanel.add(userIDExampleLabel);
valuesPanel.add(passField);
userPassPanel.add(labelsPanel, BorderLayout.WEST);
userPassPanel.add(valuesPanel, BorderLayout.CENTER);
userPassPanel.add(rememberPassBox, BorderLayout.SOUTH);
userPassPanel.setBorder(BorderFactory
.createTitledBorder(Resources.getString(
"userAndPassword")));
this.add(userPassPanel, BorderLayout.NORTH);
}
/**
* Implements the WizardPage.getIdentifier
to return
* this page identifier.
*
* @return the Identifier of the first page in this wizard.
*/
public Object getIdentifier()
{
return FIRST_PAGE_IDENTIFIER;
}
/**
* Implements the WizardPage.getNextPageIdentifier
to return
* the next page identifier - the summary page.
*
* @return the identifier of the page following this one.
*/
public Object getNextPageIdentifier()
{
return nextPageIdentifier;
}
/**
* Implements the WizardPage.getBackPageIdentifier
to return
* the next back identifier - the default page.
*
* @return the identifier of the default wizard page.
*/
public Object getBackPageIdentifier()
{
return WizardPage.DEFAULT_PAGE_IDENTIFIER;
}
/**
* Implements the WizardPage.getWizardForm
to return
* this panel.
*
* @return the component to be displayed in this wizard page.
*/
public Object getWizardForm()
{
return this;
}
/**
* Before this page is displayed enables or disables the "Next" wizard
* button according to whether the UserID field is empty.
*/
public void pageShowing()
{
this.setNextButtonAccordingToUserID();
}
/**
* Saves the user input when the "Next" wizard buttons is clicked.
*/
public void pageNext()
{
String userID = userIDField.getText();
if (!wizard.isModification() && isExistingAccount(userID))
{
nextPageIdentifier = FIRST_PAGE_IDENTIFIER;
userPassPanel.add(existingAccountLabel, BorderLayout.NORTH);
this.revalidate();
}
else
{
nextPageIdentifier = SUMMARY_PAGE_IDENTIFIER;
userPassPanel.remove(existingAccountLabel);
GibberishAccountRegistration registration
= wizard.getRegistration();
registration.setUserID(userIDField.getText());
if (passField.getPassword() != null)
registration.setPassword(new String(passField.getPassword()));
registration.setRememberPassword(rememberPassBox.isSelected());
}
}
/**
* Enables or disables the "Next" wizard button according to whether the
* User ID field is empty.
*/
private void setNextButtonAccordingToUserID()
{
if (userIDField.getText() == null || userIDField.getText().equals(""))
{
wizard.getWizardContainer().setNextFinishButtonEnabled(false);
}
else
{
wizard.getWizardContainer().setNextFinishButtonEnabled(true);
}
}
/**
* Handles the DocumentEvent triggered when user types in the
* User ID field. Enables or disables the "Next" wizard button according to
* whether the User ID field is empty.
*
* @param event the event containing the update.
*/
public void insertUpdate(DocumentEvent event)
{
this.setNextButtonAccordingToUserID();
}
/**
* Handles the DocumentEvent triggered when user deletes letters
* from the UserID field. Enables or disables the "Next" wizard button
* according to whether the UserID field is empty.
*
* @param event the event containing the update.
*/
public void removeUpdate(DocumentEvent event)
{
this.setNextButtonAccordingToUserID();
}
public void changedUpdate(DocumentEvent event)
{
}
public void pageHiding()
{
}
public void pageShown()
{
}
public void pageBack()
{
}
/**
* Fills the UserID and Password fields in this panel with the data comming
* from the given protocolProvider.
* @param protocolProvider The ProtocolProviderService to load the
* data from.
*/
public void loadAccount(ProtocolProviderService protocolProvider)
{
AccountID accountID = protocolProvider.getAccountID();
String password = (String) accountID.getAccountProperties()
.get(ProtocolProviderFactory.PASSWORD);
this.userIDField.setEnabled(false);
this.userIDField.setText(accountID.getUserID());
if (password != null)
{
this.passField.setText(password);
this.rememberPassBox.setSelected(true);
}
}
/**
* Verifies whether there is already an account installed with the same
* details as the one that the user has just entered.
*
* @param userID the name of the user that the account is registered for
* @return true if there is already an account for this userID and false
* otherwise.
*/
private boolean isExistingAccount(String userID)
{
ProtocolProviderFactory factory
= GibberishAccRegWizzActivator.getGibberishProtocolProviderFactory();
ArrayList registeredAccounts = factory.getRegisteredAccounts();
for (int i = 0; i < registeredAccounts.size(); i++)
{
AccountID accountID = (AccountID) registeredAccounts.get(i);
if (userID.equalsIgnoreCase(accountID.getUserID()))
{
return true;
}
}
return false;
}
}