Skip to content

Commit

Permalink
Add checkbox on "Sonar Rule Details"
Browse files Browse the repository at this point in the history
  • Loading branch information
philippefichet committed Apr 18, 2020
1 parent d63f946 commit 2a5c9b8
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fr.philippefichet.sonarlint</groupId>
<artifactId>sonarlint4netbeans</artifactId>
<version>1.4.0</version>
<version>1.5.0</version>
<packaging>nbm</packaging>
<name>SonarLint for Netbeans</name>
<organization>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,25 @@ public interface SonarLintEngine {
public void excludeRuleKeys(List<RuleKey> ruleKey);

/**
* incldue one rule
* set rules to include
*
* @param ruleKeys rules to include
*/
public void includeRuleKeys(List<RuleKey> ruleKeys);

/**
* include one rule
*
* @param ruleKey rule to include
*/
public void includeRuleKyes(List<RuleKey> ruleKey);
public void includeRuleKey(RuleKey ruleKey);

/**
* exclude one rule
*
* @param ruleKey rule to exclude
*/
public void excludeRuleKey(RuleKey ruleKey);

/**
* Check if rule must be exclude
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void excludeRuleKeys(List<RuleKey> ruleKeys) {
}

@Override
public void includeRuleKyes(List<RuleKey> ruleKeys) {
public void includeRuleKeys(List<RuleKey> ruleKeys) {
excludedRules.removeAll(ruleKeys);
getPreferences().put("excludedRules", gson.toJson(excludedRules));
fireConfigurationChange();
Expand Down Expand Up @@ -179,4 +179,18 @@ public void whenConfigurationChanged(Consumer<SonarLintEngine> consumer) {
private void fireConfigurationChange() {
configurationChanged.forEach(consumer -> consumer.accept(this));
}

@Override
public void includeRuleKey(RuleKey ruleKey) {
excludedRules.remove(ruleKey);
getPreferences().put("excludedRules", gson.toJson(excludedRules));
fireConfigurationChange();
}

@Override
public void excludeRuleKey(RuleKey ruleKey) {
excludedRules.add(ruleKey);
getPreferences().put("excludedRules", gson.toJson(excludedRules));
fireConfigurationChange();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* sonarlint4netbeans: SonarLint integration for Apache Netbeans
* Copyright (C) 2020 Philippe FICHET.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package fr.philippefichet.sonarlint.netbeans;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Point;
import java.util.Optional;
import java.util.logging.Logger;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import org.sonarsource.sonarlint.core.client.api.common.RuleDetails;

/**
*
* @author FICHET Philippe <philippe.fichet@laposte.net>
*/
public final class SonarLintListCellRenderer extends JPanel implements ListCellRenderer<String> {
private static final Logger LOG = Logger.getLogger(SonarLintListCellRenderer.class.getName());

private final SonarLintEngine sonarLintEngine;
private final JCheckBox enableOrDisable;
private final DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();

public SonarLintListCellRenderer(SonarLintEngine sonarLintEngine) {
this.sonarLintEngine = sonarLintEngine;
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
flowLayout.setHgap(0);
flowLayout.setVgap(0);
setLayout(flowLayout);
enableOrDisable = new JCheckBox();
enableOrDisable.setEnabled(true);
add(enableOrDisable);
add(defaultListCellRenderer);
}

@Override
public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
Optional<RuleDetails> ruleDetails = sonarLintEngine.getRuleDetails(value);
defaultListCellRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (ruleDetails.isPresent()) {
Optional<ImageIcon> toImageIcon = SonarLintUtils.toImageIcon(ruleDetails.get().getSeverity());
if (toImageIcon.isPresent()) {
defaultListCellRenderer.setIcon(toImageIcon.get());
}
enableOrDisable.setSelected(!sonarLintEngine.isExcluded(ruleDetails.get()));
}
setBackground(defaultListCellRenderer.getBackground());
enableOrDisable.setBackground(defaultListCellRenderer.getBackground());
return this;
}

public boolean clickOnCkeckBox(Point point) {
return enableOrDisable.getBounds().getX() < point.getX()
&& enableOrDisable.getBounds().getX() + enableOrDisable.getBounds().getWidth() > point.getX();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void store() {
});

sonarLintEngine.excludeRuleKeys(ruleKeysDisable);
sonarLintEngine.includeRuleKyes(ruleKeysEnable);
sonarLintEngine.includeRuleKeys(ruleKeysEnable);
}

