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 if (periodBox.getItemCount() > 1) {
79 header.setWidget(0, 1, periodBox);
80 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 loadRuleSeverities();
104 private void initFilters() {
108 ChangeHandler changeHandler = new ChangeHandler() {
109 public void onChange(ChangeEvent event) {
111 sourcePanel.filter(getCurrentRuleFilter(), getCurrentDateFilter());
112 sourcePanel.refresh();
115 filterBox.addChangeHandler(changeHandler);
116 periodBox.addChangeHandler(changeHandler);
119 private void initFilterBox() {
120 filterBox = new ListBox();
121 filterBox.addItem(I18nConstants.INSTANCE.noFilters(), "");
122 filterBox.setStyleName("small");
125 private void initPeriodBox() {
126 periodBox = new ListBox();
127 periodBox.setStyleName("small");
128 periodBox.addItem(I18nConstants.INSTANCE.addedPeriod());
136 String period = Configuration.getRequestParameter("period");
137 if (period != null && !"".equals(period)) {
138 periodBox.setSelectedIndex(Integer.valueOf(period));
142 private void initPeriod(int periodIndex) {
143 String period = Configuration.getParameter("period" + periodIndex);
144 if (period != null) {
145 Date date = JsonUtils.parseDateTime(Configuration.getParameter("period" + periodIndex + "_date"));
147 periodBox.addItem("Added " + period);
148 dateFilters.add(date);
153 private Date getCurrentDateFilter() {
154 Date dateFilter = null;
155 if (periodBox.getSelectedIndex() > 0) {
156 dateFilter = dateFilters.get(periodBox.getSelectedIndex() - 1);
161 private String getCurrentRuleFilter() {
162 return filterBox.getValue(filterBox.getSelectedIndex());
165 private void loadRuleSeverities() {
166 final ResourceQuery query = ResourceQuery.createForResource(resource, Metrics.BLOCKER_VIOLATIONS,
167 Metrics.CRITICAL_VIOLATIONS, Metrics.MAJOR_VIOLATIONS, Metrics.MINOR_VIOLATIONS, Metrics.INFO_VIOLATIONS);
168 Sonar.getInstance().find(query, new AbstractCallback<Resource>(loading) {
170 protected void doOnResponse(Resource resource) {
171 String defaultFilter = Configuration.getRequestParameter("rule");
172 if (defaultFilter == null) {
173 defaultFilter = Configuration.getRequestParameter("priority");
175 setResourceHasViolations(resource);
176 displayRuleSeverities(resource, defaultFilter);
177 loadRules(resource, defaultFilter);
182 private void displayRuleSeverities(Resource resource, String defaultFilter) {
183 final Grid grid = new Grid(1, 10);
184 header.setWidget(0, 0, grid);
186 displayRuleSeverity(grid, 0, "BLOCKER", defaultFilter, resource.getMeasure(Metrics.BLOCKER_VIOLATIONS));
187 displayRuleSeverity(grid, 2, "CRITICAL", defaultFilter, resource.getMeasure(Metrics.CRITICAL_VIOLATIONS));
188 displayRuleSeverity(grid, 4, "MAJOR", defaultFilter, resource.getMeasure(Metrics.MAJOR_VIOLATIONS));
189 displayRuleSeverity(grid, 6, "MINOR", defaultFilter, resource.getMeasure(Metrics.MINOR_VIOLATIONS));
190 displayRuleSeverity(grid, 8, "INFO", defaultFilter, resource.getMeasure(Metrics.INFO_VIOLATIONS));
193 private void displayRuleSeverity(final Grid grid, final int column, final String severity, final String defaultFilter, 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(final Resource resource, final String defaultFilter) {
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, defaultFilter);
223 private void setResourceHasViolations(Resource resource) {
224 resourceHasViolations = resource != null && resource.getMeasure(Metrics.VIOLATIONS) != null;
227 private void displayRules(final Resource resource, final String defaultFilter) {
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 if (sourcePanel == null) {
246 sourcePanel = new ViolationsPanel(resource, getCurrentRuleFilter(), getCurrentDateFilter());
247 mainPanel.add(sourcePanel);
249 mainPanel.remove(sourcePanel);
250 if (resourceHasViolations || expandCheckbox.getValue()) {
251 mainPanel.add(sourcePanel);