-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solves #190 Trigger Build on Double click
- Loading branch information
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/org/codinjutsu/tools/jenkins/view/JobClickHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters