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.ChangeEvent;
023    import com.google.gwt.event.dom.client.ChangeHandler;
024    import com.google.gwt.event.dom.client.ClickEvent;
025    import com.google.gwt.event.dom.client.ClickHandler;
026    import com.google.gwt.i18n.client.Dictionary;
027    import com.google.gwt.user.client.Window;
028    import com.google.gwt.user.client.ui.*;
029    import org.sonar.gwt.Links;
030    import org.sonar.wsclient.gwt.AbstractListCallback;
031    import org.sonar.wsclient.gwt.Sonar;
032    import org.sonar.wsclient.services.Measure;
033    import org.sonar.wsclient.services.Resource;
034    import org.sonar.wsclient.services.ResourceQuery;
035    
036    import java.util.List;
037    
038    public class MostBadlyDesignedFiles extends AbstractHotspot {
039    
040      private ListBox metricSelectBox;
041    
042      public MostBadlyDesignedFiles(Resource resource) {
043        super("design-hotspot", resource);
044      }
045    
046      @Override
047      Widget createHeader() {
048        Dictionary l10n = Dictionary.getDictionary("l10n");
049        metricSelectBox = new ListBox(false);
050        metricSelectBox.addItem(l10n.get("hotspot.lcom4"), "lcom4");
051        metricSelectBox.addItem(l10n.get("hotspot.rfc"), "rfc");
052        metricSelectBox.setStyleName("small");
053        metricSelectBox.addChangeHandler(new ChangeHandler() {
054          public void onChange(ChangeEvent event) {
055            loadData();
056          }
057        });
058    
059        final Label label = new Label(l10n.get("hotspot.designTitle"));
060        label.setStyleName("header");
061    
062        final Anchor moreLink = new Anchor(l10n.get("hotspot.moreDetails"));
063        moreLink.getElement().setId("more-design");
064        moreLink.addClickHandler(new ClickHandler() {
065          public void onClick(ClickEvent event) {
066            final String metric = getSelectedMetric();
067            Window.Location.assign(Links.baseUrl() + "/drilldown/measures/" + getResource().getId() + "?metric=" + metric);
068          }
069        });
070    
071        final HorizontalPanel horizontal = new HorizontalPanel();
072        horizontal.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
073        horizontal.setWidth("98%");
074        horizontal.add(label);
075        horizontal.add(metricSelectBox);
076        horizontal.add(moreLink);
077        horizontal.setCellHorizontalAlignment(label, HorizontalPanel.ALIGN_LEFT);
078        horizontal.setCellHorizontalAlignment(metricSelectBox, HorizontalPanel.ALIGN_LEFT);
079        horizontal.setCellHorizontalAlignment(moreLink, HorizontalPanel.ALIGN_RIGHT);
080    
081        return horizontal;
082      }
083    
084      @Override
085      void doLoadData() {
086        final ResourceQuery query = getResourceQuery();
087        Sonar.getInstance().findAll(query, new AbstractListCallback<Resource>() {
088    
089          @Override
090          protected void doOnResponse(List<Resource> resources) {
091            final Grid grid = new Grid(resources.size(), 3);
092            grid.setStyleName("gwt-Hotspot");
093            int row = 0;
094            Measure firstMeasure = null;
095            for (Resource resource : resources) {
096              if (resource.getMeasures().size() == 1) {
097                if (firstMeasure == null) {
098                  firstMeasure = resource.getMeasures().get(0);
099                }
100                renderNameCell(grid, resource, firstMeasure.getMetricKey(), row, 0);
101                renderValueCell(grid, resource.getMeasures().get(0), row, 1);
102                renderGraphCell(grid, resource.getMeasures().get(0), firstMeasure, row, 2);
103                row++;
104              }
105            }
106    
107            if (firstMeasure == null) {
108              renderEmptyResults();
109            } else {
110              render(grid);
111            }
112          }
113        });
114      }
115    
116      public ResourceQuery getResourceQuery() {
117        return ResourceQuery.createForResource(getResource(), getSelectedMetric())
118            .setDepth(-1)
119            .setQualifiers(Resource.QUALIFIER_CLASS)
120            .setLimit(LIMIT);
121      }
122    
123      private String getSelectedMetric() {
124        return metricSelectBox.getValue(metricSelectBox.getSelectedIndex());
125      }
126    }
127