001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.plugins.core.hotspots.client.widget;
021    
022    import com.google.gwt.event.dom.client.ClickEvent;
023    import com.google.gwt.event.dom.client.ClickHandler;
024    import com.google.gwt.i18n.client.Dictionary;
025    import com.google.gwt.user.client.Window;
026    import com.google.gwt.user.client.ui.*;
027    import org.sonar.gwt.Links;
028    import org.sonar.gwt.ui.Loading;
029    import org.sonar.wsclient.services.Measure;
030    import org.sonar.wsclient.services.Resource;
031    
032    public abstract class AbstractHotspot extends Composite {
033    
034      private Panel hotspot;
035      private Panel data;
036      private Resource resource;
037    
038      public static final int LIMIT = 5;
039    
040      protected AbstractHotspot(String id, Resource resource) {
041        this.resource = resource;
042        hotspot = new VerticalPanel();
043        hotspot.getElement().setId(id);
044        hotspot.setStyleName("gwt-HotspotPanel");
045        initWidget(hotspot);
046      }
047    
048      public Resource getResource() {
049        return resource;
050      }
051    
052      @Override
053      public void onLoad() {
054        hotspot.add(createHeader());
055        data = new SimplePanel();
056        hotspot.add(data);
057        loadData();
058      }
059    
060      protected void loadData() {
061        data.clear();
062        data.add(new Loading());
063        doLoadData();
064      }
065    
066      abstract Widget createHeader();
067    
068      abstract void doLoadData();
069    
070      protected void render(Widget widget) {
071        data.clear();
072        data.add(widget);
073      }
074    
075      protected void renderEmptyResults() {
076        Grid grid = new Grid(1, 1);
077        grid.setWidget(0, 0, new HTML(Dictionary.getDictionary("l10n").get("hotspot.noMeasures")));
078        grid.getCellFormatter().setStyleName(0, 0, getRowCssClass(0) + " emptyResultsCell");
079        grid.setStyleName("gwt-Hotspot");
080        render(grid);
081      }
082    
083      protected void renderNameCell(Grid hotspotGrid, final Resource resource, final String metricKey, int row, int column) {
084        Anchor link = new Anchor(resource.getName());
085        link.getElement().setAttribute("title", resource.getName(true));
086        link.getElement().setAttribute("rel", resource.getName(true));
087        link.addClickHandler(new ClickHandler() {
088          public void onClick(final ClickEvent event) {
089            if (resource.getCopy() != null) {
090              Window.Location.assign(Links.baseUrl() + "/plugins/resource/" + resource.getCopy() + "?page=org.sonar.plugins.core.hotspots.GwtHotspots");
091            } else {
092              Links.openMeasurePopup(resource.getKey(), metricKey);
093            }
094          }
095        });
096        hotspotGrid.setWidget(row, column, link);
097        hotspotGrid.getCellFormatter().setStyleName(row, column, getRowCssClass(row) + " resourceCell");
098      }
099    
100      protected void renderValueCell(Grid hotspotGrid, Measure measure, int row, int column) {
101        hotspotGrid.setHTML(row, column, measure.getFormattedValue());
102        hotspotGrid.getCellFormatter().setStyleName(row, column, getRowCssClass(row) + " resultCell");
103      }
104    
105      protected void renderGraphCell(Grid hotspotGrid, Measure measure, Measure firstMeasure, int row, int column) {
106        Double value = Double.valueOf(measure.getValue());
107        Double upperValue = Double.valueOf(firstMeasure.getValue());
108        Double percentPonderated = getPercentPonderatedValue(value, 0d, upperValue);
109        String graph = "<span style='width:100%'><ul class='hbar' style='float: right;'><li style='background-color: rgb(119, 119, 119); width: " + percentPonderated.intValue() + "%'>&nbsp;</li></ul></span>";
110        hotspotGrid.setHTML(row, column, graph);
111        hotspotGrid.getCellFormatter().setStyleName(row, column, getRowCssClass(row) + " graphCell");
112      }
113    
114      protected String getRowCssClass(int row) {
115        return row % 2 == 0 ? "even" : "odd";
116      }
117    
118      protected double getPercentPonderatedValue(Double value, Double lower, Double upper) {
119        if (value < lower) return 0;
120        if (value > upper) return 100;
121        double percentIncrement = (upper - lower) / 100d;
122        return (value - lower) / percentIncrement;
123      }
124    }