1 package com.stateofflow.eclipse.metrics.export.wizard;
2
3 import static com.stateofflow.eclipse.metrics.MetricsPlugin.*;
4
5 import java.io.File;
6 import java.lang.reflect.InvocationTargetException;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.resources.ResourcesPlugin;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.QualifiedName;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.operation.IRunnableWithProgress;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.wizard.Wizard;
19 import org.eclipse.ui.INewWizard;
20 import org.eclipse.ui.IWorkbench;
21
22 import com.stateofflow.eclipse.metrics.MetricsPlugin;
23 import com.stateofflow.eclipse.metrics.export.Exporter;
24
25 public final class MetricsExportWizard extends Wizard implements INewWizard {
26 static final QualifiedName DIRECTORY_QUALIFIED_NAME = new QualifiedName(MetricsPlugin.PLUGIN_ID, "exportDirectory");
27 static final QualifiedName HTML_ROWS_PER_PAGE_QUALIFIED_NAME = new QualifiedName(MetricsPlugin.PLUGIN_ID, "htmlExportRowsPerPage");
28 static final QualifiedName HTML_EXPORT_ENABLED_QUALIFIED_NAME = new QualifiedName(MetricsPlugin.PLUGIN_ID, "htmlExportEnabled");
29 static final QualifiedName HTML_IMAGE_PRODUCTION_ENABLED_QUALIFIED_NAME = new QualifiedName(MetricsPlugin.PLUGIN_ID, "htmlImageProductionEnabled");
30 static final QualifiedName HTML_EXPORT_JAVA_ENABLED_QUALIFIED_NAME = new QualifiedName(MetricsPlugin.PLUGIN_ID, "htmlExportJavaEnabled");
31 static final QualifiedName CSV_EXPORT_ENABLED_QUALIFIED_NAME = new QualifiedName(MetricsPlugin.PLUGIN_ID, "csvExportEnabled");
32
33 private MetricsExportWizardPage page;
34 private ISelection selection;
35
36 public MetricsExportWizard() {
37 setNeedsProgressMonitor(true);
38 }
39
40 private void addCsvExporterIfRequired(final File directory, final List<Exporter> list) {
41 if (page.isCsvExportEnabled()) {
42 list.add(getFactory().createCsvExporter(directory));
43 }
44 }
45
46 private void addHtmlExporterIfRequired(final List<Exporter> list) {
47 if (page.isHtmlExportEnabled()) {
48 list.add(getFactory().createHtmlExporter(page.getHtmlExportConfiguration()));
49 }
50 }
51
52 @Override
53 public void addPages() {
54 page = new MetricsExportWizardPage(selection);
55 addPage(page);
56 }
57
58 private Exporter[] createExporterList() {
59 final File directory = page.getExportDirectory();
60
61 final List<Exporter> list = new ArrayList<Exporter>();
62 addCsvExporterIfRequired(directory, list);
63 addHtmlExporterIfRequired(list);
64
65 return list.toArray(new Exporter[list.size()]);
66 }
67
68 public void init(final IWorkbench workbench, final IStructuredSelection selection) {
69 this.selection = selection;
70 }
71
72 private void noteException(final CoreException cex) {
73 MetricsPlugin.log(cex);
74 throw new RuntimeException("An error occurred. Please see the log for details.");
75 }
76
77 @Override
78 public boolean performFinish() {
79 persistExportProperties();
80
81 final Exporter[] exporters = createExporterList();
82 final String project = page.getProject();
83
84 return runOperationForContainer(new ExportRunnable(exporters, project));
85 }
86
87 private void persistBoolean(final QualifiedName key, final boolean value) {
88 persistString(key, Boolean.toString(value));
89 }
90
91 private void persistPath(final QualifiedName key, final File file) {
92 persistString(key, file.getAbsolutePath());
93 }
94
95 private void persistExportProperties() {
96 persistPath(MetricsExportWizard.DIRECTORY_QUALIFIED_NAME, page.getExportDirectory());
97 persistInteger(MetricsExportWizard.HTML_ROWS_PER_PAGE_QUALIFIED_NAME, page.getHtmlRowsPerPage());
98 persistBoolean(MetricsExportWizard.HTML_EXPORT_ENABLED_QUALIFIED_NAME, page.isHtmlExportEnabled());
99 persistBoolean(MetricsExportWizard.HTML_IMAGE_PRODUCTION_ENABLED_QUALIFIED_NAME, page.isHtmlImageProductionEnabled());
100 persistBoolean(MetricsExportWizard.HTML_EXPORT_JAVA_ENABLED_QUALIFIED_NAME, page.isHtmlJavaExportEnabled());
101 persistBoolean(MetricsExportWizard.CSV_EXPORT_ENABLED_QUALIFIED_NAME, page.isCsvExportEnabled());
102 }
103
104 private void persistInteger(final QualifiedName key, final int value) {
105 persistString(key, Integer.toString(value));
106 }
107
108 private void persistString(final QualifiedName key, final String value) {
109 final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(page.getProject());
110 try {
111 project.setPersistentProperty(key, value);
112 } catch (final CoreException cex) {
113 noteException(cex);
114 }
115 }
116
117 private boolean runOperationForContainer(final IRunnableWithProgress op) {
118 try {
119 getContainer().run(true, true, op);
120 } catch (final InterruptedException e) {
121 return false;
122 } catch (final InvocationTargetException e) {
123 MetricsPlugin.log(e.getTargetException());
124 MessageDialog.openError(getShell(), "Error", e.getTargetException().getMessage());
125 return false;
126 }
127
128 return true;
129 }
130 }
131