1   package com.stateofflow.eclipse.metrics.properties;
2   
3   import static java.util.Arrays.*;
4   
5   import java.lang.reflect.InvocationTargetException;
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import org.eclipse.core.resources.IProject;
10  import org.eclipse.core.resources.IProjectDescription;
11  import org.eclipse.core.resources.IResource;
12  import org.eclipse.core.runtime.CoreException;
13  import org.eclipse.core.runtime.IProgressMonitor;
14  import org.eclipse.jface.dialogs.ProgressMonitorDialog;
15  import org.eclipse.jface.operation.IRunnableWithProgress;
16  import org.eclipse.swt.widgets.Shell;
17  
18  import com.stateofflow.eclipse.metrics.MetricsBuilder;
19  import com.stateofflow.eclipse.metrics.MetricsNature;
20  import com.stateofflow.eclipse.metrics.MetricsPlugin;
21  
22  class NatureManager {
23      private final IProject project;
24  
25      public NatureManager(final IProject project) {
26          this.project = project;
27      }
28  
29      public void addNatureToProject(final Shell shell) throws InvocationTargetException, InterruptedException {
30          run(shell, new IRunnableWithProgress() {
31              public void run(final IProgressMonitor monitor) {
32                  try {
33                      final List<String> natures = getProjectNatures();
34                      natures.add(MetricsNature.NATURE_ID);
35                      setNatureIds(monitor, natures);
36                  } catch (final CoreException cex) {
37                      MetricsPlugin.log(cex);
38                  }
39              }
40          });
41      }
42  
43      private IProjectDescription getProjectDescription() throws CoreException {
44          return project.getDescription();
45      }
46  
47      private List<String> getProjectNatures() throws CoreException {
48          return new ArrayList<String>(asList(getProjectDescription().getNatureIds()));
49      }
50  
51      public boolean isActive() throws CoreException {
52          return project.hasNature(MetricsNature.NATURE_ID);
53      }
54  
55      private void removeMarkersForAllResources() throws CoreException {
56          project.deleteMarkers(MetricsBuilder.MARKER_ID, true, IResource.DEPTH_INFINITE);
57      }
58  
59      public void removeNatureFromProject(final Shell shell) throws InvocationTargetException, InterruptedException, CoreException {
60          run(shell, new IRunnableWithProgress() {
61              public void run(final IProgressMonitor monitor) {
62                  try {
63                      final List<String> natures = getProjectNatures();
64                      natures.remove(MetricsNature.NATURE_ID);
65                      setNatureIds(monitor, natures);
66                  } catch (final CoreException cex) {
67                      MetricsPlugin.log(cex);
68                  }
69              }
70          });
71          removeMarkersForAllResources();
72      }
73  
74      private void run(final Shell shell, final IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException {
75          new ProgressMonitorDialog(shell).run(true, true, runnable);
76      }
77  
78      private void setNatureIds(final IProgressMonitor monitor, final List<String> natures) throws CoreException {
79          final IProjectDescription description = getProjectDescription();
80          description.setNatureIds(natures.toArray(new String[natures.size()]));
81          project.setDescription(description, monitor);
82      }
83  }
84