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