aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidClasspathContainerPage.java
blob: b02765012ac4ecebe2612b68070cf36af91632bd (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.adt.internal.project;

import com.android.ide.eclipse.adt.AdtConstants;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
import org.eclipse.jdt.ui.wizards.IClasspathContainerPage;
import org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import java.util.Arrays;

public class AndroidClasspathContainerPage extends WizardPage implements IClasspathContainerPage,
        IClasspathContainerPageExtension {

    private IProject mOwnerProject;

    private String mLibsProjectName;

    private Combo mProjectsCombo;

    private IStatus mCurrStatus;

    private boolean mPageVisible;

    public AndroidClasspathContainerPage() {
        super("AndroidClasspathContainerPage"); //$NON-NLS-1$
        mPageVisible = false;
        mCurrStatus = new StatusInfo();
        setTitle("Android Libraries");
        setDescription("This container manages classpath entries for Android container");
    }

    @Override
    public IClasspathEntry getSelection() {
        IPath path = new Path(AdtConstants.CONTAINER_FRAMEWORK);

        final int index = this.mProjectsCombo.getSelectionIndex();
        if (index != -1) {
            final String selectedProjectName = this.mProjectsCombo.getItem(index);

            if (this.mOwnerProject == null
                    || !selectedProjectName.equals(this.mOwnerProject.getName())) {
                path = path.append(selectedProjectName);
            }
        }

        return JavaCore.newContainerEntry(path);
    }

    @Override
    public void setSelection(final IClasspathEntry cpentry) {
        final IPath path = cpentry == null ? null : cpentry.getPath();

        if (path == null || path.segmentCount() == 1) {
            if (this.mOwnerProject != null) {
                this.mLibsProjectName = this.mOwnerProject.getName();
            }
        } else {
            this.mLibsProjectName = path.segment(1);
        }
    }

    @Override
    public void createControl(final Composite parent) {
        final Composite composite = new Composite(parent, SWT.NONE);
        composite.setLayout(new GridLayout(2, false));

        final Label label = new Label(composite, SWT.NONE);
        label.setText("Project:");

        final String[] androidProjects = getAndroidProjects();

        this.mProjectsCombo = new Combo(composite, SWT.READ_ONLY);
        this.mProjectsCombo.setItems(androidProjects);

        final int index;

        if (this.mOwnerProject != null) {
            index = indexOf(androidProjects, this.mLibsProjectName);
        } else {
            if (this.mProjectsCombo.getItemCount() > 0) {
                index = 0;
            } else {
                index = -1;
            }
        }

        if (index != -1) {
            this.mProjectsCombo.select(index);
        }

        final GridData gd = new GridData();
        gd.grabExcessHorizontalSpace = true;
        gd.minimumWidth = 100;

        this.mProjectsCombo.setLayoutData(gd);

        setControl(composite);
    }

    @Override
    public boolean finish() {
        return true;
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        mPageVisible = visible;
        // policy: wizards are not allowed to come up with an error message
        if (visible && mCurrStatus.matches(IStatus.ERROR)) {
            StatusInfo status = new StatusInfo();
            status.setError(""); //$NON-NLS-1$
            mCurrStatus = status;
        }
        updateStatus(mCurrStatus);
    }

    /**
     * Updates the status line and the OK button according to the given status
     *
     * @param status status to apply
     */
    protected void updateStatus(IStatus status) {
        mCurrStatus = status;
        setPageComplete(!status.matches(IStatus.ERROR));
        if (mPageVisible) {
            StatusUtil.applyToStatusLine(this, status);
        }
    }

    /**
     * Updates the status line and the OK button according to the status
     * evaluate from an array of status. The most severe error is taken. In case
     * that two status with the same severity exists, the status with lower
     * index is taken.
     *
     * @param status the array of status
     */
    protected void updateStatus(IStatus[] status) {
        updateStatus(StatusUtil.getMostSevere(status));
    }

    @Override
    public void initialize(final IJavaProject project, final IClasspathEntry[] currentEntries) {
        this.mOwnerProject = (project == null ? null : project.getProject());
    }

    private static String[] getAndroidProjects() {
        IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
        final String[] names = new String[projects.length];
        for (int i = 0; i < projects.length; i++) {
            names[i] = projects[i].getName();
        }
        Arrays.sort(names);
        return names;
    }

    private static int indexOf(final String[] array, final String str) {
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(str)) {
                return i;
            }
        }
        return -1;
    }

}