aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/exportgradle/ConfirmationPage.java
blob: 1f236fb2bfc8097b7de5e4d8123ce5efbb838eb6 (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
/*
 * Copyright (C) 2013 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.exportgradle;

import com.google.common.collect.Lists;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.model.WorkbenchLabelProvider;

import java.io.File;
import java.util.Collection;
import java.util.List;

/**
 * Confirmation page to review the actual project export
 * list and see warning about existing files.
 *
 */
public class ConfirmationPage extends WizardPage {

    private final ProjectSetupBuilder mBuilder;
    private TableViewer mTableViewer;
    private Label mModuleDescription1;
    private Label mModuleDescription2;
    private Label mModuleDescription3;
    private Label mProjectRootLabel;
    private Label mProjectRootWarning;
    private List<IJavaProject> mOverrideProjects;
    private boolean mOverrideWarning;
    private Button mForceOverride;

    public ConfirmationPage(ProjectSetupBuilder builder) {
        super("ConfirmationPage"); //$NON-NLS-1$
        mBuilder = builder;
        setPageComplete(false);
        setTitle(ExportMessages.PageTitle);
        setDescription(ExportMessages.PageDescription);
    }

    @Override
    public void createControl(Composite parent) {
        initializeDialogUnits(parent);
        GridData data;

        Composite workArea = new Composite(parent, SWT.NONE);
        setControl(workArea);

        workArea.setLayout(new GridLayout());
        workArea.setLayoutData(new GridData(GridData.FILL_BOTH
                | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));

        Label title = new Label(workArea, SWT.NONE);
        title.setText("Please review the export options.");

        Group group = new Group(workArea, SWT.NONE);
        group.setText("Project root");
        group.setLayout(new GridLayout());
        group.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

        mProjectRootLabel = new Label(group, SWT.NONE);
        mProjectRootLabel.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

        mProjectRootWarning = new Label(group, SWT.NONE);
        mProjectRootWarning.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

        Group group2 = new Group(workArea, SWT.NONE);
        group2.setText("Exported Modules");
        group2.setLayout(new GridLayout());
        group2.setLayoutData(data = new GridData(SWT.FILL, SWT.FILL, true, true));
        data.heightHint = 300;

        Table table = new Table(group2, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        mTableViewer = new TableViewer(table);
        table.setLayout(new TableLayout());
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        mTableViewer.setContentProvider(new IStructuredContentProvider() {
            @Override
            public Object[] getElements(Object inputElement) {
                if (inputElement instanceof ProjectSetupBuilder) {
                    ProjectSetupBuilder builder = (ProjectSetupBuilder) inputElement;
                    Collection<GradleModule> modules = builder.getModules();
                    Object[] array = new Object[modules.size()];
                    int i = 0;
                    for (GradleModule module : modules) {
                        array[i++] = module.getJavaProject();
                    }

                    return array;
                }

                return null;
            }

            @Override
            public void dispose() {
            }

            @Override
            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            }

        });
        mTableViewer.setLabelProvider(new WorkbenchLabelProvider() {
            @Override
            protected String decorateText(String input, Object element) {
                if (element instanceof IJavaProject) {
                    IJavaProject javaProject = (IJavaProject) element;
                    StringBuilder sb = new StringBuilder(input);
                    if (!mBuilder.isOriginalProject(javaProject)) {
                        sb.append('*');
                    }
                    // TODO: decorate icon instead?
                    if (mOverrideProjects.contains(javaProject)) {
                        sb.append(" (1 warning)");
                    }

                    return sb.toString();
                }

                return input;
            }
        });
        mTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                Object firstElement = selection.getFirstElement();
                if (firstElement instanceof IJavaProject) {
                    GradleModule module = mBuilder.getModule((IJavaProject) firstElement);
                    if (mBuilder.getOriginalModules().contains(module)) {
                        mModuleDescription1.setText("Exported because selected in previous page.");
                    } else {
                        List<GradleModule> list = mBuilder.getShortestDependencyTo(module);
                        StringBuilder sb = new StringBuilder();
                        for (GradleModule m : list) {
                            if (sb.length() > 0) {
                                sb.append(" > ");
                            }
                            sb.append(m.getJavaProject().getProject().getName());
                        }
                        mModuleDescription1.setText("Dependency chain: " + sb);
                    }
                    mModuleDescription2.setText("Path: " + module.getPath());

                    if (mOverrideProjects.contains(module.getJavaProject())) {
                        mModuleDescription3.setText(
                                "WARNING: build.gradle already exists for this project");
                    } else {
                        mModuleDescription3.setText("");
                    }
                } else {
                    mModuleDescription1.setText("");
                    mModuleDescription2.setText("");
                    mModuleDescription3.setText("");
                }
            }
        });

        mModuleDescription1 = new Label(group2, SWT.NONE);
        mModuleDescription1.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
        mModuleDescription2 = new Label(group2, SWT.NONE);
        mModuleDescription2.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
        mModuleDescription3 = new Label(group2, SWT.NONE);
        mModuleDescription3.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

        mForceOverride = new Button(workArea, SWT.CHECK);
        mForceOverride.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
        mForceOverride.setText("Force overriding of existing files");
        mForceOverride.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                updateEnablement();
            }
        });

        setControl(workArea);
        Dialog.applyDialogFont(parent);
    }

    /**
     * Get list of projects which have already a buildfile.
     *
     * @param javaProjects list of IJavaProject objects
     * @return set of project names
     */
    private void computeOverride(String commonRoot) {
        mOverrideProjects = Lists.newArrayList();
        for (GradleModule module : mBuilder.getModules()) {
            if (new File(module.getProject().getLocation().toFile(),
                    BuildFileCreator.BUILD_FILE).exists()) {
                mOverrideProjects.add(module.getJavaProject());
            }
        }

        // also check on the root settings.gradle/build.gradle
        boolean settingsFile = new File(commonRoot, BuildFileCreator.SETTINGS_FILE).exists();
        boolean buildFile = new File(commonRoot, BuildFileCreator.BUILD_FILE).exists();
        if (settingsFile && buildFile) {
             mProjectRootWarning.setText(
                     "WARNING: build.gradle/settings.gradle already exists at this location.");
        } else if (settingsFile) {
            mProjectRootWarning.setText(
                    "WARNING: settings.gradle already exists at this location.");
        } else if (buildFile) {
            mProjectRootWarning.setText("WARNING: build.gradle already exists at this location.");
        }

        mOverrideWarning = mOverrideProjects.size() > 0 || settingsFile || buildFile;
    }

    /**
     * Enables/disables the finish button on the wizard and displays error messages as needed.
     */
    private void updateEnablement() {
        if (mOverrideWarning && !mForceOverride.getSelection()) {
            setErrorMessage("Enable overriding of existing files before clicking Finish");
            mBuilder.setCanGenerate(false);
        } else {
            setErrorMessage(null);
            mBuilder.setCanGenerate(true);
        }
        setPageComplete(false);
        getContainer().updateButtons();
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if (visible) {
            mProjectRootWarning.setText("");

            String commonRoot = mBuilder.getCommonRoot().toOSString();
            computeOverride(commonRoot);
            mProjectRootLabel.setText(commonRoot);
            mTableViewer.setInput(mBuilder);
            mTableViewer.getTable().setFocus();
            mBuilder.setCanFinish(false);
            mBuilder.setCanGenerate(true);
            updateEnablement();
        }
    }
}