Skip to content

Commit

Permalink
Extract Tree for Rendering
Browse files Browse the repository at this point in the history
Improve search within Jenkins Tree
  • Loading branch information
MCMicS committed Jul 28, 2020
1 parent fab55ad commit c896ed2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
53 changes: 53 additions & 0 deletions src/main/java/org/codinjutsu/tools/jenkins/JenkinsTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.codinjutsu.tools.jenkins;

import com.intellij.openapi.project.Project;
import com.intellij.ui.TreeUIHelper;
import com.intellij.ui.treeStructure.SimpleTree;
import com.intellij.util.containers.Convertor;
import org.codinjutsu.tools.jenkins.model.Jenkins;
import org.codinjutsu.tools.jenkins.model.Job;
import org.codinjutsu.tools.jenkins.view.BuildStatusEnumRenderer;
import org.codinjutsu.tools.jenkins.view.JenkinsTreeRenderer;
import org.codinjutsu.tools.jenkins.view.JobClickHandler;
import org.jetbrains.annotations.NotNull;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class JenkinsTree extends SimpleTree {
private static final String LOADING = "Loading...";
private final transient Project project;
@NotNull
private final JenkinsSettings jenkinsSettings;
private final Jenkins jenkins;

public JenkinsTree(Project project, @NotNull JenkinsSettings jenkinsSettings, Jenkins jenkins) {
super();
this.project = project;
this.jenkinsSettings = jenkinsSettings;
this.jenkins = jenkins;
getEmptyText().setText(LOADING);
setCellRenderer(new JenkinsTreeRenderer(this.jenkinsSettings::isFavoriteJob,
BuildStatusEnumRenderer.getInstance(this.project)));
setName("jobTree");
setModel(new DefaultTreeModel(new DefaultMutableTreeNode(this.jenkins), false));
//final JobTreeHandler jobTreeHandler = new JobTreeHandler(project);
//addTreeWillExpandListener(jobTreeHandler);
addMouseListener(new JobClickHandler());
}

@Override
protected void configureUiHelper(TreeUIHelper helper) {
final Convertor<TreePath, String> convertor = treePath -> {
final DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();
final Object userObject = node.getUserObject();
if (userObject instanceof Job) {
//return ((Job) userObject).getNameToRenderSingleJob();
return ((Job) userObject).preferDisplayName();
}
return "";
};
helper.installTreeSpeedSearch(this, convertor, true);
}
}
32 changes: 3 additions & 29 deletions src/main/java/org/codinjutsu/tools/jenkins/view/BrowserPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.ui.PopupHandler;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.TreeSpeedSearch;
import com.intellij.ui.treeStructure.SimpleTree;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.ui.tree.TreeUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.codinjutsu.tools.jenkins.JenkinsAppSettings;
import org.codinjutsu.tools.jenkins.JenkinsSettings;
import org.codinjutsu.tools.jenkins.JenkinsTree;
import org.codinjutsu.tools.jenkins.exception.ConfigurationException;
import org.codinjutsu.tools.jenkins.logic.*;
import org.codinjutsu.tools.jenkins.model.*;
Expand Down Expand Up @@ -66,7 +65,6 @@ public class BrowserPanel extends SimpleToolWindowPanel {
public static final String JENKINS_PANEL_PLACE = "jenkinsBrowserActions";
private static final Logger logger = Logger.getLogger(BrowserPanel.class);
private static final String UNAVAILABLE = "No Jenkins server available";
private static final String LOADING = "Loading...";
private static final JobNameComparator JOB_NAME_COMPARATOR = new JobNameComparator();
private static final Comparator<Job> sortByStatusComparator = Comparator.comparing(BrowserPanel::toBuildStatus);
private static final Comparator<Job> sortByNameComparator = Comparator.comparing(Job::getNameToRenderSingleJob, JOB_NAME_COMPARATOR);
Expand All @@ -76,8 +74,8 @@ public class BrowserPanel extends SimpleToolWindowPanel {
private final JenkinsAppSettings jenkinsAppSettings;
@NotNull
private final JenkinsSettings jenkinsSettings;
private final RequestManager requestManager;
private final Jenkins jenkins;
private final RequestManager requestManager;
private final Map<String, Job> watchedJobs = new ConcurrentHashMap<>();
private JPanel rootPanel;
private JPanel jobPanel;
Expand All @@ -98,8 +96,7 @@ public BrowserPanel(final Project project) {
setProvideQuickActions(false);

jenkins = Jenkins.byDefault();
jobTree = createTree();

jobTree = new JenkinsTree(project, jenkinsSettings, jenkins);

jobPanel.setLayout(new BorderLayout());
jobPanel.add(ScrollPaneFactory.createScrollPane(jobTree), BorderLayout.CENTER);
Expand Down Expand Up @@ -337,29 +334,6 @@ public void notifyErrorJenkinsToolWindow(@NotNull String message) {
JenkinsNotifier.getInstance(project).error(message);
}

private Tree createTree() {
SimpleTree tree = new SimpleTree();
tree.getEmptyText().setText(LOADING);
tree.setCellRenderer(new JenkinsTreeRenderer(jenkinsSettings::isFavoriteJob,
BuildStatusEnumRenderer.getInstance(project)));
tree.setName("jobTree");
tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(jenkins), false));
//final JobTreeHandler jobTreeHandler = new JobTreeHandler(project);
//tree.addTreeWillExpandListener(jobTreeHandler);
tree.addMouseListener(new JobClickHandler());

new TreeSpeedSearch(tree, treePath -> {
final DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();
final Object userObject = node.getUserObject();
if (userObject instanceof Job) {
return ((Job) userObject).getNameToRenderSingleJob();
}
return "<empty>";
});

return tree;
}

public void handleEmptyConfiguration() {
currentSelectedView = null;
setJobsUnavailable();
Expand Down

0 comments on commit c896ed2

Please sign in to comment.