Home | History | Annotate | Download | only in wizards
      1 /*
      2  * Copyright (C) 2011 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.ndk.internal.wizards;
     18 
     19 import com.android.ide.eclipse.adt.ndk.internal.Activator;
     20 import com.android.ide.eclipse.adt.ndk.internal.NdkManager;
     21 
     22 import org.eclipse.cdt.core.CCorePlugin;
     23 import org.eclipse.cdt.make.core.MakeCorePlugin;
     24 import org.eclipse.cdt.ui.CUIPlugin;
     25 import org.eclipse.core.resources.IProject;
     26 import org.eclipse.core.resources.IWorkspace;
     27 import org.eclipse.core.resources.IWorkspaceRunnable;
     28 import org.eclipse.core.resources.ResourcesPlugin;
     29 import org.eclipse.core.runtime.CoreException;
     30 import org.eclipse.core.runtime.IProgressMonitor;
     31 import org.eclipse.core.runtime.NullProgressMonitor;
     32 import org.eclipse.jface.operation.IRunnableWithProgress;
     33 import org.eclipse.jface.wizard.Wizard;
     34 import org.eclipse.ui.IWorkbenchWindow;
     35 import org.eclipse.ui.WorkbenchException;
     36 
     37 import java.lang.reflect.InvocationTargetException;
     38 import java.util.HashMap;
     39 import java.util.Map;
     40 
     41 public class AddNativeWizard extends Wizard {
     42 
     43     private final IProject mProject;
     44     private final IWorkbenchWindow mWindow;
     45 
     46     private AddNativeWizardPage mAddNativeWizardPage;
     47     private Map<String, String> mTemplateArgs = new HashMap<String, String>();
     48 
     49     public AddNativeWizard(IProject project, IWorkbenchWindow window) {
     50         mProject = project;
     51         mWindow = window;
     52         mTemplateArgs.put(NdkManager.LIBRARY_NAME, project.getName());
     53     }
     54 
     55     @Override
     56     public void addPages() {
     57         mAddNativeWizardPage = new AddNativeWizardPage(mTemplateArgs);
     58         addPage(mAddNativeWizardPage);
     59     }
     60 
     61     @Override
     62     public boolean performFinish() {
     63         // Switch to C/C++ Perspective
     64         try {
     65             mWindow.getWorkbench().showPerspective(CUIPlugin.ID_CPERSPECTIVE, mWindow);
     66         } catch (WorkbenchException e1) {
     67             Activator.log(e1);
     68         }
     69 
     70         mAddNativeWizardPage.updateArgs(mTemplateArgs);
     71 
     72         IRunnableWithProgress op = new IRunnableWithProgress() {
     73             public void run(IProgressMonitor monitor) throws InvocationTargetException,
     74                     InterruptedException {
     75                 IWorkspaceRunnable op1 = new IWorkspaceRunnable() {
     76                     public void run(IProgressMonitor monitor1) throws CoreException {
     77                         // Convert to CDT project
     78                         CCorePlugin.getDefault().convertProjectToCC(mProject, monitor1,
     79                                 MakeCorePlugin.MAKE_PROJECT_ID);
     80                         // Set up build information
     81                         new NdkWizardHandler().convertProject(mProject, monitor1);
     82                         // Run the template
     83                         NdkManager.addNativeSupport(mProject, mTemplateArgs, monitor1);
     84                     }
     85                 };
     86                 // TODO run from a job
     87                 IWorkspace workspace = ResourcesPlugin.getWorkspace();
     88                 try {
     89                     workspace.run(op1, workspace.getRoot(), 0, new NullProgressMonitor());
     90                 } catch (CoreException e) {
     91                     throw new InvocationTargetException(e);
     92                 }
     93             }
     94         };
     95         try {
     96             getContainer().run(false, true, op);
     97             return true;
     98         } catch (InterruptedException e) {
     99             Activator.log(e);
    100             return false;
    101         } catch (InvocationTargetException e) {
    102             Activator.log(e);
    103             return false;
    104         }
    105     }
    106 
    107 }
    108