Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
      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.pdt.internal;
     18 
     19 import com.android.ide.eclipse.pdt.PdtPlugin;
     20 
     21 import org.eclipse.core.resources.IProject;
     22 import org.eclipse.core.resources.IWorkspace;
     23 import org.eclipse.core.resources.ResourcesPlugin;
     24 import org.eclipse.core.runtime.CoreException;
     25 import org.eclipse.core.runtime.IPath;
     26 import org.eclipse.core.runtime.Path;
     27 import org.eclipse.jdt.core.JavaCore;
     28 import org.eclipse.jface.viewers.IStructuredContentProvider;
     29 import org.eclipse.jface.viewers.LabelProvider;
     30 import org.eclipse.jface.viewers.Viewer;
     31 import org.eclipse.jface.window.Window;
     32 import org.eclipse.ui.IWorkbenchWindow;
     33 import org.eclipse.ui.PlatformUI;
     34 import org.eclipse.ui.dialogs.ListDialog;
     35 
     36 import java.util.ArrayList;
     37 import java.util.Arrays;
     38 import java.util.List;
     39 
     40 /**
     41  * Base class providing a {@link #getProject()} method to find the project matching the dev tree.
     42  *
     43  */
     44 class DevTreeProjectProvider {
     45     protected IProject getProject() {
     46         // Get the list of project for the current workspace
     47         IWorkspace workspace = ResourcesPlugin.getWorkspace();
     48         List<IProject> projects = Arrays.asList(workspace.getRoot().getProjects());
     49         if (projects.size() == 0) {
     50             return null;
     51         }
     52 
     53         // get the location of the Dev tree
     54         String devTree = PdtPlugin.getDevTree();
     55         IPath devTreePath = null;
     56         if (devTree != null) {
     57             devTreePath = new Path(devTree);
     58         }
     59 
     60         // filter the list of projects in workspace by 2 conditions:
     61         //        1. Only look at Java projects
     62         //        2. If dev tree location is set, only look at projects within the dev tree
     63 
     64         List<IProject> devTreeProjects = new ArrayList<IProject>(projects.size());
     65 
     66         for (IProject p: projects) {
     67             if (!p.isOpen()) {
     68                 continue;
     69             }
     70 
     71             if (!hasJavaNature(p)) {
     72                 continue;
     73             }
     74 
     75             if (devTreePath == null || devTreePath.isPrefixOf(p.getLocation())) {
     76                 devTreeProjects.add(p);
     77             }
     78         }
     79 
     80         return selectProject(devTreeProjects);
     81     }
     82 
     83     private IProject selectProject(List<IProject> devTreeProjects) {
     84         if (devTreeProjects.size() == 0) {
     85             return null;
     86         }
     87 
     88         if (devTreeProjects.size() == 1) {
     89             return devTreeProjects.get(0);
     90         }
     91 
     92         // if there are multiple choices, prompt the user
     93         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
     94         if (window == null) {
     95             return null;
     96         }
     97 
     98         ListDialog dlg = new ListDialog(window.getShell());
     99         dlg.setMessage("Select Project");
    100         dlg.setInput(devTreeProjects);
    101         dlg.setContentProvider(new IStructuredContentProvider() {
    102             @Override
    103             public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    104             }
    105 
    106             @Override
    107             public void dispose() {
    108             }
    109 
    110             @Override
    111             public Object[] getElements(Object inputElement) {
    112                 return ((List<?>) inputElement).toArray();
    113             }
    114         });
    115         dlg.setLabelProvider(new LabelProvider() {
    116             @Override
    117             public String getText(Object element) {
    118                 return ((IProject) element).getName();
    119             }
    120         });
    121         dlg.setHelpAvailable(false);
    122 
    123         if (dlg.open() == Window.OK) {
    124             Object[] selectedMatches = dlg.getResult();
    125             if (selectedMatches.length > 0) {
    126                 return (IProject) selectedMatches[0];
    127             }
    128         }
    129 
    130         return null;
    131     }
    132 
    133     private boolean hasJavaNature(IProject p) {
    134         try {
    135             return p.hasNature(JavaCore.NATURE_ID);
    136         } catch (CoreException e) {
    137             return false;
    138         }
    139     }
    140 }
    141