Skip to content

Commit 971e501

Browse files
authored
Merge pull request #109 from jborgers/master
Order results by rule severity with 5 different levels and icons
2 parents 092a3f4 + 7fe6a1f commit 971e501

23 files changed

+543
-163
lines changed

build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ dependencies {
2828
implementation("net.sourceforge.pmd:pmd-kotlin:${pmdVersion}")
2929
implementation("net.sourceforge.pmd:pmd-xml:${pmdVersion}")
3030
}
31+
java {
32+
sourceCompatibility = JavaVersion.VERSION_11
33+
targetCompatibility = JavaVersion.VERSION_11
34+
}
3135

3236
// Configure gradle-intellij-plugin plugin.
3337
// Read more: https://github.com/JetBrains/gradle-intellij-plugin

src/main/java/com/intellij/plugins/bodhi/pmd/PMDConfigurationForm.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
1414
import com.intellij.ui.DocumentAdapter;
1515
import com.intellij.util.PlatformIcons;
16+
import org.jetbrains.annotations.NotNull;
1617

1718
import javax.swing.*;
1819
import javax.swing.event.ChangeEvent;
@@ -46,7 +47,7 @@ public class PMDConfigurationForm {
4647
private JCheckBox skipTestsCheckBox;
4748

4849
private boolean isModified;
49-
private Project project;
50+
private final Project project;
5051

5152
public static final String STATISTICS_URL = "Statistics URL";
5253
private static final String[] columnNames = new String[] {"Option", "Value"};
@@ -63,8 +64,8 @@ public PMDConfigurationForm(final Project project) {
6364
actionGroup.removeAll();
6465
//Add the toolbar actions associated to this form to it
6566
actionGroup.add(new AddRuleSetAction("Add", "Add a custom ruleset", PlatformIcons.ADD_ICON));
66-
actionGroup.add(new EditRuleSetAction("Edit", "Edit selected ruleset", IconLoader.getIcon("/actions/editSource.png")));
67-
actionGroup.add(new DeleteRuleSetAction("Delete", "Remove selected ruleset", IconLoader.getIcon("/general/remove.png")));
67+
actionGroup.add(new EditRuleSetAction("Edit", "Edit selected ruleset", IconLoader.getIcon("/actions/editSource.png", PMDConfigurationForm.class)));
68+
actionGroup.add(new DeleteRuleSetAction("Delete", "Remove selected ruleset", IconLoader.getIcon("/general/remove.png", PMDConfigurationForm.class)));
6869
ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("modify actions", actionGroup, true);
6970
toolbar.getComponent().setVisible(true);
7071
buttonPanel.setLayout(new BorderLayout());
@@ -167,7 +168,7 @@ private void modifyRuleSet(final String defaultValue, AnActionEvent e) {
167168
if (listModel.data.contains(fileName)) {
168169
return;
169170
}
170-
if (defaultValue.length() > 0) {
171+
if (defaultValue != null && defaultValue.trim().length() > 0) {
171172
listModel.set(ruleList.getSelectedIndex(), fileName);
172173
return;
173174
}
@@ -187,7 +188,7 @@ public AddRuleSetAction(String text, String description, Icon icon) {
187188
registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, KeyEvent.ALT_DOWN_MASK)), rootPanel);
188189
}
189190

190-
public void actionPerformed(AnActionEvent e) {
191+
public void actionPerformed(@NotNull AnActionEvent e) {
191192
String defaultValue = "";
192193
modifyRuleSet(defaultValue, e);
193194
}
@@ -202,13 +203,13 @@ public EditRuleSetAction(String text, String description, Icon icon) {
202203
registerCustomShortcutSet(CommonShortcuts.ALT_ENTER, rootPanel);
203204
}
204205

205-
public void actionPerformed(AnActionEvent e) {
206+
public void actionPerformed(@NotNull AnActionEvent e) {
206207
String defaultValue;
207208
defaultValue = (String) ruleList.getSelectedValue();
208209
modifyRuleSet(defaultValue, e);
209210
}
210211

211-
public void update(AnActionEvent e) {
212+
public void update(@NotNull AnActionEvent e) {
212213
super.update(e);
213214
e.getPresentation().setEnabled(!ruleList.getSelectionModel().isSelectionEmpty());
214215
}
@@ -223,7 +224,7 @@ public DeleteRuleSetAction(String text, String description, Icon icon) {
223224
registerCustomShortcutSet(CommonShortcuts.getDelete(), rootPanel);
224225
}
225226

226-
public void actionPerformed(AnActionEvent e) {
227+
public void actionPerformed(@NotNull AnActionEvent e) {
227228
int index = ruleList.getSelectedIndex();
228229
if (index != -1) {
229230
((MyListModel)ruleList.getModel()).remove(index);
@@ -232,7 +233,7 @@ public void actionPerformed(AnActionEvent e) {
232233
ruleList.repaint();
233234
}
234235

235-
public void update(AnActionEvent e) {
236+
public void update(@NotNull AnActionEvent e) {
236237
super.update(e);
237238
e.getPresentation().setEnabled(ruleList.getSelectedIndex() != -1);
238239
}
@@ -295,7 +296,7 @@ private void validateStatUrl(String url, int row, int column, Object orig, boole
295296

296297
private class MyListModel extends AbstractListModel {
297298

298-
private List<String> data;
299+
private final List<String> data;
299300

300301
public MyListModel(List<String> data) {
301302
this.data = data;
@@ -337,17 +338,12 @@ public synchronized void set(int selIndex, String fileName) {
337338
* select a ruleset file.
338339
*/
339340
static class BrowsePanel extends JPanel {
340-
private JLabel label;
341-
private JTextField path;
342-
private JButton open;
343-
344-
private Project project;
341+
private final JTextField path;
345342

346343
public BrowsePanel(String defaultValue, final DialogBuilder db, final Project project) {
347344
super();
348-
this.project = project;
349345
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
350-
label = new JLabel("Choose RuleSet: ");
346+
JLabel label = new JLabel("Choose RuleSet: ");
351347
label.setMinimumSize(new Dimension(100, 20));
352348
label.setMaximumSize(new Dimension(120, 20));
353349
label.setPreferredSize(new Dimension(100, 20));
@@ -358,7 +354,7 @@ public BrowsePanel(String defaultValue, final DialogBuilder db, final Project pr
358354
path.setPreferredSize(new Dimension(200, 20));
359355
add(path);
360356
add(Box.createHorizontalStrut(5));
361-
open = new JButton("Browse");
357+
JButton open = new JButton("Browse");
362358
label.setMinimumSize(new Dimension(50, 20));
363359
label.setMaximumSize(new Dimension(150, 20));
364360
open.setPreferredSize(new Dimension(80, 20));
@@ -381,7 +377,7 @@ public void actionPerformed(ActionEvent e) {
381377
db.setCenterPanel(this);
382378

383379
path.getDocument().addDocumentListener(new DocumentAdapter() {
384-
protected void textChanged(DocumentEvent e) {
380+
protected void textChanged(@NotNull DocumentEvent e) {
385381
try {
386382
Document doc = e.getDocument();
387383
db.setOkActionEnabled(doc.getText(0, doc.getLength()).trim().length() > 0);

src/main/java/com/intellij/plugins/bodhi/pmd/PMDInvoker.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import com.intellij.openapi.wm.ToolWindow;
1717
import com.intellij.openapi.wm.ToolWindowManager;
1818
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
19-
import com.intellij.plugins.bodhi.pmd.tree.PMDBranchNode;
20-
import com.intellij.plugins.bodhi.pmd.tree.PMDRootNode;
19+
import com.intellij.plugins.bodhi.pmd.tree.*;
2120

2221
import java.io.File;
2322
import java.util.LinkedList;
@@ -144,7 +143,9 @@ public void runPMD(AnActionEvent actionEvent, String ruleSetPaths, boolean isCus
144143
*/
145144
public void processFiles(Project project, final String ruleSetPaths, final List<File> files, final boolean isCustomRuleSet, final PMDProjectComponent projectComponent) {
146145
ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(PMDProjectComponent.TOOL_ID);
147-
toolWindow.activate(null);
146+
if (toolWindow != null) {
147+
toolWindow.activate(null);
148+
}
148149

149150
//Save all files
150151
ApplicationManager.getApplication().saveAll();
@@ -170,19 +171,17 @@ public void run() {
170171
PMDResultCollector collector = new PMDResultCollector();
171172

172173
//Get the tree nodes from result collector
173-
List<PMDBranchNode> resultRuleNodes = collector.runPMDAndGetResults(files, ruleSetPath, projectComponent);
174-
175-
if (resultRuleNodes.size() != 0) {
176-
String ruleSetName;
177-
if (isCustomRuleSet) {
178-
//For custom rulesets, using a separate format for rendering
179-
ruleSetName = PMDUtil.getBareFileNameFromPath(ruleSetPath) + ";" + ruleSetPath;
180-
} else {
181-
ruleSetName = PMDUtil.getBareFileNameFromPath(ruleSetPath);
182-
}
183-
PMDBranchNode ruleSetNode = resultPanel.addCreateBranchNodeAtRoot(ruleSetName);
174+
List<PMDRuleSetEntryNode> resultRuleNodes = collector.runPMDAndGetResults(files, ruleSetPath, projectComponent);
175+
// sort rules by priority, rule and suppressed nodes are comparable
176+
resultRuleNodes.sort(null);
177+
178+
if (!resultRuleNodes.isEmpty()) {
179+
String ruleSetName = PMDUtil.getBareFileNameFromPath(ruleSetPath);
180+
String desc = PMDResultCollector.getRuleSetDescription(ruleSetPath);
181+
PMDRuleSetNode ruleSetNode = resultPanel.addCreateRuleSetNodeAtRoot(ruleSetName);
182+
ruleSetNode.setToolTip(desc);
184183
//Add all rule nodes to the tree
185-
for (PMDBranchNode resultRuleNode : resultRuleNodes) {
184+
for (PMDRuleSetEntryNode resultRuleNode : resultRuleNodes) {
186185
resultPanel.addNode(ruleSetNode, resultRuleNode);
187186
}
188187
rootNode.calculateCounts();

src/main/java/com/intellij/plugins/bodhi/pmd/PMDProjectComponent.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
1919
import com.intellij.ui.content.Content;
2020
import com.intellij.ui.content.ContentFactory;
21-
import net.sourceforge.pmd.RuleSet;
2221
import org.jetbrains.annotations.NonNls;
2322
import org.jetbrains.annotations.NotNull;
2423

25-
import javax.swing.*;
2624
import java.util.*;
2725

2826
/**
@@ -115,14 +113,7 @@ void updateCustomRulesMenu() {
115113
List<AnAction> newActionList = new ArrayList<>();
116114
boolean hasDuplicate = hasDuplicateBareFileName(customRuleSetPaths);
117115
for (final String ruleSetPath : customRuleSetPaths) {
118-
String ruleSetName;
119-
try {
120-
RuleSet ruleSet = PMDResultCollector.loadRuleSet(ruleSetPath);
121-
ruleSetName = ruleSet.getName(); // from the xml
122-
} catch (PMDResultCollector.InvalidRuleSetException e) {
123-
String msg = (e.getCause() == null) ? e.getMessage(): e.getCause().getMessage();
124-
ruleSetName = msg.substring(0, Math.min(25, msg.length()));
125-
}
116+
String ruleSetName = PMDResultCollector.getRuleSetName(ruleSetPath);
126117
String extFileName = PMDUtil.getExtendedFileNameFromPath(ruleSetPath);
127118
String bareFileName = PMDUtil.getBareFileNameFromPath(ruleSetPath);
128119
String actionText = ruleSetName;

src/main/java/com/intellij/plugins/bodhi/pmd/PMDResultPanel.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.intellij.plugins.bodhi.pmd;
22

3+
import com.intellij.CommonBundle;
34
import com.intellij.ide.*;
45
import com.intellij.openapi.actionSystem.*;
56
import com.intellij.openapi.application.ApplicationManager;
@@ -24,6 +25,7 @@
2425
import com.intellij.util.ui.tree.TreeUtil;
2526
import net.sourceforge.pmd.Report;
2627
import net.sourceforge.pmd.renderers.HTMLRenderer;
28+
import org.jetbrains.annotations.NotNull;
2729
import org.jetbrains.annotations.Nullable;
2830

2931
import javax.swing.*;
@@ -52,12 +54,12 @@
5254
*/
5355
public class PMDResultPanel extends JPanel {
5456

55-
private JTree resultTree;
56-
private PMDProjectComponent projectComponent;
57+
private final JTree resultTree;
58+
private final PMDProjectComponent projectComponent;
5759
private PMDRootNode rootNode;
58-
private PMDBranchNode processingErrorsNode;
60+
private PMDErrorBranchNode processingErrorsNode;
5961
private boolean scrolling;
60-
private PMDPopupMenu popupMenu;
62+
private final PMDPopupMenu popupMenu;
6163
public static final String PMD_SUPPRESSION = "//NOPMD";
6264

6365
/**
@@ -122,7 +124,9 @@ public void actionPerformed(ActionEvent e) {
122124
for (PMDViolation result : unique.values()) {
123125
//Suppress the violation
124126
final Editor editor = openEditor(result);
125-
executeWrite(editor, result);
127+
if (editor != null) {
128+
executeWrite(editor, result);
129+
}
126130
}
127131
} else if (e.getActionCommand().equals(PMDPopupMenu.DETAILS)) {
128132
Set<String> urls = new HashSet<>();
@@ -278,11 +282,11 @@ protected Navigatable createDescriptorForNode(DefaultMutableTreeNode node) {
278282
}
279283

280284
public String getNextOccurenceActionName() {
281-
return null;
285+
return UsageViewBundle.message("action.next.occurrence");
282286
}
283287

284288
public String getPreviousOccurenceActionName() {
285-
return null;
289+
return UsageViewBundle.message("action.previous.occurrence");
286290
}
287291
};
288292

@@ -303,13 +307,13 @@ public JComponent getSettingsEditor() {
303307
return null;
304308
}
305309

306-
public void addSettingsChangedListener(ChangeListener listener) throws TooManyListenersException {
310+
public void addSettingsChangedListener(ChangeListener listener) {
307311
}
308312

309313
public void removeSettingsChangedListener(ChangeListener listener) {
310314
}
311315

312-
public String getReportText() {
316+
public @NotNull String getReportText() {
313317
Report r = PMDResultCollector.getReport();
314318
HTMLRenderer renderer = new HTMLRenderer();
315319
StringWriter w = new StringWriter();
@@ -322,11 +326,11 @@ public String getReportText() {
322326
return "";
323327
}
324328

325-
public String getDefaultFilePath() {
329+
@NotNull public String getDefaultFilePath() {
326330
return "report.html";
327331
}
328332

329-
public void exportedTo(String filePath) {
333+
public void exportedTo(@NotNull String filePath) {
330334
}
331335

332336
public boolean canExport() {
@@ -351,7 +355,7 @@ public final void initializeTree() {
351355
* @param treeNode The tree node having the violation/suppressed/error
352356
*/
353357
public void highlightFindingInEditor(DefaultMutableTreeNode treeNode) {
354-
if (treeNode != null && treeNode instanceof Navigatable) {
358+
if (treeNode instanceof Navigatable) {
355359
((Navigatable)treeNode).navigate(true);
356360
}
357361
}
@@ -387,13 +391,13 @@ private Editor openEditor(HasPositionInFile finding) {
387391
}
388392

389393
/**
390-
* Adds a branch node to the tree as a direct child of the root, and return it
394+
* Adds a rule set node to the tree as a direct child of the root, and return it
391395
*
392396
* @param name the rule name
393397
* @return the created rule set node
394398
*/
395-
public PMDBranchNode addCreateBranchNodeAtRoot(String name) {
396-
return (PMDBranchNode)addNode(rootNode, new PMDBranchNode(name));
399+
public PMDRuleSetNode addCreateRuleSetNodeAtRoot(String name) {
400+
return (PMDRuleSetNode)addNode(rootNode, new PMDRuleSetNode(name));
397401
}
398402

399403
/**
@@ -429,10 +433,11 @@ public PMDRootNode getRootNode() {
429433
/**
430434
* Creates and returns the processingErrors branch node.
431435
*
432-
* @return the new processingErrors brnach node
436+
* @return the new processingErrors branch node
433437
*/
434-
public PMDBranchNode getNewProcessingErrorsNode() {
435-
return processingErrorsNode = new PMDBranchNode("Processing errors");
438+
public PMDErrorBranchNode getNewProcessingErrorsNode() {
439+
processingErrorsNode = new PMDErrorBranchNode("Processing errors");
440+
return processingErrorsNode;
436441
}
437442

438443
/**
@@ -449,7 +454,7 @@ public void addProcessingErrorsNodeToRootIfHasAny() {
449454
*/
450455
private class ReRunAction extends AnAction {
451456
public ReRunAction() {
452-
super(UsageViewBundle.message("action.rerun"), UsageViewBundle.message("action.description.rerun"), IconLoader.getIcon("/actions/refreshUsages.png"));
457+
super(CommonBundle.message("action.rerun"), UsageViewBundle.message("action.description.rerun"), IconLoader.getIcon("/actions/refreshUsages.png", ReRunAction.class));
453458
registerCustomShortcutSet(CommonShortcuts.getRerun(), PMDResultPanel.this);
454459
}
455460

@@ -471,18 +476,20 @@ public void actionPerformed(AnActionEvent e) {
471476
/**
472477
* Inner class for close action.
473478
*/
474-
private class CloseAction extends AnAction {
479+
private static class CloseAction extends AnAction {
475480
private static final String ACTION_CLOSE = "action.close";
476481

477482
private CloseAction() {
478-
super(UsageViewBundle.message(ACTION_CLOSE), null, IconLoader.getIcon("/actions/cancel.png"));
483+
super(CommonBundle.message(ACTION_CLOSE), null, IconLoader.getIcon("/actions/cancel.png", CloseAction.class));
479484
}
480485

481486
public void actionPerformed(AnActionEvent e) {
482487
Project project = e.getData(PlatformDataKeys.PROJECT);
483488
if (project != null) {
484489
ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(PMDProjectComponent.TOOL_ID);
485-
toolWindow.activate(null);
490+
if (toolWindow != null) {
491+
toolWindow.activate(null);
492+
}
486493
PMDProjectComponent plugin = project.getComponent(PMDProjectComponent.class);
487494
plugin.closeResultWindow();
488495
}

0 commit comments

Comments
 (0)