Skip to content

Commit

Permalink
feat: refresh capability on commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Xstoudi committed May 3, 2024
1 parent 992bb4f commit 94575e1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 14 deletions.
33 changes: 33 additions & 0 deletions src/main/java/io/stouder/adonis/action/RefreshCommandsAction.java
Original file line number Diff line number Diff line change
@@ -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<String, Optional<Command[]>> commands = AdonisAceService.getInstance(project).runAceGetCommandOnEveryRoots(
AdonisBundle.message("adonis.actions.refresh.commands"),
List.of("list", "--json"),
Command[].class
);
publisher.commands(commands);
e.getPresentation().setEnabled(true);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +22,7 @@
public class MakeToolWindowContent implements AdonisRcUpdateNotifier {
private final JPanel rootPanel = new JPanel();
private final ComboBox<String> comboBox = new ComboBox<>();
private final Map<String, JBTabbedPane> tabbedPane = new HashMap<>();
private final Map<String, JBTabbedPane> tabbedPanes = new HashMap<>();
private Map<String, List<Command>> makeCommands = new HashMap<>();
private String selectedModule;
private final Project project;
Expand All @@ -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();
});
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@
>
<add-to-group group-id="io.stouder.adonis.RoutesToolbar" />
</action>

<group
id="io.stouder.adonis.MakeToolbar"
text="Adonis"
description="Adonis"
popup="false"
icon="io.stouder.adonis.AdonisIcons.ADONIS"
/>
<action
id="io.stouder.adonis.action.RefreshCommandsAction"
class="io.stouder.adonis.action.RefreshCommandsAction"
text="Refresh Commands"
description="Refresh Adonis commands"
icon="AllIcons.Actions.Refresh"
>
<add-to-group group-id="io.stouder.adonis.MakeToolbar" />
</action>
</actions>

<projectListeners>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages/AdonisBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 94575e1

Please sign in to comment.