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.project; 18 19 import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper.IProjectFilter; 20 import com.android.ide.eclipse.adt.internal.sdk.ProjectState; 21 import com.android.ide.eclipse.adt.internal.sdk.Sdk; 22 23 import org.eclipse.core.resources.IProject; 24 import org.eclipse.core.resources.IWorkspaceRoot; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.jdt.core.IJavaModel; 27 import org.eclipse.jdt.core.IJavaProject; 28 import org.eclipse.jdt.core.JavaCore; 29 import org.eclipse.jdt.ui.JavaElementLabelProvider; 30 import org.eclipse.jface.viewers.ILabelProvider; 31 import org.eclipse.jface.window.Window; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 34 35 /** 36 * Helper class to deal with displaying a project choosing dialog that lists only the 37 * projects with the Android nature. 38 */ 39 public class ProjectChooserHelper { 40 41 private final Shell mParentShell; 42 private final IProjectChooserFilter mFilter; 43 44 /** 45 * List of current android projects. Since the dialog is modal, we'll just get 46 * the list once on-demand. 47 */ 48 private IJavaProject[] mAndroidProjects; 49 50 /** 51 * Interface to filter out some project displayed by {@link ProjectChooserHelper}. 52 * 53 * @see IProjectFilter 54 */ 55 public interface IProjectChooserFilter extends IProjectFilter { 56 /** 57 * Whether the Project Chooser can compute the project list once and cache the result. 58 * </p>If false the project list is recomputed every time the dialog is opened. 59 */ useCache()60 boolean useCache(); 61 } 62 63 /** 64 * An implementation of {@link IProjectChooserFilter} that only displays non-library projects. 65 */ 66 public final static class NonLibraryProjectOnlyFilter implements IProjectChooserFilter { accept(IProject project)67 public boolean accept(IProject project) { 68 ProjectState state = Sdk.getProjectState(project); 69 if (state != null) { 70 return state.isLibrary() == false; 71 } 72 73 return false; 74 } 75 useCache()76 public boolean useCache() { 77 return true; 78 } 79 } 80 81 /** 82 * An implementation of {@link IProjectChooserFilter} that only displays library projects. 83 */ 84 public final static class LibraryProjectOnlyFilter implements IProjectChooserFilter { accept(IProject project)85 public boolean accept(IProject project) { 86 ProjectState state = Sdk.getProjectState(project); 87 if (state != null ) { 88 return state.isLibrary(); 89 } 90 91 return false; 92 } 93 useCache()94 public boolean useCache() { 95 return true; 96 } 97 } 98 99 /** 100 * Creates a new project chooser. 101 * @param parentShell the parent {@link Shell} for the dialog. 102 * @param filter a filter to only accept certain projects. Can be null. 103 */ ProjectChooserHelper(Shell parentShell, IProjectChooserFilter filter)104 public ProjectChooserHelper(Shell parentShell, IProjectChooserFilter filter) { 105 mParentShell = parentShell; 106 mFilter = filter; 107 } 108 109 /** 110 * Displays a project chooser dialog which lists all available projects with the Android nature. 111 * <p/> 112 * The list of project is built from Android flagged projects currently opened in the workspace. 113 * 114 * @param projectName If non null and not empty, represents the name of an Android project 115 * that will be selected by default. 116 * @param message Message for the dialog box. Can be null in which case a default message 117 * is displayed. 118 * @return the project chosen by the user in the dialog, or null if the dialog was canceled. 119 */ chooseJavaProject(String projectName, String message)120 public IJavaProject chooseJavaProject(String projectName, String message) { 121 ILabelProvider labelProvider = new JavaElementLabelProvider( 122 JavaElementLabelProvider.SHOW_DEFAULT); 123 ElementListSelectionDialog dialog = new ElementListSelectionDialog( 124 mParentShell, labelProvider); 125 dialog.setTitle("Project Selection"); 126 if (message == null) { 127 message = "Please select a project"; 128 } 129 dialog.setMessage(message); 130 131 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); 132 IJavaModel javaModel = JavaCore.create(workspaceRoot); 133 134 // set the elements in the dialog. These are opened android projects. 135 dialog.setElements(getAndroidProjects(javaModel)); 136 137 // look for the project matching the given project name 138 IJavaProject javaProject = null; 139 if (projectName != null && projectName.length() > 0) { 140 javaProject = javaModel.getJavaProject(projectName); 141 } 142 143 // if we found it, we set the initial selection in the dialog to this one. 144 if (javaProject != null) { 145 dialog.setInitialSelections(new Object[] { javaProject }); 146 } 147 148 // open the dialog and return the object selected if OK was clicked, or null otherwise 149 if (dialog.open() == Window.OK) { 150 return (IJavaProject) dialog.getFirstResult(); 151 } 152 return null; 153 } 154 155 /** 156 * Returns the list of Android projects. 157 * <p/> 158 * Because this list can be time consuming, this class caches the list of project. 159 * It is recommended to call this method instead of 160 * {@link BaseProjectHelper#getAndroidProjects()}. 161 * 162 * @param javaModel the java model. Can be null. 163 */ getAndroidProjects(IJavaModel javaModel)164 public IJavaProject[] getAndroidProjects(IJavaModel javaModel) { 165 // recompute only if we don't have the projects already or the filter is dynamic 166 // and prevent usage of a cache. 167 if (mAndroidProjects == null || (mFilter != null && mFilter.useCache() == false)) { 168 if (javaModel == null) { 169 mAndroidProjects = BaseProjectHelper.getAndroidProjects(mFilter); 170 } else { 171 mAndroidProjects = BaseProjectHelper.getAndroidProjects(javaModel, mFilter); 172 } 173 } 174 175 return mAndroidProjects; 176 } 177 178 /** 179 * Helper method to get the Android project with the given name 180 * 181 * @param projectName the name of the project to find 182 * @return the {@link IProject} for the Android project. <code>null</code> if not found. 183 */ getAndroidProject(String projectName)184 public IProject getAndroidProject(String projectName) { 185 IProject iproject = null; 186 IJavaProject[] javaProjects = getAndroidProjects(null); 187 if (javaProjects != null) { 188 for (IJavaProject javaProject : javaProjects) { 189 if (javaProject.getElementName().equals(projectName)) { 190 iproject = javaProject.getProject(); 191 break; 192 } 193 } 194 } 195 return iproject; 196 } 197 } 198