aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.pdt/src/com/android/ide/eclipse/pdt/internal/DevTreeProjectProvider.java
blob: b44d2cb49b988b52f74373d440439de022b0517c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.pdt.internal;

import com.android.ide.eclipse.pdt.PdtPlugin;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ListDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Base class providing a {@link #getProject()} method to find the project matching the dev tree.
 *
 */
class DevTreeProjectProvider {
    protected IProject getProject() {
        // Get the list of project for the current workspace
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        List<IProject> projects = Arrays.asList(workspace.getRoot().getProjects());
        if (projects.size() == 0) {
            return null;
        }

        // get the location of the Dev tree
        String devTree = PdtPlugin.getDevTree();
        IPath devTreePath = null;
        if (devTree != null) {
            devTreePath = new Path(devTree);
        }

        // filter the list of projects in workspace by 2 conditions:
        //        1. Only look at Java projects
        //        2. If dev tree location is set, only look at projects within the dev tree

        List<IProject> devTreeProjects = new ArrayList<IProject>(projects.size());

        for (IProject p: projects) {
            if (!p.isOpen()) {
                continue;
            }

            if (!hasJavaNature(p)) {
                continue;
            }

            if (devTreePath == null || devTreePath.isPrefixOf(p.getLocation())) {
                devTreeProjects.add(p);
            }
        }

        return selectProject(devTreeProjects);
    }

    private IProject selectProject(List<IProject> devTreeProjects) {
        if (devTreeProjects.size() == 0) {
            return null;
        }

        if (devTreeProjects.size() == 1) {
            return devTreeProjects.get(0);
        }

        // if there are multiple choices, prompt the user
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (window == null) {
            return null;
        }

        ListDialog dlg = new ListDialog(window.getShell());
        dlg.setMessage("Select Project");
        dlg.setInput(devTreeProjects);
        dlg.setContentProvider(new IStructuredContentProvider() {
            @Override
            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            }

            @Override
            public void dispose() {
            }

            @Override
            public Object[] getElements(Object inputElement) {
                return ((List<?>) inputElement).toArray();
            }
        });
        dlg.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                return ((IProject) element).getName();
            }
        });
        dlg.setHelpAvailable(false);

        if (dlg.open() == Window.OK) {
            Object[] selectedMatches = dlg.getResult();
            if (selectedMatches.length > 0) {
                return (IProject) selectedMatches[0];
            }
        }

        return null;
    }

    private boolean hasJavaNature(IProject p) {
        try {
            return p.hasNature(JavaCore.NATURE_ID);
        } catch (CoreException e) {
            return false;
        }
    }
}