• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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.actions;
18 
19 import com.android.ide.eclipse.adt.AdtPlugin;
20 import com.android.ide.eclipse.adt.AndroidConstants;
21 import com.android.ide.eclipse.adt.internal.project.ProjectHelper;
22 
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IProjectDescription;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IAdaptable;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.jobs.Job;
31 import org.eclipse.jdt.core.IJavaProject;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.core.JavaModelException;
34 import org.eclipse.jface.action.IAction;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.ui.IObjectActionDelegate;
38 import org.eclipse.ui.IWorkbenchPart;
39 
40 import java.util.Iterator;
41 
42 /**
43  * Converts a project created with the activity creator into an
44  * Android project.
45  */
46 public class ConvertToAndroidAction implements IObjectActionDelegate {
47 
48     private ISelection mSelection;
49 
50     /*
51      * (non-Javadoc)
52      *
53      * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
54      */
setActivePart(IAction action, IWorkbenchPart targetPart)55     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
56         // pass
57     }
58 
59     /*
60      * (non-Javadoc)
61      *
62      * @see IActionDelegate#run(IAction)
63      */
run(IAction action)64     public void run(IAction action) {
65         if (mSelection instanceof IStructuredSelection) {
66             for (Iterator<?> it = ((IStructuredSelection)mSelection).iterator(); it.hasNext();) {
67                 Object element = it.next();
68                 IProject project = null;
69                 if (element instanceof IProject) {
70                     project = (IProject)element;
71                 } else if (element instanceof IAdaptable) {
72                     project = (IProject)((IAdaptable)element).getAdapter(IProject.class);
73                 }
74                 if (project != null) {
75                     convertProject(project);
76                 }
77             }
78         }
79     }
80 
81     /*
82      * (non-Javadoc)
83      *
84      * @see IActionDelegate#selectionChanged(IAction, ISelection)
85      */
selectionChanged(IAction action, ISelection selection)86     public void selectionChanged(IAction action, ISelection selection) {
87         mSelection = selection;
88     }
89 
90     /**
91      * Toggles sample nature on a project
92      *
93      * @param project to have sample nature added or removed
94      */
convertProject(final IProject project)95     private void convertProject(final IProject project) {
96         new Job("Convert Project") {
97             @Override
98             protected IStatus run(IProgressMonitor monitor) {
99                 try {
100                     if (monitor != null) {
101                         monitor.beginTask(String.format(
102                                 "Convert %1$s to Android", project.getName()), 5);
103                     }
104 
105                     IProjectDescription description = project.getDescription();
106                     String[] natures = description.getNatureIds();
107 
108                     // check if the project already has the android nature.
109                     for (int i = 0; i < natures.length; ++i) {
110                         if (AndroidConstants.NATURE_DEFAULT.equals(natures[i])) {
111                             // we shouldn't be here as the visibility of the item
112                             // is dependent on the project.
113                             return new Status(Status.WARNING, AdtPlugin.PLUGIN_ID,
114                                     "Project is already an Android project");
115                         }
116                     }
117 
118                     if (monitor != null) {
119                         monitor.worked(1);
120                     }
121 
122                     String[] newNatures = new String[natures.length + 1];
123                     System.arraycopy(natures, 0, newNatures, 1, natures.length);
124                     newNatures[0] = AndroidConstants.NATURE_DEFAULT;
125 
126                     // set the new nature list in the project
127                     description.setNatureIds(newNatures);
128                     project.setDescription(description, null);
129                     if (monitor != null) {
130                         monitor.worked(1);
131                     }
132 
133                     // Fix the classpath entries.
134                     // get a java project
135                     IJavaProject javaProject = JavaCore.create(project);
136                     ProjectHelper.fixProjectClasspathEntries(javaProject);
137                     if (monitor != null) {
138                         monitor.worked(1);
139                     }
140 
141                     return Status.OK_STATUS;
142                 } catch (JavaModelException e) {
143                     return e.getJavaModelStatus();
144                 } catch (CoreException e) {
145                     return e.getStatus();
146                 } finally {
147                     if (monitor != null) {
148                         monitor.done();
149                     }
150                 }
151             }
152         }.schedule();
153     }
154 }
155