1 /* 2 * Copyright (C) 2012 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.templates; 17 18 import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_BUILD_API; 19 import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_MIN_API; 20 import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_MIN_API_LEVEL; 21 import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_PACKAGE_NAME; 22 import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_TARGET_API; 23 import static org.eclipse.core.resources.IResource.DEPTH_INFINITE; 24 25 import com.android.annotations.NonNull; 26 import com.android.ide.eclipse.adt.AdtPlugin; 27 import com.android.ide.eclipse.adt.AdtUtils; 28 29 import org.eclipse.core.resources.IProject; 30 import org.eclipse.core.runtime.CoreException; 31 import org.eclipse.core.runtime.IProgressMonitor; 32 import org.eclipse.core.runtime.NullProgressMonitor; 33 import org.eclipse.jface.operation.IRunnableWithProgress; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.jface.wizard.IWizardPage; 36 import org.eclipse.jface.wizard.WizardPage; 37 import org.eclipse.ltk.core.refactoring.Change; 38 import org.eclipse.ltk.core.refactoring.CompositeChange; 39 import org.eclipse.ui.IWorkbench; 40 41 import java.lang.reflect.InvocationTargetException; 42 import java.util.List; 43 import java.util.Set; 44 45 /** 46 * Wizard for creating new activities. This is a hybrid between a New Project 47 * Wizard and a New Template Wizard: it has the "Activity selector" page from 48 * the New Project Wizard, which is used to dynamically select a wizard for the 49 * second page, but beyond that it runs the normal template wizard when it comes 50 * time to create the template. 51 */ 52 public class NewActivityWizard extends TemplateWizard { 53 private NewTemplatePage mTemplatePage; 54 private ActivityPage mActivityPage; 55 private NewProjectWizardState mValues; 56 private NewTemplateWizardState mActivityValues; 57 protected boolean mOnlyActivities; 58 59 /** Creates a new {@link NewActivityWizard} */ 60 public NewActivityWizard() { 61 mOnlyActivities = true; 62 } 63 64 @Override 65 protected boolean shouldAddIconPage() { 66 return mActivityValues.getIconState() != null; 67 } 68 69 @Override 70 public void init(IWorkbench workbench, IStructuredSelection selection) { 71 super.init(workbench, selection); 72 73 setWindowTitle(mOnlyActivities ? "New Activity" : "New Android Object"); 74 75 mValues = new NewProjectWizardState(); 76 mActivityPage = new ActivityPage(mValues, mOnlyActivities, false); 77 78 mActivityValues = mValues.activityValues; 79 List<IProject> projects = AdtUtils.getSelectedProjects(selection); 80 if (projects.size() == 1) { 81 mActivityValues.project = projects.get(0); 82 } 83 } 84 85 @Override 86 public void addPages() { 87 super.addPages(); 88 addPage(mActivityPage); 89 } 90 91 @Override 92 public IWizardPage getNextPage(IWizardPage page) { 93 if (page == mActivityPage) { 94 if (mTemplatePage == null) { 95 Set<String> hidden = mActivityValues.hidden; 96 hidden.add(ATTR_PACKAGE_NAME); 97 hidden.add(ATTR_MIN_API); 98 hidden.add(ATTR_MIN_API_LEVEL); 99 hidden.add(ATTR_TARGET_API); 100 hidden.add(ATTR_BUILD_API); 101 102 mTemplatePage = new NewTemplatePage(mActivityValues, true); 103 addPage(mTemplatePage); 104 } 105 return mTemplatePage; 106 } else if (page == mTemplatePage && shouldAddIconPage()) { 107 WizardPage iconPage = getIconPage(mActivityValues.getIconState()); 108 mActivityValues.updateIconState(mTemplatePage.getEvaluator()); 109 return iconPage; 110 } else if (page == mTemplatePage 111 || shouldAddIconPage() && page == getIconPage(mActivityValues.getIconState())) { 112 TemplateMetadata template = mActivityValues.getTemplateHandler().getTemplate(); 113 if (template != null) { 114 if (InstallDependencyPage.isInstalled(template.getDependencies())) { 115 return getPreviewPage(mActivityValues); 116 } else { 117 return getDependencyPage(template, true); 118 } 119 } 120 } else { 121 TemplateMetadata template = mActivityValues.getTemplateHandler().getTemplate(); 122 if (template != null && page == getDependencyPage(template, false)) { 123 return getPreviewPage(mActivityValues); 124 } 125 } 126 127 return super.getNextPage(page); 128 } 129 130 @Override 131 public boolean canFinish() { 132 // Deal with lazy creation of some pages: these may not be in the page-list yet 133 // since they are constructed lazily, so consider that option here. 134 if (mTemplatePage == null || !mTemplatePage.isPageComplete()) { 135 return false; 136 } 137 138 return super.canFinish(); 139 } 140 141 @Override 142 public boolean performFinish(IProgressMonitor monitor) throws InvocationTargetException { 143 boolean success = super.performFinish(monitor); 144 145 if (success) { 146 List<Runnable> finalizingTasks = getFinalizingActions(); 147 for (Runnable r : finalizingTasks) { 148 r.run(); 149 } 150 return true; 151 } 152 return false; 153 } 154 155 @Override 156 @NonNull 157 protected IProject getProject() { 158 return mActivityValues.project; 159 } 160 161 @Override 162 @NonNull 163 protected List<String> getFilesToOpen() { 164 TemplateHandler activityTemplate = mActivityValues.getTemplateHandler(); 165 return activityTemplate.getFilesToOpen(); 166 } 167 168 @Override 169 @NonNull 170 protected List<Runnable> getFinalizingActions() { 171 TemplateHandler activityTemplate = mActivityValues.getTemplateHandler(); 172 return activityTemplate.getFinalizingActions(); 173 } 174 175 @Override 176 protected List<Change> computeChanges() { 177 return mActivityValues.computeChanges(); 178 } 179 180 /** Wizard for creating other Android components */ 181 public static class OtherWizard extends NewActivityWizard { 182 /** Create new {@link OtherWizard} */ 183 public OtherWizard() { 184 mOnlyActivities = false; 185 } 186 } 187 } 188