Package com.ubx1.pdpscanner.client.views.project

Source Code of com.ubx1.pdpscanner.client.views.project.DuplicationWidget

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 degree of code duplication in a project
*
* @author wbraik
*
*/
public class DuplicationWidget 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("Duplication de code");
  private final Label sourcesLabel = new Label("(d'après PMD/CPD)");
  private final HorizontalPanel duplPanel = new HorizontalPanel();
  private final Label duplLabel = new Label("Duplication de code :");
  private final Label duplValueLabel = new Label();
  private final Label diffDuplLabel = new Label();
  private final DuplicationPieChart chart;

  /**
   * Threshold to bad duplication level
   */
  private static final int DUPL_THRESHOLD = 10;

  public DuplicationWidget(Project project) {

    super(new VerticalPanel());
    this.project = project;

    this.chart = new DuplicationPieChart(project);

    this.titleLabel.setStyleDependentName("widgetTitle", true);
    this.sourcesLabel.setStyleDependentName("widgetSources", true);
    this.duplValueLabel.setStyleDependentName("diff", true);
    this.diffDuplLabel.setStyleDependentName("diff", true);
    this.viewPanel.setStyleName("duplicationWidget");
  }

  public void setVersion(int version) {

    this.version = version;

    this.chart.setVersion(version);
  }

  @Override
  public void buildView() {

    duplValueLabel.setText("");
    diffDuplLabel.setText("");

    fillValueLabel();
    fillDiffLabel();
    this.chart.buildView();

    duplPanel.add(duplLabel);
    duplPanel.add(duplValueLabel);
    duplPanel.add(diffDuplLabel);

    titlePanel.add(titleLabel);
    titlePanel.add(sourcesLabel);

    viewPanel.add(titlePanel);
    viewPanel.add(duplPanel);
    viewPanel.add(this.chart.getViewPanel());
  }

  /**
   * Fill the value label
   */
  private void fillValueLabel() {

    Project p = this.project;
    int v = this.version;

    int ndupl = p.getNdupl(v);
    int nloc = p.getNloc(v);

    if (ndupl == -1 || nloc == -1) {
      duplValueLabel.setText("--");
    } else {

      Double pdupl = (new Double(ndupl) / new Double(nloc)) * 100.;

      duplValueLabel.setText(Math.round(pdupl) + "%");
      if (pdupl > DuplicationWidget.DUPL_THRESHOLD) {
        duplValueLabel.setStyleDependentName("green", false);
        duplValueLabel.setStyleDependentName("red", true);
      } else {
        duplValueLabel.setStyleDependentName("red", false);
        duplValueLabel.setStyleDependentName("green", 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) {

      if (p.getNdupl(v) != -1 && p.getNdupl(v - 1) != -1) {

        if (p.getNdupl(v) > p.getNdupl(v - 1)) {
          diffDuplLabel.setText("(+)");
          diffDuplLabel.setStyleDependentName("green", false);
          diffDuplLabel.setStyleDependentName("red", true);
        } else if (p.getNdupl(v) < p.getNdupl(v - 1)) {
          diffDuplLabel.setText("(-)");
          diffDuplLabel.setStyleDependentName("red", false);
          diffDuplLabel.setStyleDependentName("green", true);
        }
      }
    }
  }
}
TOP

Related Classes of com.ubx1.pdpscanner.client.views.project.DuplicationWidget

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.