2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.core.hotspots.client.widget;
22 import com.google.gwt.event.dom.client.ChangeEvent;
23 import com.google.gwt.event.dom.client.ChangeHandler;
24 import com.google.gwt.event.dom.client.ClickEvent;
25 import com.google.gwt.event.dom.client.ClickHandler;
26 import com.google.gwt.i18n.client.Dictionary;
27 import com.google.gwt.user.client.Window;
28 import com.google.gwt.user.client.ui.*;
29 import org.sonar.gwt.Links;
30 import org.sonar.gwt.Metrics;
31 import org.sonar.gwt.ui.Icons;
32 import org.sonar.wsclient.gwt.AbstractCallback;
33 import org.sonar.wsclient.gwt.Sonar;
34 import org.sonar.wsclient.services.Measure;
35 import org.sonar.wsclient.services.Resource;
36 import org.sonar.wsclient.services.ResourceQuery;
38 public class MostViolatedRules extends AbstractHotspot {
40 private ListBox severity;
42 public MostViolatedRules(Resource resource) {
43 super("rules-hotspot", resource);
47 Widget createHeader() {
48 Dictionary l10n = Dictionary.getDictionary("l10n");
49 severity = new ListBox(false);
50 severity.addItem(l10n.get("hotspot.anySeverity"), "");
51 severity.addItem("Blocker", "BLOCKER");
52 severity.addItem("Critical", "CRITICAL");
53 severity.addItem("Major", "MAJOR");
54 severity.addItem("Minor", "MINOR");
55 severity.addItem("Info", "INFO");
56 severity.setStyleName("small");
57 severity.addChangeHandler(new ChangeHandler() {
58 public void onChange(ChangeEvent event) {
63 final Label label = new Label(l10n.get("hotspot.titleMostViolatedRules"));
64 label.setStyleName("header");
66 final Anchor moreLink = new Anchor(l10n.get("hotspot.moreDetails"));
67 moreLink.getElement().setId("more-rules");
68 moreLink.addClickHandler(new ClickHandler() {
69 public void onClick(ClickEvent event) {
70 Window.Location.assign(Links.baseUrl() + "/drilldown/violations/" + getResource().getId());
74 final HorizontalPanel horizontal = new HorizontalPanel();
75 horizontal.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
76 horizontal.setWidth("98%");
77 horizontal.add(label);
78 horizontal.add(severity);
79 horizontal.add(moreLink);
80 horizontal.setCellHorizontalAlignment(label, HorizontalPanel.ALIGN_LEFT);
81 horizontal.setCellHorizontalAlignment(severity, HorizontalPanel.ALIGN_LEFT);
82 horizontal.setCellHorizontalAlignment(moreLink, HorizontalPanel.ALIGN_RIGHT);
89 final ResourceQuery query = getResourceQuery();
90 Sonar.getInstance().find(query, new AbstractCallback<Resource>() {
93 protected void doOnResponse(Resource resource) {
94 if (resource.getMeasures().isEmpty()) {
103 private void renderGrid(Resource resource) {
104 final Grid grid = new Grid(resource.getMeasures().size(), 4);
105 grid.setStyleName("gwt-Hotspot");
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);
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());
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");
131 public ResourceQuery getResourceQuery() {
132 ResourceQuery query = ResourceQuery.createForResource(getResource(), Metrics.VIOLATIONS)
134 .setExcludeRules(false)
136 String priority = getSelectedPriority();
137 if (priority!=null) {
138 query.setRulePriorities(priority);
143 private String getSelectedPriority() {
144 String priority = severity.getValue(severity.getSelectedIndex());
145 if ("".equals(priority) || priority == null) {