From 86eb7e9c64078f8790d398942fd7278384d05531 Mon Sep 17 00:00:00 2001 From: anusreelakshmi934 Date: Thu, 25 Jan 2024 15:05:35 +0530 Subject: [PATCH] Fixes #175 --- .../intellij/actions/RunLibertyDevTask.java | 66 +++++++------------ 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/actions/RunLibertyDevTask.java b/src/main/java/io/openliberty/tools/intellij/actions/RunLibertyDevTask.java index a6aa2958b..72c21cfb7 100644 --- a/src/main/java/io/openliberty/tools/intellij/actions/RunLibertyDevTask.java +++ b/src/main/java/io/openliberty/tools/intellij/actions/RunLibertyDevTask.java @@ -52,30 +52,40 @@ public void update(@NotNull AnActionEvent e) { //if the action event data context returns a null project, just return and let successive update calls modify the presentation return; } + getLibertyTree(e, project, true); + } + + private static void getLibertyTree(@NotNull AnActionEvent e, Project project, boolean isUpdate) { ToolWindow libertyDevToolWindow = ToolWindowManager.getInstance(project).getToolWindow(Constants.LIBERTY_DEV_DASHBOARD_ID); if (libertyDevToolWindow != null) { Content content = libertyDevToolWindow.getContentManager().findContent(LocalizedResourceUtil.getMessage("liberty.tool.window.display.name")); JComponent libertyWindow = content.getComponent(); Component[] components = libertyWindow.getComponents(); + Tree libertyTree = null; for (Component comp : components) { - if (comp instanceof JBScrollPane && comp.getName() != null && comp.getName().equals(Constants.LIBERTY_SCROLL_PANE)) { - JBScrollPane scrollPane = (JBScrollPane) comp; - if (scrollPane.getViewport().getView() instanceof Tree) { - Tree libertyTree = (Tree) scrollPane.getViewport().getView(); - - TreePath[] selectionPaths = libertyTree.getSelectionPaths(); - - // when only one child node is selected, enable this action - if (selectionPaths != null && selectionPaths.length == 1) { - String lastPathComponent = selectionPaths[0].getLastPathComponent().toString(); - if (Constants.FULL_ACTIONS_MAP.containsKey(lastPathComponent)) { - e.getPresentation().setEnabled(true); + if (comp instanceof JBScrollPane scrollPane && comp.getName() != null && comp.getName().equals(Constants.LIBERTY_SCROLL_PANE)) { + Component view = scrollPane.getViewport().getView(); + if (view instanceof Tree) { + libertyTree = (Tree) view; + TreePath[] selectionPaths = libertyTree.getSelectionPaths(); + // when only one child node is selected, enable this action + if (selectionPaths != null && selectionPaths.length == 1) { + String lastPathComponent = selectionPaths[0].getLastPathComponent().toString(); + if (Constants.FULL_ACTIONS_MAP.containsKey(lastPathComponent)) { + if (isUpdate) { + e.getPresentation().setEnabled(true); + } else { + AnAction action = ActionManager.getInstance().getAction(Constants.FULL_ACTIONS_MAP.get(lastPathComponent)); + action.actionPerformed(new AnActionEvent(null, DataManager.getInstance().getDataContext(libertyTree), e.getPlace(), e.getPresentation(), ActionManager.getInstance(), 0)); + } + } } } - }} else { - LOGGER.debug("Tree view not built, no valid projects to run Liberty dev actions on"); } } + if (libertyTree == null) { + LOGGER.debug("Tree view not built, no valid projects to run Liberty dev actions on"); + } } } @@ -97,33 +107,7 @@ public void actionPerformed(@NotNull AnActionEvent e) { action.actionPerformed(new AnActionEvent(null, e.getDataContext(), e.getPlace(), e.getPresentation(), ActionManager.getInstance(), 0)); } } else { - // triggered through icon on tool window - ToolWindow libertyDevToolWindow = ToolWindowManager.getInstance(project).getToolWindow(Constants.LIBERTY_DEV_DASHBOARD_ID); - - Content content = libertyDevToolWindow.getContentManager().findContent(LocalizedResourceUtil.getMessage("liberty.tool.window.display.name")); - - JComponent libertyWindow = content.getComponent(); - - Component[] components = libertyWindow.getComponents(); - - for (Component comp : components) { - if (comp instanceof JBScrollPane && comp.getName() != null && comp.getName().equals(Constants.LIBERTY_SCROLL_PANE)) { - JBScrollPane scrollPane = (JBScrollPane) comp; - if (scrollPane.getViewport().getView() instanceof Tree) { - Tree libertyTree = (Tree) scrollPane.getViewport().getView(); - TreePath[] selectionPaths = libertyTree.getSelectionPaths(); - if (selectionPaths != null && selectionPaths.length == 1) { - String lastPathComponent = selectionPaths[0].getLastPathComponent().toString(); - if (Constants.FULL_ACTIONS_MAP.containsKey(lastPathComponent)) { - // calls selected action - AnAction action = ActionManager.getInstance().getAction(Constants.FULL_ACTIONS_MAP.get(lastPathComponent)); - action.actionPerformed(new AnActionEvent(null, DataManager.getInstance().getDataContext(libertyTree), e.getPlace(), e.getPresentation(), ActionManager.getInstance(), 0)); - } - } - }} else { - LOGGER.debug("Tree view not built, no valid projects to run Liberty dev actions on"); - } - } + getLibertyTree(e, project, false); } } }