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;
57 private ViolationsPanel sourcePanel;
59 private boolean resourceHasViolations = false;
62 protected Widget doOnResourceLoad(Resource resource) {
63 this.resource = resource;
65 mainPanel.add(loading);
66 mainPanel.setWidth("100%");
67 mainPanel.setStyleName("gwt-Violations");
69 header = new Grid(1, 5);
70 header.setWidth("100%");
71 header.setStylePrimaryName("gwt-ViewerHeader");
72 header.getCellFormatter().setStyleName(0, 0, "thin left");
75 header.getCellFormatter().setStyleName(0, 1, "right");
80 header.setWidget(0, 1, periodBox);
81 header.getCellFormatter().setStyleName(0, 1, "thin cell right");
83 header.setWidget(0, 2, filterBox);
84 header.getCellFormatter().setStyleName(0, 2, "thin cell right");
86 header.setHTML(0, 3, "<div class='note'>" + I18nConstants.INSTANCE.expand() + "</div>");
87 header.getCellFormatter().setStyleName(0, 3, "thin right");
89 expandCheckbox = new CheckBox();
90 expandCheckbox.addClickHandler(new ClickHandler() {
91 public void onClick(ClickEvent event) {
93 sourcePanel.setExpand(expandCheckbox.getValue());
94 sourcePanel.refresh();
97 header.setWidget(0, 4, expandCheckbox);
98 header.getCellFormatter().setStyleName(0, 4, "thin cell left");
100 sourcePanel = new ViolationsPanel(resource, getCurrentRuleFilter(), getCurrentDateFilter());
102 loadRuleSeverities();
106 private void initFilterBoxes() {
110 ChangeHandler changeHandler = new ChangeHandler() {
111 public void onChange(ChangeEvent event) {
113 sourcePanel.filter(getCurrentRuleFilter(), getCurrentDateFilter());
114 sourcePanel.refresh();
117 filterBox.addChangeHandler(changeHandler);
118 periodBox.addChangeHandler(changeHandler);
121 private void initFilterBox() {
122 filterBox = new ListBox();
123 filterBox.addItem(I18nConstants.INSTANCE.noFilters(), "");
124 filterBox.setStyleName("small");
127 private void initPeriodBox() {
128 periodBox = new ListBox();
129 periodBox.setStyleName("small");
130 periodBox.addItem(I18nConstants.INSTANCE.addedPeriod());
139 private void initPeriod(int periodIndex) {
140 String period = Configuration.getParameter("period" + periodIndex);
141 if (period != null) {
142 Date date = JsonUtils.parseDateTime(Configuration.getParameter("period" + periodIndex + "_date"));
144 periodBox.addItem("Added " + period);
145 dateFilters.add(date);
150 private Date getCurrentDateFilter() {
151 Date dateFilter = null;
152 if (periodBox.getSelectedIndex() > 0) {
153 dateFilter = dateFilters.get(periodBox.getSelectedIndex() - 1);
158 private String getCurrentRuleFilter() {
159 return filterBox.getValue(filterBox.getSelectedIndex());
162 private void initDefaultFilter() {
163 defaultFilter = Configuration.getRequestParameter("rule");
164 if (defaultFilter == null) {
165 defaultFilter = Configuration.getRequestParameter("priority");
169 private void loadRuleSeverities() {
170 final ResourceQuery query = ResourceQuery.createForResource(resource, Metrics.BLOCKER_VIOLATIONS,
171 Metrics.CRITICAL_VIOLATIONS, Metrics.MAJOR_VIOLATIONS, Metrics.MINOR_VIOLATIONS, Metrics.INFO_VIOLATIONS);
172 Sonar.getInstance().find(query, new AbstractCallback<Resource>(loading) {
174 protected void doOnResponse(Resource resource) {
175 setResourceHasViolations(resource);
176 displayRuleSeverities(resource);
182 private void displayRuleSeverities(Resource resource) {
183 final Grid grid = new Grid(1, 10);
184 header.setWidget(0, 0, grid);
186 displayRuleSeverity(grid, 0, "BLOCKER", resource.getMeasure(Metrics.BLOCKER_VIOLATIONS));
187 displayRuleSeverity(grid, 2, "CRITICAL", resource.getMeasure(Metrics.CRITICAL_VIOLATIONS));
188 displayRuleSeverity(grid, 4, "MAJOR", resource.getMeasure(Metrics.MAJOR_VIOLATIONS));
189 displayRuleSeverity(grid, 6, "MINOR", resource.getMeasure(Metrics.MINOR_VIOLATIONS));
190 displayRuleSeverity(grid, 8, "INFO", resource.getMeasure(Metrics.INFO_VIOLATIONS));
193 private void displayRuleSeverity(final Grid grid, final int column, final String severity, final Measure measure) {
195 if (measure != null) {
196 value = measure.getFormattedValue();
197 filterBox.addItem(severity + " (" + value + ")", severity);
198 if (severity.equals(defaultFilter)) {
199 filterBox.setSelectedIndex(filterBox.getItemCount() - 1);
203 grid.setHTML(0, column, Icons.forSeverity(severity).getHTML());
204 grid.setHTML(0, column + 1, value);
205 grid.getCellFormatter().setStyleName(0, column, "thin metric right");
206 grid.getCellFormatter().setStyleName(0, column + 1, "thin left value");
209 private void loadRules(Resource resource) {
210 final ResourceQuery query = ResourceQuery.createForResource(resource, Metrics.VIOLATIONS)
211 .setExcludeRules(false);
212 Sonar.getInstance().find(query, new AbstractCallback<Resource>(loading) {
215 protected void doOnResponse(Resource resource) {
216 setResourceHasViolations(resource);
217 displayRules(resource);
223 private void setResourceHasViolations(Resource resource) {
224 resourceHasViolations = resource != null && resource.getMeasure(Metrics.VIOLATIONS) != null;
227 private void displayRules(Resource resource) {
228 Collections.sort(resource.getMeasures(), new Comparator<Measure>() {
229 public int compare(Measure m1, Measure m2) {
230 return m1.getRuleName().compareTo(m2.getRuleName());
233 filterBox.addItem("", "");
234 for (Measure measure : resource.getMeasures()) {
235 filterBox.addItem(measure.getRuleName() + " (" + measure.getFormattedValue() + ")", measure.getRuleKey());
236 if (measure.getRuleKey().equals(defaultFilter)) {
237 filterBox.setSelectedIndex(filterBox.getItemCount() - 1);
240 loading.removeFromParent();
241 mainPanel.add(header);
244 private void loadSources() {
245 mainPanel.remove(sourcePanel);
246 if (resourceHasViolations || expandCheckbox.getValue()) {
247 mainPanel.add(sourcePanel);