|
| 1 | +import com.intellij.execution.ExecutionException; |
| 2 | +import com.intellij.execution.configurations.GeneralCommandLine; |
| 3 | +import com.intellij.execution.process.NopProcessHandler; |
| 4 | +import com.intellij.execution.process.OSProcessHandler; |
| 5 | +import com.intellij.execution.process.ProcessEvent; |
| 6 | +import com.intellij.execution.process.ProcessListener; |
| 7 | +import com.intellij.execution.util.ExecutionErrorDialog; |
| 8 | +import com.intellij.ide.macro.MacroManager; |
| 9 | +import com.intellij.openapi.actionSystem.AnAction; |
| 10 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 11 | +import com.intellij.openapi.actionSystem.CommonDataKeys; |
| 12 | +import com.intellij.openapi.actionSystem.DataContext; |
| 13 | +import com.intellij.openapi.editor.Document; |
| 14 | +import com.intellij.openapi.editor.Editor; |
| 15 | +import com.intellij.openapi.fileEditor.FileDocumentManager; |
| 16 | +import com.intellij.openapi.project.Project; |
| 17 | +import com.intellij.tools.HackyDataContext; |
| 18 | +import com.intellij.tools.Tool; |
| 19 | +import com.intellij.tools.ToolProcessAdapter; |
| 20 | +import com.intellij.tools.ToolsBundle; |
| 21 | +import com.intellij.tools.ToolsProvider; |
| 22 | +import org.jetbrains.annotations.NotNull; |
| 23 | +import org.jetbrains.annotations.Nullable; |
| 24 | + |
| 25 | +public class PopupAction extends AnAction { |
| 26 | + private final String actionId; |
| 27 | + |
| 28 | + public PopupAction(@NotNull Tool tool) { |
| 29 | + actionId = tool.getActionId(); |
| 30 | + getTemplatePresentation().setText(tool.getName(), false); |
| 31 | + getTemplatePresentation().setDescription(tool.getDescription()); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void actionPerformed(@NotNull AnActionEvent event) { |
| 36 | + final Editor editor = event.getRequiredData(CommonDataKeys.EDITOR); |
| 37 | + final Project project = event.getRequiredData(CommonDataKeys.PROJECT); |
| 38 | + final Document document = editor.getDocument(); |
| 39 | + Tool tool = findTool(actionId, event.getDataContext()); |
| 40 | + if (tool != null) { |
| 41 | + ReplaceAdapter processListener = new ReplaceAdapter(editor, project, document); |
| 42 | + execute(tool, new HackyDataContext(event.getDataContext()), processListener); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static Tool findTool(String actionId, DataContext context) { |
| 47 | + MacroManager.getInstance().cacheMacrosPreview(context); |
| 48 | + for (Tool tool: ToolsProvider.getAllTools()) { |
| 49 | + if (actionId.equals(tool.getActionId())) { |
| 50 | + return tool; |
| 51 | + } |
| 52 | + } |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + private void execute(Tool tool, DataContext context, @Nullable final ProcessListener processListener) { |
| 57 | + if (!executeIfPossible(tool, context, processListener)) { |
| 58 | + notifyCouldNotStart(processListener); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private boolean executeIfPossible(Tool tool, DataContext context, @Nullable final ProcessListener processListener) { |
| 63 | + final Project project = CommonDataKeys.PROJECT.getData(context); |
| 64 | + if (project == null) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + FileDocumentManager.getInstance().saveAllDocuments(); |
| 69 | + try { |
| 70 | + GeneralCommandLine commandLine = tool.createCommandLine(context); |
| 71 | + if (commandLine == null) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + OSProcessHandler handler = new OSProcessHandler(commandLine); |
| 75 | + handler.addProcessListener(new ToolProcessAdapter(project, tool.synchronizeAfterExecution(), tool.getName())); |
| 76 | + if (processListener != null) { |
| 77 | + handler.addProcessListener(processListener); |
| 78 | + } |
| 79 | + handler.startNotify(); |
| 80 | + } catch (ExecutionException ex) { |
| 81 | + ExecutionErrorDialog.show(ex, ToolsBundle.message("tools.process.start.error"), project); |
| 82 | + return false; |
| 83 | + } |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + private static void notifyCouldNotStart(@Nullable ProcessListener listener) { |
| 88 | + if (listener != null) listener.processTerminated(new ProcessEvent(new NopProcessHandler(), -1)); |
| 89 | + } |
| 90 | +} |
0 commit comments