1   package com.stateofflow.eclipse.metrics.properties;
2   
3   import java.lang.reflect.InvocationTargetException;
4   
5   import org.eclipse.core.resources.IProject;
6   import org.eclipse.core.resources.IncrementalProjectBuilder;
7   import org.eclipse.core.runtime.CoreException;
8   import org.eclipse.core.runtime.IProgressMonitor;
9   import org.eclipse.core.runtime.IStatus;
10  import org.eclipse.core.runtime.Status;
11  import org.eclipse.jdt.core.IJavaProject;
12  import org.eclipse.jface.dialogs.ErrorDialog;
13  import org.eclipse.jface.dialogs.ProgressMonitorDialog;
14  import org.eclipse.jface.operation.IRunnableWithProgress;
15  import org.eclipse.swt.SWT;
16  import org.eclipse.swt.layout.GridData;
17  import org.eclipse.swt.layout.GridLayout;
18  import org.eclipse.swt.widgets.Button;
19  import org.eclipse.swt.widgets.Composite;
20  import org.eclipse.swt.widgets.Control;
21  import org.eclipse.ui.dialogs.PropertyPage;
22  
23  import com.stateofflow.eclipse.metrics.MetricsPlugin;
24  import com.stateofflow.eclipse.metrics.properties.exclusions.View;
25  
26  public final class MetricsPropertiesPage extends PropertyPage {
27      private View excludedResources;
28      private Button enabledButton;
29      private boolean rebuildRequired;
30  
31      private void activatePlugin() throws InvocationTargetException, InterruptedException {
32          getNatureManager().addNatureToProject(getShell());
33          rebuildRequired = true;
34      }
35  
36      private void buildProject() throws InvocationTargetException, InterruptedException {
37          if (rebuildRequired) {
38              rebuild();
39              rebuildRequired = false;
40          }
41      }
42  
43      protected Control createContents(final Composite parent) {
44          noDefaultAndApplyButton();
45          final Composite composite = new Composite(parent, SWT.NULL);
46          try {
47              initialiseLayout(composite);
48              initialiseEnableCheckBox(composite);
49              excludedResources = new View(composite, getProject());
50          } catch (final Exception ex) {
51              showException("Caught exception", ex);
52          }
53  
54          return composite;
55      }
56  
57      private void deactivatePlugin() throws CoreException, InvocationTargetException, InterruptedException {
58          getNatureManager().removeNatureFromProject(getShell());
59      }
60  
61      private NatureManager getNatureManager() {
62          return new NatureManager(getProject());
63      }
64  
65      private IProject getProject() {
66          return getElement() instanceof IProject ? (IProject) getElement() : ((IJavaProject) getElement()).getProject();
67      }
68  
69      private void initialiseEnableCheckBox(final Composite parent) throws CoreException {
70          enabledButton = new Button(parent, SWT.CHECK | SWT.LEFT);
71          enabledButton.setText("Enable Metrics Gathering");
72          enabledButton.setSelection(isPluginActive());
73      }
74  
75      private void initialiseLayout(final Composite parent) {
76          final GridLayout layout = new GridLayout(1, false);
77          parent.setLayout(layout);
78          parent.setLayoutData(new GridData(GridData.FILL_BOTH));
79      }
80  
81      private boolean isPluginActive() throws CoreException {
82          return getNatureManager().isActive();
83      }
84  
85      private void performActivateOrDeactivate() throws CoreException, InvocationTargetException, InterruptedException {
86          boolean pluginActive = isPluginActive();
87  
88          if (enabledButton.getSelection() && !pluginActive) {
89              activatePlugin();
90          } else if (!enabledButton.getSelection() && pluginActive) {
91              deactivatePlugin();
92          }
93      }
94  
95      public boolean performOk() {
96          try {
97              performOkProtected();
98          } catch (final Exception ex) {
99              showException("Caught exception while performing OK", ex);
100             return false;
101         }
102 
103         return true;
104     }
105 
106     private void performOkProtected() throws CoreException, InvocationTargetException, InterruptedException {
107         rebuildRequired |= excludedResources.isModified();
108         excludedResources.store();
109         performActivateOrDeactivate();
110         buildProject();
111     }
112 
113     private void rebuild() throws InvocationTargetException, InterruptedException {
114         new ProgressMonitorDialog(getShell()).run(true, true, new IRunnableWithProgress() {
115             public void run(final IProgressMonitor monitor) {
116                 try {
117                     getProject().build(IncrementalProjectBuilder.FULL_BUILD, monitor);
118                 } catch (final CoreException cex) {
119                     MetricsPlugin.log(cex);
120                 }
121             }
122         });
123     }
124 
125     private void showException(final String message, final Exception ex) {
126         IStatus status;
127         if (ex instanceof CoreException) {
128             status = ((CoreException) ex).getStatus();
129         } else {
130             status = new Status(IStatus.ERROR, MetricsPlugin.PLUGIN_ID, IStatus.OK, message, ex);
131         }
132 
133         MetricsPlugin.log(ex);
134         ErrorDialog.openError(getShell(), "CoreException", ex.getMessage(), status);
135     }
136 }