Skip to content

Commit

Permalink
Split up result tree node instead of reusing for multiple things
Browse files Browse the repository at this point in the history
  • Loading branch information
jshiell committed Aug 8, 2024
1 parent 6be328b commit 6152fc9
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private void clearProgress() {
*/
private void scrollToError(final TreePath treePath) {
final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
if (treeNode == null || !(treeNode.getUserObject() instanceof ResultTreeNode nodeInfo)) {
if (treeNode == null || !(treeNode.getUserObject() instanceof ProblemResultTreeNode nodeInfo)) {
return;
}

Expand Down Expand Up @@ -326,11 +326,11 @@ private void scrollToError(final TreePath treePath) {
}, ModalityState.NON_MODAL);
}

private int lineFor(final ResultTreeNode nodeInfo) {
private int lineFor(final ProblemResultTreeNode nodeInfo) {
return nodeInfo.getProblem().line();
}

private int columnFor(final ResultTreeNode nodeInfo) {
private int columnFor(final ProblemResultTreeNode nodeInfo) {
return nodeInfo.getProblem().column();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.infernus.idea.checkstyle.toolwindow;

import com.intellij.icons.AllIcons;
import org.infernus.idea.checkstyle.CheckStyleBundle;

public class FileResultTreeNode extends ResultTreeNode {

private final String fileName;
private final int totalProblems;
private int visibleProblems;

/**
* Construct a file node.
*
* @param fileName the name of the file.
* @param problemCount the number of problems in the file.
*/
public FileResultTreeNode(final String fileName, final int problemCount) {
super(CheckStyleBundle.message("plugin.results.scan-file-result", fileName, problemCount));

if (fileName == null) {
throw new IllegalArgumentException("Filename may not be null");
}

this.fileName = fileName;
this.totalProblems = problemCount;
this.visibleProblems = problemCount;

updateTextForFileNode();

setIcon(AllIcons.FileTypes.Java);
}

private void updateTextForFileNode() {
if (totalProblems == visibleProblems) {
setText(CheckStyleBundle.message("plugin.results.scan-file-result", fileName, totalProblems));
} else {
setText(CheckStyleBundle.message("plugin.results.scan-file-result.filtered", fileName, visibleProblems, totalProblems - visibleProblems));
}
}

void setVisibleProblems(final int visibleProblems) {
this.visibleProblems = visibleProblems;

updateTextForFileNode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.infernus.idea.checkstyle.toolwindow;

import com.intellij.icons.AllIcons;
import com.intellij.psi.PsiFile;
import org.infernus.idea.checkstyle.CheckStyleBundle;
import org.infernus.idea.checkstyle.checker.Problem;
import org.infernus.idea.checkstyle.csapi.SeverityLevel;
import org.jetbrains.annotations.NotNull;

public class ProblemResultTreeNode extends ResultTreeNode {

private final PsiFile file;
private final Problem problem;
private final SeverityLevel severity;

/**
* Construct a node for a given problem.
*
* @param file the file the problem exists in.
* @param problem the problem.
*/
public ProblemResultTreeNode(@NotNull final PsiFile file,
@NotNull final Problem problem) {
super(CheckStyleBundle.message("plugin.results.file-result",
file.getName(),
problem.message(),
problem.line(),
Integer.toString(problem.column()),
problem.sourceCheck()));

this.file = file;
this.problem = problem;

severity = problem.severityLevel();

updateIconsForProblem();
}

private void updateIconsForProblem() {
if (SeverityLevel.Ignore.equals(severity)) {
setIcon(AllIcons.General.Note);
} else if (SeverityLevel.Warning.equals(severity)) {
setIcon(AllIcons.General.Warning);
} else if (SeverityLevel.Info.equals(severity)) {
setIcon(AllIcons.General.Information);
} else {
setIcon(AllIcons.General.Error);
}
}

/**
* Get the file associated with this node.
*
* @return the file associated with this node.
*/
public PsiFile getFile() {
return file;
}

/**
* Get the problem associated with this node.
*
* @return the problem associated with this node.
*/
public Problem getProblem() {
return problem;
}

/**
* Get the severity of the problem.
*
* @return the severity, or null if not applicable.
*/
public SeverityLevel getSeverity() {
return severity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;

import com.intellij.psi.PsiFile;
Expand Down Expand Up @@ -86,15 +85,15 @@ public void filter(final SeverityLevel... levels) {
private void filter(final boolean sendEvents, final SeverityLevel... levels) {
for (final ToggleableTreeNode fileNode : visibleRootNode.getAllChildren()) {
for (final ToggleableTreeNode problemNode : fileNode.getAllChildren()) {
final ResultTreeNode result = (ResultTreeNode) problemNode.getUserObject();
final ProblemResultTreeNode result = (ProblemResultTreeNode) problemNode.getUserObject();

final boolean resultShouldBeVisible = contains(levels, result.getSeverity());
if (problemNode.isVisible() != resultShouldBeVisible) {
problemNode.setVisible(resultShouldBeVisible);
}
}

((ResultTreeNode) fileNode.getUserObject()).setVisibleProblems(fileNode.getChildCount());
((FileResultTreeNode) fileNode.getUserObject()).setVisibleProblems(fileNode.getChildCount());
final boolean fileNodeShouldBeVisible = fileNode.getChildCount() > 0;
if (fileNode.isVisible() != fileNodeShouldBeVisible) {
fileNode.setVisible(fileNodeShouldBeVisible);
Expand Down Expand Up @@ -158,7 +157,7 @@ public void setModel(final Map<PsiFile, List<Problem>> results,
if (problems != null) {
for (final Problem problem : problems) {
if (problem.severityLevel() != SeverityLevel.Ignore) {
final ResultTreeNode problemObj = new ResultTreeNode(file, problem);
final ResultTreeNode problemObj = new ProblemResultTreeNode(file, problem);

final ToggleableTreeNode problemNode = new ToggleableTreeNode(problemObj);
fileNode.add(problemNode);
Expand All @@ -171,7 +170,7 @@ public void setModel(final Map<PsiFile, List<Problem>> results,
itemCount += problemCount;

if (problemCount > 0) {
final ResultTreeNode nodeObject = new ResultTreeNode(file.getName(), problemCount);
final ResultTreeNode nodeObject = new FileResultTreeNode(file.getName(), problemCount);
fileNode.setUserObject(nodeObject);

visibleRootNode.add(fileNode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
package org.infernus.idea.checkstyle.toolwindow;

import javax.swing.Icon;

import com.intellij.icons.AllIcons;
import com.intellij.psi.PsiFile;
import org.infernus.idea.checkstyle.CheckStyleBundle;
import org.infernus.idea.checkstyle.checker.Problem;
import org.infernus.idea.checkstyle.csapi.SeverityLevel;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

import static org.infernus.idea.checkstyle.util.Strings.isBlank;


/**
* The user object for meta-data on tree nodes in the tool window.
*
* TODO this is covering for both files & problems at present, which is messy
*/
public class ResultTreeNode {

private PsiFile file;
private Problem problem;
private Icon icon;
private String text;
private String description;
private SeverityLevel severity;
private String fileName;
private int totalProblems;
private int visibleProblems;
private Icon icon = AllIcons.General.Information;

/**
* Construct an informational node.
Expand All @@ -39,80 +26,6 @@ public ResultTreeNode(final String text) {
}

this.text = text;
icon = AllIcons.General.Information;
}


/**
* Construct a file node.
*
* @param fileName the name of the file.
* @param problemCount the number of problems in the file.
*/
public ResultTreeNode(final String fileName, final int problemCount) {
if (fileName == null) {
throw new IllegalArgumentException("Filename may not be null");
}

this.fileName = fileName;
this.totalProblems = problemCount;
this.visibleProblems = problemCount;
updateTextForFileNode();
icon = AllIcons.FileTypes.Java;
}

private void updateTextForFileNode() {
if (totalProblems == visibleProblems) {
this.text = CheckStyleBundle.message("plugin.results.scan-file-result", fileName, totalProblems);
} else {
this.text = CheckStyleBundle.message("plugin.results.scan-file-result.filtered", fileName, visibleProblems, totalProblems - visibleProblems);
}
}

/**
* Construct a node for a given problem.
*
* @param file the file the problem exists in.
* @param problem the problem.
*/
public ResultTreeNode(@NotNull final PsiFile file,
@NotNull final Problem problem) {
this.file = file;
this.problem = problem;

severity = problem.severityLevel();

updateIconsForProblem();
}

private void updateIconsForProblem() {
if (SeverityLevel.Ignore.equals(severity)) {
icon = AllIcons.General.Note;
} else if (SeverityLevel.Warning.equals(severity)) {
icon = AllIcons.General.Warning;
} else if (SeverityLevel.Info.equals(severity)) {
icon = AllIcons.General.Information;
} else {
icon = AllIcons.General.Error;
}
}

/**
* Get the severity of the problem.
*
* @return the severity, or null if not applicable.
*/
public SeverityLevel getSeverity() {
return severity;
}

/**
* Get the problem associated with this node.
*
* @return the problem associated with this node.
*/
public Problem getProblem() {
return problem;
}

/**
Expand Down Expand Up @@ -147,53 +60,19 @@ public String getText() {
*
* @param text the file the node represents.
*/
public void setText(final String text) {
void setText(final String text) {
if (isBlank(text)) {
throw new IllegalArgumentException("Text may not be null/empty");
}
this.text = text;
}

/**
* Get the file associated with this node.
*
* @return the file associated with this node.
*/
public PsiFile getFile() {
return file;
}

/**
* Get the description of this node, if any.
*
* @return the description of this node, or null if none.
*/
public String getDescription() {
return description;
}

/**
* Get the description of this node, if any.
*
* @param description the description of this node, or null if none.
*/
public void setDescription(final String description) {
this.description = description;
}

public void setVisibleProblems(final int visibleProblems) {
this.visibleProblems = visibleProblems;

updateTextForFileNode();
protected void setIcon(final Icon icon) {
this.icon = icon;
}

@Override
public String toString() {
if (text != null) {
return text;
}

return CheckStyleBundle.message("plugin.results.file-result", file.getName(),
problem.message(), problem.line(), Integer.toString(problem.column()), problem.sourceCheck());
return text;
}
}

0 comments on commit 6152fc9

Please sign in to comment.