package com.ubx1.pdpscanner.client.views.project;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ubx1.pdpscanner.client.views.AbstractView;
import com.ubx1.pdpscanner.shared.Project;
/**
* The widget for showing the code coverage
*
* @author wbraik
*
*/
public class CoverageWidget extends AbstractView {
/**
* The associated Project object
*/
protected Project project = null;
/**
* The current version to display the stats for
*/
private int version;
// View components
private final HorizontalPanel titlePanel = new HorizontalPanel();
private final Label titleLabel = new Label("Couverture de code");
private final Label sourcesLabel = new Label("(d'après Cobertura)");
private final HorizontalPanel covPanel = new HorizontalPanel();
private final Label covLocLabel = new Label("Lignes de code testées :");
private final Label covValueLabel = new Label();
private final Label diffCovLabel = new Label();
private final CoveragePieChart covPieChart;
/**
* Threshold to good coverage level
*/
private static final int COV_THRESHOLD = 80;
public CoverageWidget(Project project) {
super(new VerticalPanel());
this.project = project;
this.covPieChart = new CoveragePieChart(project);
this.titleLabel.setStyleDependentName("widgetTitle", true);
this.sourcesLabel.setStyleDependentName("widgetSources", true);
this.covValueLabel.setStyleDependentName("diff", true);
this.diffCovLabel.setStyleDependentName("diff", true);
this.viewPanel.setStyleName("coverageWidget");
}
public void setVersion(int version) {
this.version = version;
covPieChart.setVersion(version);
}
@Override
public void buildView() {
this.covValueLabel.setText("");
this.diffCovLabel.setText("");
fillValueLabel();
fillDiffLabel();
this.covPieChart.buildView();
this.covPanel.add(this.covLocLabel);
this.covPanel.add(this.covValueLabel);
this.covPanel.add(this.diffCovLabel);
this.covPanel.add(this.covPieChart.getViewPanel());
titlePanel.add(titleLabel);
titlePanel.add(sourcesLabel);
this.viewPanel.add(this.titlePanel);
this.viewPanel.add(this.covPanel);
}
/**
* Fill the value label
*/
private void fillValueLabel() {
Project p = this.project;
int v = this.version;
int ncov = p.getNcovl(v);
int nloc = p.getNloc(v);
// If the test stats are unavailable, don't display them
if (ncov == -1 || nloc == -1) {
this.covValueLabel.setText("--");
} else {
Double pcov = (new Double(ncov) / new Double(nloc)) * 100.;
this.covValueLabel.setText(ncov + " / " + nloc + " ("
+ Math.round(pcov) + "%)");
if (pcov > CoverageWidget.COV_THRESHOLD) {
covValueLabel.setStyleDependentName("true", false);
covValueLabel.setStyleDependentName("green", true);
} else {
covValueLabel.setStyleDependentName("green", false);
covValueLabel.setStyleDependentName("red", true);
}
}
}
/**
* Fill the label for showing the differential with the previous version
*/
private void fillDiffLabel() {
Project p = this.project;
int v = this.version;
// If there is an anterior version
if (v > 1) {
int ncov = p.getNcovl(v);
int oldNcov = p.getNcovl(v - 1);
int nloc = p.getNloc(v);
int oldNloc = p.getNloc(v - 1);
Double pcov = (new Double(ncov) / new Double(nloc)) * 100.;
Double oldPcov = (new Double(oldNcov) / new Double(oldNloc)) * 100.;
if (pcov > oldPcov) {
diffCovLabel.setText("(+)");
diffCovLabel.setStyleDependentName("red", false);
diffCovLabel.setStyleDependentName("green", true);
} else if (pcov < oldPcov) {
diffCovLabel.setText("(-)");
diffCovLabel.setStyleDependentName("green", false);
diffCovLabel.setStyleDependentName("red", true);
}
}
}
}