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.wsclient.gwt.AbstractListCallback;
029    import org.sonar.wsclient.gwt.Sonar;
030    import org.sonar.wsclient.services.Measure;
031    import org.sonar.wsclient.services.Resource;
032    import org.sonar.wsclient.services.ResourceQuery;
033    
034    import java.util.ArrayList;
035    import java.util.List;
036    
037    public class MetricHotspot extends AbstractHotspot {
038    
039      private String metric;
040      private String title;
041    
042      public MetricHotspot(Resource resource, String metric, String title) {
043        super(metric + "-hotspot", resource);
044        this.metric = metric;
045        this.title = title;
046      }
047    
048      @Override
049      Widget createHeader() {
050        Dictionary l10n = Dictionary.getDictionary("l10n");
051        final Label label = new Label(title);
052        label.setStyleName("header");
053    
054        final Anchor moreLink = new Anchor(l10n.get("hotspot.moreDetails"));
055        moreLink.getElement().setId("more-" + metric);
056        moreLink.addClickHandler(new ClickHandler() {
057          public void onClick(ClickEvent event) {
058            Window.Location.assign(Links.baseUrl() + "/drilldown/measures/" + getResource().getKey() + "?metric=" + metric);
059          }
060        });
061    
062        final HorizontalPanel horizontal = new HorizontalPanel();
063        horizontal.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
064        horizontal.setWidth("98%");
065        horizontal.add(label);
066        horizontal.add(moreLink);
067        horizontal.setCellHorizontalAlignment(label, HorizontalPanel.ALIGN_LEFT);
068        horizontal.setCellHorizontalAlignment(moreLink, HorizontalPanel.ALIGN_RIGHT);
069    
070        return horizontal;
071      }
072    
073      @Override
074      void doLoadData() {
075        final ResourceQuery query = getResourceQuery();
076        Sonar.getInstance().findAll(query, new AbstractListCallback<Resource>() {
077    
078          @Override
079          protected void doOnResponse(List<Resource> resources) {
080            List<HotspotMeasure> measures = new ArrayList<HotspotMeasure>();
081            for (Resource resource : resources) {
082              for (Measure measure : resource.getMeasures()) {
083                measures.add(new HotspotMeasure(resource, measure));
084              }
085            }
086    
087            if (measures.isEmpty()) {
088              renderEmptyResults();
089              
090            } else {
091              final Grid grid = new Grid(measures.size(), 3);
092              grid.setStyleName("gwt-Hotspot");
093              int row = 0;
094              HotspotMeasure firstMeasure = measures.get(0);
095              for (HotspotMeasure measure : measures) {
096                renderNameCell(grid, measure.getResource(), metric, row, 0);
097                renderValueCell(grid, measure.getMeasure(), row, 1);
098                renderGraphCell(grid, measure.getMeasure(), firstMeasure.getMeasure(), row, 2);
099                row++;
100              }
101    
102              render(grid);
103            }
104          }
105        });
106      }
107    
108      protected ResourceQuery getResourceQuery() {
109        return ResourceQuery.createForResource(getResource(), metric)
110            .setScopes(Resource.SCOPE_ENTITY)
111            .setDepth(ResourceQuery.DEPTH_UNLIMITED)
112            .setLimit(LIMIT);
113      }
114    
115      public static class HotspotMeasure {
116        private Resource resource;
117        private Measure measure;
118    
119        public HotspotMeasure(Resource resource, Measure measure) {
120          super();
121          this.resource = resource;
122          this.measure = measure;
123        }
124    
125        public Resource getResource() {
126          return resource;
127        }
128    
129        public Measure getMeasure() {
130          return measure;
131        }
132    
133      }
134    }