boolean valid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<EventHandler event="valueChanged" listener="javax.swing.event.ListSelectionListener" parameters="javax.swing.event.ListSelectionEvent" handler="sonarLintAllRulesValueChanged"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_InitCodePre" type="java.lang.String" value="SonarLintEngine sonarLintEngine = Lookup.getDefault().lookup(SonarLintEngine.class);&#xa;sonarLintAllRules.setCellRenderer(new ListCellRenderer&lt;String&gt;() {&#xa; @Override&#xa; public Component getListCellRendererComponent(JList&lt;? extends String&gt; list, String value, int index, boolean isSelected, boolean cellHasFocus) {&#xa; Optional&lt;RuleDetails&gt; ruleDetails = sonarLintEngine.getRuleDetails(value);&#xa; DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();&#xa; Component cell = defaultListCellRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);&#xa; if (ruleDetails.isPresent()) {&#xa; Optional&lt;ImageIcon&gt; toImageIcon = SonarLintUtils.toImageIcon(ruleDetails.get().getSeverity());&#xa; if (toImageIcon.isPresent()) {&#xa; defaultListCellRenderer.setIcon(toImageIcon.get());&#xa; }&#xa; }&#xa; return cell;&#xa; }&#xa;});&#xa;initListAllRuleDetails();"/>
<AuxValue name="JavaCodeGenerator_InitCodePre" type="java.lang.String" value="initListAllRuleDetailsRenderer();&#xa;initListAllRuleDetails();"/>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
</AuxValues>
</Component>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
*/
package fr.philippefichet.sonarlint.netbeans;

import java.awt.Component;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Optional;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import org.netbeans.api.settings.ConvertAsProperties;
Expand All @@ -41,6 +38,7 @@
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;
import org.sonarsource.sonarlint.core.client.api.common.RuleDetails;
import org.sonarsource.sonarlint.core.client.api.common.RuleKey;

/**
* Top component which displays something.
Expand Down Expand Up @@ -121,22 +119,7 @@ public void hyperlinkUpdate(HyperlinkEvent e) {

add(jScrollPane1, java.awt.BorderLayout.CENTER);

SonarLintEngine sonarLintEngine = Lookup.getDefault().lookup(SonarLintEngine.class);
sonarLintAllRules.setCellRenderer(new ListCellRenderer<String>() {
@Override
public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
Optional<RuleDetails> ruleDetails = sonarLintEngine.getRuleDetails(value);
DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
Component cell = defaultListCellRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (ruleDetails.isPresent()) {
Optional<ImageIcon> toImageIcon = SonarLintUtils.toImageIcon(ruleDetails.get().getSeverity());
if (toImageIcon.isPresent()) {
defaultListCellRenderer.setIcon(toImageIcon.get());
}
}
return cell;
}
});
initListAllRuleDetailsRenderer();
initListAllRuleDetails();
sonarLintAllRules.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
sonarLintAllRules.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
Expand Down Expand Up @@ -170,6 +153,35 @@ private void sonarLintAllRulesValueChanged(javax.swing.event.ListSelectionEvent
});
}//GEN-LAST:event_sonarLintAllRulesValueChanged

private void initListAllRuleDetailsRenderer()
{
SonarLintEngine sonarLintEngine = Lookup.getDefault().lookup(SonarLintEngine.class);
sonarLintEngine.whenConfigurationChanged(engine -> sonarLintAllRules.repaint());
sonarLintAllRules.setCellRenderer(new SonarLintListCellRenderer(sonarLintEngine));
sonarLintAllRules.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int locationToIndex = sonarLintAllRules.locationToIndex(e.getPoint());
if (locationToIndex != -1 &&
sonarLintAllRules.getCellBounds(locationToIndex,locationToIndex).contains(e.getPoint())
&& ((SonarLintListCellRenderer)sonarLintAllRules.getCellRenderer())
.clickOnCkeckBox(e.getPoint())
) {
Optional<RuleDetails> ruleDetails = sonarLintEngine.getRuleDetails(sonarLintAllRules.getSelectedValue());
ruleDetails.ifPresent(rule -> {
RuleKey ruleKey = RuleKey.parse(rule.getKey());
if (sonarLintEngine.isExcluded(rule)) {
sonarLintEngine.includeRuleKey(ruleKey);
} else {
sonarLintEngine.excludeRuleKey(ruleKey);
}
sonarLintAllRules.repaint();
});
}
}
});
}

private void initListAllRuleDetails() {
SonarLintEngine sonarLintEngine = Lookup.getDefault().lookup(SonarLintEngine.class);
sonarLintEngine.whenInitialized((SonarLintEngine engine) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Features:\n\
<li>Support Java and Javascript</li>\
<li>Annotation in editor</li>\
<li>Type SonarLint in Action Items</li>\
<li>Enable/Disable Rules in Tools/Options/Miscellaneous/SonarLint</li>\
<li>Filter Rules in Tools/Options/Miscellaneous/SonarLint option panel and "Sonar Rule Details" window</li>\
<li>Enable/Disable Rules in Tools/Options/Miscellaneous/SonarLint and "Sonar Rule Details"</li>\
<li>Severity icons</li>\
</ul>
OpenIDE-Module-Name=sonarlint4netbeans
Expand Down

0 comments on commit 2a5c9b8

Please sign in to comment.