aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/wizards/AddNativeWizard.java
blob: b3675ed274c242dfc5ccb20dbc2466c7ac5c9f09 (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
/*
 * 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.ndk.internal.wizards;

import com.android.ide.eclipse.ndk.internal.Activator;
import com.android.ide.eclipse.ndk.internal.NdkManager;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.WorkbenchException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class AddNativeWizard extends Wizard {

    private final IProject mProject;
    private final IWorkbenchWindow mWindow;

    private AddNativeWizardPage mAddNativeWizardPage;
    private Map<String, String> mTemplateArgs = new HashMap<String, String>();

    public AddNativeWizard(IProject project, IWorkbenchWindow window) {
        mProject = project;
        mWindow = window;
        mTemplateArgs.put(NdkManager.LIBRARY_NAME, project.getName());
    }

    @Override
    public void addPages() {
        mAddNativeWizardPage = new AddNativeWizardPage(mTemplateArgs);
        addPage(mAddNativeWizardPage);
    }

    @Override
    public boolean performFinish() {
        // Switch to C/C++ Perspective
        try {
            mWindow.getWorkbench().showPerspective(CUIPlugin.ID_CPERSPECTIVE, mWindow);
        } catch (WorkbenchException e1) {
            Activator.log(e1);
        }

        mAddNativeWizardPage.updateArgs(mTemplateArgs);

        IRunnableWithProgress op = new IRunnableWithProgress() {
            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException,
                    InterruptedException {
                IWorkspaceRunnable op1 = new IWorkspaceRunnable() {
                    @Override
                    public void run(IProgressMonitor monitor1) throws CoreException {
                        // Convert to CDT project
                        CCorePlugin.getDefault().convertProjectToCC(mProject, monitor1,
                                MakeCorePlugin.MAKE_PROJECT_ID);
                        // Set up build information
                        new NdkWizardHandler().convertProject(mProject, monitor1);

                        // When using CDT 8.1.x, disable the language settings provider mechanism
                        // for scanner discovery. Use the classloader to load the class since it
                        // will not be available pre 8.1.
                        try {
                            @SuppressWarnings("rawtypes")
                            Class c = getClass().getClassLoader().loadClass(
                                    "org.eclipse.cdt.core.language.settings.providers.ScannerDiscoveryLegacySupport"); //$NON-NLS-1$

                            @SuppressWarnings("unchecked")
                            Method m = c.getMethod(
                                    "setLanguageSettingsProvidersFunctionalityEnabled", //$NON-NLS-1$
                                    IProject.class, boolean.class);

                            m.invoke(null, mProject, false);
                        } catch (Exception e) {
                            // ignore all exceptions: On pre 8.1.x CDT, this class will not be
                            // found, but this options only needs to be set in 8.1.x
                        }

                        // Run the template
                        NdkManager.addNativeSupport(mProject, mTemplateArgs, monitor1);
                    }
                };
                // TODO run from a job
                IWorkspace workspace = ResourcesPlugin.getWorkspace();
                try {
                    workspace.run(op1, workspace.getRoot(), 0, new NullProgressMonitor());
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        try {
            getContainer().run(false, true, op);
            return true;
        } catch (InterruptedException e) {
            Activator.log(e);
            return false;
        } catch (InvocationTargetException e) {
            Activator.log(e);
            return false;
        }
    }

}