2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
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.violationsviewer.client;
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.gen2.table.override.client.Grid;
27 import com.google.gwt.user.client.ui.*;
28 import org.sonar.gwt.Configuration;
29 import org.sonar.gwt.JsonUtils;
30 import org.sonar.gwt.Metrics;
31 import org.sonar.gwt.ui.Icons;
32 import org.sonar.gwt.ui.Loading;
33 import org.sonar.gwt.ui.Page;
34 import org.sonar.wsclient.gwt.AbstractCallback;
35 import org.sonar.wsclient.gwt.Sonar;
36 import org.sonar.wsclient.services.Measure;
37 import org.sonar.wsclient.services.Resource;
38 import org.sonar.wsclient.services.ResourceQuery;
42 public class ViolationsViewer extends Page {
43 public static final String GWT_ID = "org.sonar.plugins.core.violationsviewer.ViolationsViewer";
45 private Resource resource;
46 private final Panel mainPanel = new VerticalPanel();
47 private final Loading loading = new Loading(I18nConstants.INSTANCE.loading());
50 private Grid header = null;
51 private ListBox filterBox = null, periodBox = null;
52 private List<Date> dateFilters = new ArrayList<Date>();
53 private CheckBox expandCheckbox = null;
56 private ViolationsPanel sourcePanel;
58 private boolean resourceHasViolations = false;
61 protected Widget doOnResourceLoad(Resource resource) {
62 this.resource = resource;
64 mainPanel.add(loading);
65 mainPanel.setWidth("100%");
66 mainPanel.setStyleName("gwt-Violations");
68 header = new Grid(1, 5);
69 header.setWidth("100%");
70 header.setStylePrimaryName("gwt-ViewerHeader");
71 header.getCellFormatter().setStyleName(0, 0, "thin left");
74 header.getCellFormatter().setStyleName(0, 1, "right");
78 header.setWidget(0, 1, periodBox);
79 header.getCellFormatter().setStyleName(0, 1, "thin cell right");
81 header.setWidget(0, 2, filterBox);
82 header.getCellFormatter().setStyleName(0, 2, "thin cell right");
84 header.setHTML(0, 3, "<div class='note'>" + I18nConstants.INSTANCE.expand() + "</div>");
85 header.getCellFormatter().setStyleName(0, 3, "thin right");
87 expandCheckbox = new CheckBox();
88 expandCheckbox.addClickHandler(new ClickHandler() {
89 public void onClick(ClickEvent event) {
91 sourcePanel.setExpand(expandCheckbox.getValue());
92 sourcePanel.refresh();
95 header.setWidget(0, 4, expandCheckbox);
96 header.getCellFormatter().setStyleName(0, 4, "thin cell left");
102 private void initFilters() {
106 ChangeHandler changeHandler = new ChangeHandler() {
107 public void onChange(ChangeEvent event) {
109 sourcePanel.filter(getCurrentRuleFilter(), getCurrentDateFilter());
110 sourcePanel.refresh();
113 filterBox.addChangeHandler(changeHandler);
114 periodBox.addChangeHandler(changeHandler);
117 private void initFilterBox() {
118 filterBox = new ListBox();
119 filterBox.addItem(I18nConstants.INSTANCE.noFilters(), "");
120 filterBox.setStyleName("small");
123 private void initPeriodBox() {
124 periodBox = new ListBox();
125 periodBox.setStyleName("small");
126 periodBox.addItem(I18nConstants.INSTANCE.addedPeriod());
134 String period = Configuration.getRequestParameter("period");
135 if (period != null && !"".equals(period)) {
136 periodBox.setSelectedIndex(Integer.valueOf(period));
140 private void initPeriod(int periodIndex) {
141 String period = Configuration.getParameter("period" + periodIndex);
142 if (period != null) {
143 Date date = JsonUtils.parseDateTime(Configuration.getParameter("period" + periodIndex + "_date"));
145 periodBox.addItem("Added " + period);
146 dateFilters.add(date);
151 private Date getCurrentDateFilter() {
152 Date dateFilter = null;
153 if (periodBox.getSelectedIndex() > 0) {
154 dateFilter = dateFilters.get(periodBox.getSelectedIndex() - 1);
159 private String getCurrentRuleFilter() {
160 return filterBox.getValue(filterBox.getSelectedIndex());
163 private void loadRuleSeverities() {
164 final ResourceQuery query = ResourceQuery.createForResource(resource, Metrics.BLOCKER_VIOLATIONS,
165 Metrics.CRITICAL_VIOLATIONS, Metrics.MAJOR_VIOLATIONS, Metrics.MINOR_VIOLATIONS, Metrics.INFO_VIOLATIONS);
166 Sonar.getInstance().find(query, new AbstractCallback<Resource>(loading) {
168 protected void doOnResponse(Resource resource) {
169 String defaultFilter = Configuration.getRequestParameter("rule");
170 if (defaultFilter == null) {
171 defaultFilter = Configuration.getRequestParameter("priority");
173 setResourceHasViolations(resource);
174 displayRuleSeverities(resource, defaultFilter);
175 loadRules(resource, defaultFilter);
180 private void displayRuleSeverities(Resource resource, String defaultFilter) {
181 final Grid grid = new Grid(1, 10);
182 header.setWidget(0, 0, grid);
184 displayRuleSeverity(grid, 0, "BLOCKER", defaultFilter, resource.getMeasure(Metrics.BLOCKER_VIOLATIONS));
185 displayRuleSeverity(grid, 2, "CRITICAL", defaultFilter, resource.getMeasure(Metrics.CRITICAL_VIOLATIONS));
186 displayRuleSeverity(grid, 4, "MAJOR", defaultFilter, resource.getMeasure(Metrics.MAJOR_VIOLATIONS));
187 displayRuleSeverity(grid, 6, "MINOR", defaultFilter, resource.getMeasure(Metrics.MINOR_VIOLATIONS));
188 displayRuleSeverity(grid, 8, "INFO", defaultFilter, resource.getMeasure(Metrics.INFO_VIOLATIONS));
191 private void displayRuleSeverity(final Grid grid, final int column, final String severity, final String defaultFilter, final Measure measure) {
193 if (measure != null) {
194 value = measure.getFormattedValue();
195 filterBox.addItem(severity + " (" + value + ")", severity);
196 if (severity.equals(defaultFilter)) {
197 filterBox.setSelectedIndex(filterBox.getItemCount() - 1);
201 grid.setHTML(0, column, Icons.forSeverity(severity).getHTML());
202 grid.setHTML(0, column + 1, value);
203 grid.getCellFormatter().setStyleName(0, column, "thin metric right");
204 grid.getCellFormatter().setStyleName(0, column + 1, "thin left value");
207 private void loadRules(final Resource resource, final String defaultFilter) {
208 final ResourceQuery query = ResourceQuery.createForResource(resource, Metrics.VIOLATIONS)
209 .setExcludeRules(false);
210 Sonar.getInstance().find(query, new AbstractCallback<Resource>(loading) {
213 protected void doOnResponse(Resource resource) {
214 setResourceHasViolations(resource);
215 displayRules(resource, defaultFilter);
221 private void setResourceHasViolations(Resource resource) {
222 resourceHasViolations = resource != null && resource.getMeasure(Metrics.VIOLATIONS) != null;
225 private void displayRules(final Resource resource, final String defaultFilter) {
226 Collections.sort(resource.getMeasures(), new Comparator<Measure>() {
227 public int compare(Measure m1, Measure m2) {
228 return m1.getRuleName().compareTo(m2.getRuleName());
231 filterBox.addItem("", "");
232 for (Measure measure : resource.getMeasures()) {
233 filterBox.addItem(measure.getRuleName() + " (" + measure.getFormattedValue() + ")", measure.getRuleKey());
234 if (measure.getRuleKey().equals(defaultFilter)) {
235 filterBox.setSelectedIndex(filterBox.getItemCount() - 1);
238 loading.removeFromParent();
239 mainPanel.add(header);
242 private void loadSources() {
243 if (sourcePanel == null) {
244 sourcePanel = new ViolationsPanel(resource, getCurrentRuleFilter(), getCurrentDateFilter());
245 mainPanel.add(sourcePanel);
247 mainPanel.remove(sourcePanel);
248 if (resourceHasViolations || expandCheckbox.getValue()) {
249 mainPanel.add(sourcePanel);