1   package com.stateofflow.eclipse.metrics.export.html;
2   
3   import java.io.IOException;
4   
5   import com.stateofflow.eclipse.metrics.collator.MetricsCollator;
6   import com.stateofflow.eclipse.metrics.collator.MetricsCollatorSet;
7   import com.stateofflow.eclipse.metrics.metric.MetricId;
8   import com.stateofflow.eclipse.metrics.util.ProgressMonitor;
9   
10  final class IndexPageWriter {
11      private final HtmlExportFactory factory;
12      private final HtmlPageBuilder pageBuilder;
13  
14      public IndexPageWriter(final HtmlExportFactory factory) {
15          this.factory = factory;
16          pageBuilder = factory.createHtmlPageBuilder();
17      }
18  
19      public void write(final MetricsCollatorSet collatorSet, final ProgressMonitor progressMonitor) throws IOException {
20          progressMonitor.beginTask("Creating index page", 1);
21          pageBuilder.openPage("index", "Metrics for " + factory.getConfiguration().getProject());
22          createFragments(collatorSet);
23          pageBuilder.closePage();
24      }
25  
26      private void createFragments(final MetricsCollatorSet collatorSet) {
27          createDashboardIndexPageFragment();
28          createIndexPageProjectFragment();
29          createIndexPageFragment(collatorSet.getPackageMetrics(), HtmlExporter.PACKAGE_METRIC_LEVEL);
30          createIndexPageFragment(collatorSet.getTypeMetrics(), HtmlExporter.TYPE_METRIC_LEVEL);
31          createIndexPageFragment(collatorSet.getMethodMetrics(), HtmlExporter.METHOD_METRIC_LEVEL);
32      }
33  
34      private void createDashboardIndexPageFragment() {
35          if (factory.getConfiguration().isProduceImagesAndDashboard()) {
36              pageBuilder.createElementAndNewline("H2", "Dashboard").openElement("P").openElementAndNewline("UL").openElement("LI").printLink(HtmlExporter.DASHBOARD_FILE_NAME, "Visual Summary").closeElementAndNewline("LI").closeElementAndNewline("UL");
37          }
38      }
39  
40      private void createIndexPageFragment(final MetricsCollator collator, final String level) {
41          pageBuilder.createElementAndNewline("H2", level + " Metrics").openElement("P").println("Order by:").openElementAndNewline("UL");
42          createIndexPagePackageAndTypeNameFragment(level);
43          createIndexPageMetricsFragment(collator, level);
44          pageBuilder.closeElementAndNewline("UL");
45      }
46  
47      private void createIndexPageProjectFragment() {
48          pageBuilder.createElementAndNewline("H2", HtmlExporter.PROJECT_METRIC_LEVEL + " Metrics").openElementAndNewline("UL");
49          pageBuilder.openElement("LI").printLink("Project.1", "Summary Statistics").closeElementAndNewline("LI");
50          pageBuilder.closeElementAndNewline("UL");
51      }
52  
53      private void createIndexPagePackageAndTypeNameFragment(final String level) {
54          if (HtmlExporter.PACKAGE_METRIC_LEVEL.equals(level)) {
55              pageBuilder.openElement("LI").printLink("Package.1", "Package").closeElementAndNewline("LI");
56          } else {
57              pageBuilder.openElement("LI").printLink(HtmlExporter.UNQUALIFIED_TYPE_NAME_PAGE_NAME_START + level + ".1", "Unqualified type name").closeElementAndNewline("LI");
58              pageBuilder.openElement("LI").printLink(HtmlExporter.QUALIFIED_TYPE_NAME_PAGE_NAME_START + level + ".1", "Qualified type name").closeElementAndNewline("LI");
59          }
60      }
61  
62      private void createIndexPageMetricsFragment(final MetricsCollator collator, final String level) {
63          final MetricId[] metricIds = collator.getSortedMetricIds();
64          for (int i = 0; i < metricIds.length; i++) {
65              pageBuilder.openElement("LI").printLink(factory.getIndexFileName(i, level) + ".1", factory.getPresentations().getPresentationName(metricIds[i])).closeElementAndNewline("LI");
66          }
67      }
68  }
69