1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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.adt.internal.wizards.exportgradle;
18 
19 import com.android.ide.eclipse.adt.AdtPlugin;
20 
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.SubMonitor;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.wizard.Wizard;
26 import org.eclipse.jface.wizard.WizardPage;
27 import org.eclipse.ui.IExportWizard;
28 import org.eclipse.ui.IWorkbench;
29 
30 import java.lang.reflect.InvocationTargetException;
31 import java.util.Collection;
32 
33 public class GradleExportWizard extends Wizard implements IExportWizard {
34 
35     private ProjectSetupBuilder mBuilder = new ProjectSetupBuilder();
36 
37     private ProjectSelectionPage mFirstPage;
38     private ConfirmationPage mSecondPage;
39     private FinalPage mFinalPage;
40 
41     /**
42      * Creates buildfile.
43      */
44     @Override
performFinish()45     public boolean performFinish() {
46         if (mBuilder.canGenerate()) {
47             generateBuildfiles(mSecondPage);
48             getContainer().showPage(mFinalPage);
49             return false;
50         }
51 
52         return true;
53     }
54 
55     @Override
addPages()56     public void addPages() {
57         addPage(new ImportInsteadPage());
58         mFirstPage = new ProjectSelectionPage(mBuilder);
59         addPage(mFirstPage);
60         mSecondPage = new ConfirmationPage(mBuilder);
61         addPage(mSecondPage);
62         mFinalPage = new FinalPage(mBuilder);
63         addPage(mFinalPage);
64     }
65 
66     @Override
init(IWorkbench workbench, IStructuredSelection selection)67     public void init(IWorkbench workbench, IStructuredSelection selection) {
68         setWindowTitle(ExportMessages.WindowTitle);
69         setNeedsProgressMonitor(true);
70     }
71 
72     @Override
canFinish()73     public boolean canFinish() {
74         return mBuilder.canFinish() || mBuilder.canGenerate();
75     }
76 
77     /**
78      * Converts Eclipse Java projects to Gradle build files. Displays error dialogs.
79      */
generateBuildfiles(final WizardPage page)80     public boolean generateBuildfiles(final WizardPage page) {
81         IRunnableWithProgress runnable = new IRunnableWithProgress() {
82             @Override
83             public void run(IProgressMonitor pm) throws InterruptedException {
84                 Collection<GradleModule> modules = mBuilder.getModules();
85                 final int count = modules.size();
86 
87                 SubMonitor localmonitor = SubMonitor.convert(pm, ExportMessages.StatusMessage,
88                         count);
89                 BuildFileCreator.createBuildFiles(
90                         mBuilder,
91                         page.getShell(),
92                         localmonitor.newChild(count));
93             }
94         };
95 
96         try {
97             getContainer().run(false, false, runnable);
98         } catch (InvocationTargetException e) {
99             AdtPlugin.log(e, null);
100             return false;
101         } catch (InterruptedException e) {
102             AdtPlugin.log(e, null);
103             return false;
104         }
105         if (page.getErrorMessage() != null) {
106             return false;
107         }
108         return true;
109     }
110 
111 }
112