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.Composite; 33 import org.eclipse.swt.widgets.FileDialog; 34 import org.eclipse.swt.widgets.Label; 35 import org.eclipse.swt.widgets.Text; 36 37 import java.io.File; 38 39 /** 40 * Keystore selection page. This page allows to choose to create a new keystore or use an 41 * existing one. 42 */ 43 final class KeystoreSelectionPage extends ExportWizardPage { 44 45 private final ExportWizard mWizard; 46 private Button mUseExistingKeystore; 47 private Button mCreateKeystore; 48 private Text mKeystore; 49 private Text mKeystorePassword; 50 private Label mConfirmLabel; 51 private Text mKeystorePassword2; 52 private boolean mDisableOnChange = false; 53 54 protected KeystoreSelectionPage(ExportWizard wizard, String pageName) { 55 super(pageName); 56 mWizard = wizard; 57 58 setTitle("Keystore selection"); 59 setDescription(""); //TODO 60 } 61 62 @Override 63 public void createControl(Composite parent) { 64 Composite composite = new Composite(parent, SWT.NULL); 65 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 66 GridLayout gl = new GridLayout(3, false); 67 composite.setLayout(gl); 68 69 GridData gd; 70 71 mUseExistingKeystore = new Button(composite, SWT.RADIO); 72 mUseExistingKeystore.setText("Use existing keystore"); 73 mUseExistingKeystore.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL)); 74 gd.horizontalSpan = 3; 75 mUseExistingKeystore.setSelection(true); 76 77 mCreateKeystore = new Button(composite, SWT.RADIO); 78 mCreateKeystore.setText("Create new keystore"); 79 mCreateKeystore.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL)); 80 gd.horizontalSpan = 3; 81 82 new Label(composite, SWT.NONE).setText("Location:"); 83 mKeystore = new Text(composite, SWT.BORDER); 84 mKeystore.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL)); 85 final Button browseButton = new Button(composite, SWT.PUSH); 86 browseButton.setText("Browse..."); 87 browseButton.addSelectionListener(new SelectionAdapter() { 88 @Override 89 public void widgetSelected(SelectionEvent e) { 90 FileDialog fileDialog; 91 if (mUseExistingKeystore.getSelection()) { 92 fileDialog = new FileDialog(browseButton.getShell(),SWT.OPEN); 93 fileDialog.setText("Load Keystore"); 94 } else { 95 fileDialog = new FileDialog(browseButton.getShell(),SWT.SAVE); 96 fileDialog.setText("Select Keystore Name"); 97 } 98 99 String fileName = fileDialog.open(); 100 if (fileName != null) { 101 mKeystore.setText(fileName); 102 } 103 } 104 }); 105 106 new Label(composite, SWT.NONE).setText("Password:"); 107 mKeystorePassword = new Text(composite, SWT.BORDER | SWT.PASSWORD); 108 mKeystorePassword.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL)); 109 mKeystorePassword.addVerifyListener(sPasswordVerifier); 110 new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData()); 111 gd.heightHint = gd.widthHint = 0; 112 113 mConfirmLabel = new Label(composite, SWT.NONE); 114 mConfirmLabel.setText("Confirm:"); 115 mKeystorePassword2 = new Text(composite, SWT.BORDER | SWT.PASSWORD); 116 mKeystorePassword2.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL)); 117 mKeystorePassword2.addVerifyListener(sPasswordVerifier); 118 new Composite(composite, SWT.NONE).setLayoutData(gd = new GridData()); 119 gd.heightHint = gd.widthHint = 0; 120 mKeystorePassword2.setEnabled(false); 121 122 // Show description the first time 123 setErrorMessage(null); 124 setMessage(null); 125 setControl(composite); 126 127 mUseExistingKeystore.addSelectionListener(new SelectionAdapter() { 128 @Override 129 public void widgetSelected(SelectionEvent e) { 130 boolean createStore = !mUseExistingKeystore.getSelection(); 131 mKeystorePassword2.setEnabled(createStore); 132 mConfirmLabel.setEnabled(createStore); 133 mWizard.setKeystoreCreationMode(createStore); 134 onChange(); 135 } 136 }); 137 138 mKeystore.addModifyListener(new ModifyListener() { 139 @Override 140 public void modifyText(ModifyEvent e) { 141 mWizard.setKeystore(mKeystore.getText().trim()); 142 onChange(); 143 } 144 }); 145 146 mKeystorePassword.addModifyListener(new ModifyListener() { 147 @Override 148 public void modifyText(ModifyEvent e) { 149 mWizard.setKeystorePassword(mKeystorePassword.getText()); 150 onChange(); 151 } 152 }); 153 154 mKeystorePassword2.addModifyListener(new ModifyListener() { 155 @Override 156 public void modifyText(ModifyEvent e) { 157 onChange(); 158 } 159 }); 160 } 161 162 @Override 163 public IWizardPage getNextPage() { 164 if (mUseExistingKeystore.getSelection()) { 165 return mWizard.getKeySelectionPage(); 166 } 167 168 return mWizard.getKeyCreationPage(); 169 } 170 171 @Override 172 void onShow() { 173 // fill the texts with information loaded from the project. 174 if ((mProjectDataChanged & DATA_PROJECT) != 0) { 175 // reset the keystore/alias from the content of the project 176 IProject project = mWizard.getProject(); 177 178 // disable onChange for now. we'll call it once at the end. 179 mDisableOnChange = true; 180 181 String keystore = ProjectHelper.loadStringProperty(project, 182 ExportWizard.PROPERTY_KEYSTORE); 183 if (keystore != null) { 184 mKeystore.setText(keystore); 185 } 186 187 // reset the passwords 188 mKeystorePassword.setText(""); //$NON-NLS-1$ 189 mKeystorePassword2.setText(""); //$NON-NLS-1$ 190 191 // enable onChange, and call it to display errors and enable/disable pageCompleted. 192 mDisableOnChange = false; 193 onChange(); 194 } 195 } 196 197 /** 198 * Handles changes and update the error message and calls {@link #setPageComplete(boolean)}. 199 */ 200 private void onChange() { 201 if (mDisableOnChange) { 202 return; 203 } 204 205 setErrorMessage(null); 206 setMessage(null); 207 208 boolean createStore = !mUseExistingKeystore.getSelection(); 209 210 // checks the keystore path is non null. 211 String keystore = mKeystore.getText().trim(); 212 if (keystore.length() == 0) { 213 setErrorMessage("Enter path to keystore."); 214 setPageComplete(false); 215 return; 216 } else { 217 File f = new File(keystore); 218 if (f.exists() == false) { 219 if (createStore == false) { 220 setErrorMessage("Keystore does not exist."); 221 setPageComplete(false); 222 return; 223 } 224 } else if (f.isDirectory()) { 225 setErrorMessage("Keystore path is a directory."); 226 setPageComplete(false); 227 return; 228 } else if (f.isFile()) { 229 if (createStore) { 230 setErrorMessage("File already exists."); 231 setPageComplete(false); 232 return; 233 } 234 } 235 } 236 237 String value = mKeystorePassword.getText(); 238 if (value.length() == 0) { 239 setErrorMessage("Enter keystore password."); 240 setPageComplete(false); 241 return; 242 } else if (createStore && value.length() < 6) { 243 setErrorMessage("Keystore password is too short - must be at least 6 characters."); 244 setPageComplete(false); 245 return; 246 } 247 248 if (createStore) { 249 if (mKeystorePassword2.getText().length() == 0) { 250 setErrorMessage("Confirm keystore password."); 251 setPageComplete(false); 252 return; 253 } 254 255 if (mKeystorePassword.getText().equals(mKeystorePassword2.getText()) == false) { 256 setErrorMessage("Keystore passwords do not match."); 257 setPageComplete(false); 258 return; 259 } 260 } 261 262 setPageComplete(true); 263 } 264 } 265