cc.warlock.rcp.stormfront.ui.wizards.AccountWizardPage.java Source code

Java tutorial

Introduction

Here is the source code for cc.warlock.rcp.stormfront.ui.wizards.AccountWizardPage.java

Source

/**
 * Warlock, the open-source cross-platform game client
 *  
 * Copyright 2008, Warlock LLC, and individual contributors as indicated
 * by the @authors tag. 
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
/*
 * Created on Dec 31, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cc.warlock.rcp.stormfront.ui.wizards;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import cc.warlock.core.network.IConnection;
import cc.warlock.core.network.IConnection.ErrorType;
import cc.warlock.core.network.IConnectionListener;
import cc.warlock.core.settings.Account;
import cc.warlock.core.settings.AccountProvider;
import cc.warlock.core.settings.WarlockPreferencesScope;
import cc.warlock.core.stormfront.network.ISGEConnectionListener;
import cc.warlock.core.stormfront.network.ISGEGame;
import cc.warlock.core.stormfront.network.SGEConnection;
import cc.warlock.rcp.stormfront.adapters.SWTSGEConnectionListenerAdapter;
import cc.warlock.rcp.stormfront.ui.util.LoginUtil;
import cc.warlock.rcp.ui.ComboField;
import cc.warlock.rcp.ui.TextField;
import cc.warlock.rcp.ui.WarlockSharedImages;
import cc.warlock.rcp.ui.network.SWTConnectionListenerAdapter;
import cc.warlock.rcp.wizards.WizardPageWithNotification;

/**
 * @author Marshall
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class AccountWizardPage extends WizardPageWithNotification implements IConnectionListener {

    private SGEConnection connection;
    private ComboField account;
    private TextField password;
    private Listener listener;
    private SWTConnectionListenerAdapter connectionListener;
    private Account savedAccount;
    //private String accountName;

    public AccountWizardPage(SGEConnection connection) {
        super(WizardMessages.AccountWizardPage_title, WizardMessages.AccountWizardPage_description,
                WarlockSharedImages.getImageDescriptor(WarlockSharedImages.IMG_WIZBAN_WARLOCK));

        this.connection = connection;
        listener = new Listener();
        connectionListener = new SWTConnectionListenerAdapter(this);

        connection.addConnectionListener(connectionListener);
        connection.addSGEConnectionListener(new SWTSGEConnectionListenerAdapter(listener));
    }

    @Override
    public void createControl(Composite parent) {

        Composite controls = new Composite(parent, SWT.NONE);
        controls.setLayout(new GridLayout(1, false));

        new Label(controls, SWT.NONE).setText(WizardMessages.AccountWizardPage_label_accountName);
        account = new ComboField(controls, SWT.BORDER | SWT.DROP_DOWN);
        account.getCombo().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        new Label(controls, SWT.NONE).setText(WizardMessages.AccountWizardPage_label_password);
        password = new TextField(controls, SWT.BORDER);
        password.getTextControl().setEchoChar('*');
        password.getTextControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        setControl(controls);

        final Collection<Account> accounts = AccountProvider.getInstance().getSettings();
        for (Account account : accounts) {
            this.account.getCombo().add(account.getAccountName());
        }
        if (accounts.size() > 0) {
            account.getCombo().select(0);
            Account selectedAccount = AccountProvider.getInstance().getAccount(account.getCombo().getText());
            if (selectedAccount != null) {
                String passwdString = selectedAccount.getPassword();
                if (passwdString != null)
                    password.getTextControl().setText(passwdString);
            }

            account.getCombo().addSelectionListener(new SelectionListener() {
                public void widgetDefaultSelected(SelectionEvent e) {
                    widgetSelected(e);
                }

                public void widgetSelected(SelectionEvent e) {
                    String accountName = account.getCombo().getText();
                    password.getTextControl()
                            .setText(AccountProvider.getInstance().getAccount(accountName).getPassword());
                }
            });
        }
    }

    @Override
    public void pageExited(int button) {
        //      if (button == WizardWithNotification.NEXT)
        //      {   
        //accountName = account.getText();
        savedAccount = AccountProvider.getInstance().getAccount(account.getText());
        if (savedAccount == null) {
            boolean save = MessageDialog.openQuestion(Display.getDefault().getActiveShell(),
                    WizardMessages.AccountWizardPage_saveAccount_title,
                    WizardMessages.AccountWizardPage_saveAccount_description);

            if (save) {
                savedAccount = AccountProvider.getInstance().createSetting();
                savedAccount.setAccountName(account.getText());
                savedAccount.setPassword(password.getText());
                WarlockPreferencesScope.getInstance().flush();
            }
        }

        try {
            getContainer().run(false, true, new IRunnableWithProgress() {
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    listener.setProgressMonitor(monitor);

                    monitor.beginTask(WizardMessages.bind(WizardMessages.AccountWizardPage_progressMessage,
                            account.getText()), 4);
                    if (!connection.isConnected()) {
                        connection.connect();
                    } else {
                        connection.login(account.getText(), password.getText());
                    }
                    monitor.worked(1);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        //      }
    }

    private class Listener implements ISGEConnectionListener {
        private IProgressMonitor monitor;

        public void setProgressMonitor(IProgressMonitor monitor) {
            this.monitor = monitor;
        }

        @Override
        public void loginReady(SGEConnection connection) {
            if (monitor != null) {
                monitor.worked(1);
            }

            connection.login(account.getText(), password.getText());
        }

        @Override
        public void loginFinished(SGEConnection connection) {
            if (monitor != null) {
                monitor.worked(1);
            }
        }

        @Override
        public void sgeError(SGEConnection connection, int errorCode) {
            LoginUtil.showAuthenticationError(errorCode);
            getContainer().showPage(AccountWizardPage.this);
        }

        @Override
        public void gamesReady(SGEConnection connection, List<? extends ISGEGame> games) {
            if (monitor != null) {
                monitor.worked(1);
                monitor.done();
            }
        }

        @Override
        public void charactersReady(SGEConnection connection, Map<String, String> characters) {
            // TODO Auto-generated method stub

        }

        @Override
        public void readyToPlay(SGEConnection connection, Map<String, String> loginProperties) {
            // TODO Auto-generated method stub

        }
    }

    protected Text createTextWithLabel(Composite parent, String label) {
        new Label(parent, SWT.NONE).setText(label);
        return new Text(parent, SWT.BORDER);
    }

    public Account getSavedAccount() {
        return savedAccount;
    }

    @Override
    public void connectionError(IConnection connection, ErrorType errorType) {
        getWizard().getContainer().showPage(this);
        LoginUtil.showConnectionError(errorType);
    }

    @Override
    public void connected(IConnection connection) {
    }

    @Override
    public void disconnected(IConnection connection) {
    }

    @Override
    public void dataReady(IConnection connection, String data) {
    }

    @Override
    public void dataSent(IConnection connection, String data) {
        // TODO Auto-generated method stub

    }
}