From 5ee3fe2398b07b41029c95915a0df798e557490f Mon Sep 17 00:00:00 2001 From: Vladyslav_Yanytskyi Date: Tue, 26 Apr 2022 19:08:09 +0300 Subject: [PATCH 1/5] Added ability to copy text from Impex and Flexible Search files to Hybris Console. --- .../META-INF/lang-optional-dependencies.xml | 14 ++- ...AbstractCopyFileToHybrisConsoleAction.java | 92 +++++++++++++++++++ .../actions/CopyFlexibleSearchFileAction.java | 50 ++++++++++ .../file/actions/CopyImpexFileAction.java | 34 +++++++ 4 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java create mode 100644 src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java create mode 100644 src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java diff --git a/resources/META-INF/lang-optional-dependencies.xml b/resources/META-INF/lang-optional-dependencies.xml index 4b35b86d9..a17a4d69a 100644 --- a/resources/META-INF/lang-optional-dependencies.xml +++ b/resources/META-INF/lang-optional-dependencies.xml @@ -269,12 +269,24 @@ - + + + + + + + + diff --git a/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java b/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java new file mode 100644 index 000000000..e7857a23c --- /dev/null +++ b/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java @@ -0,0 +1,92 @@ +/* + * This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. + * Copyright (C) 2019 EPAM Systems + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.intellij.idea.plugin.hybris.actions; + +import com.intellij.ide.projectView.ProjectView; +import com.intellij.ide.projectView.impl.AbstractProjectViewPane; +import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsole; +import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanel; +import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanelView; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; + +import javax.swing.tree.TreePath; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public abstract class AbstractCopyFileToHybrisConsoleAction extends AnAction { + + public static boolean isRequiredFileExtension( + @NotNull final DataContext dataContext, + final String fileExtension, + final boolean oneFile + ) { + boolean isImpex = false; + final List filesName = getFilesName(dataContext); + if (oneFile) { + return filesName.size() == 1 && filesName.get(0).contains(fileExtension); + } else { + for (String name : filesName) { + if (name.contains(fileExtension)) { + isImpex = true; + } else { + return false; + } + } + } + return isImpex; + } + + private static List getFilesName(@NotNull final DataContext dataContext) { + final Object[] files = getFiles(dataContext); + final List names = new ArrayList<>(); + if (files != null) { + for (final Object file : files) { + final TreePath treePath = (TreePath) file; + names.add(treePath.getLastPathComponent().toString()); + } + } + return names; + } + + public static Object[] getFiles(@NotNull final DataContext dataContext) { + final Project project = CommonDataKeys.PROJECT.getData(dataContext); + if (project != null) { + final AbstractProjectViewPane currentProjectViewPane = ProjectView.getInstance(project) + .getCurrentProjectViewPane(); + return currentProjectViewPane.getSelectionPaths(); + } + return new Object[0]; + } + + public static void copyToHybrisConsole(final Project project, final String consoleTitle, final String query) { + final HybrisConsolePanel hybrisConsolePanel = HybrisConsolePanelView.Companion.getInstance(Objects.requireNonNull( + project)).getConsolePanel(); + final HybrisConsole hybrisConsole = hybrisConsolePanel.findConsole(consoleTitle); + if (hybrisConsole != null) { + hybrisConsole.clear(); + hybrisConsole.setInputText(query); + hybrisConsolePanel.setActiveConsole(hybrisConsole); + } + } +} diff --git a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java new file mode 100644 index 000000000..2e3bd641e --- /dev/null +++ b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java @@ -0,0 +1,50 @@ +/* + * This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. + * Copyright (C) 2019 EPAM Systems + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.intellij.idea.plugin.hybris.flexibleSearch.file.actions; + +import com.intellij.ide.projectView.impl.nodes.PsiFileNode; +import com.intellij.idea.plugin.hybris.actions.AbstractCopyFileToHybrisConsoleAction; +import com.intellij.idea.plugin.hybris.actions.ActionUtils; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.DataContext; +import org.jetbrains.annotations.NotNull; + +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.TreePath; + +public class CopyFlexibleSearchFileAction extends AbstractCopyFileToHybrisConsoleAction { + + @Override + public void update(@NotNull final AnActionEvent e) { + final DataContext dataContext = e.getDataContext(); + e.getPresentation() + .setEnabledAndVisible(ActionUtils.isHybrisContext(dataContext) && isRequiredFileExtension( + dataContext, ".fxs", true)); + } + + @Override + public void actionPerformed(@NotNull final AnActionEvent e) { + final TreePath[] files = (TreePath[]) getFiles(e.getDataContext()); + for (final TreePath treePath : files) { + final DefaultMutableTreeNode lastPathNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); + final PsiFileNode file = (PsiFileNode) lastPathNode.getUserObject(); + copyToHybrisConsole(e.getProject(), "Hybris FS Console", file.getValue().getText()); + } + } +} diff --git a/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java b/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java new file mode 100644 index 000000000..e4fe9c480 --- /dev/null +++ b/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java @@ -0,0 +1,34 @@ +package com.intellij.idea.plugin.hybris.impex.file.actions; + +import com.intellij.ide.projectView.impl.nodes.PsiFileNode; +import com.intellij.idea.plugin.hybris.actions.AbstractCopyFileToHybrisConsoleAction; +import com.intellij.idea.plugin.hybris.actions.ActionUtils; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.DataContext; +import org.jetbrains.annotations.NotNull; + +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.TreePath; + +public class CopyImpexFileAction extends AbstractCopyFileToHybrisConsoleAction { + + @Override + public void update(@NotNull final AnActionEvent e) { + final DataContext dataContext = e.getDataContext(); + e.getPresentation() + .setEnabledAndVisible(ActionUtils.isHybrisContext(dataContext) && isRequiredFileExtension( + dataContext, ".impex", false)); + } + + @Override + public void actionPerformed(@NotNull final AnActionEvent e) { + final TreePath[] files = (TreePath[]) getFiles(e.getDataContext()); + final StringBuilder query = new StringBuilder(); + for (final TreePath treePath : files) { + final DefaultMutableTreeNode lastPathNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); + final PsiFileNode file = (PsiFileNode) lastPathNode.getUserObject(); + query.append(file.getValue().getText()).append('\n'); + } + copyToHybrisConsole(e.getProject(), "Hybris Impex Console", query.toString()); + } +} From 23fb49e2838c65784e87ab3411cf0082336ea5aa Mon Sep 17 00:00:00 2001 From: Vladyslav_Yanytskyi Date: Wed, 27 Apr 2022 14:55:19 +0300 Subject: [PATCH 2/5] Added warning message for copy files to hybris console. Added move to Hybris console when we choose copy function. --- ...AbstractCopyFileToHybrisConsoleAction.java | 37 +++++++++++++--- .../actions/CopyFlexibleSearchFileAction.java | 21 ++++++++- .../file/actions/CopyImpexFileAction.java | 22 +++++++++- .../CopyFileToHybrisConsoleDialog.form | 26 +++++++++++ .../CopyFileToHybrisConsoleDialog.java | 44 +++++++++++++++++++ 5 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.form create mode 100644 src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.java diff --git a/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java b/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java index e7857a23c..2cb9ad5b9 100644 --- a/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java +++ b/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java @@ -18,15 +18,20 @@ package com.intellij.idea.plugin.hybris.actions; +import com.intellij.execution.console.ConsoleExecutionEditor; +import com.intellij.execution.console.LanguageConsoleImpl; import com.intellij.ide.projectView.ProjectView; import com.intellij.ide.projectView.impl.AbstractProjectViewPane; import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsole; +import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsoleToolWindowFactory; import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanel; import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanelView; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.project.Project; +import com.intellij.openapi.wm.ToolWindow; +import com.intellij.openapi.wm.ToolWindowManager; import org.jetbrains.annotations.NotNull; import javax.swing.tree.TreePath; @@ -36,7 +41,7 @@ public abstract class AbstractCopyFileToHybrisConsoleAction extends AnAction { - public static boolean isRequiredFileExtension( + public boolean isRequiredFileExtension( @NotNull final DataContext dataContext, final String fileExtension, final boolean oneFile @@ -57,7 +62,7 @@ public static boolean isRequiredFileExtension( return isImpex; } - private static List getFilesName(@NotNull final DataContext dataContext) { + private List getFilesName(@NotNull final DataContext dataContext) { final Object[] files = getFiles(dataContext); final List names = new ArrayList<>(); if (files != null) { @@ -69,7 +74,7 @@ private static List getFilesName(@NotNull final DataContext dataContext) return names; } - public static Object[] getFiles(@NotNull final DataContext dataContext) { + public Object[] getFiles(@NotNull final DataContext dataContext) { final Project project = CommonDataKeys.PROJECT.getData(dataContext); if (project != null) { final AbstractProjectViewPane currentProjectViewPane = ProjectView.getInstance(project) @@ -79,14 +84,36 @@ public static Object[] getFiles(@NotNull final DataContext dataContext) { return new Object[0]; } - public static void copyToHybrisConsole(final Project project, final String consoleTitle, final String query) { - final HybrisConsolePanel hybrisConsolePanel = HybrisConsolePanelView.Companion.getInstance(Objects.requireNonNull( + public HybrisConsolePanel getHybrisConsolePanel(final Project project) { + return HybrisConsolePanelView.Companion.getInstance(Objects.requireNonNull( project)).getConsolePanel(); + } + + public String getTextFromHybrisConsole(final Project project, final HybrisConsole hybrisConsole) { + final LanguageConsoleImpl.Helper helper = new LanguageConsoleImpl.Helper( + project, + hybrisConsole.getVirtualFile() + ); + final ConsoleExecutionEditor consoleExecutionEditor = new ConsoleExecutionEditor(helper); + return consoleExecutionEditor.getDocument().getText(); + } + + public void copyToHybrisConsole( + final Project project, + final String consoleTitle, + final String query + ) { + final HybrisConsolePanel hybrisConsolePanel = getHybrisConsolePanel(project); final HybrisConsole hybrisConsole = hybrisConsolePanel.findConsole(consoleTitle); if (hybrisConsole != null) { hybrisConsole.clear(); hybrisConsole.setInputText(query); hybrisConsolePanel.setActiveConsole(hybrisConsole); + final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow( + HybrisConsoleToolWindowFactory.ID); + if (toolWindow != null) { + toolWindow.activate(null); + } } } } diff --git a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java index 2e3bd641e..97a397df4 100644 --- a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java +++ b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java @@ -21,8 +21,12 @@ import com.intellij.ide.projectView.impl.nodes.PsiFileNode; import com.intellij.idea.plugin.hybris.actions.AbstractCopyFileToHybrisConsoleAction; import com.intellij.idea.plugin.hybris.actions.ActionUtils; +import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsole; +import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanel; +import com.intellij.idea.plugin.hybris.toolwindow.CopyFileToHybrisConsoleDialog; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; import javax.swing.tree.DefaultMutableTreeNode; @@ -30,6 +34,8 @@ public class CopyFlexibleSearchFileAction extends AbstractCopyFileToHybrisConsoleAction { + private static final String FLEXIBLE_SEARCH_CONSOLE_TITLE = "Hybris FS Console"; + @Override public void update(@NotNull final AnActionEvent e) { final DataContext dataContext = e.getDataContext(); @@ -40,11 +46,24 @@ public void update(@NotNull final AnActionEvent e) { @Override public void actionPerformed(@NotNull final AnActionEvent e) { + final Project project = e.getProject(); final TreePath[] files = (TreePath[]) getFiles(e.getDataContext()); for (final TreePath treePath : files) { final DefaultMutableTreeNode lastPathNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); final PsiFileNode file = (PsiFileNode) lastPathNode.getUserObject(); - copyToHybrisConsole(e.getProject(), "Hybris FS Console", file.getValue().getText()); + final HybrisConsolePanel hybrisConsolePanel = getHybrisConsolePanel(project); + final HybrisConsole hybrisConsole = hybrisConsolePanel.findConsole(FLEXIBLE_SEARCH_CONSOLE_TITLE); + if (hybrisConsole != null && project != null) { + if (!getTextFromHybrisConsole(project, hybrisConsole).isEmpty()) { + final CopyFileToHybrisConsoleDialog copyFileToHybrisConsoleDialog = new CopyFileToHybrisConsoleDialog(project); + copyFileToHybrisConsoleDialog.setTitle("Flexible Search Console"); + if (copyFileToHybrisConsoleDialog.showAndGet()) { + copyToHybrisConsole(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, file.getValue().getText()); + } + } else { + copyToHybrisConsole(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, file.getValue().getText()); + } + } } } } diff --git a/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java b/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java index e4fe9c480..ecb9e657b 100644 --- a/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java +++ b/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java @@ -3,8 +3,12 @@ import com.intellij.ide.projectView.impl.nodes.PsiFileNode; import com.intellij.idea.plugin.hybris.actions.AbstractCopyFileToHybrisConsoleAction; import com.intellij.idea.plugin.hybris.actions.ActionUtils; +import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsole; +import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanel; +import com.intellij.idea.plugin.hybris.toolwindow.CopyFileToHybrisConsoleDialog; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; import javax.swing.tree.DefaultMutableTreeNode; @@ -12,6 +16,8 @@ public class CopyImpexFileAction extends AbstractCopyFileToHybrisConsoleAction { + private static final String IMPEX_CONSOLE_TITLE = "Hybris Impex Console"; + @Override public void update(@NotNull final AnActionEvent e) { final DataContext dataContext = e.getDataContext(); @@ -22,6 +28,7 @@ public void update(@NotNull final AnActionEvent e) { @Override public void actionPerformed(@NotNull final AnActionEvent e) { + final Project project = e.getProject(); final TreePath[] files = (TreePath[]) getFiles(e.getDataContext()); final StringBuilder query = new StringBuilder(); for (final TreePath treePath : files) { @@ -29,6 +36,19 @@ public void actionPerformed(@NotNull final AnActionEvent e) { final PsiFileNode file = (PsiFileNode) lastPathNode.getUserObject(); query.append(file.getValue().getText()).append('\n'); } - copyToHybrisConsole(e.getProject(), "Hybris Impex Console", query.toString()); + final HybrisConsolePanel hybrisConsolePanel = getHybrisConsolePanel(project); + final HybrisConsole hybrisConsole = hybrisConsolePanel.findConsole(IMPEX_CONSOLE_TITLE); + if (hybrisConsole != null) { + if (!getTextFromHybrisConsole(project, hybrisConsole).isEmpty()) { + final CopyFileToHybrisConsoleDialog copyFileToHybrisConsoleDialog = new CopyFileToHybrisConsoleDialog( + project); + copyFileToHybrisConsoleDialog.setTitle("Impex Console"); + if (copyFileToHybrisConsoleDialog.showAndGet()) { + copyToHybrisConsole(project, IMPEX_CONSOLE_TITLE, query.toString()); + } + } else { + copyToHybrisConsole(project, IMPEX_CONSOLE_TITLE, query.toString()); + } + } } } diff --git a/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.form b/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.form new file mode 100644 index 000000000..c02f23f55 --- /dev/null +++ b/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.form @@ -0,0 +1,26 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.java b/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.java new file mode 100644 index 000000000..621157a7a --- /dev/null +++ b/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.java @@ -0,0 +1,44 @@ +/* + * This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. + * Copyright (C) 2019 EPAM Systems + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.intellij.idea.plugin.hybris.toolwindow; + +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +import static com.intellij.openapi.ui.DialogWrapper.IdeModalityType.PROJECT; + +public class CopyFileToHybrisConsoleDialog extends DialogWrapper { + + private JPanel contentPane; + + public CopyFileToHybrisConsoleDialog( + @Nullable final Project project + ) { + super(project, false, PROJECT); + init(); + } + + @Override + protected @Nullable JComponent createCenterPanel() { + return contentPane; + } +} From 0967197ea1e8184f9bea1e2a0db1dda1d8b6c1f1 Mon Sep 17 00:00:00 2001 From: Vladyslav_Yanytskyi Date: Fri, 29 Apr 2022 16:13:50 +0300 Subject: [PATCH 3/5] Refactored code --- .../META-INF/lang-optional-dependencies.xml | 23 +++--- resources/i18n/HybrisBundle.properties | 3 + ...AbstractCopyFileToHybrisConsoleAction.java | 81 +++++++++++++------ .../plugin/hybris/actions/ActionUtils.java | 4 + .../plugin/hybris/common/HybrisConstants.java | 4 + .../actions/CopyFlexibleSearchFileAction.java | 38 ++------- .../FlexibleSearchFileCreateAction.java | 6 -- .../file/actions/CopyImpexFileAction.java | 42 +++------- .../CopyFileToHybrisConsoleDialog.form | 2 +- 9 files changed, 98 insertions(+), 105 deletions(-) diff --git a/resources/META-INF/lang-optional-dependencies.xml b/resources/META-INF/lang-optional-dependencies.xml index a17a4d69a..a6bc3e7bf 100644 --- a/resources/META-INF/lang-optional-dependencies.xml +++ b/resources/META-INF/lang-optional-dependencies.xml @@ -269,11 +269,18 @@ - - - + + + + + + + + - - - - diff --git a/resources/i18n/HybrisBundle.properties b/resources/i18n/HybrisBundle.properties index bc5f7f0ef..5637a4994 100644 --- a/resources/i18n/HybrisBundle.properties +++ b/resources/i18n/HybrisBundle.properties @@ -229,3 +229,6 @@ flexible.search.table.empty.text=query parameters appear here hybris.ts.items.validation.settings.enabled=Warn if generated items are out of date hybris.ts.items.validation.warn=Generated classes are out of date. Please run Build -> Build Project or ant all. + +copy.file.dialog.impex=Impex Console +copy.file.dialog.fxs=Flexible Search Console diff --git a/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java b/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java index 2cb9ad5b9..70c6ec755 100644 --- a/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java +++ b/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java @@ -21,38 +21,69 @@ import com.intellij.execution.console.ConsoleExecutionEditor; import com.intellij.execution.console.LanguageConsoleImpl; import com.intellij.ide.projectView.ProjectView; +import com.intellij.ide.projectView.ProjectViewNode; import com.intellij.ide.projectView.impl.AbstractProjectViewPane; +import com.intellij.ide.projectView.impl.nodes.PsiFileNode; +import com.intellij.idea.plugin.hybris.common.utils.HybrisI18NBundleUtils; import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsole; import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsoleToolWindowFactory; import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanel; import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanelView; +import com.intellij.idea.plugin.hybris.toolwindow.CopyFileToHybrisConsoleDialog; import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.CommonDataKeys; -import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.wm.ToolWindow; import com.intellij.openapi.wm.ToolWindowManager; import org.jetbrains.annotations.NotNull; +import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; import java.util.ArrayList; import java.util.List; -import java.util.Objects; + +import static com.intellij.idea.plugin.hybris.common.HybrisConstants.*; public abstract class AbstractCopyFileToHybrisConsoleAction extends AnAction { + public void performed(final Project project, final String consoleTitle, final String dialogTitle) { + if (project != null) { + final TreePath[] paths = getPaths(project); + final StringBuilder query = new StringBuilder(); + for (final TreePath treePath : paths) { + final DefaultMutableTreeNode lastPathNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); + final PsiFileNode file = (PsiFileNode) lastPathNode.getUserObject(); + query.append(file.getValue().getText()).append(System.lineSeparator()); + } + final HybrisConsolePanel hybrisConsolePanel = getHybrisConsolePanel(project); + final HybrisConsole hybrisConsole = hybrisConsolePanel.findConsole(consoleTitle); + if (hybrisConsole != null) { + if (!getTextFromHybrisConsole(project, hybrisConsole).isEmpty()) { + final CopyFileToHybrisConsoleDialog copyFileToHybrisConsoleDialog = new CopyFileToHybrisConsoleDialog( + project); + copyFileToHybrisConsoleDialog.setTitle(dialogTitle); + if (copyFileToHybrisConsoleDialog.showAndGet()) { + copyToHybrisConsole(project, consoleTitle, query.toString()); + } + } else { + copyToHybrisConsole(project, consoleTitle, query.toString()); + } + } + } + } + public boolean isRequiredFileExtension( - @NotNull final DataContext dataContext, + @NotNull final Project project, final String fileExtension, final boolean oneFile ) { boolean isImpex = false; - final List filesName = getFilesName(dataContext); + final List fileExtensions = getFileExtensions(project); if (oneFile) { - return filesName.size() == 1 && filesName.get(0).contains(fileExtension); + return fileExtensions.size() == 1 && fileExtensions.get(0).equals(fileExtension); } else { - for (String name : filesName) { - if (name.contains(fileExtension)) { + for (String extension : fileExtensions) { + if (extension.equals(fileExtension)) { isImpex = true; } else { return false; @@ -62,31 +93,29 @@ public boolean isRequiredFileExtension( return isImpex; } - private List getFilesName(@NotNull final DataContext dataContext) { - final Object[] files = getFiles(dataContext); + private List getFileExtensions(@NotNull final Project project) { + final TreePath[] paths = getPaths(project); final List names = new ArrayList<>(); - if (files != null) { - for (final Object file : files) { - final TreePath treePath = (TreePath) file; - names.add(treePath.getLastPathComponent().toString()); + for (final TreePath treePath : paths) { + final DefaultMutableTreeNode lastPathNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); + final ProjectViewNode file = (ProjectViewNode) lastPathNode.getUserObject(); + final VirtualFile virtualFile = file.getVirtualFile(); + if (virtualFile != null && !virtualFile.isDirectory()) { + names.add(file.getVirtualFile().getExtension()); } } return names; } - public Object[] getFiles(@NotNull final DataContext dataContext) { - final Project project = CommonDataKeys.PROJECT.getData(dataContext); - if (project != null) { - final AbstractProjectViewPane currentProjectViewPane = ProjectView.getInstance(project) - .getCurrentProjectViewPane(); - return currentProjectViewPane.getSelectionPaths(); - } - return new Object[0]; + public TreePath[] getPaths(@NotNull final Project project) { + final AbstractProjectViewPane currentProjectViewPane = ProjectView.getInstance(project) + .getCurrentProjectViewPane(); + final TreePath[] selectionPaths = currentProjectViewPane.getSelectionPaths(); + return selectionPaths == null ? new TreePath[0] : selectionPaths; } public HybrisConsolePanel getHybrisConsolePanel(final Project project) { - return HybrisConsolePanelView.Companion.getInstance(Objects.requireNonNull( - project)).getConsolePanel(); + return HybrisConsolePanelView.Companion.getInstance(project).getConsolePanel(); } public String getTextFromHybrisConsole(final Project project, final HybrisConsole hybrisConsole) { @@ -116,4 +145,8 @@ public void copyToHybrisConsole( } } } + + public String getDialogTitle(final String fileExtension) { + return HybrisI18NBundleUtils.message(DIALOG_TITLE + fileExtension); + } } diff --git a/src/com/intellij/idea/plugin/hybris/actions/ActionUtils.java b/src/com/intellij/idea/plugin/hybris/actions/ActionUtils.java index fbb5a481f..84f33ffb2 100644 --- a/src/com/intellij/idea/plugin/hybris/actions/ActionUtils.java +++ b/src/com/intellij/idea/plugin/hybris/actions/ActionUtils.java @@ -41,4 +41,8 @@ public static boolean isHybrisContext(@NotNull final DataContext dataContext) { final Project project = CommonDataKeys.PROJECT.getData(dataContext); return project != null && CommonIdeaService.getInstance().isHybrisProject(project); } + + public static boolean isHybrisContext(@NotNull final Project project) { + return CommonIdeaService.getInstance().isHybrisProject(project); + } } diff --git a/src/com/intellij/idea/plugin/hybris/common/HybrisConstants.java b/src/com/intellij/idea/plugin/hybris/common/HybrisConstants.java index 45ee97f82..79e691fbe 100644 --- a/src/com/intellij/idea/plugin/hybris/common/HybrisConstants.java +++ b/src/com/intellij/idea/plugin/hybris/common/HybrisConstants.java @@ -331,4 +331,8 @@ interface IMPEX { "Undo Reformat Code", "Auto-Indent Lines" }; + + String DIALOG_TITLE = "copy.file.dialog."; + String FLEXIBLE_SEARCH_FILE_EXTENSION = "fxs"; + String IMPEX_FILE_EXTENSION = "impex"; } diff --git a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java index 97a397df4..66b07498e 100644 --- a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java +++ b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java @@ -18,52 +18,30 @@ package com.intellij.idea.plugin.hybris.flexibleSearch.file.actions; -import com.intellij.ide.projectView.impl.nodes.PsiFileNode; import com.intellij.idea.plugin.hybris.actions.AbstractCopyFileToHybrisConsoleAction; import com.intellij.idea.plugin.hybris.actions.ActionUtils; -import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsole; -import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanel; -import com.intellij.idea.plugin.hybris.toolwindow.CopyFileToHybrisConsoleDialog; import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; -import javax.swing.tree.DefaultMutableTreeNode; -import javax.swing.tree.TreePath; +import static com.intellij.idea.plugin.hybris.common.HybrisConstants.*; public class CopyFlexibleSearchFileAction extends AbstractCopyFileToHybrisConsoleAction { - private static final String FLEXIBLE_SEARCH_CONSOLE_TITLE = "Hybris FS Console"; - @Override public void update(@NotNull final AnActionEvent e) { - final DataContext dataContext = e.getDataContext(); - e.getPresentation() - .setEnabledAndVisible(ActionUtils.isHybrisContext(dataContext) && isRequiredFileExtension( - dataContext, ".fxs", true)); + final Project project = e.getProject(); + if (project != null) { + e.getPresentation().setEnabledAndVisible(ActionUtils.isHybrisContext(project) && isRequiredFileExtension( + project, FLEXIBLE_SEARCH_FILE_EXTENSION, true)); + } } @Override public void actionPerformed(@NotNull final AnActionEvent e) { final Project project = e.getProject(); - final TreePath[] files = (TreePath[]) getFiles(e.getDataContext()); - for (final TreePath treePath : files) { - final DefaultMutableTreeNode lastPathNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); - final PsiFileNode file = (PsiFileNode) lastPathNode.getUserObject(); - final HybrisConsolePanel hybrisConsolePanel = getHybrisConsolePanel(project); - final HybrisConsole hybrisConsole = hybrisConsolePanel.findConsole(FLEXIBLE_SEARCH_CONSOLE_TITLE); - if (hybrisConsole != null && project != null) { - if (!getTextFromHybrisConsole(project, hybrisConsole).isEmpty()) { - final CopyFileToHybrisConsoleDialog copyFileToHybrisConsoleDialog = new CopyFileToHybrisConsoleDialog(project); - copyFileToHybrisConsoleDialog.setTitle("Flexible Search Console"); - if (copyFileToHybrisConsoleDialog.showAndGet()) { - copyToHybrisConsole(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, file.getValue().getText()); - } - } else { - copyToHybrisConsole(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, file.getValue().getText()); - } - } + if (project != null) { + performed(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, getDialogTitle(FLEXIBLE_SEARCH_FILE_EXTENSION)); } } } diff --git a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/FlexibleSearchFileCreateAction.java b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/FlexibleSearchFileCreateAction.java index 7a5cfd08c..9d8c8a392 100644 --- a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/FlexibleSearchFileCreateAction.java +++ b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/FlexibleSearchFileCreateAction.java @@ -22,7 +22,6 @@ import com.intellij.ide.actions.CreateFileFromTemplateDialog; import com.intellij.idea.plugin.hybris.actions.ActionUtils; import com.intellij.idea.plugin.hybris.common.utils.HybrisIcons; -import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; @@ -75,11 +74,6 @@ protected void postProcess(PsiFile createdElement, String templateName, Map - + From d2586c17b781d1d2c3d8e7a6d1af0d53a783a67a Mon Sep 17 00:00:00 2001 From: Vladyslav_Yanytskyi Date: Tue, 3 May 2022 18:56:22 +0300 Subject: [PATCH 4/5] Refactored code --- ...AbstractCopyFileToHybrisConsoleAction.java | 152 ---------------- .../actions/CopyFileToHybrisConsoleUtils.java | 164 ++++++++++++++++++ .../actions/CopyFlexibleSearchFileAction.java | 18 +- .../file/actions/CopyImpexFileAction.java | 18 +- .../CopyFileToHybrisConsoleDialog.java | 12 +- 5 files changed, 194 insertions(+), 170 deletions(-) delete mode 100644 src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java create mode 100644 src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java diff --git a/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java b/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java deleted file mode 100644 index 70c6ec755..000000000 --- a/src/com/intellij/idea/plugin/hybris/actions/AbstractCopyFileToHybrisConsoleAction.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. - * Copyright (C) 2019 EPAM Systems - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.intellij.idea.plugin.hybris.actions; - -import com.intellij.execution.console.ConsoleExecutionEditor; -import com.intellij.execution.console.LanguageConsoleImpl; -import com.intellij.ide.projectView.ProjectView; -import com.intellij.ide.projectView.ProjectViewNode; -import com.intellij.ide.projectView.impl.AbstractProjectViewPane; -import com.intellij.ide.projectView.impl.nodes.PsiFileNode; -import com.intellij.idea.plugin.hybris.common.utils.HybrisI18NBundleUtils; -import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsole; -import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsoleToolWindowFactory; -import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanel; -import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanelView; -import com.intellij.idea.plugin.hybris.toolwindow.CopyFileToHybrisConsoleDialog; -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.wm.ToolWindow; -import com.intellij.openapi.wm.ToolWindowManager; -import org.jetbrains.annotations.NotNull; - -import javax.swing.tree.DefaultMutableTreeNode; -import javax.swing.tree.TreePath; -import java.util.ArrayList; -import java.util.List; - -import static com.intellij.idea.plugin.hybris.common.HybrisConstants.*; - -public abstract class AbstractCopyFileToHybrisConsoleAction extends AnAction { - - public void performed(final Project project, final String consoleTitle, final String dialogTitle) { - if (project != null) { - final TreePath[] paths = getPaths(project); - final StringBuilder query = new StringBuilder(); - for (final TreePath treePath : paths) { - final DefaultMutableTreeNode lastPathNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); - final PsiFileNode file = (PsiFileNode) lastPathNode.getUserObject(); - query.append(file.getValue().getText()).append(System.lineSeparator()); - } - final HybrisConsolePanel hybrisConsolePanel = getHybrisConsolePanel(project); - final HybrisConsole hybrisConsole = hybrisConsolePanel.findConsole(consoleTitle); - if (hybrisConsole != null) { - if (!getTextFromHybrisConsole(project, hybrisConsole).isEmpty()) { - final CopyFileToHybrisConsoleDialog copyFileToHybrisConsoleDialog = new CopyFileToHybrisConsoleDialog( - project); - copyFileToHybrisConsoleDialog.setTitle(dialogTitle); - if (copyFileToHybrisConsoleDialog.showAndGet()) { - copyToHybrisConsole(project, consoleTitle, query.toString()); - } - } else { - copyToHybrisConsole(project, consoleTitle, query.toString()); - } - } - } - } - - public boolean isRequiredFileExtension( - @NotNull final Project project, - final String fileExtension, - final boolean oneFile - ) { - boolean isImpex = false; - final List fileExtensions = getFileExtensions(project); - if (oneFile) { - return fileExtensions.size() == 1 && fileExtensions.get(0).equals(fileExtension); - } else { - for (String extension : fileExtensions) { - if (extension.equals(fileExtension)) { - isImpex = true; - } else { - return false; - } - } - } - return isImpex; - } - - private List getFileExtensions(@NotNull final Project project) { - final TreePath[] paths = getPaths(project); - final List names = new ArrayList<>(); - for (final TreePath treePath : paths) { - final DefaultMutableTreeNode lastPathNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); - final ProjectViewNode file = (ProjectViewNode) lastPathNode.getUserObject(); - final VirtualFile virtualFile = file.getVirtualFile(); - if (virtualFile != null && !virtualFile.isDirectory()) { - names.add(file.getVirtualFile().getExtension()); - } - } - return names; - } - - public TreePath[] getPaths(@NotNull final Project project) { - final AbstractProjectViewPane currentProjectViewPane = ProjectView.getInstance(project) - .getCurrentProjectViewPane(); - final TreePath[] selectionPaths = currentProjectViewPane.getSelectionPaths(); - return selectionPaths == null ? new TreePath[0] : selectionPaths; - } - - public HybrisConsolePanel getHybrisConsolePanel(final Project project) { - return HybrisConsolePanelView.Companion.getInstance(project).getConsolePanel(); - } - - public String getTextFromHybrisConsole(final Project project, final HybrisConsole hybrisConsole) { - final LanguageConsoleImpl.Helper helper = new LanguageConsoleImpl.Helper( - project, - hybrisConsole.getVirtualFile() - ); - final ConsoleExecutionEditor consoleExecutionEditor = new ConsoleExecutionEditor(helper); - return consoleExecutionEditor.getDocument().getText(); - } - - public void copyToHybrisConsole( - final Project project, - final String consoleTitle, - final String query - ) { - final HybrisConsolePanel hybrisConsolePanel = getHybrisConsolePanel(project); - final HybrisConsole hybrisConsole = hybrisConsolePanel.findConsole(consoleTitle); - if (hybrisConsole != null) { - hybrisConsole.clear(); - hybrisConsole.setInputText(query); - hybrisConsolePanel.setActiveConsole(hybrisConsole); - final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow( - HybrisConsoleToolWindowFactory.ID); - if (toolWindow != null) { - toolWindow.activate(null); - } - } - } - - public String getDialogTitle(final String fileExtension) { - return HybrisI18NBundleUtils.message(DIALOG_TITLE + fileExtension); - } -} diff --git a/src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java b/src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java new file mode 100644 index 000000000..ad51f17f4 --- /dev/null +++ b/src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java @@ -0,0 +1,164 @@ +/* + * This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. + * Copyright (C) 2019 EPAM Systems + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.intellij.idea.plugin.hybris.actions; + +import com.intellij.execution.console.ConsoleExecutionEditor; +import com.intellij.execution.console.LanguageConsoleImpl; +import com.intellij.ide.projectView.ProjectView; +import com.intellij.ide.projectView.ProjectViewNode; +import com.intellij.idea.plugin.hybris.common.utils.HybrisI18NBundleUtils; +import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsole; +import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsoleToolWindowFactory; +import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanel; +import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolePanelView; +import com.intellij.idea.plugin.hybris.toolwindow.CopyFileToHybrisConsoleDialog; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.wm.ToolWindowManager; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiManager; +import org.apache.commons.lang3.StringUtils; + +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.TreePath; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static com.intellij.idea.plugin.hybris.common.HybrisConstants.DIALOG_TITLE; +import static java.lang.System.lineSeparator; + +public final class CopyFileToHybrisConsoleUtils { + + private CopyFileToHybrisConsoleUtils() { + } + + private static Optional cast(R obj, Class clazz) { + if (clazz.isAssignableFrom(obj.getClass())) { + return Optional.ofNullable((T) obj); + } else { + return Optional.empty(); + } + } + + public static void copySelectedFilesToHybris(Project project, String consoleTitle, String dialogTitle) { + var hybrisConsole = getHybrisConsole(project, consoleTitle); + if (hybrisConsole != null) { + boolean isConsoleNotEmpty = StringUtils.isNotEmpty(getTextFromHybrisConsole(project, hybrisConsole)); + String query = getQueryFromSelectedFiles(project); + if (isConsoleNotEmpty) { + var consoleDialog = new CopyFileToHybrisConsoleDialog(project, getDialogTitleFromProperties(dialogTitle)); + consoleDialog.show(() -> copyToHybrisConsole(project, consoleTitle, query)); + } else { + copyToHybrisConsole(project, consoleTitle, query); + } + } + } + + public static boolean isRequiredFileExtension(Project project, String fileExtension, boolean singleFile) { + var fileExtensions = getFileExtensions(project); + if (singleFile) { + return fileExtensions.size() == 1 && fileExtensions.get(0).equals(fileExtension); + } else { + return !fileExtensions.isEmpty() && fileExtensions.stream().allMatch(fileExtension::equals); + } + } + + private static List getFileExtensions(Project project) { + var names = new ArrayList(); + for (var virtualFile : getSelectedFiles(project)) { + if (virtualFile != null) { + boolean isNotDirectory = !virtualFile.isDirectory(); + if (isNotDirectory) { + names.add(virtualFile.getExtension()); + } else { + return Collections.emptyList(); + } + } + } + return names; + } + + private static HybrisConsole getHybrisConsole(Project project, String consoleTitle) { + var hybrisConsolePanel = HybrisConsolePanelView.Companion.getInstance(project).getConsolePanel(); + return hybrisConsolePanel.findConsole(consoleTitle); + } + + private static String getQueryFromSelectedFiles(Project project) { + return getSelectedFiles(project).stream() + .map(virtualFile -> getPsiFileNode(project, virtualFile)) + .map(psiFile -> psiFile.getText() + lineSeparator()). + collect(Collectors.joining()); + } + + private static PsiFile getPsiFileNode(Project project, VirtualFile virtualFile) { + return PsiManager.getInstance(project).findFile(virtualFile); + } + + private static List getSelectedFiles(Project project) { + return Arrays.stream(getSelectedTreePaths(project)) + .map(treePath -> cast(treePath.getLastPathComponent(), DefaultMutableTreeNode.class)) + .filter(Optional::isPresent) + .map(lastPathNode -> cast(lastPathNode.get().getUserObject(), ProjectViewNode.class)) + .filter(Optional::isPresent) + .map(file -> file.get().getVirtualFile()) + .collect(Collectors.toList()); + } + + private static TreePath[] getSelectedTreePaths(final Project project) { + var currentProjectViewPane = ProjectView.getInstance(project).getCurrentProjectViewPane(); + var selectionPaths = currentProjectViewPane.getSelectionPaths(); + return selectionPaths != null ? selectionPaths : new TreePath[0]; + } + + private static HybrisConsolePanel getHybrisConsolePanel(Project project) { + return HybrisConsolePanelView.Companion.getInstance(project).getConsolePanel(); + } + + private static String getTextFromHybrisConsole(Project project, HybrisConsole hybrisConsole) { + var helper = new LanguageConsoleImpl.Helper(project, hybrisConsole.getVirtualFile()); + var consoleExecutionEditor = new ConsoleExecutionEditor(helper); + return consoleExecutionEditor.getDocument().getText(); + } + + private static void copyToHybrisConsole(Project project, String consoleTitle, String query) { + var hybrisConsolePanel = getHybrisConsolePanel(project); + var hybrisConsole = hybrisConsolePanel.findConsole(consoleTitle); + if (hybrisConsole != null) { + hybrisConsole.clear(); + hybrisConsole.setInputText(query); + hybrisConsolePanel.setActiveConsole(hybrisConsole); + openHybrisConsole(project); + } + } + + private static void openHybrisConsole(Project project) { + var toolWindow = ToolWindowManager.getInstance(project).getToolWindow(HybrisConsoleToolWindowFactory.ID); + if (toolWindow != null) { + toolWindow.activate(null); + } + } + + private static String getDialogTitleFromProperties(String fileExtension) { + return HybrisI18NBundleUtils.message(DIALOG_TITLE + fileExtension); + } +} diff --git a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java index 66b07498e..b63bf069b 100644 --- a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java +++ b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java @@ -18,30 +18,32 @@ package com.intellij.idea.plugin.hybris.flexibleSearch.file.actions; -import com.intellij.idea.plugin.hybris.actions.AbstractCopyFileToHybrisConsoleAction; import com.intellij.idea.plugin.hybris.actions.ActionUtils; +import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; +import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.isRequiredFileExtension; +import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.copySelectedFilesToHybris; import static com.intellij.idea.plugin.hybris.common.HybrisConstants.*; -public class CopyFlexibleSearchFileAction extends AbstractCopyFileToHybrisConsoleAction { +public class CopyFlexibleSearchFileAction extends AnAction { @Override - public void update(@NotNull final AnActionEvent e) { - final Project project = e.getProject(); + public void update(@NotNull final AnActionEvent event) { + Project project = event.getProject(); if (project != null) { - e.getPresentation().setEnabledAndVisible(ActionUtils.isHybrisContext(project) && isRequiredFileExtension( + event.getPresentation().setEnabledAndVisible(ActionUtils.isHybrisContext(project) && isRequiredFileExtension( project, FLEXIBLE_SEARCH_FILE_EXTENSION, true)); } } @Override - public void actionPerformed(@NotNull final AnActionEvent e) { - final Project project = e.getProject(); + public void actionPerformed(@NotNull final AnActionEvent event) { + Project project = event.getProject(); if (project != null) { - performed(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, getDialogTitle(FLEXIBLE_SEARCH_FILE_EXTENSION)); + copySelectedFilesToHybris(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, FLEXIBLE_SEARCH_FILE_EXTENSION); } } } diff --git a/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java b/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java index c752a40d0..f02b22cdc 100644 --- a/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java +++ b/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java @@ -1,30 +1,32 @@ package com.intellij.idea.plugin.hybris.impex.file.actions; -import com.intellij.idea.plugin.hybris.actions.AbstractCopyFileToHybrisConsoleAction; import com.intellij.idea.plugin.hybris.actions.ActionUtils; +import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; +import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.isRequiredFileExtension; +import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.copySelectedFilesToHybris; import static com.intellij.idea.plugin.hybris.common.HybrisConstants.*; -public class CopyImpexFileAction extends AbstractCopyFileToHybrisConsoleAction { +public class CopyImpexFileAction extends AnAction { @Override - public void update(@NotNull final AnActionEvent e) { - final Project project = e.getProject(); + public void update(@NotNull final AnActionEvent event) { + Project project = event.getProject(); if (project != null) { - e.getPresentation() + event.getPresentation() .setEnabledAndVisible(ActionUtils.isHybrisContext(project) && isRequiredFileExtension( project, IMPEX_FILE_EXTENSION, false)); } } @Override - public void actionPerformed(@NotNull final AnActionEvent e) { - final Project project = e.getProject(); + public void actionPerformed(@NotNull final AnActionEvent event) { + Project project = event.getProject(); if (project != null) { - performed(project, IMPEX_CONSOLE_TITLE, getDialogTitle(IMPEX_FILE_EXTENSION)); + copySelectedFilesToHybris(project, IMPEX_CONSOLE_TITLE, IMPEX_FILE_EXTENSION); } } } diff --git a/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.java b/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.java index 621157a7a..d39899f7e 100644 --- a/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.java +++ b/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.java @@ -26,14 +26,16 @@ import static com.intellij.openapi.ui.DialogWrapper.IdeModalityType.PROJECT; -public class CopyFileToHybrisConsoleDialog extends DialogWrapper { +public final class CopyFileToHybrisConsoleDialog extends DialogWrapper { private JPanel contentPane; public CopyFileToHybrisConsoleDialog( - @Nullable final Project project + @Nullable final Project project, + @Nullable final String dialogTitle ) { super(project, false, PROJECT); + setTitle(dialogTitle); init(); } @@ -41,4 +43,10 @@ public CopyFileToHybrisConsoleDialog( protected @Nullable JComponent createCenterPanel() { return contentPane; } + + public void show(final Runnable runnable) { + if(showAndGet()) { + runnable.run(); + } + } } From 1f2a8a35727858e9c94f2962d1b19cac10038ab4 Mon Sep 17 00:00:00 2001 From: Vladyslav_Yanytskyi Date: Wed, 4 May 2022 15:58:59 +0300 Subject: [PATCH 5/5] Refactored code --- .../actions/CopyFileToHybrisConsoleUtils.java | 60 +++++++++++-------- .../actions/CopyFlexibleSearchFileAction.java | 11 ++-- .../file/actions/CopyImpexFileAction.java | 10 ++-- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java b/src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java index ad51f17f4..3d48a70ed 100644 --- a/src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java +++ b/src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java @@ -31,9 +31,12 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.wm.ToolWindowManager; +import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; @@ -52,7 +55,7 @@ public final class CopyFileToHybrisConsoleUtils { private CopyFileToHybrisConsoleUtils() { } - private static Optional cast(R obj, Class clazz) { + private static Optional cast(@NotNull R obj, Class clazz) { if (clazz.isAssignableFrom(obj.getClass())) { return Optional.ofNullable((T) obj); } else { @@ -60,13 +63,16 @@ private static Optional cast(R obj, Class clazz) { } } - public static void copySelectedFilesToHybris(Project project, String consoleTitle, String dialogTitle) { + public static void copySelectedFilesToHybrisConsole(Project project, String consoleTitle, String dialogTitle) { var hybrisConsole = getHybrisConsole(project, consoleTitle); if (hybrisConsole != null) { boolean isConsoleNotEmpty = StringUtils.isNotEmpty(getTextFromHybrisConsole(project, hybrisConsole)); String query = getQueryFromSelectedFiles(project); if (isConsoleNotEmpty) { - var consoleDialog = new CopyFileToHybrisConsoleDialog(project, getDialogTitleFromProperties(dialogTitle)); + var consoleDialog = new CopyFileToHybrisConsoleDialog( + project, + getDialogTitleFromProperties(dialogTitle) + ); consoleDialog.show(() -> copyToHybrisConsole(project, consoleTitle, query)); } else { copyToHybrisConsole(project, consoleTitle, query); @@ -74,28 +80,25 @@ public static void copySelectedFilesToHybris(Project project, String consoleTitl } } - public static boolean isRequiredFileExtension(Project project, String fileExtension, boolean singleFile) { + public static boolean isRequiredSingleFileExtension(Project project, String fileExtension) { var fileExtensions = getFileExtensions(project); - if (singleFile) { - return fileExtensions.size() == 1 && fileExtensions.get(0).equals(fileExtension); - } else { - return !fileExtensions.isEmpty() && fileExtensions.stream().allMatch(fileExtension::equals); - } + return fileExtensions.size() == 1 && fileExtensions.get(0).equals(fileExtension); + } + + public static boolean isRequiredMultipleFileExtension(Project project, String fileExtension) { + var fileExtensions = getFileExtensions(project); + return !fileExtensions.isEmpty() && fileExtensions.stream().allMatch(fileExtension::equals); } private static List getFileExtensions(Project project) { - var names = new ArrayList(); + var extensions = new ArrayList(); for (var virtualFile : getSelectedFiles(project)) { - if (virtualFile != null) { - boolean isNotDirectory = !virtualFile.isDirectory(); - if (isNotDirectory) { - names.add(virtualFile.getExtension()); - } else { - return Collections.emptyList(); - } + if (virtualFile.isDirectory()) { + return Collections.emptyList(); } + extensions.add(virtualFile.getExtension()); } - return names; + return extensions; } private static HybrisConsole getHybrisConsole(Project project, String consoleTitle) { @@ -106,8 +109,8 @@ private static HybrisConsole getHybrisConsole(Project project, String consoleTit private static String getQueryFromSelectedFiles(Project project) { return getSelectedFiles(project).stream() .map(virtualFile -> getPsiFileNode(project, virtualFile)) - .map(psiFile -> psiFile.getText() + lineSeparator()). - collect(Collectors.joining()); + .map(PsiElement::getText) + .collect(Collectors.joining(lineSeparator())); } private static PsiFile getPsiFileNode(Project project, VirtualFile virtualFile) { @@ -116,18 +119,23 @@ private static PsiFile getPsiFileNode(Project project, VirtualFile virtualFile) private static List getSelectedFiles(Project project) { return Arrays.stream(getSelectedTreePaths(project)) - .map(treePath -> cast(treePath.getLastPathComponent(), DefaultMutableTreeNode.class)) - .filter(Optional::isPresent) - .map(lastPathNode -> cast(lastPathNode.get().getUserObject(), ProjectViewNode.class)) + .map(CopyFileToHybrisConsoleUtils::getVirtualFile) .filter(Optional::isPresent) - .map(file -> file.get().getVirtualFile()) + .map(Optional::get) .collect(Collectors.toList()); } + @NotNull + private static Optional getVirtualFile(final TreePath treePath) { + return cast(treePath.getLastPathComponent(), DefaultMutableTreeNode.class) + .flatMap(lastPathNode -> cast(lastPathNode.getUserObject(), ProjectViewNode.class)) + .map(ProjectViewNode::getVirtualFile); + } + private static TreePath[] getSelectedTreePaths(final Project project) { var currentProjectViewPane = ProjectView.getInstance(project).getCurrentProjectViewPane(); var selectionPaths = currentProjectViewPane.getSelectionPaths(); - return selectionPaths != null ? selectionPaths : new TreePath[0]; + return ObjectUtils.getIfNull(selectionPaths, () -> new TreePath[0]); } private static HybrisConsolePanel getHybrisConsolePanel(Project project) { @@ -161,4 +169,4 @@ private static void openHybrisConsole(Project project) { private static String getDialogTitleFromProperties(String fileExtension) { return HybrisI18NBundleUtils.message(DIALOG_TITLE + fileExtension); } -} +} \ No newline at end of file diff --git a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java index b63bf069b..7937c87d3 100644 --- a/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java +++ b/src/com/intellij/idea/plugin/hybris/flexibleSearch/file/actions/CopyFlexibleSearchFileAction.java @@ -24,8 +24,8 @@ import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; -import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.isRequiredFileExtension; -import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.copySelectedFilesToHybris; +import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.copySelectedFilesToHybrisConsole; +import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.isRequiredSingleFileExtension; import static com.intellij.idea.plugin.hybris.common.HybrisConstants.*; public class CopyFlexibleSearchFileAction extends AnAction { @@ -34,8 +34,9 @@ public class CopyFlexibleSearchFileAction extends AnAction { public void update(@NotNull final AnActionEvent event) { Project project = event.getProject(); if (project != null) { - event.getPresentation().setEnabledAndVisible(ActionUtils.isHybrisContext(project) && isRequiredFileExtension( - project, FLEXIBLE_SEARCH_FILE_EXTENSION, true)); + event.getPresentation() + .setEnabledAndVisible(ActionUtils.isHybrisContext(project) && isRequiredSingleFileExtension( + project, FLEXIBLE_SEARCH_FILE_EXTENSION)); } } @@ -43,7 +44,7 @@ public void update(@NotNull final AnActionEvent event) { public void actionPerformed(@NotNull final AnActionEvent event) { Project project = event.getProject(); if (project != null) { - copySelectedFilesToHybris(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, FLEXIBLE_SEARCH_FILE_EXTENSION); + copySelectedFilesToHybrisConsole(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, FLEXIBLE_SEARCH_FILE_EXTENSION); } } } diff --git a/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java b/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java index f02b22cdc..e278ab1c2 100644 --- a/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java +++ b/src/com/intellij/idea/plugin/hybris/impex/file/actions/CopyImpexFileAction.java @@ -6,8 +6,8 @@ import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; -import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.isRequiredFileExtension; -import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.copySelectedFilesToHybris; +import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.copySelectedFilesToHybrisConsole; +import static com.intellij.idea.plugin.hybris.actions.CopyFileToHybrisConsoleUtils.isRequiredMultipleFileExtension; import static com.intellij.idea.plugin.hybris.common.HybrisConstants.*; public class CopyImpexFileAction extends AnAction { @@ -17,8 +17,8 @@ public void update(@NotNull final AnActionEvent event) { Project project = event.getProject(); if (project != null) { event.getPresentation() - .setEnabledAndVisible(ActionUtils.isHybrisContext(project) && isRequiredFileExtension( - project, IMPEX_FILE_EXTENSION, false)); + .setEnabledAndVisible(ActionUtils.isHybrisContext(project) && isRequiredMultipleFileExtension( + project, IMPEX_FILE_EXTENSION)); } } @@ -26,7 +26,7 @@ public void update(@NotNull final AnActionEvent event) { public void actionPerformed(@NotNull final AnActionEvent event) { Project project = event.getProject(); if (project != null) { - copySelectedFilesToHybris(project, IMPEX_CONSOLE_TITLE, IMPEX_FILE_EXTENSION); + copySelectedFilesToHybrisConsole(project, IMPEX_CONSOLE_TITLE, IMPEX_FILE_EXTENSION); } } }