From 81529bb0b0e998416a1e190c1db4137d2235890b Mon Sep 17 00:00:00 2001 From: zaaarf Date: Fri, 17 Nov 2023 21:14:01 +0100 Subject: [PATCH] chore: moved modal messages to notification --- .../intellij/actions/ConnectAction.java | 13 ++++++++---- .../intellij/actions/FastForwardAction.java | 2 +- .../actions/buffer/BufferAttachAction.java | 11 +++++----- .../actions/buffer/BufferCreateAction.java | 16 +++++++------- .../buffer/BufferCreateWithContentAction.java | 11 +++++----- .../actions/buffer/BufferDetachAction.java | 21 ++++++++++--------- .../workspace/WorkspaceJoinAction.java | 15 ++++++------- .../workspace/WorkspaceLeaveAction.java | 12 +++++------ .../listeners/BufferEventListener.java | 1 - .../listeners/CursorEventListener.java | 1 - .../intellij/task/CursorEventAwaiterTask.java | 1 - .../com/codemp/intellij/util/ActionUtil.java | 21 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 3 +-- 13 files changed, 75 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/codemp/intellij/actions/ConnectAction.java b/src/main/java/com/codemp/intellij/actions/ConnectAction.java index d9c73f6..fdd21f7 100644 --- a/src/main/java/com/codemp/intellij/actions/ConnectAction.java +++ b/src/main/java/com/codemp/intellij/actions/ConnectAction.java @@ -2,16 +2,19 @@ package com.codemp.intellij.actions; import com.codemp.intellij.CodeMP; import com.codemp.intellij.jni.CodeMPHandler; +import com.codemp.intellij.util.ActionUtil; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.ui.Messages; import org.jetbrains.annotations.NotNull; public class ConnectAction extends AnAction { - public static void connect(String url, boolean silent) throws Exception { + public static void connect(AnActionEvent e, String url, boolean silent) throws Exception { CodeMP.loadLibrary(); //will only load it the first time CodeMPHandler.connect(url); - if(!silent) Messages.showInfoMessage(String.format("Connected to %s!", url), "CodeMP"); + + if(!silent) ActionUtil.notify(e, + "Success", String.format("Connected to %s!", url)); CodeMP.LOGGER.debug("Connected to {}!", url); } @@ -20,9 +23,11 @@ public class ConnectAction extends AnAction { String url = Messages.showInputDialog("URL to CodeMP instance:", "CodeMP Connect", Messages.getQuestionIcon()); try { - connect(url, false); + connect(e, url, false); } catch(Exception ex) { - Messages.showErrorDialog(String.format("Failed to connect to %s: %s!", url, ex.getMessage()), "CodeMP"); + ActionUtil.notifyError(e, String.format( + "Failed to connect to %s!", + url), ex); } } } diff --git a/src/main/java/com/codemp/intellij/actions/FastForwardAction.java b/src/main/java/com/codemp/intellij/actions/FastForwardAction.java index e2ba196..066e9f9 100644 --- a/src/main/java/com/codemp/intellij/actions/FastForwardAction.java +++ b/src/main/java/com/codemp/intellij/actions/FastForwardAction.java @@ -13,7 +13,7 @@ public class FastForwardAction extends AnAction { @Override public void actionPerformed(@NotNull AnActionEvent e) { try { - ConnectAction.connect("http://alemi.dev:50052", true); + ConnectAction.connect(e, "http://alemi.dev:50052", true); WorkspaceJoinAction.join(e, "default", true); BufferAttachAction.attach(e, "test", true); } catch(Exception ex) { diff --git a/src/main/java/com/codemp/intellij/actions/buffer/BufferAttachAction.java b/src/main/java/com/codemp/intellij/actions/buffer/BufferAttachAction.java index c7a714c..4295c3f 100644 --- a/src/main/java/com/codemp/intellij/actions/buffer/BufferAttachAction.java +++ b/src/main/java/com/codemp/intellij/actions/buffer/BufferAttachAction.java @@ -14,8 +14,9 @@ import org.jetbrains.annotations.NotNull; public class BufferAttachAction extends AnAction { public static void attach(AnActionEvent e, String buffer, boolean silent) throws Exception { BufferHandler bufferHandler = CodeMPHandler.attach(buffer); - if(!silent) Messages.showInfoMessage(String.format("Attached to buffer to %s!", buffer), - "CodeMP Buffer Attach"); + if(!silent) ActionUtil.notify(e, "Success", + String.format("Successfully attached to buffer to buffer to %s!", buffer) + ); CodeMP.LOGGER.debug("Attached to buffer to {}!", buffer); //TODO "get" the Editor corresponding to buffer, for now use the current one @@ -35,9 +36,9 @@ public class BufferAttachAction extends AnAction { try { attach(e, buffer, false); } catch(Exception ex) { - Messages.showErrorDialog(String.format( - "Failed to attach to buffer %s: %s!", - buffer, ex.getMessage()), "Attach to CodeMP Buffer"); + ActionUtil.notifyError(e, String.format( + "Failed to attach to buffer %s!", + buffer), ex); } } } diff --git a/src/main/java/com/codemp/intellij/actions/buffer/BufferCreateAction.java b/src/main/java/com/codemp/intellij/actions/buffer/BufferCreateAction.java index 2b1b706..c425bd2 100644 --- a/src/main/java/com/codemp/intellij/actions/buffer/BufferCreateAction.java +++ b/src/main/java/com/codemp/intellij/actions/buffer/BufferCreateAction.java @@ -2,16 +2,18 @@ package com.codemp.intellij.actions.buffer; import com.codemp.intellij.CodeMP; import com.codemp.intellij.jni.CodeMPHandler; +import com.codemp.intellij.util.ActionUtil; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.ui.Messages; import org.jetbrains.annotations.NotNull; public class BufferCreateAction extends AnAction { - public static void create(String buffer, boolean silent) throws Exception { + public static void create(AnActionEvent e, String buffer, boolean silent) throws Exception { CodeMPHandler.create(buffer); - if(!silent) Messages.showInfoMessage(String.format("Created buffer %s!", buffer), - "Create CodeMP Buffer" ); + if(!silent) ActionUtil.notify(e, "Success", + String.format("Created buffer %s!", buffer) + ); CodeMP.LOGGER.debug("Created buffer {}!", buffer); } @@ -23,11 +25,11 @@ public class BufferCreateAction extends AnAction { Messages.getQuestionIcon()); try { - create(buffer, false); + create(e, buffer, false); } catch(Exception ex) { - Messages.showErrorDialog(String.format( - "Failed to create buffer with name %s: %s!", - buffer, ex.getMessage()), "Create CodeMP Buffer"); + ActionUtil.notifyError(e, String.format( + "Failed to create buffer with name %s!", + buffer), ex); } } } diff --git a/src/main/java/com/codemp/intellij/actions/buffer/BufferCreateWithContentAction.java b/src/main/java/com/codemp/intellij/actions/buffer/BufferCreateWithContentAction.java index 32f2e9b..b593957 100644 --- a/src/main/java/com/codemp/intellij/actions/buffer/BufferCreateWithContentAction.java +++ b/src/main/java/com/codemp/intellij/actions/buffer/BufferCreateWithContentAction.java @@ -13,8 +13,9 @@ public class BufferCreateWithContentAction extends AnAction { public static void createWithContent(AnActionEvent event, String buffer, boolean silent) throws Exception { String content = ActionUtil.getCurrentEditor(event).getDocument().getText(); CodeMPHandler.createWithContent(buffer, content); - if(!silent) Messages.showInfoMessage(String.format("Created buffer %s with content %s!", buffer, content), - "Create CodeMP Buffer" ); + + if(!silent) ActionUtil.notify(event, "Success", String.format( + "Created buffer %s with content %s!", buffer, content)); CodeMP.LOGGER.debug("Created buffer {} with content {}!", buffer, content); } @Override @@ -27,9 +28,9 @@ public class BufferCreateWithContentAction extends AnAction { try { createWithContent(e, buffer, false); } catch(Exception ex) { - Messages.showErrorDialog(String.format( - "Failed to create buffer with name %s: %s!", - buffer, ex.getMessage()), "Create CodeMP Buffer with Content"); + ActionUtil.notifyError(e, String.format( + "Failed to create buffer with name %s!", + buffer), ex); } } } diff --git a/src/main/java/com/codemp/intellij/actions/buffer/BufferDetachAction.java b/src/main/java/com/codemp/intellij/actions/buffer/BufferDetachAction.java index 0933c2a..428d902 100644 --- a/src/main/java/com/codemp/intellij/actions/buffer/BufferDetachAction.java +++ b/src/main/java/com/codemp/intellij/actions/buffer/BufferDetachAction.java @@ -5,13 +5,14 @@ import com.codemp.intellij.exceptions.ide.BufferDetachException; import com.codemp.intellij.jni.CodeMPHandler; import com.codemp.intellij.task.BufferEventAwaiterTask; import com.codemp.intellij.task.TaskManager; +import com.codemp.intellij.util.ActionUtil; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.ui.Messages; import org.jetbrains.annotations.NotNull; public class BufferDetachAction extends AnAction { - public static void detach(String buffer, boolean silent) throws Exception { + public static void detach(AnActionEvent e, String buffer, boolean silent) throws Exception { boolean res = CodeMPHandler.detach(buffer); if(!res) throw new BufferDetachException(buffer); @@ -19,13 +20,13 @@ public class BufferDetachAction extends AnAction { BufferEventAwaiterTask task = TaskManager.getBufferTask(); if(task != null) { task.unregisterListener(buffer); - if(!silent) Messages.showInfoMessage(String.format("Detached from buffer %s!", buffer), - "Detach from CodeMP Buffer"); + if(!silent) ActionUtil.notify(e, "Success", + String.format("Detached from buffer %s!", buffer) + ); CodeMP.LOGGER.debug("Detached from buffer {}!", buffer); } else { - if(!silent) Messages.showErrorDialog( - String.format("Failed to detach from %s: buffer event task was dead!", buffer), - "Detach from CodeMP Buffer"); + if(!silent) ActionUtil.notifyError(e, String.format("Failed to detach from %s", buffer), + "Buffer event task was dead!"); CodeMP.LOGGER.debug("Failed to detach from {}: buffer event task was dead!", buffer); } } @@ -38,11 +39,11 @@ public class BufferDetachAction extends AnAction { Messages.getQuestionIcon()); try { - detach(buffer, false); + detach(e, buffer, false); } catch(Exception ex) { - Messages.showErrorDialog(String.format( - "Failed to detach from buffer with name %s: %s!", - buffer, ex.getMessage()), "Detach from CodeMP Buffer"); + ActionUtil.notifyError(e, String.format( + "Failed to detach from buffer with name %s!", + buffer), ex); } } } diff --git a/src/main/java/com/codemp/intellij/actions/workspace/WorkspaceJoinAction.java b/src/main/java/com/codemp/intellij/actions/workspace/WorkspaceJoinAction.java index 6066e22..8139c2a 100644 --- a/src/main/java/com/codemp/intellij/actions/workspace/WorkspaceJoinAction.java +++ b/src/main/java/com/codemp/intellij/actions/workspace/WorkspaceJoinAction.java @@ -10,8 +10,6 @@ import com.codemp.intellij.util.ActionUtil; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.editor.EditorFactory; -import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import org.jetbrains.annotations.NotNull; @@ -19,8 +17,9 @@ public class WorkspaceJoinAction extends AnAction { public static void join(AnActionEvent e, String workspace, boolean silent) throws Exception { CursorHandler cursorHandler = CodeMPHandler.join(workspace); - if(!silent) Messages.showInfoMessage(String.format("Joined workspace %s!", workspace), "CodeMP"); - else CodeMP.LOGGER.debug("Joined workspace {}!", workspace); + if(!silent) ActionUtil.notify(e, + "Success", String.format("Joined workspace %s!", workspace)); + CodeMP.LOGGER.debug("Joined workspace {}!", workspace); CursorEventAwaiterTask task = TaskManager .getOrCreateCursorTask(ActionUtil.getCurrentProject(e), cursorHandler); @@ -40,11 +39,9 @@ public class WorkspaceJoinAction extends AnAction { try { join(e, session, false); } catch(Exception ex) { - Messages.showErrorDialog(String.format( - "Failed to join session %s: %s!", - session, - ex.getMessage()), - "CodeMP Join"); + ActionUtil.notifyError(e, String.format( + "Failed to join session %s!", + session), ex); } } } diff --git a/src/main/java/com/codemp/intellij/actions/workspace/WorkspaceLeaveAction.java b/src/main/java/com/codemp/intellij/actions/workspace/WorkspaceLeaveAction.java index 43d5d18..971b8e0 100644 --- a/src/main/java/com/codemp/intellij/actions/workspace/WorkspaceLeaveAction.java +++ b/src/main/java/com/codemp/intellij/actions/workspace/WorkspaceLeaveAction.java @@ -2,28 +2,26 @@ package com.codemp.intellij.actions.workspace; import com.codemp.intellij.CodeMP; import com.codemp.intellij.jni.CodeMPHandler; +import com.codemp.intellij.util.ActionUtil; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.ui.Messages; import org.jetbrains.annotations.NotNull; public class WorkspaceLeaveAction extends AnAction { - public static void leave(boolean silent) throws Exception { + public static void leave(AnActionEvent e, boolean silent) throws Exception { CodeMPHandler.leaveWorkspace(); - if(!silent) Messages.showInfoMessage("Left workspace!", - "Detach from CodeMP Buffer" ); + if(!silent) ActionUtil.notify(e, "Success", "Left workspace"); CodeMP.LOGGER.debug("Left workspace!"); } @Override public void actionPerformed(@NotNull AnActionEvent e) { try { - leave(false); + leave(e, false); } catch(Exception ex) { - Messages.showErrorDialog(String.format( - "Failed to leave workspace: %s!", - ex.getMessage()), "Leave CodeMP Workspace"); + ActionUtil.notifyError(e, "Failed to leave workspace!", ex); } } } diff --git a/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java b/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java index 02b5515..707ff87 100644 --- a/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java +++ b/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java @@ -2,7 +2,6 @@ package com.codemp.intellij.listeners; import com.codemp.intellij.CodeMP; import com.codemp.intellij.jni.BufferHandler; -import com.codemp.intellij.jni.CodeMPHandler; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.editor.event.DocumentEvent; import com.intellij.openapi.editor.event.DocumentListener; diff --git a/src/main/java/com/codemp/intellij/listeners/CursorEventListener.java b/src/main/java/com/codemp/intellij/listeners/CursorEventListener.java index 269e699..b482904 100644 --- a/src/main/java/com/codemp/intellij/listeners/CursorEventListener.java +++ b/src/main/java/com/codemp/intellij/listeners/CursorEventListener.java @@ -1,7 +1,6 @@ package com.codemp.intellij.listeners; import com.codemp.intellij.CodeMP; -import com.codemp.intellij.jni.CodeMPHandler; import com.codemp.intellij.jni.CursorHandler; import com.intellij.openapi.editor.Caret; import com.intellij.openapi.editor.VisualPosition; diff --git a/src/main/java/com/codemp/intellij/task/CursorEventAwaiterTask.java b/src/main/java/com/codemp/intellij/task/CursorEventAwaiterTask.java index f3474a5..0541352 100644 --- a/src/main/java/com/codemp/intellij/task/CursorEventAwaiterTask.java +++ b/src/main/java/com/codemp/intellij/task/CursorEventAwaiterTask.java @@ -18,7 +18,6 @@ import com.intellij.openapi.util.Disposer; import org.jetbrains.annotations.NotNull; import java.awt.*; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; diff --git a/src/main/java/com/codemp/intellij/util/ActionUtil.java b/src/main/java/com/codemp/intellij/util/ActionUtil.java index 1596d79..914c463 100644 --- a/src/main/java/com/codemp/intellij/util/ActionUtil.java +++ b/src/main/java/com/codemp/intellij/util/ActionUtil.java @@ -1,6 +1,9 @@ package com.codemp.intellij.util; import com.codemp.intellij.exceptions.ide.BadActionEventStateException; +import com.intellij.notification.Notification; +import com.intellij.notification.NotificationType; +import com.intellij.notification.Notifications; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileEditor.FileEditorManager; @@ -21,4 +24,22 @@ public class ActionUtil { throw new BadActionEventStateException("Editor was null!"); return editor; } + + public static void notify(AnActionEvent event, String title, String msg) { + Notifications.Bus.notify(new Notification( + "CodeMP", title, msg, NotificationType.INFORMATION + ), event.getProject()); + } + + public static void notifyError(AnActionEvent event, String title, String msg) { + Notifications.Bus.notify(new Notification( + "CodeMP", title, msg, NotificationType.ERROR + ), event.getProject()); + } + + public static void notifyError(AnActionEvent event, String title, Throwable t) { + Notifications.Bus.notify(new Notification( + "CodeMP", title, t.getMessage(), NotificationType.ERROR + ), event.getProject()); + } } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 082ce41..62ff542 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -49,7 +49,6 @@ - +