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 org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IWorkspaceRoot; 21 import org.eclipse.core.resources.ResourcesPlugin; 22 import org.eclipse.jdt.core.IJavaModel; 23 import org.eclipse.jdt.core.IJavaProject; 24 import org.eclipse.jdt.core.JavaCore; 25 import org.eclipse.jdt.ui.JavaElementLabelProvider; 26 import org.eclipse.jface.viewers.ILabelProvider; 27 import org.eclipse.jface.window.Window; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 30 31 /** 32 * Helper class to deal with displaying a project choosing dialog that lists only the 33 * projects with the Android nature. 34 */ 35 public class ProjectChooserHelper { 36 37 private final Shell mParentShell; 38 39 /** 40 * List of current android projects. Since the dialog is modal, we'll just get 41 * the list once on-demand. 42 */ 43 private IJavaProject[] mAndroidProjects; 44 ProjectChooserHelper(Shell parentShell)45 public ProjectChooserHelper(Shell parentShell) { 46 mParentShell = parentShell; 47 } 48 /** 49 * Displays a project chooser dialog which lists all available projects with the Android nature. 50 * <p/> 51 * The list of project is built from Android flagged projects currently opened in the workspace. 52 * 53 * @param projectName If non null and not empty, represents the name of an Android project 54 * that will be selected by default. 55 * @return the project chosen by the user in the dialog, or null if the dialog was canceled. 56 */ chooseJavaProject(String projectName)57 public IJavaProject chooseJavaProject(String projectName) { 58 ILabelProvider labelProvider = new JavaElementLabelProvider( 59 JavaElementLabelProvider.SHOW_DEFAULT); 60 ElementListSelectionDialog dialog = new ElementListSelectionDialog( 61 mParentShell, labelProvider); 62 dialog.setTitle("Project Selection"); 63 dialog.setMessage("Select a project to constrain your search."); 64 65 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); 66 IJavaModel javaModel = JavaCore.create(workspaceRoot); 67 68 // set the elements in the dialog. These are opened android projects. 69 dialog.setElements(getAndroidProjects(javaModel)); 70 71 // look for the project matching the given project name 72 IJavaProject javaProject = null; 73 if (projectName != null && projectName.length() > 0) { 74 javaProject = javaModel.getJavaProject(projectName); 75 } 76 77 // if we found it, we set the initial selection in the dialog to this one. 78 if (javaProject != null) { 79 dialog.setInitialSelections(new Object[] { javaProject }); 80 } 81 82 // open the dialog and return the object selected if OK was clicked, or null otherwise 83 if (dialog.open() == Window.OK) { 84 return (IJavaProject) dialog.getFirstResult(); 85 } 86 return null; 87 } 88 89 /** 90 * Returns the list of Android projects. 91 * <p/> 92 * Because this list can be time consuming, this class caches the list of project. 93 * It is recommended to call this method instead of 94 * {@link BaseProjectHelper#getAndroidProjects()}. 95 * 96 * @param javaModel the java model. Can be null. 97 */ getAndroidProjects(IJavaModel javaModel)98 public IJavaProject[] getAndroidProjects(IJavaModel javaModel) { 99 if (mAndroidProjects == null) { 100 if (javaModel == null) { 101 mAndroidProjects = BaseProjectHelper.getAndroidProjects(); 102 } else { 103 mAndroidProjects = BaseProjectHelper.getAndroidProjects(javaModel); 104 } 105 } 106 107 return mAndroidProjects; 108 } 109 110 /** 111 * Helper method to get the Android project with the given name 112 * 113 * @param projectName the name of the project to find 114 * @return the {@link IProject} for the Android project. <code>null</code> if not found. 115 */ getAndroidProject(String projectName)116 public IProject getAndroidProject(String projectName) { 117 IProject iproject = null; 118 IJavaProject[] javaProjects = getAndroidProjects(null); 119 if (javaProjects != null) { 120 for (IJavaProject javaProject : javaProjects) { 121 if (javaProject.getElementName().equals(projectName)) { 122 iproject = javaProject.getProject(); 123 break; 124 } 125 } 126 } 127 return iproject; 128 } 129 } 130