diff --git a/build.gradle b/build.gradle index 2e9b4f5..1432dfa 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ dependencies { tasks { patchPluginXml { sinceBuild.set('222') - untilBuild.set('233.*') + untilBuild.set('242.*') } signPlugin { diff --git a/src/main/java/mp/code/intellij/actions/workspace/WorkspaceCreateAction.java b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceCreateAction.java new file mode 100644 index 0000000..e51b0bb --- /dev/null +++ b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceCreateAction.java @@ -0,0 +1,28 @@ +package mp.code.intellij.actions.workspace; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.ui.Messages; +import mp.code.intellij.util.InteractionUtil; +import org.jetbrains.annotations.NotNull; + +public class WorkspaceCreateAction extends AnAction { + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + String workspaceName = Messages.showInputDialog( + "Input name of new workspace", + "CodeMP Create Workspace", + Messages.getQuestionIcon() + ); + + if(workspaceName == null) { + Messages.showErrorDialog( + "Workspace name cannot be null!", + "CodeMP Create Workspace" + ); + return; + } + + InteractionUtil.createWorkspace(e.getProject(), workspaceName, null); + } +} diff --git a/src/main/java/mp/code/intellij/actions/workspace/WorkspaceDeleteAction.java b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceDeleteAction.java new file mode 100644 index 0000000..ed3595e --- /dev/null +++ b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceDeleteAction.java @@ -0,0 +1,31 @@ +package mp.code.intellij.actions.workspace; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.ui.Messages; +import mp.code.intellij.util.InteractionUtil; +import org.jetbrains.annotations.NotNull; + +public class WorkspaceDeleteAction extends AnAction { + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + String[] availableWorkspaces = InteractionUtil.listWorkspaces(e.getProject(), true, false); + if(availableWorkspaces.length == 0) { + Messages.showErrorDialog( + "You do not own any workspaces. Ensure you own at least one!", + "CodeMP Delete Workspace" + ); + } + + int choice = Messages.showDialog( // TODO NOT THE ONE + e.getProject(), + "Please choose a workspace to delete:", + "CodeMP Delete Workspace", + availableWorkspaces, + 0, + Messages.getQuestionIcon() + ); + + InteractionUtil.deleteWorkspace(e.getProject(), availableWorkspaces[choice], null); + } +} diff --git a/src/main/java/mp/code/intellij/actions/workspace/WorkspaceInviteAction.java b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceInviteAction.java new file mode 100644 index 0000000..1ec8726 --- /dev/null +++ b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceInviteAction.java @@ -0,0 +1,49 @@ +package mp.code.intellij.actions.workspace; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.ui.Messages; +import mp.code.intellij.CodeMP; +import mp.code.intellij.util.InteractionUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.List; + +public class WorkspaceInviteAction extends AnAction { + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + String[] availableWorkspaces = InteractionUtil.listWorkspaces(e.getProject(), true, false); + if(availableWorkspaces.length == 0) { + Messages.showErrorDialog( + "You do not own any workspaces. Ensure you own at least one!", + "CodeMP Invite To Workspace" + ); + } + + int choice = Messages.showDialog( // TODO NOT THE ONE + e.getProject(), + "Please choose a workspace to invite to:", + "CodeMP Invite To Workspace", + availableWorkspaces, + 0, + Messages.getQuestionIcon() + ); + + String userName = Messages.showInputDialog( + "Input name of user to invite:", + "CodeMP Invite To Workspace", + Messages.getQuestionIcon() + ); + + if(userName == null) { + Messages.showErrorDialog( + "Username cannot be null!", + "CodeMP Invite To Workspace" + ); + return; + } + + InteractionUtil.inviteToWorkspace(e.getProject(), availableWorkspaces[choice], userName, null); + } +} diff --git a/src/main/java/mp/code/intellij/actions/workspace/WorkspaceJoinAction.java b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceJoinAction.java index 1344ba0..ead67be 100644 --- a/src/main/java/mp/code/intellij/actions/workspace/WorkspaceJoinAction.java +++ b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceJoinAction.java @@ -11,7 +11,7 @@ import org.jetbrains.annotations.NotNull; public class WorkspaceJoinAction extends AnAction { @Override public void actionPerformed(@NotNull AnActionEvent e) { - String[] availableWorkspaces = InteractionUtil.listWorkspaces(e.getProject()); + String[] availableWorkspaces = InteractionUtil.listWorkspaces(e.getProject(), true, true); if(availableWorkspaces.length == 0) { Messages.showErrorDialog( "There are no available workspaces. Ensure you have rights to access at least one!", diff --git a/src/main/java/mp/code/intellij/ui/CodeMPWindowFactory.java b/src/main/java/mp/code/intellij/ui/CodeMPWindowFactory.java index c707084..6570316 100644 --- a/src/main/java/mp/code/intellij/ui/CodeMPWindowFactory.java +++ b/src/main/java/mp/code/intellij/ui/CodeMPWindowFactory.java @@ -81,7 +81,7 @@ public class CodeMPWindowFactory implements ToolWindowFactory, DumbAware { } case CONNECTED -> { this.setLayout(new GridLayout(0, 1)); - JTree tree = drawTree(InteractionUtil.listWorkspaces(project)); + JTree tree = drawTree(InteractionUtil.listWorkspaces(project, true, true)); tree.addMouseListener(new SimpleMouseListener() { @Override public void mouseClicked(MouseEvent e) { diff --git a/src/main/java/mp/code/intellij/util/InteractionUtil.java b/src/main/java/mp/code/intellij/util/InteractionUtil.java index 17e2e7e..e834aeb 100644 --- a/src/main/java/mp/code/intellij/util/InteractionUtil.java +++ b/src/main/java/mp/code/intellij/util/InteractionUtil.java @@ -69,6 +69,70 @@ public class InteractionUtil { notifyInfo(project, "Success", "Disconnected from server!"); } + public static void createWorkspace(Project project, @NotNull String workspaceId, @Nullable Runnable after) { + ProgressManager.getInstance().run(new Task.Backgroundable(project, String.format("Creating workspace %s...", workspaceId)) { + @Override + public void run(@NotNull ProgressIndicator indicator) { + if(project == null) { + Notifications.Bus.notify(new Notification( + "CodeMP", + "No project found", + "Please ensure that you have an open project before attempting to create a workspace.", + NotificationType.ERROR + ), null); + return; + } + + try { + CodeMP.getClient("workspace create").createWorkspace(workspaceId); + if(after != null) after.run(); + notifyInfo( + project, + "Success", + String.format("Created workspace %s!", workspaceId) + ); + } catch(ConnectionException e) { + InteractionUtil.notifyError(project, String.format( + "Failed to create workspace %s!", + workspaceId + ), e); + } + } + }); + } + + public static void inviteToWorkspace(Project project, @NotNull String workspaceId, @NotNull String userName, @Nullable Runnable after) { + ProgressManager.getInstance().run(new Task.Backgroundable(project, String.format("Inviting %s to workspace %s...", userName, workspaceId)) { + @Override + public void run(@NotNull ProgressIndicator indicator) { + if(project == null) { + Notifications.Bus.notify(new Notification( + "CodeMP", + "No project found", + "Please ensure that you have an open project before attempting to join a workspace.", + NotificationType.ERROR + ), null); + return; + } + + try { + CodeMP.getClient("workspace invite").inviteToWorkspace(workspaceId, userName); + if(after != null) after.run(); + notifyInfo( + project, + "Success", + String.format("Joined workspace %s!", workspaceId) + ); + } catch(ConnectionException e) { + InteractionUtil.notifyError(project, String.format( + "Failed to invite to workspace %s!", + workspaceId + ), e); + } + } + }); + } + public static void joinWorkspace(Project project, @NotNull String workspaceId, @Nullable Runnable after) { ProgressManager.getInstance().run(new Task.Backgroundable(project, String.format("Joining workspace %s...", workspaceId)) { @Override @@ -117,6 +181,47 @@ public class InteractionUtil { }); } + public static void deleteWorkspace(Project project, @NotNull String workspaceId, @Nullable Runnable after) { + ProgressManager.getInstance().run(new Task.Backgroundable(project, String.format("Deleting workspace %s...", workspaceId)) { + @Override + public void run(@NotNull ProgressIndicator indicator) { + if(project == null) { + Notifications.Bus.notify(new Notification( + "CodeMP", + "No project found", + "Please ensure that you have an open project before attempting to delete a workspace.", + NotificationType.ERROR + ), null); + return; + } + + try { + Client client = CodeMP.getClient("workspace delete"); + client.deleteWorkspace(workspaceId); + + Optional ws = client.getWorkspace("workspace leave"); + if(ws.isPresent() && ws.get().getWorkspaceId().equals(workspaceId)) { + CodeMP.leaveWorkspace(); + MemoryManager.startWorkspaceLifetime(workspaceId); + } + + if(after != null) after.run(); + + notifyInfo( + project, + "Success", + String.format("Joined workspace %s!", workspaceId) + ); + } catch(ConnectionException e) { + InteractionUtil.notifyError(project, String.format( + "Failed to join workspace %s!", + workspaceId + ), e); + } + } + }); + } + public static void leaveWorkspace(Project project, String workspaceId) { CodeMP.leaveWorkspace(); MemoryManager.endWorkspaceLifetime(workspaceId); @@ -127,10 +232,10 @@ public class InteractionUtil { ); } - public static String[] listWorkspaces(Project project) { + public static String[] listWorkspaces(Project project, boolean owned, boolean invited) { try { Client client = CodeMP.getClient("drawActiveWorkspaces"); - return client.listWorkspaces(true, true); + return client.listWorkspaces(owned, invited); } catch(ConnectionRemoteException exception) { notifyError(project, "Failed to list workspaces!", exception); return new String[0]; diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 4c48926..ef9b658 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -12,8 +12,14 @@ + + +