From 94575e15c189d744606750a44af2fb74b386a7ae Mon Sep 17 00:00:00 2001 From: Xavier Stouder Date: Fri, 3 May 2024 20:14:20 +0200 Subject: [PATCH] feat: refresh capability on commands --- .../adonis/action/RefreshCommandsAction.java | 33 +++++++++++++ .../content/MakeToolWindowContent.java | 48 +++++++++++++------ src/main/resources/META-INF/plugin.xml | 17 +++++++ .../messages/AdonisBundle.properties | 1 + 4 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 src/main/java/io/stouder/adonis/action/RefreshCommandsAction.java diff --git a/src/main/java/io/stouder/adonis/action/RefreshCommandsAction.java b/src/main/java/io/stouder/adonis/action/RefreshCommandsAction.java new file mode 100644 index 0000000..82ac1a0 --- /dev/null +++ b/src/main/java/io/stouder/adonis/action/RefreshCommandsAction.java @@ -0,0 +1,33 @@ +package io.stouder.adonis.action; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.project.Project; +import io.stouder.adonis.AdonisBundle; +import io.stouder.adonis.cli.json.ace.Command; +import io.stouder.adonis.notifier.AdonisRcUpdateNotifier; +import io.stouder.adonis.service.AdonisAceService; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class RefreshCommandsAction extends AnAction { + + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + Project project = e.getProject(); + assert project != null; + + e.getPresentation().setEnabled(false); + AdonisRcUpdateNotifier publisher = project.getMessageBus().syncPublisher(AdonisRcUpdateNotifier.ADONIS_RC_UPDATE_TOPIC); + Map> commands = AdonisAceService.getInstance(project).runAceGetCommandOnEveryRoots( + AdonisBundle.message("adonis.actions.refresh.commands"), + List.of("list", "--json"), + Command[].class + ); + publisher.commands(commands); + e.getPresentation().setEnabled(true); + } +} diff --git a/src/main/java/io/stouder/adonis/tool_window/content/MakeToolWindowContent.java b/src/main/java/io/stouder/adonis/tool_window/content/MakeToolWindowContent.java index 5b46191..a684549 100644 --- a/src/main/java/io/stouder/adonis/tool_window/content/MakeToolWindowContent.java +++ b/src/main/java/io/stouder/adonis/tool_window/content/MakeToolWindowContent.java @@ -1,5 +1,8 @@ package io.stouder.adonis.tool_window.content; +import com.intellij.openapi.actionSystem.ActionGroup; +import com.intellij.openapi.actionSystem.ActionManager; +import com.intellij.openapi.actionSystem.ActionToolbar; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.ComboBox; @@ -19,7 +22,7 @@ public class MakeToolWindowContent implements AdonisRcUpdateNotifier { private final JPanel rootPanel = new JPanel(); private final ComboBox comboBox = new ComboBox<>(); - private final Map tabbedPane = new HashMap<>(); + private final Map tabbedPanes = new HashMap<>(); private Map> makeCommands = new HashMap<>(); private String selectedModule; private final Project project; @@ -30,7 +33,7 @@ public MakeToolWindowContent(Project project) { this.rootPanel.setLayout(new BorderLayout()); this.comboBox.addActionListener(e -> { - this.rootPanel.remove(this.tabbedPane.get(this.selectedModule)); + this.rootPanel.remove(this.tabbedPanes.get(this.selectedModule)); this.selectedModule = (String) comboBox.getSelectedItem(); this.updateUi(); }); @@ -42,12 +45,37 @@ public JComponent getRootPanel() { return this.rootPanel; } + private void buildToolbar() { + ActionGroup actionGroup = (ActionGroup) ActionManager.getInstance().getAction("io.stouder.adonis.MakeToolbar"); + ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar( + "AdonisCommandsToolbar", + actionGroup, + true + ); + + this.comboBox.setModel( + new DefaultComboBoxModel<>(this.makeCommands.keySet().toArray(new String[0])) + ); + if(this.selectedModule != null) { + comboBox.setSelectedItem(this.selectedModule); + } + + + JPanel toolbarAndComboPanel = new JPanel(new BorderLayout()); + toolbarAndComboPanel.add(toolbar.getComponent(), BorderLayout.WEST); + toolbarAndComboPanel.add(this.comboBox, BorderLayout.CENTER); + toolbar.setTargetComponent(this.rootPanel); + + this.rootPanel.add(toolbarAndComboPanel, BorderLayout.NORTH); + } + + private void buildTabs() { for (String module : this.makeCommands.keySet()) { - JBTabbedPane tabbedPane = this.tabbedPane.get(module); + JBTabbedPane tabbedPane = this.tabbedPanes.get(module); if(tabbedPane == null) { tabbedPane = new JBTabbedPane(); - this.tabbedPane.put(module, tabbedPane); + this.tabbedPanes.put(module, tabbedPane); } while(tabbedPane.getTabCount() > 0) { @@ -65,17 +93,9 @@ private void buildTabs() { private void updateUi() { this.buildTabs(); + this.buildToolbar(); - this.comboBox.setModel( - new DefaultComboBoxModel<>(this.makeCommands.keySet().toArray(new String[0])) - ); - if(this.selectedModule != null) { - comboBox.setSelectedItem(this.selectedModule); - } - this.rootPanel.remove(comboBox); - this.rootPanel.add(comboBox, BorderLayout.NORTH); - - JBTabbedPane tabbedPane = this.tabbedPane.get(this.selectedModule); + JBTabbedPane tabbedPane = this.tabbedPanes.get(this.selectedModule); if(tabbedPane != null) { this.rootPanel.add(tabbedPane, BorderLayout.CENTER); } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index dfc3815..7afab49 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -82,6 +82,23 @@ > + + + + + diff --git a/src/main/resources/messages/AdonisBundle.properties b/src/main/resources/messages/AdonisBundle.properties index 8f87729..80082bb 100644 --- a/src/main/resources/messages/AdonisBundle.properties +++ b/src/main/resources/messages/AdonisBundle.properties @@ -28,3 +28,4 @@ adonis.tool_window.make=Make adonis.tool_window.content.tabs.make.create=Create adonis.actions.refresh.routes=Refresh Adonis routes +adonis.actions.refresh.commands=Refresh Adonis commands