This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseAction.java
More file actions
76 lines (62 loc) · 2.73 KB
/
BaseAction.java
File metadata and controls
76 lines (62 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gitextensions.commands;
import com.google.common.base.Strings;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import gitextensions.GitExtensionsService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class BaseAction extends AnAction {
private static final String ERROR_MESSAGE = "Installed GitExtensions application is not found. You can set path to the executable file manually (File | Settings... | GitExtensions) or download and install GitExtensions from http://gitextensions.github.io/";
private String command;
public BaseAction(@NotNull String command) {
this.command = command;
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
try {
String fileName = getFileNameFromEvent(e);
if (fileName != null) {
GitExtensionsService service = ApplicationManager.getApplication().getService(GitExtensionsService.class);
String path = service.getSettings().getExecutablePath();
if (Strings.isNullOrEmpty(path)) {
Messages.showMessageDialog(ERROR_MESSAGE, "Error", Messages.getErrorIcon());
return;
}
if (!new File(path).isFile()) {
Messages.showMessageDialog(String.format("File %s is not found", path), "Error", Messages.getErrorIcon());
return;
}
List<String> args = new ArrayList<>(Arrays.asList(path, command, fileName));
String additionalArgs = getAdditionalParameters(e);
if (additionalArgs != null) {
args.add(additionalArgs);
}
new ProcessBuilder(args).start();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Nullable
protected String getFileNameFromEvent(@NotNull AnActionEvent e) {
VirtualFile file = e.getData(PlatformDataKeys.VIRTUAL_FILE);
return getFileName(file);
}
protected String getFileName(@Nullable VirtualFile file) {
return file != null ? file.getCanonicalPath() : null;
}
@Nullable
protected String getAdditionalParameters(AnActionEvent e) {
return null;
}
}