aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/newproject/TestTargetPage.java
blob: f1c188ae99294b67dd06e09a248b67e6475c36b1 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (C) 2011 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.wizards.newproject;

import com.android.ide.common.xml.ManifestData;
import com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper;
import com.android.ide.eclipse.adt.internal.project.ProjectChooserHelper;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.sdklib.IAndroidTarget;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.JavaElementLabelProvider;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.dialogs.FilteredList;

/**
 * Page shown when creating a test project which lets you choose between testing
 * yourself and testing a different project
 */
class TestTargetPage extends WizardPage implements SelectionListener {
    private final NewProjectWizardState mValues;
    /** Flag used when setting button/text state manually to ignore listener updates */
    private boolean mIgnore;
    private String mLastExistingPackageName;

    private Button mCurrentRadioButton;
    private Button mExistingRadioButton;
    private FilteredList mProjectList;
    private boolean mPageShown;

    /**
     * Create the wizard.
     */
    TestTargetPage(NewProjectWizardState values) {
        super("testTargetPage"); //$NON-NLS-1$
        setTitle("Select Test Target");
        setDescription("Choose a project to test");
        mValues = values;
    }

    /**
     * Create contents of the wizard.
     */
    @Override
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);

        setControl(container);
        container.setLayout(new GridLayout(2, false));

        mCurrentRadioButton = new Button(container, SWT.RADIO);
        mCurrentRadioButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
        mCurrentRadioButton.setText("This project");
        mCurrentRadioButton.addSelectionListener(this);

        mExistingRadioButton = new Button(container, SWT.RADIO);
        mExistingRadioButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
        mExistingRadioButton.setText("An existing Android project:");
        mExistingRadioButton.addSelectionListener(this);

        ILabelProvider labelProvider = new JavaElementLabelProvider(
                JavaElementLabelProvider.SHOW_DEFAULT);
        mProjectList = new FilteredList(container,
                SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE, labelProvider,
                true /*ignoreCase*/, false /*allowDuplicates*/, true /* matchEmptyString*/);
        mProjectList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
        mProjectList.addSelectionListener(this);
    }

    private void initializeList() {
        ProjectChooserHelper helper = new ProjectChooserHelper(getShell(), null /*filter*/);
        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        IJavaModel javaModel = JavaCore.create(workspaceRoot);
        IJavaProject[] androidProjects = helper.getAndroidProjects(javaModel);
        mProjectList.setElements(androidProjects);
        if (mValues.testedProject != null) {
            for (IJavaProject project : androidProjects) {
                if (project.getProject() == mValues.testedProject) {
                    mProjectList.setSelection(new Object[] { project });
                    break;
                }
            }
        } else {
            // No initial selection: force the user to choose
            mProjectList.setSelection(new int[0]);
        }
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        mPageShown = true;

        if (visible) {
            try {
                mIgnore = true;
                mCurrentRadioButton.setSelection(mValues.testingSelf);
                mExistingRadioButton.setSelection(!mValues.testingSelf);
                mProjectList.setEnabled(!mValues.testingSelf);

                if (mProjectList.isEmpty()) {
                    initializeList();
                }
                if (!mValues.testingSelf) {
                    mProjectList.setFocus();
                    IProject project = getSelectedProject();
                    if (project != null) {
                        // The FilteredList seems to -insist- on selecting the first item
                        // in the list, even when the selection is explicitly set to an empty
                        // array. This means the user is looking at a selection, so we need
                        // to also go ahead and select this item in the model such that the
                        // two agree, even if we would have preferred to have no initial
                        // selection.
                        mValues.testedProject = project;
                    }
                }
            } finally {
                mIgnore = false;
            }
        }

        validatePage();
    }

    @Override
    public void widgetSelected(SelectionEvent e) {
        if (mIgnore) {
            return;
        }

        Object source = e.getSource();
        if (source == mExistingRadioButton) {
            mProjectList.setEnabled(true);
            mValues.testingSelf = false;
            setExistingProject(getSelectedProject());
            mProjectList.setFocus();
        } else if (source == mCurrentRadioButton) {
            mProjectList.setEnabled(false);
            mValues.testingSelf = true;
            mValues.testedProject = null;
        } else {
            // The event must be from the project list, which unfortunately doesn't
            // pass itself as the selection event, it passes a reference to some internal
            // table widget that it uses, so we check for this case last
            IProject project = getSelectedProject();
            if (project != mValues.testedProject) {
                setExistingProject(project);
            }
        }

        validatePage();
    }

    private IProject getSelectedProject() {
        Object[] selection = mProjectList.getSelection();
        IProject project = selection != null && selection.length == 1
            ? ((IJavaProject) selection[0]).getProject() : null;
        return project;
    }

    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
    }

    private void setExistingProject(IProject project) {
        mValues.testedProject = project;

        // Try to update the application, package, sdk target and minSdkVersion accordingly
        if (project != null &&
                (!mValues.applicationNameModifiedByUser ||
                 !mValues.packageNameModifiedByUser     ||
                 !mValues.targetModifiedByUser          ||
                 !mValues.minSdkModifiedByUser)) {
            ManifestData manifestData = AndroidManifestHelper.parseForData(project);
            if (manifestData != null) {
                String appName = String.format("%1$sTest", project.getName());
                String packageName = manifestData.getPackage();
                String minSdkVersion = manifestData.getMinSdkVersionString();
                IAndroidTarget sdkTarget = null;
                if (Sdk.getCurrent() != null) {
                    sdkTarget = Sdk.getCurrent().getTarget(project);
                }

                if (packageName == null) {
                    packageName = "";  //$NON-NLS-1$
                }
                mLastExistingPackageName = packageName;

                if (!mValues.projectNameModifiedByUser) {
                    mValues.projectName = appName;
                }

                if (!mValues.applicationNameModifiedByUser) {
                    mValues.applicationName = appName;
                }

                if (!mValues.packageNameModifiedByUser) {
                    packageName += ".test";  //$NON-NLS-1$
                    mValues.packageName = packageName;
                }

                if (!mValues.targetModifiedByUser && sdkTarget != null) {
                    mValues.target = sdkTarget;
                }

                if (!mValues.minSdkModifiedByUser) {
                    if (minSdkVersion != null || sdkTarget != null) {
                        mValues.minSdk = minSdkVersion;
                    }
                    if (sdkTarget == null) {
                        mValues.updateSdkTargetToMatchMinSdkVersion();
                    }
                }
            }
        }

        updateTestTargetPackageField(mLastExistingPackageName);
    }

    /**
     * Updates the test target package name
     *
     * When using the "self-test" option, the packageName argument is ignored and the
     * current value from the project package is used.
     *
     * Otherwise the packageName is used if it is not null.
     */
    private void updateTestTargetPackageField(String packageName) {
        if (mValues.testingSelf) {
            mValues.testTargetPackageName = mValues.packageName;
        } else if (packageName != null) {
            mValues.testTargetPackageName = packageName;
        }
    }

    @Override
    public boolean isPageComplete() {
        // Ensure that the user sees the page and makes a selection
        if (!mPageShown) {
            return false;
        }

        return super.isPageComplete();
    }

    private void validatePage() {
        String error = null;

        if (!mValues.testingSelf) {
            if (mValues.testedProject == null) {
                error = "Please select an existing Android project as a test target.";
            } else if (mValues.projectName.equals(mValues.testedProject.getName())) {
                error = "The main project name and the test project name must be different.";
            }
        }

        // -- update UI & enable finish if there's no error
        setPageComplete(error == null);
        if (error != null) {
            setMessage(error, IMessageProvider.ERROR);
        } else {
            setErrorMessage(null);
            setMessage(null);
        }
    }
}