• 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.annotations.NonNull;
20 import com.android.ide.eclipse.adt.internal.project.AndroidNature;
21 import com.android.ide.eclipse.adt.internal.project.ProjectHelper;
22 
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.core.runtime.jobs.Job;
30 import org.eclipse.jdt.core.JavaModelException;
31 import org.eclipse.jface.action.IAction;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.ui.IObjectActionDelegate;
35 import org.eclipse.ui.IWorkbenchPart;
36 import org.eclipse.ui.IWorkbenchWindow;
37 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
38 
39 import java.util.Iterator;
40 
41 /**
42  * Action to fix the project properties:
43  * <ul>
44  * <li>Make sure the framework archive is present with the link to the java
45  * doc</li>
46  * </ul>
47  */
48 public class FixProjectAction implements IObjectActionDelegate {
49 
50     private ISelection mSelection;
51 
52     /**
53      * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
54      */
55     @Override
setActivePart(IAction action, IWorkbenchPart targetPart)56     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
57     }
58 
59     @Override
run(IAction action)60     public void run(IAction action) {
61         if (mSelection instanceof IStructuredSelection) {
62 
63             for (Iterator<?> it = ((IStructuredSelection) mSelection).iterator();
64                     it.hasNext();) {
65                 Object element = it.next();
66                 IProject project = null;
67                 if (element instanceof IProject) {
68                     project = (IProject) element;
69                 } else if (element instanceof IAdaptable) {
70                     project = (IProject) ((IAdaptable) element)
71                             .getAdapter(IProject.class);
72                 }
73                 if (project != null) {
74                     fixProject(project);
75                 }
76             }
77         }
78     }
79 
80     @Override
selectionChanged(IAction action, ISelection selection)81     public void selectionChanged(IAction action, ISelection selection) {
82         mSelection = selection;
83     }
84 
fixProject(final IProject project)85     private void fixProject(final IProject project) {
86         createFixProjectJob(project).schedule();
87     }
88 
89     /**
90      * Creates a job to fix the project
91      *
92      * @param project the project to fix
93      * @return a job to perform the fix (not yet scheduled)
94      */
95     @NonNull
createFixProjectJob(@onNull final IProject project)96     public static Job createFixProjectJob(@NonNull final IProject project) {
97         return new Job("Fix Project Properties") {
98 
99             @Override
100             protected IStatus run(IProgressMonitor monitor) {
101                 try {
102                     if (monitor != null) {
103                         monitor.beginTask("Fix Project Properties", 6);
104                     }
105 
106                     ProjectHelper.fixProject(project);
107                     if (monitor != null) {
108                         monitor.worked(1);
109                     }
110 
111                     // fix the nature order to have the proper project icon
112                     ProjectHelper.fixProjectNatureOrder(project);
113                     if (monitor != null) {
114                         monitor.worked(1);
115                     }
116 
117                     // now we fix the builders
118                     AndroidNature.configureResourceManagerBuilder(project);
119                     if (monitor != null) {
120                         monitor.worked(1);
121                     }
122 
123                     AndroidNature.configurePreBuilder(project);
124                     if (monitor != null) {
125                         monitor.worked(1);
126                     }
127 
128                     AndroidNature.configureApkBuilder(project);
129                     if (monitor != null) {
130                         monitor.worked(1);
131                     }
132 
133                     return Status.OK_STATUS;
134                 } catch (JavaModelException e) {
135                     return e.getJavaModelStatus();
136                 } catch (CoreException e) {
137                     return e.getStatus();
138                 } finally {
139                     if (monitor != null) {
140                         monitor.done();
141                     }
142                 }
143             }
144         };
145     }
146 
147     /**
148      * @see IWorkbenchWindowActionDelegate#init
149      */
150     public void init(IWorkbenchWindow window) {
151         // pass
152     }
153 
154 }
155