1 /* 2 * Copyright (C) 2011 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 package com.android.ide.eclipse.adt.internal.wizards.newproject; 17 18 import static com.android.SdkConstants.FN_PROJECT_PROGUARD_FILE; 19 import static com.android.SdkConstants.OS_SDK_TOOLS_LIB_FOLDER; 20 21 import com.android.ide.eclipse.adt.AdtPlugin; 22 import com.android.ide.eclipse.adt.AdtUtils; 23 import com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectWizardState.Mode; 24 25 import org.eclipse.jdt.ui.actions.OpenJavaPerspectiveAction; 26 import org.eclipse.jface.resource.ImageDescriptor; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.wizard.IWizardPage; 29 import org.eclipse.jface.wizard.Wizard; 30 import org.eclipse.ui.INewWizard; 31 import org.eclipse.ui.IWorkbench; 32 33 import java.io.File; 34 35 36 /** 37 * A "New Android Project" Wizard. 38 * <p/> 39 * Note: this class is public so that it can be accessed from unit tests. 40 * It is however an internal class. Its API may change without notice. 41 * It should semantically be considered as a private final class. 42 * <p/> 43 * Do not derive from this class. 44 */ 45 public class NewProjectWizard extends Wizard implements INewWizard { 46 private static final String PROJECT_LOGO_LARGE = "icons/android-64.png"; //$NON-NLS-1$ 47 48 private NewProjectWizardState mValues; 49 private ProjectNamePage mNamePage; 50 private SdkSelectionPage mSdkPage; 51 private SampleSelectionPage mSamplePage; 52 private ApplicationInfoPage mPropertiesPage; 53 private final Mode mMode; 54 private IStructuredSelection mSelection; 55 56 /** Constructs a new wizard default project wizard */ 57 public NewProjectWizard() { 58 this(Mode.ANY); 59 } 60 61 protected NewProjectWizard(Mode mode) { 62 mMode = mode; 63 switch (mMode) { 64 case SAMPLE: 65 setWindowTitle("New Android Sample Project"); 66 break; 67 case TEST: 68 setWindowTitle("New Android Test Project"); 69 break; 70 default: 71 setWindowTitle("New Android Project"); 72 break; 73 } 74 } 75 76 @Override 77 public void addPages() { 78 mValues = new NewProjectWizardState(mMode); 79 80 if (mMode != Mode.SAMPLE) { 81 mNamePage = new ProjectNamePage(mValues); 82 83 if (mSelection != null) { 84 mNamePage.init(mSelection, AdtUtils.getActivePart()); 85 } 86 87 addPage(mNamePage); 88 } 89 90 if (mMode == Mode.TEST) { 91 addPage(new TestTargetPage(mValues)); 92 } 93 94 mSdkPage = new SdkSelectionPage(mValues); 95 addPage(mSdkPage); 96 97 if (mMode != Mode.TEST) { 98 // Sample projects can be created when entering the new/existing wizard, or 99 // the sample wizard 100 mSamplePage = new SampleSelectionPage(mValues); 101 addPage(mSamplePage); 102 } 103 104 if (mMode != Mode.SAMPLE) { 105 // Project properties are entered in all project types except sample projects 106 mPropertiesPage = new ApplicationInfoPage(mValues); 107 addPage(mPropertiesPage); 108 } 109 } 110 111 @Override 112 public void init(IWorkbench workbench, IStructuredSelection selection) { 113 mSelection = selection; 114 115 setHelpAvailable(false); // TODO have help 116 ImageDescriptor desc = AdtPlugin.getImageDescriptor(PROJECT_LOGO_LARGE); 117 setDefaultPageImageDescriptor(desc); 118 119 // Trigger a check to see if the SDK needs to be reloaded (which will 120 // invoke onSdkLoaded asynchronously as needed). 121 AdtPlugin.getDefault().refreshSdk(); 122 } 123 124 @Override 125 public boolean performFinish() { 126 File file = new File(AdtPlugin.getOsSdkFolder(), OS_SDK_TOOLS_LIB_FOLDER + File.separator 127 + FN_PROJECT_PROGUARD_FILE); 128 if (!file.exists()) { 129 AdtPlugin.displayError("Tools Out of Date?", 130 String.format("It looks like you do not have the latest version of the " 131 + "SDK Tools installed. Make sure you update via the SDK Manager " 132 + "first. (Could not find %1$s)", file.getPath())); 133 return false; 134 } 135 136 NewProjectCreator creator = new NewProjectCreator(mValues, getContainer()); 137 if (!(creator.createAndroidProjects())) { 138 return false; 139 } 140 141 // Open the default Java Perspective 142 OpenJavaPerspectiveAction action = new OpenJavaPerspectiveAction(); 143 action.run(); 144 return true; 145 } 146 147 @Override 148 public IWizardPage getNextPage(IWizardPage page) { 149 if (page == mNamePage) { 150 // Skip the test target selection page unless creating a test project 151 if (mValues.mode != Mode.TEST) { 152 return mSdkPage; 153 } 154 } else if (page == mSdkPage) { 155 if (mValues.mode == Mode.SAMPLE) { 156 return mSamplePage; 157 } else if (mValues.mode != Mode.TEST) { 158 return mPropertiesPage; 159 } else { 160 // Done with wizard when creating from existing or creating test projects 161 return null; 162 } 163 } else if (page == mSamplePage) { 164 // Nothing more to be entered for samples 165 return null; 166 } 167 168 return super.getNextPage(page); 169 } 170 171 /** 172 * Returns the package name currently set by the wizard 173 * 174 * @return the current package name, or null 175 */ 176 public String getPackageName() { 177 return mValues.packageName; 178 } 179 180 // TBD: Call setDialogSettings etc to store persistent state between wizard invocations. 181 } 182