package com.ubx1.pdpscanner.client.views.project;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ubx1.pdpscanner.client.views.AbstractView;
import com.ubx1.pdpscanner.shared.FindbugsBugInstance;
import com.ubx1.pdpscanner.shared.Project;
/**
* The widget for showing the bad practices for a particular version, in a tree
* view
*
* @author wbraik
*
*/
public class BadPracticeWidget 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("Mauvaises pratiques");
private final Label sourcesLabel = new Label("(d'après Findbugs)");
private final Label nothingLabel = new Label(
"Pas de mauvaises pratiques pour cette version.");
private final HorizontalPanel widgetsPanel = new HorizontalPanel();
private final Tree tree = new Tree();
private final BadPracticeBarChart chart;
public BadPracticeWidget(Project project) {
super(new VerticalPanel());
this.project = project;
this.chart = new BadPracticeBarChart(project);
this.tree.setAnimationEnabled(true);
this.titleLabel.setStyleDependentName("widgetTitle", true);
this.sourcesLabel.setStyleDependentName("widgetSources", true);
this.viewPanel.setStyleName("badPracticeWidget");
}
public void setVersion(int version) {
this.version = version;
this.chart.setVersion(version);
}
@Override
public void buildView() {
this.viewPanel.clear();
this.tree.clear();
this.titlePanel.add(titleLabel);
this.titlePanel.add(sourcesLabel);
this.viewPanel.add(this.titlePanel);
// If the bad practice stats are available for this version
if (this.project.getBp(this.version) != null
&& this.project.getBp(this.version).isEmpty() == false) {
fillTree();
this.chart.buildView();
this.widgetsPanel.add(this.tree);
this.widgetsPanel.add(this.chart.getViewPanel());
this.viewPanel.add(this.widgetsPanel);
} else {
this.viewPanel.add(nothingLabel);
}
}
/**
* Fill the bad practice UI tree to display the bad pratices grouped by
* categories
*/
private void fillTree() {
Project p = this.project;
ArrayList<FindbugsBugInstance> bp = p.getBp(this.version);
HashMap<String, HashMap<String, ArrayList<FindbugsBugInstance>>> bpMap = Project
.getFbMap(bp);
Iterator<String> it1 = bpMap.keySet().iterator();
while (it1.hasNext()) {
String abbrev = it1.next();
TreeItem ti1 = tree.addItem(FindbugsBugInstance
.getAbbrevDescription(abbrev)
+ " : "
+ getAbbrevErrorCount(abbrev));
Iterator<String> it2 = bpMap.get(abbrev).keySet().iterator();
while (it2.hasNext()) {
String type = it2.next();
TreeItem ti2 = ti1.addItem(FindbugsBugInstance
.getTypeDescription(type)
+ " : "
+ getTypeErrorCount(type));
Iterator<FindbugsBugInstance> it3 = bpMap.get(abbrev).get(type)
.iterator();
while (it3.hasNext()) {
FindbugsBugInstance fb = it3.next();
String file = "--";
if (file != null) {
file = fb.getFile();
}
String line = "--";
if (fb.getLine() != -1) {
line = String.valueOf(fb.getLine());
}
ti2.addItem("Fichier : " + file + ", Ligne : " + line);
}
}
}
}
/**
* Get the number of bug instances which match an abbreviation
*
* @param abbrev
* the abbreviation
* @return the number of bug instances which match abbrev as their
* abbreviation
*/
private int getAbbrevErrorCount(String abbrev) {
int count = 0;
ArrayList<FindbugsBugInstance> bp = this.project.getBp(this.version);
for (int i = 0; i < bp.size(); i++) {
if (bp.get(i).getAbbrev().equals(abbrev)) {
count++;
}
}
return count;
}
/**
* Get the number of bad practice bug instances which match a type
*
* @param type
* the type
* @return the number of bad practice bug instances which match abbrev as
* their type
*/
private int getTypeErrorCount(String type) {
int count = 0;
ArrayList<FindbugsBugInstance> bp = this.project.getBp(this.version);
for (int i = 0; i < bp.size(); i++) {
if (bp.get(i).getType().equals(type)) {
count++;
}
}
return count;
}
}