OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / eclipse / plugins / com.android.ide.eclipse.adt / src / com / android / ide / eclipse / adt / internal / wizards / export / KeySelectionPage.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
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
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
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.
15  */
16
17 package com.android.ide.eclipse.adt.internal.wizards.export;
18
19 import com.android.ide.eclipse.adt.internal.project.ProjectHelper;
20 import com.android.ide.eclipse.adt.internal.wizards.export.ExportWizard.ExportWizardPage;
21
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;
36
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;
46
47 /**
48  * Key Selection Page. This is used when an existing keystore is used. 
49  */
50 final class KeySelectionPage extends ExportWizardPage {
51
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;
60
61     protected KeySelectionPage(ExportWizard wizard, String pageName) {
62         super(pageName);
63         mWizard = wizard;
64
65         setTitle("Key alias selection");
66         setDescription(""); // TODO
67     }
68
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);
74
75         GridData gd;
76
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);
82
83         new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData());
84         gd.heightHint = 0;
85         gd.widthHint = 50;
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));
90         
91         new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData());
92         gd.heightHint = 0;
93         gd.widthHint = 50;
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));
98
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;
103
104         // Show description the first time
105         setErrorMessage(null);
106         setMessage(null);
107         setControl(composite);
108         
109         mUseExistingKey.addSelectionListener(new SelectionAdapter() {
110             @Override
111             public void widgetSelected(SelectionEvent e) {
112                 mWizard.setKeyCreationMode(!mUseExistingKey.getSelection());
113                 enableWidgets();
114                 onChange();
115             }
116         });
117         
118         mKeyAliases.addSelectionListener(new SelectionAdapter() {
119             @Override
120             public void widgetSelected(SelectionEvent e) {
121                 mWizard.setKeyAlias(mKeyAliases.getItem(mKeyAliases.getSelectionIndex()));
122                 onChange();
123             }
124         });
125         
126         mKeyPassword.addModifyListener(new ModifyListener() {
127             public void modifyText(ModifyEvent e) {
128                 mWizard.setKeyPassword(mKeyPassword.getText());
129                 onChange();
130             }
131         });
132     }
133     
134     @Override
135     void onShow() {
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;
140
141             // reset the alias from the content of the project
142             try {
143                 // reset to using a key
144                 mWizard.setKeyCreationMode(false);
145                 mUseExistingKey.setSelection(true);
146                 mCreateKey.setSelection(false);
147                 enableWidgets();
148
149                 // remove the content of the alias combo always and first, in case the
150                 // keystore password is wrong
151                 mKeyAliases.removeAll();
152
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());
157                 fis.close();
158                 
159                 Enumeration<String> aliases = keyStore.aliases();
160
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();
164
165                 String keyAlias = ProjectHelper.loadStringProperty(project,
166                         ExportWizard.PROPERTY_ALIAS);
167                 
168                 ArrayList<String> aliasList = new ArrayList<String>();
169
170                 int selection = -1;
171                 int count = 0;
172                 while (aliases.hasMoreElements()) {
173                     String alias = aliases.nextElement();
174                     mKeyAliases.add(alias);
175                     aliasList.add(alias);
176                     if (selection == -1 && alias.equalsIgnoreCase(keyAlias)) {
177                         selection = count;
178                     }
179                     count++;
180                 }
181                 
182                 mWizard.setExistingAliases(aliasList);
183
184                 if (selection != -1) {
185                     mKeyAliases.select(selection);
186
187                     // since a match was found and is selected, we need to give it to
188                     // the wizard as well
189                     mWizard.setKeyAlias(keyAlias);
190                 } else {
191                     mKeyAliases.clearSelection();
192                 }
193
194                 // reset the password
195                 mKeyPassword.setText(""); //$NON-NLS-1$
196
197                 // enable onChange, and call it to display errors and enable/disable pageCompleted.
198                 mDisableOnChange = false;
199                 onChange();
200             } catch (KeyStoreException e) {
201                 onException(e);
202             } catch (FileNotFoundException e) {
203                 onException(e);
204             } catch (NoSuchAlgorithmException e) {
205                 onException(e);
206             } catch (CertificateException e) {
207                 onException(e);
208             } catch (IOException e) {
209                 onException(e);
210             } finally {
211                 // in case we exit with an exception, we need to reset this
212                 mDisableOnChange = false;
213             }
214         }
215     }
216     
217     @Override
218     public IWizardPage getPreviousPage() {
219         return mWizard.getKeystoreSelectionPage();
220     }
221
222     @Override
223     public IWizardPage getNextPage() {
224         if (mWizard.getKeyCreationMode()) {
225             return mWizard.getKeyCreationPage();
226         }
227         
228         return mWizard.getKeyCheckPage();
229     }
230
231     /**
232      * Handles changes and update the error message and calls {@link #setPageComplete(boolean)}.
233      */
234     private void onChange() {
235         if (mDisableOnChange) {
236             return;
237         }
238
239         setErrorMessage(null);
240         setMessage(null);
241
242         if (mWizard.getKeyCreationMode() == false) {
243             if (mKeyAliases.getSelectionIndex() == -1) {
244                 setErrorMessage("Select a key alias.");
245                 setPageComplete(false);
246                 return;
247             }
248     
249             if (mKeyPassword.getText().trim().length() == 0) {
250                 setErrorMessage("Enter key password.");
251                 setPageComplete(false);
252                 return;
253             }
254         }
255
256         setPageComplete(true);
257     }
258     
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);
265     }
266 }