1   package com.stateofflow.eclipse.metrics.export.html;
2   
3   import java.io.File;
4   
5   import org.eclipse.core.resources.IResource;
6   import org.eclipse.core.resources.IWorkspaceRoot;
7   import org.eclipse.core.resources.ResourcesPlugin;
8   import org.eclipse.core.runtime.Path;
9   import org.eclipse.jdt.core.IJavaProject;
10  import org.eclipse.jdt.core.JavaCore;
11  import org.jfree.data.CategoryDataset;
12  
13  import com.stateofflow.eclipse.metrics.collator.MetricsCollator;
14  import com.stateofflow.eclipse.metrics.collator.MetricsCollatorSet;
15  import com.stateofflow.eclipse.metrics.configuration.MetricPresentations;
16  import com.stateofflow.eclipse.metrics.configuration.MetricsConfiguration;
17  import com.stateofflow.eclipse.metrics.export.html.histogram.HistogramFactory;
18  import com.stateofflow.eclipse.metrics.export.html.histogram.HistogramWriter;
19  import com.stateofflow.eclipse.metrics.export.html.histogram.JpegProducer;
20  import com.stateofflow.eclipse.metrics.export.html.histogram.MetricsDataset;
21  import com.stateofflow.eclipse.metrics.metric.MetricId;
22  import com.stateofflow.eclipse.metrics.util.ProgressMonitor;
23  
24  public final class HtmlExportFactory implements HistogramFactory {
25      private static final String JAVA_EXPORT_DIRECTORY = "src";
26      private static final String IMAGES_DIRECTORY = "images";
27      private final HtmlExportConfiguration exportConfiguration;
28      private final MetricPresentations presentations;
29      private final MetricsConfiguration metricsConfiguration;
30  
31      public HtmlExportFactory(final HtmlExportConfiguration exportConfiguration, final MetricsConfiguration metricsConfiguration, final MetricPresentations presentations) {
32          this.exportConfiguration = exportConfiguration;
33          this.metricsConfiguration = metricsConfiguration;
34          this.presentations = presentations;
35      }
36  
37      DashboardWriter createDashboardWriter() {
38          return new DashboardWriter(this);
39      }
40  
41      public HtmlExporter createExporter() {
42          return new HtmlExporter(this);
43      }
44  
45      HistogramWriter createHistogramWriter() {
46          return new HistogramWriter(this);
47      }
48  
49      HtmlPageBuilder createHtmlPageBuilder() {
50          return new HtmlPageBuilder(exportConfiguration.getDirectory());
51      }
52  
53      ProgressMonitor.Runnable createImageAndDashboardWriter(final MetricsCollatorSet collatorSet) {
54          return exportConfiguration.isProduceImagesAndDashboard() ? new ImageAndDashboardWriter(this, collatorSet) : ProgressMonitor.Runnable.NULL;
55      }
56  
57      IndexPageWriter createIndexPageWriter() {
58          return new IndexPageWriter(this);
59      }
60  
61      public JpegProducer createJpegProducer() {
62          return new JpegProducer(this);
63      }
64  
65      public CategoryDataset createMetricsDataset(final MetricsCollator collator, final MetricId metricId) {
66          return new MetricsDataset(collator, metricId, metricsConfiguration.getUpperBound(metricId));
67      }
68  
69      MetricsPagesWriter createMetricsPagesWriter() {
70          return new MetricsPagesWriter(this);
71      }
72  
73      MetricsTableHeaderWriter createMetricsTableHeaderWriter() {
74          return new MetricsTableHeaderWriter(this);
75      }
76  
77      MetricsTableRowWriter createMetricsTableRowWriter(final HtmlPageBuilder pageBuilder) {
78          return new MetricsTableRowWriter(pageBuilder, metricsConfiguration, exportConfiguration.isExportJava() ? JAVA_EXPORT_DIRECTORY : null);
79      }
80  
81      OrderedPageWriter createOrderedPageWriter(final MetricsCollator collator, final String fileName, final String imageFileName, final String orderItem, final String level) {
82          return new OrderedPageWriter(this, collator, fileName, imageFileName, orderItem, level);
83      }
84  
85      PageHeaderWriter createPageHeaderWriter() {
86          return new PageHeaderWriter(presentations);
87      }
88  
89      SummaryPageWriter createSummaryPageWriter(final MetricsCollatorSet collator) {
90          return new SummaryPageWriter(this, collator);
91      }
92  
93      HtmlExportConfiguration getConfiguration() {
94          return exportConfiguration;
95      }
96  
97      public File getImageDirectory() {
98          final File directory = new File(exportConfiguration.getDirectory(), IMAGES_DIRECTORY);
99          directory.mkdir();
100         return directory;
101     }
102 
103     public File getImageFile(final MetricId metricId) {
104         return new File(getImageDirectory(), getImageFileName(metricId));
105     }
106 
107     public String getImageFilePath(final MetricId id) {
108         return "images/" + getImageFileName(id);
109     }
110 
111     public String getImageFileName(final MetricId id) {
112         return exportConfiguration.isProduceImagesAndDashboard() ? id + ".jpg" : null;
113     }
114 
115     public String getIndexFileName(final int index, final String level) {
116         return level + index;
117     }
118 
119     public MetricPresentations getPresentations() {
120         return presentations;
121     }
122 
123     public ProgressMonitor.Runnable createJavaWriter() {
124         if (!exportConfiguration.isExportJava()) {
125             return ProgressMonitor.Runnable.NULL;
126         }
127 
128         final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
129         final IResource resource = root.findMember(new Path(exportConfiguration.getProject()));
130         final IJavaProject javaProject = JavaCore.create(resource).getJavaProject();
131 
132         return new Java2HtmlWriter(new File(exportConfiguration.getDirectory(), JAVA_EXPORT_DIRECTORY), javaProject);
133     }
134 }
135