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 24 import com.android.annotations.NonNull; 25 import com.android.ide.eclipse.adt.AdtPlugin; 26 import com.android.ide.eclipse.adt.AdtUtils; 27 28 import org.eclipse.core.resources.IFile; 29 import org.eclipse.core.resources.IProject; 30 import org.eclipse.core.resources.IResource; 31 import org.eclipse.jface.operation.IRunnableWithProgress; 32 import org.eclipse.jface.viewers.IStructuredSelection; 33 import org.eclipse.jface.wizard.IWizardPage; 34 import org.eclipse.jface.wizard.WizardPage; 35 import org.eclipse.ltk.core.refactoring.Change; 36 import org.eclipse.ui.IWorkbench; 37 import org.eclipse.ui.PartInitException; 38 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard; 39 40 import java.io.File; 41 import java.util.List; 42 import java.util.Set; 43 44 /** 45 * Template wizard which creates parameterized templates 46 */ 47 public class NewTemplateWizard extends TemplateWizard { 48 /** Template name and location under $sdk/templates for the default activity */ 49 static final String BLANK_ACTIVITY = "activities/BlankActivity"; //$NON-NLS-1$ 50 /** Template name and location under $sdk/templates for the custom view template */ 51 static final String CUSTOM_VIEW = "other/CustomView"; //$NON-NLS-1$ 52 53 protected NewTemplatePage mMainPage; 54 protected NewTemplateWizardState mValues; 55 private final String mTemplateName; 56 NewTemplateWizard(String templateName)57 NewTemplateWizard(String templateName) { 58 mTemplateName = templateName; 59 } 60 61 @Override init(IWorkbench workbench, IStructuredSelection selection)62 public void init(IWorkbench workbench, IStructuredSelection selection) { 63 super.init(workbench, selection); 64 65 mValues = new NewTemplateWizardState(); 66 67 File template = TemplateManager.getTemplateLocation(mTemplateName); 68 if (template != null) { 69 mValues.setTemplateLocation(template); 70 } 71 hideBuiltinParameters(); 72 73 List<IProject> projects = AdtUtils.getSelectedProjects(selection); 74 if (projects.size() == 1) { 75 mValues.project = projects.get(0); 76 } 77 78 mMainPage = new NewTemplatePage(mValues, true); 79 } 80 81 @Override shouldAddIconPage()82 protected boolean shouldAddIconPage() { 83 return mValues.getIconState() != null; 84 } 85 86 /** 87 * Hide those parameters that the template requires but that we don't want 88 * to ask the users about, since we can derive it from the target project 89 * the template is written into. 90 */ hideBuiltinParameters()91 protected void hideBuiltinParameters() { 92 Set<String> hidden = mValues.hidden; 93 hidden.add(ATTR_PACKAGE_NAME); 94 hidden.add(ATTR_MIN_API); 95 hidden.add(ATTR_MIN_API_LEVEL); 96 hidden.add(ATTR_TARGET_API); 97 hidden.add(ATTR_BUILD_API); 98 } 99 100 @Override addPages()101 public void addPages() { 102 super.addPages(); 103 addPage(mMainPage); 104 } 105 106 @Override getNextPage(IWizardPage page)107 public IWizardPage getNextPage(IWizardPage page) { 108 TemplateMetadata template = mValues.getTemplateHandler().getTemplate(); 109 110 if (page == mMainPage && shouldAddIconPage()) { 111 WizardPage iconPage = getIconPage(mValues.getIconState()); 112 mValues.updateIconState(mMainPage.getEvaluator()); 113 return iconPage; 114 } else if (page == mMainPage 115 || shouldAddIconPage() && page == getIconPage(mValues.getIconState())) { 116 if (template != null) { 117 if (InstallDependencyPage.isInstalled(template.getDependencies())) { 118 return getPreviewPage(mValues); 119 } else { 120 return getDependencyPage(template, true); 121 } 122 } 123 } else if (page == getDependencyPage(template, false)) { 124 return getPreviewPage(mValues); 125 } 126 127 return super.getNextPage(page); 128 } 129 130 @Override 131 @NonNull getProject()132 protected IProject getProject() { 133 return mValues.project; 134 } 135 136 @Override 137 @NonNull getFilesToOpen()138 protected List<String> getFilesToOpen() { 139 TemplateHandler activityTemplate = mValues.getTemplateHandler(); 140 return activityTemplate.getFilesToOpen(); 141 } 142 143 @Override 144 @NonNull getFinalizingActions()145 protected List<Runnable> getFinalizingActions() { 146 TemplateHandler activityTemplate = mValues.getTemplateHandler(); 147 return activityTemplate.getFinalizingActions(); 148 } 149 150 @Override computeChanges()151 protected List<Change> computeChanges() { 152 return mValues.computeChanges(); 153 } 154 155 /** 156 * Opens the given set of files (as relative paths within a given project 157 * 158 * @param project the project containing the paths 159 * @param relativePaths the paths to files to open 160 * @param mWorkbench the workbench to open the files in 161 */ openFiles( @onNull final IProject project, @NonNull final List<String> relativePaths, @NonNull final IWorkbench mWorkbench)162 public static void openFiles( 163 @NonNull final IProject project, 164 @NonNull final List<String> relativePaths, 165 @NonNull final IWorkbench mWorkbench) { 166 if (!relativePaths.isEmpty()) { 167 // This has to be delayed in order for focus handling to work correctly 168 AdtPlugin.getDisplay().asyncExec(new Runnable() { 169 @Override 170 public void run() { 171 for (String path : relativePaths) { 172 IResource resource = project.findMember(path); 173 if (resource != null) { 174 if (resource instanceof IFile) { 175 try { 176 AdtPlugin.openFile((IFile) resource, null, false); 177 } catch (PartInitException e) { 178 AdtPlugin.log(e, "Failed to open %1$s", //$NON-NLS-1$ 179 resource.getFullPath().toString()); 180 } 181 } 182 boolean isLast = relativePaths.size() == 1 || 183 path.equals(relativePaths.get(relativePaths.size() - 1)); 184 if (isLast) { 185 BasicNewResourceWizard.selectAndReveal(resource, 186 mWorkbench.getActiveWorkbenchWindow()); 187 } 188 } 189 } 190 } 191 }); 192 } 193 } 194 195 /** 196 * Specific New Custom View wizard 197 */ 198 public static class NewCustomViewWizard extends NewTemplateWizard { 199 /** Creates a new {@link NewCustomViewWizard} */ NewCustomViewWizard()200 public NewCustomViewWizard() { 201 super(CUSTOM_VIEW); 202 } 203 204 @Override init(IWorkbench workbench, IStructuredSelection selection)205 public void init(IWorkbench workbench, IStructuredSelection selection) { 206 super.init(workbench, selection); 207 setWindowTitle("New Custom View"); 208 super.mMainPage.setTitle("New Custom View"); 209 super.mMainPage.setDescription("Creates a new custom view"); 210 } 211 } 212 } 213