From 7b9f00fd016f7ec34efea2103f759676c2ea4142 Mon Sep 17 00:00:00 2001 From: Kathryn Kodama Date: Wed, 29 Jul 2020 15:59:16 -0400 Subject: [PATCH] Terminal issue fix, collapse/expand fix, execute task fix (#16) * Terminal issue fix, collapse/expand fix, execute task fix Signed-off-by: Kathryn Kodama * fixed action maps Signed-off-by: Kathryn Kodama --- .../tools/intellij/LibertyExplorer.java | 39 ++++++++------- .../actions/ExecuteLibertyDevTask.java | 40 ++++++++------- .../actions/LibertyToolbarActionGroup.java | 50 +++++++++++++++++++ .../actions/RefreshLibertyToolbar.java | 35 ++++++++++--- .../tools/intellij/util/Constants.java | 31 ++++++++++-- .../intellij/util/LibertyProjectUtil.java | 9 ++-- .../tools/intellij/util/TreeDataProvider.java | 9 +++- src/main/resources/META-INF/plugin.xml | 2 +- 8 files changed, 160 insertions(+), 55 deletions(-) create mode 100644 src/main/java/io/openliberty/tools/intellij/actions/LibertyToolbarActionGroup.java diff --git a/src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java b/src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java index 0e214bb7..8d41862e 100644 --- a/src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java +++ b/src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java @@ -2,6 +2,7 @@ import com.intellij.ide.DataManager; import com.intellij.openapi.actionSystem.*; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.fileEditor.OpenFileDescriptor; import com.intellij.openapi.project.Project; @@ -12,8 +13,7 @@ import com.intellij.ui.DoubleClickListener; import com.intellij.ui.PopupHandler; import com.intellij.ui.treeStructure.Tree; -import com.intellij.ui.treeStructure.actions.CollapseAllAction; -import com.intellij.ui.treeStructure.actions.ExpandAllAction; +import io.openliberty.tools.intellij.actions.LibertyToolbarActionGroup; import io.openliberty.tools.intellij.util.*; import org.jetbrains.annotations.NotNull; @@ -28,8 +28,6 @@ import java.util.ArrayList; import java.util.HashMap; -import com.intellij.openapi.diagnostic.Logger; - public class LibertyExplorer extends SimpleToolWindowPanel { private static Logger log; @@ -37,26 +35,27 @@ public LibertyExplorer(@NotNull Project project) { super(true, true); log = Logger.getInstance(LibertyExplorer.class); - // create ActionToolBar - final ActionManager actionManager = ActionManager.getInstance(); - DefaultActionGroup actionGroup = new DefaultActionGroup("DefaultActionGroup", false); - actionGroup.add(ActionManager.getInstance().getAction("io.openliberty.tools.intellij.actions.RefreshLibertyToolbar")); - actionGroup.addSeparator(); - actionGroup.add(ActionManager.getInstance().getAction("io.openliberty.tools.intellij.actions.ExecuteLibertyDevTask")); - // build tree Tree tree = buildTree(project, getBackground()); + if (tree != null) { - actionGroup.addSeparator(); - actionGroup.add(new CollapseAllAction(tree)); - actionGroup.add(new ExpandAllAction(tree)); - setContent(tree); + this.setContent(tree); } - ActionToolbar actionToolbar = actionManager.createActionToolbar(ActionPlaces.UNKNOWN, actionGroup, true); + ActionToolbar actionToolbar = buildActionToolbar(tree); + this.setToolbar(actionToolbar.getComponent()); + } + + public static ActionToolbar buildActionToolbar(Tree tree) { + // create ActionToolBar + final ActionManager actionManager = ActionManager.getInstance(); + LibertyToolbarActionGroup libertyActionGroup = new LibertyToolbarActionGroup(tree); + + ActionToolbar actionToolbar = actionManager.createActionToolbar(ActionPlaces.UNKNOWN, libertyActionGroup, true); actionToolbar.setOrientation(SwingConstants.HORIZONTAL); actionToolbar.setShowSeparatorTitles(true); - this.setToolbar(actionToolbar.getComponent()); + actionToolbar.getComponent().setName(Constants.LIBERTY_ACTION_TOOLBAR); + return actionToolbar; } /** @@ -66,6 +65,8 @@ public LibertyExplorer(@NotNull Project project) { * @return Tree object of all valid Liberty Gradle and Liberty Maven projects */ public static Tree buildTree(Project project, Color backgroundColor) { + DefaultMutableTreeNode top = new DefaultMutableTreeNode("Root node"); + ArrayList mavenBuildFiles = null; ArrayList gradleBuildFiles = null; ArrayList projectNames = new ArrayList(); @@ -79,8 +80,6 @@ public static Tree buildTree(Project project, Color backgroundColor) { return null; } - DefaultMutableTreeNode top = new DefaultMutableTreeNode("Root node"); - for (PsiFile file : mavenBuildFiles) { String projectName = null; VirtualFile virtualFile = file.getVirtualFile(); @@ -264,6 +263,8 @@ ActionPlaces.UNKNOWN, new Presentation(), newRenderer.setBackgroundNonSelectionColor(backgroundColor); tree.setCellRenderer(newRenderer); + + treeDataProvider.setTreeOnRefresh(tree); return tree; } diff --git a/src/main/java/io/openliberty/tools/intellij/actions/ExecuteLibertyDevTask.java b/src/main/java/io/openliberty/tools/intellij/actions/ExecuteLibertyDevTask.java index bacb91dc..3d1fd10d 100644 --- a/src/main/java/io/openliberty/tools/intellij/actions/ExecuteLibertyDevTask.java +++ b/src/main/java/io/openliberty/tools/intellij/actions/ExecuteLibertyDevTask.java @@ -43,7 +43,6 @@ public void actionPerformed(@NotNull AnActionEvent e) { JComponent libertyWindow = content.getComponent(); - libertyWindow.list(); Component[] components = libertyWindow.getComponents(); for (Component comp : components) { @@ -54,7 +53,7 @@ public void actionPerformed(@NotNull AnActionEvent e) { if (selectionPaths != null && selectionPaths.length == 1) { // if one node is selected confirm that you would like to execute that task String lastPathComponent = selectionPaths[0].getLastPathComponent().toString(); - if (Constants.ACTIONS_MAP.containsKey(lastPathComponent)) { + if (Constants.getFullActionMap().containsKey(lastPathComponent)) { // verify user would like to execute this action final String projectName = (String) e.getDataContext().getData(Constants.LIBERTY_PROJECT_NAME); boolean confirm = ConfirmationDialog.requestForConfirmation( @@ -64,7 +63,7 @@ public void actionPerformed(@NotNull AnActionEvent e) { , Constants.libertyIcon_40); if (confirm) { // calls action - AnAction action = ActionManager.getInstance().getAction(Constants.ACTIONS_MAP.get(lastPathComponent)); + AnAction action = ActionManager.getInstance().getAction(Constants.getFullActionMap().get(lastPathComponent)); action.actionPerformed(new AnActionEvent(null, DataManager.getInstance().getDataContext(libertyTree), ActionPlaces.UNKNOWN, new Presentation(), @@ -85,24 +84,12 @@ ActionPlaces.UNKNOWN, new Presentation(), private void chooseActionToExecute(Tree libertyTree) { // if 0 or multiple nodes are selected display all possible nodes and allow users to select talk - String[] keyArray = Constants.ACTIONS_MAP.keySet().toArray(new String[Constants.ACTIONS_MAP.keySet().size()]); - - int taskSelected = Messages.showChooseDialog("Choose a Liberty Dev task to execute" - , "Execute Liberty Dev Task" - , keyArray, keyArray[0] - , Constants.libertyIcon_40); - if (taskSelected == -1) { - return; // -1 indicates users cancelled dialog - } - - String task = keyArray[taskSelected]; - HashMap> map = (HashMap>) DataManager.getInstance() .getDataContext(libertyTree).getData(Constants.LIBERTY_PROJECT_MAP); String[] projectNamesArr = map.keySet().toArray(new String[map.keySet().size()]); int projectSelected = Messages.showChooseDialog( - "Choose a project to execute Liberty Dev " + task + "Choose a project to execute a Liberty Dev task on" , "Choose a project" , projectNamesArr, projectNamesArr[0] , Constants.libertyIcon_40); @@ -116,10 +103,29 @@ private void chooseActionToExecute(Tree libertyTree) { ArrayList settings = map.get(project); VirtualFile file = (VirtualFile) settings.get(0); String projectType = (String) settings.get(1); + + HashMap actionsMap = new HashMap(); + if (projectType.equals(Constants.LIBERTY_GRADLE_PROJECT)) { + actionsMap = Constants.getGradleMap(); + } else if (projectType.equals(Constants.LIBERTY_MAVEN_PROJECT)) { + actionsMap = Constants.getMavenMap(); + } + String[] keyArray = actionsMap.keySet().toArray(new String[actionsMap.keySet().size()]); + + int taskSelected = Messages.showChooseDialog("Choose a Liberty Dev task to execute on " + project + , "Execute Liberty Dev Task" + , keyArray, keyArray[0] + , Constants.libertyIcon_40); + if (taskSelected == -1) { + return; // -1 indicates users cancelled dialog + } + + String task = keyArray[taskSelected]; + treeDataProvider.saveData(file, project, projectType); // execute selected task on selected project - AnAction action = ActionManager.getInstance().getAction(Constants.ACTIONS_MAP.get(task)); + AnAction action = ActionManager.getInstance().getAction(actionsMap.get(task)); action.actionPerformed(new AnActionEvent(null, DataManager.getInstance().getDataContext(libertyTree), ActionPlaces.UNKNOWN, new Presentation(), diff --git a/src/main/java/io/openliberty/tools/intellij/actions/LibertyToolbarActionGroup.java b/src/main/java/io/openliberty/tools/intellij/actions/LibertyToolbarActionGroup.java new file mode 100644 index 00000000..ae83adb8 --- /dev/null +++ b/src/main/java/io/openliberty/tools/intellij/actions/LibertyToolbarActionGroup.java @@ -0,0 +1,50 @@ +package io.openliberty.tools.intellij.actions; + +import com.intellij.openapi.actionSystem.ActionManager; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.DefaultActionGroup; +import com.intellij.ui.treeStructure.Tree; +import com.intellij.ui.treeStructure.actions.CollapseAllAction; +import com.intellij.ui.treeStructure.actions.ExpandAllAction; +import io.openliberty.tools.intellij.util.Constants; + +public class LibertyToolbarActionGroup extends DefaultActionGroup { + + private CollapseAllAction collapseAction; + private ExpandAllAction expandAction; + private Tree tree; + + public LibertyToolbarActionGroup(Tree tree) { + super("LibertyActionToolBar", false); + this.tree = tree; + final ActionManager actionManager = ActionManager.getInstance(); + add(actionManager.getAction("io.openliberty.tools.intellij.actions.RefreshLibertyToolbar")); + addSeparator(); + add(actionManager.getAction("io.openliberty.tools.intellij.actions.ExecuteLibertyDevTask")); + addSeparator(); + + this.collapseAction = new CollapseAllAction(tree); + this.expandAction = new ExpandAllAction(tree); + add(this.collapseAction); + add(this.expandAction); + } + + @Override + public void update(AnActionEvent event) { + // Enable/disable depending on whether user is editing... + Tree updatedTree = (Tree) event.getDataContext().getData(Constants.LIBERTY_DASHBOARD_TREE); + + if (updatedTree != null && updatedTree != this.tree) { + remove(this.collapseAction); + remove(this.expandAction); + CollapseAllAction newCollapseAction = new CollapseAllAction(updatedTree); + ExpandAllAction newExpandAction = new ExpandAllAction(updatedTree); + add(newCollapseAction); + add(newExpandAction); + this.collapseAction = newCollapseAction; + this.expandAction = newExpandAction; + this.tree = updatedTree; + } + } + +} diff --git a/src/main/java/io/openliberty/tools/intellij/actions/RefreshLibertyToolbar.java b/src/main/java/io/openliberty/tools/intellij/actions/RefreshLibertyToolbar.java index 8c9c951b..380c79c1 100644 --- a/src/main/java/io/openliberty/tools/intellij/actions/RefreshLibertyToolbar.java +++ b/src/main/java/io/openliberty/tools/intellij/actions/RefreshLibertyToolbar.java @@ -1,5 +1,8 @@ package io.openliberty.tools.intellij.actions; +import com.intellij.ide.DataManager; +import com.intellij.ide.projectView.ProjectView; +import com.intellij.openapi.actionSystem.ActionToolbar; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.diagnostic.Logger; @@ -8,10 +11,11 @@ import com.intellij.openapi.wm.ToolWindowManager; import com.intellij.ui.content.Content; import com.intellij.ui.treeStructure.Tree; -import org.jetbrains.annotations.NotNull; import io.openliberty.tools.intellij.LibertyExplorer; import io.openliberty.tools.intellij.util.Constants; import io.openliberty.tools.intellij.util.LibertyProjectUtil; +import io.openliberty.tools.intellij.util.TreeDataProvider; +import org.jetbrains.annotations.NotNull; import javax.swing.*; import java.awt.*; @@ -25,13 +29,15 @@ public void update(@NotNull AnActionEvent e) { @Override public void actionPerformed(@NotNull AnActionEvent e) { - Logger log = Logger.getInstance(RefreshLibertyToolbar.class);; + Logger log = Logger.getInstance(RefreshLibertyToolbar.class); + final Project project = LibertyProjectUtil.getProject(e.getDataContext()); if (project == null) { log.debug("Unable to refresh Liberty toolbar, could not resolve project"); return; } + ProjectView.getInstance(project).refresh(); ToolWindow libertyDevToolWindow = ToolWindowManager.getInstance(project).getToolWindow(Constants.LIBERTY_DEV_DASHBOARD_ID); @@ -39,16 +45,33 @@ public void actionPerformed(@NotNull AnActionEvent e) { JComponent libertyWindow = content.getComponent(); - libertyWindow.list(); Component[] components = libertyWindow.getComponents(); + Component existingTree = null; + Component existingActionToolbar = null; for (Component comp: components) { if (comp.getName() != null && comp.getName().equals(Constants.LIBERTY_TREE)) { - Tree tree = LibertyExplorer.buildTree(project, content.getComponent().getBackground()); - content.getComponent().remove(comp); - content.getComponent().add(tree); + existingTree = comp; + } + if (comp.getName() != null && comp.getName().equals(Constants.LIBERTY_ACTION_TOOLBAR)) { + existingActionToolbar = comp; } } + if (existingTree != null && existingActionToolbar != null) { + Tree tree = LibertyExplorer.buildTree(project, content.getComponent().getBackground()); + + ActionToolbar actionToolbar = (ActionToolbar) existingActionToolbar; + + libertyWindow.remove(existingTree); + libertyWindow.add(tree, BorderLayout.CENTER); + libertyWindow.revalidate(); + libertyWindow.repaint(); + + TreeDataProvider treeDataProvider = (TreeDataProvider) DataManager.getDataProvider(tree); + treeDataProvider.setTreeOnRefresh(tree); + actionToolbar.updateActionsImmediately(); + actionToolbar.setShowSeparatorTitles(true); + } } } diff --git a/src/main/java/io/openliberty/tools/intellij/util/Constants.java b/src/main/java/io/openliberty/tools/intellij/util/Constants.java index 07eacffe..881dd6e3 100644 --- a/src/main/java/io/openliberty/tools/intellij/util/Constants.java +++ b/src/main/java/io/openliberty/tools/intellij/util/Constants.java @@ -4,7 +4,6 @@ import javax.swing.*; import java.util.HashMap; -import java.util.Map; public final class Constants { public static final String LIBERTY_DEV_DASHBOARD_ID = "Liberty Dev Dashboard"; @@ -36,6 +35,8 @@ public final class Constants { public static final String LIBERTY_PROJECT_NAME = "LIBERTY_PROJECT_NAME"; public static final String LIBERTY_PROJECT_TYPE = "LIBERTY_PROJECT_TYPE"; public static final String LIBERTY_PROJECT_MAP = "LIBERTY_PROJECT_MAP"; + public static final String LIBERTY_DASHBOARD_TREE = "LIBERTY_DASHBOARD_TREE"; + public static final String LIBERTY_ACTION_TOOLBAR = "LIBERTY_ACTION_TOOLBAR"; /** * Constants for Action IDs @@ -50,16 +51,36 @@ public final class Constants { public static final String VIEW_GRADLE_CONFIG_ACTION_ID = "io.openliberty.tools.intellij.actions.ViewGradleConfig"; public static final String VIEW_EFFECTIVE_POM_ACTION_ID = "io.openliberty.tools.intellij.actions.ViewEffectivePom"; - public static final Map ACTIONS_MAP = new HashMap() { + private static HashMap CORE_ACTIONS_MAP = new HashMap() { { put(LIBERTY_DEV_START, LIBERTY_DEV_START_ACTION_ID); put(LIBERTY_DEV_STOP, LIBERTY_DEV_STOP_ACTION_ID); put(LIBERTY_DEV_CUSTOM_START, LIBERTY_DEV_CUSTOM_START_ACTION_ID); put(LIBERTY_DEV_TESTS, LIBERTY_DEV_TESTS_ACTION_ID); - put(VIEW_UNIT_TEST_REPORT, VIEW_UNIT_TEST_REPORT_ACTION_ID); - put(VIEW_INTEGRATION_TEST_REPORT, VIEW_INTEGRATION_TEST_REPORT_ACTION_ID); - put(VIEW_GRADLE_TEST_REPORT, VIEW_GRADLE_TEST_REPORT_ACTION_ID); } }; + public static HashMap getFullActionMap() { + HashMap fullActionsMap = new HashMap(); + fullActionsMap.putAll(CORE_ACTIONS_MAP); + fullActionsMap.put(VIEW_UNIT_TEST_REPORT, VIEW_UNIT_TEST_REPORT_ACTION_ID); + fullActionsMap.put(VIEW_INTEGRATION_TEST_REPORT, VIEW_INTEGRATION_TEST_REPORT_ACTION_ID); + fullActionsMap.put(VIEW_GRADLE_TEST_REPORT, VIEW_GRADLE_TEST_REPORT_ACTION_ID); + return fullActionsMap; + } + + public static HashMap getMavenMap() { + HashMap mavenActionsMap = new HashMap(); + mavenActionsMap.putAll(CORE_ACTIONS_MAP); + mavenActionsMap.put(VIEW_UNIT_TEST_REPORT, VIEW_UNIT_TEST_REPORT_ACTION_ID); + mavenActionsMap.put(VIEW_INTEGRATION_TEST_REPORT, VIEW_INTEGRATION_TEST_REPORT_ACTION_ID); + return mavenActionsMap; + } + + public static HashMap getGradleMap() { + HashMap gradleActionsMap = new HashMap(); + gradleActionsMap.putAll(CORE_ACTIONS_MAP); + gradleActionsMap.put(VIEW_GRADLE_TEST_REPORT, VIEW_GRADLE_TEST_REPORT_ACTION_ID); + return gradleActionsMap; + } } diff --git a/src/main/java/io/openliberty/tools/intellij/util/LibertyProjectUtil.java b/src/main/java/io/openliberty/tools/intellij/util/LibertyProjectUtil.java index d9673e3f..e0cb4d3a 100644 --- a/src/main/java/io/openliberty/tools/intellij/util/LibertyProjectUtil.java +++ b/src/main/java/io/openliberty/tools/intellij/util/LibertyProjectUtil.java @@ -12,10 +12,7 @@ import com.intellij.terminal.JBTerminalWidget; import com.intellij.ui.content.Content; import com.sun.istack.Nullable; -import org.jetbrains.plugins.terminal.AbstractTerminalRunner; -import org.jetbrains.plugins.terminal.ShellTerminalWidget; -import org.jetbrains.plugins.terminal.TerminalTabState; -import org.jetbrains.plugins.terminal.TerminalView; +import org.jetbrains.plugins.terminal.*; import java.util.ArrayList; import java.util.Objects; @@ -64,11 +61,11 @@ public static ShellTerminalWidget getTerminalWidget(Project project, String proj } else if (createWidget) { // create a new terminal tab TerminalView terminalView = TerminalView.getInstance(project); - AbstractTerminalRunner terminalRunner = terminalView.getTerminalRunner(); + LocalTerminalDirectRunner terminalRun = new LocalTerminalDirectRunner(project); TerminalTabState tabState = new TerminalTabState(); tabState.myTabName = projectName; tabState.myWorkingDirectory = project.getBasePath(); - terminalView.createNewSession(terminalRunner, tabState); + terminalView.createNewSession(terminalRun, tabState); return getTerminalWidget(terminalWindow, projectName); } return null; diff --git a/src/main/java/io/openliberty/tools/intellij/util/TreeDataProvider.java b/src/main/java/io/openliberty/tools/intellij/util/TreeDataProvider.java index 6883c1b7..91376d5d 100644 --- a/src/main/java/io/openliberty/tools/intellij/util/TreeDataProvider.java +++ b/src/main/java/io/openliberty/tools/intellij/util/TreeDataProvider.java @@ -2,6 +2,7 @@ import com.intellij.openapi.actionSystem.DataProvider; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.ui.treeStructure.Tree; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -13,7 +14,8 @@ public class TreeDataProvider implements DataProvider { public VirtualFile currentFile; public String projectName; public String projectType; - HashMap> map = new HashMap>(); + public HashMap> map = new HashMap>(); + public Tree tree; @Nullable @Override @@ -26,6 +28,8 @@ public Object getData(@NotNull String dataId) { return this.projectType; } else if (dataId.equals(Constants.LIBERTY_PROJECT_MAP)) { return this.map; + } else if (dataId.equals(Constants.LIBERTY_DASHBOARD_TREE)) { + return this.tree; } return null; } @@ -40,5 +44,8 @@ public void setProjectMap(@NotNull HashMap> map) { this.map = map; } + public void setTreeOnRefresh(@NotNull Tree tree) { + this.tree = tree; + } } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 5aaf0fd8..6b8576f6 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -2,7 +2,7 @@ open-liberty.intellij Open Liberty Tools - Open Liberty + Open-Liberty Framework Integration