Skip to content

Commit

Permalink
solves #190 Trigger Build on Double click
Browse files Browse the repository at this point in the history
  • Loading branch information
MCMicS committed Apr 25, 2020
1 parent 4aada51 commit a7d542f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ private Tree createTree() {
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();
Expand Down Expand Up @@ -414,7 +415,9 @@ private void installActionsInToolbar() {
final LoadBuildsAction loadBuildsAction = new LoadBuildsAction();
ActionManager.getInstance().registerAction(LoadBuildsAction.ACTION_ID, loadBuildsAction, PluginId.getId(JenkinsSettings.PLUGIN_ID));
actionGroup.add(loadBuildsAction);
actionGroup.add(new RunBuildAction(this));
final RunBuildAction runBuildAction = new RunBuildAction(this);
actionGroup.add(runBuildAction);
ActionManager.getInstance().registerAction(RunBuildAction.ACTION_ID, runBuildAction, PluginId.getId(JenkinsSettings.PLUGIN_ID));
actionGroup.add(new StopBuildAction(this));
actionGroup.add(new SortByStatusAction(this));
actionGroup.add(new RefreshRssAction());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.codinjutsu.tools.jenkins.view;

import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.ui.playback.commands.ActionCommand;
import org.codinjutsu.tools.jenkins.model.Job;
import org.codinjutsu.tools.jenkins.view.action.RunBuildAction;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Optional;

public class JobClickHandler extends MouseAdapter {

private static void triggerBuild(@NotNull Job job) {
final AnAction action = ActionManager.getInstance().getAction(RunBuildAction.ACTION_ID);
if (action instanceof RunBuildAction) {
final InputEvent inputEvent = ActionCommand.getInputEvent(RunBuildAction.ACTION_ID);
ActionManager.getInstance().tryToExecute(action, inputEvent, null, BrowserPanel.JENKINS_PANEL_PLACE, true);
}
}

@NotNull
private static Optional<TreePath> getTreePath(MouseEvent event) {
return Optional.ofNullable(event.getSource())
.filter(JTree.class::isInstance).map(JTree.class::cast)
.map(tree -> tree.getPathForLocation(event.getX(), event.getY()));
}

@NotNull
private static Optional<Job> getJob(TreePath treePath) {
final Object node = treePath.getLastPathComponent();
return Optional.ofNullable(node)
.filter(DefaultMutableTreeNode.class::isInstance).map(DefaultMutableTreeNode.class::cast)
.map(DefaultMutableTreeNode::getUserObject)
.filter(Job.class::isInstance).map(Job.class::cast);
}

@NotNull
private static Optional<Job> getJob(MouseEvent event) {
return getTreePath(event).flatMap(JobClickHandler::getJob);
}

@Override
public void mouseClicked(@NotNull MouseEvent event) {
if (event.getClickCount() == 2) { // Double click
getJob(event).filter(Job::isBuildable).ifPresent(JobClickHandler::triggerBuild);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

public class RunBuildAction extends AnAction implements DumbAware {

public static final String ACTION_ID = "Jenkins.RunBuild";

private static final Icon EXECUTE_ICON = AllIcons.Actions.Execute;
private static final Logger LOG = Logger.getLogger(RunBuildAction.class.getName());
public static final int BUILD_STATUS_UPDATE_DELAY = 1;
Expand Down

0 comments on commit a7d542f

Please sign in to comment.