FileDocCategorySizeDatePackage
KeySelectionPage.javaAPI DocAndroid 1.5 API9619Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.project.export

KeySelectionPage

public final class KeySelectionPage extends com.android.ide.eclipse.adt.project.export.ExportWizard.ExportWizardPage
Key Selection Page. This is used when an existing keystore is used.

Fields Summary
private final ExportWizard
mWizard
private org.eclipse.swt.widgets.Label
mKeyAliasesLabel
private org.eclipse.swt.widgets.Combo
mKeyAliases
private org.eclipse.swt.widgets.Label
mKeyPasswordLabel
private org.eclipse.swt.widgets.Text
mKeyPassword
private boolean
mDisableOnChange
private org.eclipse.swt.widgets.Button
mUseExistingKey
private org.eclipse.swt.widgets.Button
mCreateKey
Constructors Summary
protected KeySelectionPage(ExportWizard wizard, String pageName)


         
        super(pageName);
        mWizard = wizard;

        setTitle("Key alias selection");
        setDescription(""); // TODO
    
Methods Summary
public voidcreateControl(org.eclipse.swt.widgets.Composite parent)

        Composite composite = new Composite(parent, SWT.NULL);
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));
        GridLayout gl = new GridLayout(3, false);
        composite.setLayout(gl);

        GridData gd;

        mUseExistingKey = new Button(composite, SWT.RADIO);
        mUseExistingKey.setText("Use existing key");
        mUseExistingKey.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
        gd.horizontalSpan = 3;
        mUseExistingKey.setSelection(true);

        new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData());
        gd.heightHint = 0;
        gd.widthHint = 50;
        mKeyAliasesLabel = new Label(composite, SWT.NONE);
        mKeyAliasesLabel.setText("Alias:");
        mKeyAliases = new Combo(composite, SWT.READ_ONLY);
        mKeyAliases.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        
        new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData());
        gd.heightHint = 0;
        gd.widthHint = 50;
        mKeyPasswordLabel = new Label(composite, SWT.NONE);
        mKeyPasswordLabel.setText("Password:");
        mKeyPassword = new Text(composite, SWT.BORDER | SWT.PASSWORD);
        mKeyPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        mCreateKey = new Button(composite, SWT.RADIO);
        mCreateKey.setText("Create new key");
        mCreateKey.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
        gd.horizontalSpan = 3;

        // Show description the first time
        setErrorMessage(null);
        setMessage(null);
        setControl(composite);
        
        mUseExistingKey.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                mWizard.setKeyCreationMode(!mUseExistingKey.getSelection());
                enableWidgets();
                onChange();
            }
        });
        
        mKeyAliases.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                mWizard.setKeyAlias(mKeyAliases.getItem(mKeyAliases.getSelectionIndex()));
                onChange();
            }
        });
        
        mKeyPassword.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                mWizard.setKeyPassword(mKeyPassword.getText());
                onChange();
            }
        });
    
private voidenableWidgets()

        boolean useKey = !mWizard.getKeyCreationMode();
        mKeyAliasesLabel.setEnabled(useKey);
        mKeyAliases.setEnabled(useKey);
        mKeyPassword.setEnabled(useKey);
        mKeyPasswordLabel.setEnabled(useKey);
    
public org.eclipse.jface.wizard.IWizardPagegetNextPage()

        if (mWizard.getKeyCreationMode()) {
            return mWizard.getKeyCreationPage();
        }
        
        return mWizard.getKeyCheckPage();
    
public org.eclipse.jface.wizard.IWizardPagegetPreviousPage()

        return mWizard.getKeystoreSelectionPage();
    
private voidonChange()
Handles changes and update the error message and calls {@link #setPageComplete(boolean)}.

        if (mDisableOnChange) {
            return;
        }

        setErrorMessage(null);
        setMessage(null);

        if (mWizard.getKeyCreationMode() == false) {
            if (mKeyAliases.getSelectionIndex() == -1) {
                setErrorMessage("Select a key alias.");
                setPageComplete(false);
                return;
            }
    
            if (mKeyPassword.getText().trim().length() == 0) {
                setErrorMessage("Enter key password.");
                setPageComplete(false);
                return;
            }
        }

        setPageComplete(true);
    
voidonShow()

        // fill the texts with information loaded from the project.
        if ((mProjectDataChanged & (DATA_PROJECT | DATA_KEYSTORE)) != 0) {
            // disable onChange for now. we'll call it once at the end.
            mDisableOnChange = true;

            // reset the alias from the content of the project
            try {
                // reset to using a key
                mWizard.setKeyCreationMode(false);
                mUseExistingKey.setSelection(true);
                mCreateKey.setSelection(false);
                enableWidgets();

                // remove the content of the alias combo always and first, in case the
                // keystore password is wrong
                mKeyAliases.removeAll();

                // get the alias list (also used as a keystore password test)
                KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
                FileInputStream fis = new FileInputStream(mWizard.getKeystore());
                keyStore.load(fis, mWizard.getKeystorePassword().toCharArray());
                fis.close();
                
                Enumeration<String> aliases = keyStore.aliases();

                // get the alias from the project previous export, and look for a match as
                // we add the aliases to the combo.
                IProject project = mWizard.getProject();

                String keyAlias = ProjectHelper.loadStringProperty(project,
                        ExportWizard.PROPERTY_ALIAS);
                
                ArrayList<String> aliasList = new ArrayList<String>();

                int selection = -1;
                int count = 0;
                while (aliases.hasMoreElements()) {
                    String alias = aliases.nextElement();
                    mKeyAliases.add(alias);
                    aliasList.add(alias);
                    if (selection == -1 && alias.equalsIgnoreCase(keyAlias)) {
                        selection = count;
                    }
                    count++;
                }
                
                mWizard.setExistingAliases(aliasList);

                if (selection != -1) {
                    mKeyAliases.select(selection);

                    // since a match was found and is selected, we need to give it to
                    // the wizard as well
                    mWizard.setKeyAlias(keyAlias);
                } else {
                    mKeyAliases.clearSelection();
                }

                // reset the password
                mKeyPassword.setText(""); //$NON-NLS-1$

                // enable onChange, and call it to display errors and enable/disable pageCompleted.
                mDisableOnChange = false;
                onChange();
            } catch (KeyStoreException e) {
                onException(e);
            } catch (FileNotFoundException e) {
                onException(e);
            } catch (NoSuchAlgorithmException e) {
                onException(e);
            } catch (CertificateException e) {
                onException(e);
            } catch (IOException e) {
                onException(e);
            } finally {
                // in case we exit with an exception, we need to reset this
                mDisableOnChange = false;
            }
        }