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.gwt.Metrics;
031    import org.sonar.gwt.ui.Icons;
032    import org.sonar.wsclient.gwt.AbstractCallback;
033    import org.sonar.wsclient.gwt.Sonar;
034    import org.sonar.wsclient.services.Measure;
035    import org.sonar.wsclient.services.Resource;
036    import org.sonar.wsclient.services.ResourceQuery;
037    
038    public class MostViolatedRules extends AbstractHotspot {
039    
040      private ListBox severity;
041    
042      public MostViolatedRules(Resource resource) {
043        super("rules-hotspot", resource);
044      }
045    
046      @Override
047      Widget createHeader() {
048        Dictionary l10n = Dictionary.getDictionary("l10n");
049        severity = new ListBox(false);
050        severity.addItem(l10n.get("hotspot.anySeverity"), "");
051        severity.addItem("Blocker", "BLOCKER");
052        severity.addItem("Critical", "CRITICAL");
053        severity.addItem("Major", "MAJOR");
054        severity.addItem("Minor", "MINOR");
055        severity.addItem("Info", "INFO");
056        severity.setStyleName("small");
057        severity.addChangeHandler(new ChangeHandler() {
058          public void onChange(ChangeEvent event) {
059            loadData();
060          }
061        });
062    
063        final Label label = new Label(l10n.get("hotspot.titleMostViolatedRules"));
064        label.setStyleName("header");
065    
066        final Anchor moreLink = new Anchor(l10n.get("hotspot.moreDetails"));
067        moreLink.getElement().setId("more-rules");
068        moreLink.addClickHandler(new ClickHandler() {
069          public void onClick(ClickEvent event) {
070            Window.Location.assign(Links.baseUrl() + "/drilldown/violations/" + getResource().getId());
071          }
072        });
073    
074        final HorizontalPanel horizontal = new HorizontalPanel();
075        horizontal.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
076        horizontal.setWidth("98%");
077        horizontal.add(label);
078        horizontal.add(severity);
079        horizontal.add(moreLink);
080        horizontal.setCellHorizontalAlignment(label, HorizontalPanel.ALIGN_LEFT);
081        horizontal.setCellHorizontalAlignment(severity, HorizontalPanel.ALIGN_LEFT);
082        horizontal.setCellHorizontalAlignment(moreLink, HorizontalPanel.ALIGN_RIGHT);
083    
084        return horizontal;
085      }
086    
087      @Override
088      void doLoadData() {
089        final ResourceQuery query = getResourceQuery();
090        Sonar.getInstance().find(query, new AbstractCallback<Resource>() {
091    
092          @Override
093          protected void doOnResponse(Resource resource) {
094            if (resource==null || resource.getMeasures().isEmpty()) {
095              renderEmptyResults();
096            } else {
097              renderGrid(resource);
098            }
099          }
100        });
101      }
102    
103      private void renderGrid(Resource resource) {
104        final Grid grid = new Grid(resource.getMeasures().size(), 4);
105        grid.setStyleName("gwt-Hotspot");
106        int row = 0;
107        Measure firstMeasure = resource.getMeasures().get(0);
108        for (Measure measure : resource.getMeasures()) {
109          renderRule(grid, measure, row);
110          renderValueCell(grid, measure, row, 2);
111          renderGraphCell(grid, measure, firstMeasure, row, 3);
112          row++;
113        }
114        render(grid);
115      }
116    
117      protected void renderRule(final Grid grid, final Measure measure, final int row) {
118        Anchor drillDown = new Anchor(measure.getRuleName());
119        drillDown.addClickHandler(new ClickHandler() {
120          public void onClick(ClickEvent event) {
121            Window.Location.assign(Links.baseUrl() + "/drilldown/violations/" + getResource().getId() + "?rule=" + measure.getRuleKey());
122          }
123        });
124    
125        grid.setWidget(row, 0, new HTML("<a id=\"rule" + row + "\" href=\"" + Links.urlForRule(measure.getRuleKey(), false) + "\" onclick=\"window.open(this.href,'rule','height=800,width=900,scrollbars=1,resizable=1');return false;\" title=\"" + measure.getRuleKey() + "\">" + Icons.forPriority(measure.getRulePriority()).getHTML() + "</a>"));
126        grid.setWidget(row, 1, drillDown);
127        grid.getCellFormatter().setStyleName(row, 0, getRowCssClass(row) + "");
128        grid.getCellFormatter().setStyleName(row, 1, getRowCssClass(row) + " resourceCell");
129      }
130    
131      public ResourceQuery getResourceQuery() {
132        ResourceQuery query = ResourceQuery.createForResource(getResource(), Metrics.VIOLATIONS)
133            .setDepth(0)
134            .setExcludeRules(false)
135            .setLimit(LIMIT);
136        String severity = getSelectedPriority();
137        if (severity!=null) {
138          query.setRuleSeverities(severity);
139        }
140        return query;
141      }
142    
143      private String getSelectedPriority() {
144        String priority = severity.getValue(severity.getSelectedIndex());
145        if ("".equals(priority) || priority == null) {
146          return null;
147        }
148        return priority;
149      }
150    }