PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/ProjectChooserHelper.java

https://github.com/CyanogenMod/android_sdk
Java | 304 lines | 163 code | 34 blank | 107 comment | 38 complexity | a8e76a0d69ee0c329f10f8017713b34d MD5 | raw file
  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. package com.android.ide.eclipse.adt.internal.project;
  17. import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper.IProjectFilter;
  18. import com.android.ide.eclipse.adt.internal.sdk.ProjectState;
  19. import com.android.ide.eclipse.adt.internal.sdk.Sdk;
  20. import org.eclipse.core.resources.IProject;
  21. import org.eclipse.core.resources.IWorkspaceRoot;
  22. import org.eclipse.core.resources.ResourcesPlugin;
  23. import org.eclipse.jdt.core.IJavaModel;
  24. import org.eclipse.jdt.core.IJavaProject;
  25. import org.eclipse.jdt.core.JavaCore;
  26. import org.eclipse.jdt.ui.JavaElementLabelProvider;
  27. import org.eclipse.jface.viewers.ILabelProvider;
  28. import org.eclipse.jface.window.Window;
  29. import org.eclipse.swt.SWT;
  30. import org.eclipse.swt.events.SelectionEvent;
  31. import org.eclipse.swt.events.SelectionListener;
  32. import org.eclipse.swt.widgets.Combo;
  33. import org.eclipse.swt.widgets.Composite;
  34. import org.eclipse.swt.widgets.Shell;
  35. import org.eclipse.ui.dialogs.ElementListSelectionDialog;
  36. /**
  37. * Helper class to deal with displaying a project choosing dialog that lists only the
  38. * projects with the Android nature.
  39. */
  40. public class ProjectChooserHelper {
  41. private final Shell mParentShell;
  42. private final IProjectChooserFilter mFilter;
  43. /**
  44. * List of current android projects. Since the dialog is modal, we'll just get
  45. * the list once on-demand.
  46. */
  47. private IJavaProject[] mAndroidProjects;
  48. /**
  49. * Interface to filter out some project displayed by {@link ProjectChooserHelper}.
  50. *
  51. * @see IProjectFilter
  52. */
  53. public interface IProjectChooserFilter extends IProjectFilter {
  54. /**
  55. * Whether the Project Chooser can compute the project list once and cache the result.
  56. * </p>If false the project list is recomputed every time the dialog is opened.
  57. */
  58. boolean useCache();
  59. }
  60. /**
  61. * An implementation of {@link IProjectChooserFilter} that only displays non-library projects.
  62. */
  63. public final static class NonLibraryProjectOnlyFilter implements IProjectChooserFilter {
  64. @Override
  65. public boolean accept(IProject project) {
  66. ProjectState state = Sdk.getProjectState(project);
  67. if (state != null) {
  68. return state.isLibrary() == false;
  69. }
  70. return false;
  71. }
  72. @Override
  73. public boolean useCache() {
  74. return true;
  75. }
  76. }
  77. /**
  78. * An implementation of {@link IProjectChooserFilter} that only displays library projects.
  79. */
  80. public final static class LibraryProjectOnlyFilter implements IProjectChooserFilter {
  81. @Override
  82. public boolean accept(IProject project) {
  83. ProjectState state = Sdk.getProjectState(project);
  84. if (state != null ) {
  85. return state.isLibrary();
  86. }
  87. return false;
  88. }
  89. @Override
  90. public boolean useCache() {
  91. return true;
  92. }
  93. }
  94. /**
  95. * Creates a new project chooser.
  96. * @param parentShell the parent {@link Shell} for the dialog.
  97. * @param filter a filter to only accept certain projects. Can be null.
  98. */
  99. public ProjectChooserHelper(Shell parentShell, IProjectChooserFilter filter) {
  100. mParentShell = parentShell;
  101. mFilter = filter;
  102. }
  103. /**
  104. * Displays a project chooser dialog which lists all available projects with the Android nature.
  105. * <p/>
  106. * The list of project is built from Android flagged projects currently opened in the workspace.
  107. *
  108. * @param projectName If non null and not empty, represents the name of an Android project
  109. * that will be selected by default.
  110. * @param message Message for the dialog box. Can be null in which case a default message
  111. * is displayed.
  112. * @return the project chosen by the user in the dialog, or null if the dialog was canceled.
  113. */
  114. public IJavaProject chooseJavaProject(String projectName, String message) {
  115. ILabelProvider labelProvider = new JavaElementLabelProvider(
  116. JavaElementLabelProvider.SHOW_DEFAULT);
  117. ElementListSelectionDialog dialog = new ElementListSelectionDialog(
  118. mParentShell, labelProvider);
  119. dialog.setTitle("Project Selection");
  120. if (message == null) {
  121. message = "Please select a project";
  122. }
  123. dialog.setMessage(message);
  124. IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
  125. IJavaModel javaModel = JavaCore.create(workspaceRoot);
  126. // set the elements in the dialog. These are opened android projects.
  127. dialog.setElements(getAndroidProjects(javaModel));
  128. // look for the project matching the given project name
  129. IJavaProject javaProject = null;
  130. if (projectName != null && projectName.length() > 0) {
  131. javaProject = javaModel.getJavaProject(projectName);
  132. }
  133. // if we found it, we set the initial selection in the dialog to this one.
  134. if (javaProject != null) {
  135. dialog.setInitialSelections(new Object[] { javaProject });
  136. }
  137. // open the dialog and return the object selected if OK was clicked, or null otherwise
  138. if (dialog.open() == Window.OK) {
  139. return (IJavaProject) dialog.getFirstResult();
  140. }
  141. return null;
  142. }
  143. /**
  144. * Returns the list of Android projects.
  145. * <p/>
  146. * Because this list can be time consuming, this class caches the list of project.
  147. * It is recommended to call this method instead of
  148. * {@link BaseProjectHelper#getAndroidProjects()}.
  149. *
  150. * @param javaModel the java model. Can be null.
  151. */
  152. public IJavaProject[] getAndroidProjects(IJavaModel javaModel) {
  153. // recompute only if we don't have the projects already or the filter is dynamic
  154. // and prevent usage of a cache.
  155. if (mAndroidProjects == null || (mFilter != null && mFilter.useCache() == false)) {
  156. if (javaModel == null) {
  157. mAndroidProjects = BaseProjectHelper.getAndroidProjects(mFilter);
  158. } else {
  159. mAndroidProjects = BaseProjectHelper.getAndroidProjects(javaModel, mFilter);
  160. }
  161. }
  162. return mAndroidProjects;
  163. }
  164. /**
  165. * Helper method to get the Android project with the given name
  166. *
  167. * @param projectName the name of the project to find
  168. * @return the {@link IProject} for the Android project. <code>null</code> if not found.
  169. */
  170. public IProject getAndroidProject(String projectName) {
  171. IProject iproject = null;
  172. IJavaProject[] javaProjects = getAndroidProjects(null);
  173. if (javaProjects != null) {
  174. for (IJavaProject javaProject : javaProjects) {
  175. if (javaProject.getElementName().equals(projectName)) {
  176. iproject = javaProject.getProject();
  177. break;
  178. }
  179. }
  180. }
  181. return iproject;
  182. }
  183. /**
  184. * A selector combo for showing the currently selected project and for
  185. * changing the selection
  186. */
  187. public static class ProjectCombo extends Combo implements SelectionListener {
  188. /** Currently chosen project, or null when no project has been initialized or selected */
  189. private IProject mProject;
  190. private IJavaProject[] mAvailableProjects;
  191. /**
  192. * Creates a new project selector combo
  193. *
  194. * @param helper associated {@link ProjectChooserHelper} for looking up
  195. * projects
  196. * @param parent parent composite to add the combo to
  197. * @param initialProject the initial project to select, or null (which
  198. * will show a "Please Choose Project..." label instead.)
  199. */
  200. public ProjectCombo(ProjectChooserHelper helper, Composite parent,
  201. IProject initialProject) {
  202. super(parent, SWT.BORDER | SWT.FLAT | SWT.READ_ONLY);
  203. mProject = initialProject;
  204. mAvailableProjects = helper.getAndroidProjects(null);
  205. String[] items = new String[mAvailableProjects.length + 1];
  206. items[0] = "--- Choose Project ---";
  207. ILabelProvider labelProvider = new JavaElementLabelProvider(
  208. JavaElementLabelProvider.SHOW_DEFAULT);
  209. int selectionIndex = 0;
  210. for (int i = 0, n = mAvailableProjects.length; i < n; i++) {
  211. IProject project = mAvailableProjects[i].getProject();
  212. items[i + 1] = labelProvider.getText(project);
  213. if (project == initialProject) {
  214. selectionIndex = i + 1;
  215. }
  216. }
  217. setItems(items);
  218. select(selectionIndex);
  219. addSelectionListener(this);
  220. }
  221. /**
  222. * Returns the project selected by this chooser (or the initial project
  223. * passed to the constructor if the user did not change it)
  224. *
  225. * @return the selected project
  226. */
  227. public IProject getSelectedProject() {
  228. return mProject;
  229. }
  230. /**
  231. * Sets the project selected by this chooser
  232. *
  233. * @param project the selected project
  234. */
  235. public void setSelectedProject(IProject project) {
  236. mProject = project;
  237. int selectionIndex = 0;
  238. for (int i = 0, n = mAvailableProjects.length; i < n; i++) {
  239. if (project == mAvailableProjects[i].getProject()) {
  240. selectionIndex = i + 1; // +1: Slot 0 is reserved for "Choose Project"
  241. select(selectionIndex);
  242. break;
  243. }
  244. }
  245. }
  246. /**
  247. * Click handler for the button: Open the {@link ProjectChooserHelper}
  248. * dialog for selecting a new project.
  249. */
  250. @Override
  251. public void widgetSelected(SelectionEvent e) {
  252. int selectionIndex = getSelectionIndex();
  253. if (selectionIndex > 0 && mAvailableProjects != null
  254. && selectionIndex <= mAvailableProjects.length) {
  255. // selection index 0 is "Choose Project", all other projects are offset
  256. // by 1 from the selection index
  257. mProject = mAvailableProjects[selectionIndex - 1].getProject();
  258. } else {
  259. mProject = null;
  260. }
  261. }
  262. @Override
  263. public void widgetDefaultSelected(SelectionEvent e) {
  264. }
  265. @Override
  266. protected void checkSubclass() {
  267. // Disable the check that prevents subclassing of SWT components
  268. }
  269. }
  270. }