2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.eclipse.org/org/documents/epl-v10.php
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.ide.eclipse.adt.internal.wizards.export;
19 import com.android.ide.eclipse.adt.internal.project.ProjectHelper;
20 import com.android.ide.eclipse.adt.internal.wizards.export.ExportWizard.ExportWizardPage;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.jface.wizard.IWizardPage;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Combo;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Text;
37 import java.io.FileInputStream;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import java.security.KeyStore;
41 import java.security.KeyStoreException;
42 import java.security.NoSuchAlgorithmException;
43 import java.security.cert.CertificateException;
44 import java.util.ArrayList;
45 import java.util.Enumeration;
48 * Key Selection Page. This is used when an existing keystore is used.
50 final class KeySelectionPage extends ExportWizardPage {
52 private final ExportWizard mWizard;
53 private Label mKeyAliasesLabel;
54 private Combo mKeyAliases;
55 private Label mKeyPasswordLabel;
56 private Text mKeyPassword;
57 private boolean mDisableOnChange = false;
58 private Button mUseExistingKey;
59 private Button mCreateKey;
61 protected KeySelectionPage(ExportWizard wizard, String pageName) {
65 setTitle("Key alias selection");
66 setDescription(""); // TODO
69 public void createControl(Composite parent) {
70 Composite composite = new Composite(parent, SWT.NULL);
71 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
72 GridLayout gl = new GridLayout(3, false);
73 composite.setLayout(gl);
77 mUseExistingKey = new Button(composite, SWT.RADIO);
78 mUseExistingKey.setText("Use existing key");
79 mUseExistingKey.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
80 gd.horizontalSpan = 3;
81 mUseExistingKey.setSelection(true);
83 new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData());
86 mKeyAliasesLabel = new Label(composite, SWT.NONE);
87 mKeyAliasesLabel.setText("Alias:");
88 mKeyAliases = new Combo(composite, SWT.READ_ONLY);
89 mKeyAliases.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
91 new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData());
94 mKeyPasswordLabel = new Label(composite, SWT.NONE);
95 mKeyPasswordLabel.setText("Password:");
96 mKeyPassword = new Text(composite, SWT.BORDER | SWT.PASSWORD);
97 mKeyPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
99 mCreateKey = new Button(composite, SWT.RADIO);
100 mCreateKey.setText("Create new key");
101 mCreateKey.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
102 gd.horizontalSpan = 3;
104 // Show description the first time
105 setErrorMessage(null);
107 setControl(composite);
109 mUseExistingKey.addSelectionListener(new SelectionAdapter() {
111 public void widgetSelected(SelectionEvent e) {
112 mWizard.setKeyCreationMode(!mUseExistingKey.getSelection());
118 mKeyAliases.addSelectionListener(new SelectionAdapter() {
120 public void widgetSelected(SelectionEvent e) {
121 mWizard.setKeyAlias(mKeyAliases.getItem(mKeyAliases.getSelectionIndex()));
126 mKeyPassword.addModifyListener(new ModifyListener() {
127 public void modifyText(ModifyEvent e) {
128 mWizard.setKeyPassword(mKeyPassword.getText());
136 // fill the texts with information loaded from the project.
137 if ((mProjectDataChanged & (DATA_PROJECT | DATA_KEYSTORE)) != 0) {
138 // disable onChange for now. we'll call it once at the end.
139 mDisableOnChange = true;
141 // reset the alias from the content of the project
143 // reset to using a key
144 mWizard.setKeyCreationMode(false);
145 mUseExistingKey.setSelection(true);
146 mCreateKey.setSelection(false);
149 // remove the content of the alias combo always and first, in case the
150 // keystore password is wrong
151 mKeyAliases.removeAll();
153 // get the alias list (also used as a keystore password test)
154 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
155 FileInputStream fis = new FileInputStream(mWizard.getKeystore());
156 keyStore.load(fis, mWizard.getKeystorePassword().toCharArray());
159 Enumeration<String> aliases = keyStore.aliases();
161 // get the alias from the project previous export, and look for a match as
162 // we add the aliases to the combo.
163 IProject project = mWizard.getProject();
165 String keyAlias = ProjectHelper.loadStringProperty(project,
166 ExportWizard.PROPERTY_ALIAS);
168 ArrayList<String> aliasList = new ArrayList<String>();
172 while (aliases.hasMoreElements()) {
173 String alias = aliases.nextElement();
174 mKeyAliases.add(alias);
175 aliasList.add(alias);
176 if (selection == -1 && alias.equalsIgnoreCase(keyAlias)) {
182 mWizard.setExistingAliases(aliasList);
184 if (selection != -1) {
185 mKeyAliases.select(selection);
187 // since a match was found and is selected, we need to give it to
188 // the wizard as well
189 mWizard.setKeyAlias(keyAlias);
191 mKeyAliases.clearSelection();
194 // reset the password
195 mKeyPassword.setText(""); //$NON-NLS-1$
197 // enable onChange, and call it to display errors and enable/disable pageCompleted.
198 mDisableOnChange = false;
200 } catch (KeyStoreException e) {
202 } catch (FileNotFoundException e) {
204 } catch (NoSuchAlgorithmException e) {
206 } catch (CertificateException e) {
208 } catch (IOException e) {
211 // in case we exit with an exception, we need to reset this
212 mDisableOnChange = false;
218 public IWizardPage getPreviousPage() {
219 return mWizard.getKeystoreSelectionPage();
223 public IWizardPage getNextPage() {
224 if (mWizard.getKeyCreationMode()) {
225 return mWizard.getKeyCreationPage();
228 return mWizard.getKeyCheckPage();
232 * Handles changes and update the error message and calls {@link #setPageComplete(boolean)}.
234 private void onChange() {
235 if (mDisableOnChange) {
239 setErrorMessage(null);
242 if (mWizard.getKeyCreationMode() == false) {
243 if (mKeyAliases.getSelectionIndex() == -1) {
244 setErrorMessage("Select a key alias.");
245 setPageComplete(false);
249 if (mKeyPassword.getText().trim().length() == 0) {
250 setErrorMessage("Enter key password.");
251 setPageComplete(false);
256 setPageComplete(true);
259 private void enableWidgets() {
260 boolean useKey = !mWizard.getKeyCreationMode();
261 mKeyAliasesLabel.setEnabled(useKey);
262 mKeyAliases.setEnabled(useKey);
263 mKeyPassword.setEnabled(useKey);
264 mKeyPasswordLabel.setEnabled(useKey);