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;
54 private String defaultFilter;
55 private int defaultPeriodIndex = -1;
58 private ViolationsPanel sourcePanel;
60 private boolean resourceHasViolations = false;
63 protected Widget doOnResourceLoad(Resource resource) {
64 this.resource = resource;
66 mainPanel.add(loading);
67 mainPanel.setWidth("100%");
68 mainPanel.setStyleName("gwt-Violations");
70 header = new Grid(1, 5);
71 header.setWidth("100%");
72 header.setStylePrimaryName("gwt-ViewerHeader");
73 header.getCellFormatter().setStyleName(0, 0, "thin left");
76 header.getCellFormatter().setStyleName(0, 1, "right");
81 header.setWidget(0, 1, periodBox);
82 header.getCellFormatter().setStyleName(0, 1, "thin cell right");
84 header.setWidget(0, 2, filterBox);
85 header.getCellFormatter().setStyleName(0, 2, "thin cell right");
87 header.setHTML(0, 3, "<div class='note'>" + I18nConstants.INSTANCE.expand() + "</div>");
88 header.getCellFormatter().setStyleName(0, 3, "thin right");
90 expandCheckbox = new CheckBox();
91 expandCheckbox.addClickHandler(new ClickHandler() {
92 public void onClick(ClickEvent event) {
94 sourcePanel.setExpand(expandCheckbox.getValue());
95 sourcePanel.refresh();
98 header.setWidget(0, 4, expandCheckbox);
99 header.getCellFormatter().setStyleName(0, 4, "thin cell left");
101 sourcePanel = new ViolationsPanel(resource, getCurrentRuleFilter(), getCurrentDateFilter());
103 loadRuleSeverities();
107 private void initFilterBoxes() {
111 ChangeHandler changeHandler = new ChangeHandler() {
112 public void onChange(ChangeEvent event) {
114 sourcePanel.filter(getCurrentRuleFilter(), getCurrentDateFilter());
115 sourcePanel.refresh();
118 filterBox.addChangeHandler(changeHandler);
119 periodBox.addChangeHandler(changeHandler);
122 private void initFilterBox() {
123 filterBox = new ListBox();
124 filterBox.addItem(I18nConstants.INSTANCE.noFilters(), "");
125 filterBox.setStyleName("small");
128 private void initPeriodBox() {
129 periodBox = new ListBox();
130 periodBox.setStyleName("small");
131 periodBox.addItem(I18nConstants.INSTANCE.addedPeriod());
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);
149 if (defaultPeriodIndex > -1) {
150 periodBox.setSelectedIndex(defaultPeriodIndex);
154 private Date getCurrentDateFilter() {
155 Date dateFilter = null;
156 if (periodBox.getSelectedIndex() > 0) {
157 dateFilter = dateFilters.get(periodBox.getSelectedIndex() - 1);
162 private String getCurrentRuleFilter() {
163 return filterBox.getValue(filterBox.getSelectedIndex());
166 private void initDefaultFilter() {
167 defaultFilter = Configuration.getRequestParameter("rule");
168 if (defaultFilter == null) {
169 defaultFilter = Configuration.getRequestParameter("priority");
171 String period = Configuration.getRequestParameter("period");
172 if (period != null && !"".equals(period)) {
173 defaultPeriodIndex = Integer.valueOf(period);
177 private void loadRuleSeverities() {
178 final ResourceQuery query = ResourceQuery.createForResource(resource, Metrics.BLOCKER_VIOLATIONS,
179 Metrics.CRITICAL_VIOLATIONS, Metrics.MAJOR_VIOLATIONS, Metrics.MINOR_VIOLATIONS, Metrics.INFO_VIOLATIONS);
180 Sonar.getInstance().find(query, new AbstractCallback<Resource>(loading) {
182 protected void doOnResponse(Resource resource) {
183 setResourceHasViolations(resource);
184 displayRuleSeverities(resource);
190 private void displayRuleSeverities(Resource resource) {
191 final Grid grid = new Grid(1, 10);
192 header.setWidget(0, 0, grid);
194 displayRuleSeverity(grid, 0, "BLOCKER", resource.getMeasure(Metrics.BLOCKER_VIOLATIONS));
195 displayRuleSeverity(grid, 2, "CRITICAL", resource.getMeasure(Metrics.CRITICAL_VIOLATIONS));
196 displayRuleSeverity(grid, 4, "MAJOR", resource.getMeasure(Metrics.MAJOR_VIOLATIONS));
197 displayRuleSeverity(grid, 6, "MINOR", resource.getMeasure(Metrics.MINOR_VIOLATIONS));
198 displayRuleSeverity(grid, 8, "INFO", resource.getMeasure(Metrics.INFO_VIOLATIONS));
201 private void displayRuleSeverity(final Grid grid, final int column, final String severity, final Measure measure) {
203 if (measure != null) {
204 value = measure.getFormattedValue();
205 filterBox.addItem(severity + " (" + value + ")", severity);
206 if (severity.equals(defaultFilter)) {
207 filterBox.setSelectedIndex(filterBox.getItemCount() - 1);
211 grid.setHTML(0, column, Icons.forSeverity(severity).getHTML());
212 grid.setHTML(0, column + 1, value);
213 grid.getCellFormatter().setStyleName(0, column, "thin metric right");
214 grid.getCellFormatter().setStyleName(0, column + 1, "thin left value");
217 private void loadRules(Resource resource) {
218 final ResourceQuery query = ResourceQuery.createForResource(resource, Metrics.VIOLATIONS)
219 .setExcludeRules(false);
220 Sonar.getInstance().find(query, new AbstractCallback<Resource>(loading) {
223 protected void doOnResponse(Resource resource) {
224 setResourceHasViolations(resource);
225 displayRules(resource);
231 private void setResourceHasViolations(Resource resource) {
232 resourceHasViolations = resource != null && resource.getMeasure(Metrics.VIOLATIONS) != null;
235 private void displayRules(Resource resource) {
236 Collections.sort(resource.getMeasures(), new Comparator<Measure>() {
237 public int compare(Measure m1, Measure m2) {
238 return m1.getRuleName().compareTo(m2.getRuleName());
241 filterBox.addItem("", "");
242 for (Measure measure : resource.getMeasures()) {
243 filterBox.addItem(measure.getRuleName() + " (" + measure.getFormattedValue() + ")", measure.getRuleKey());
244 if (measure.getRuleKey().equals(defaultFilter)) {
245 filterBox.setSelectedIndex(filterBox.getItemCount() - 1);
248 loading.removeFromParent();
249 mainPanel.add(header);
252 private void loadSources() {
253 mainPanel.remove(sourcePanel);
254 if (resourceHasViolations || expandCheckbox.getValue()) {
255 mainPanel.add(sourcePanel);