diff --git a/resources/META-INF/lang-optional-dependencies.xml b/resources/META-INF/lang-optional-dependencies.xml index 4b35b86d9..a6bc3e7bf 100644 --- a/resources/META-INF/lang-optional-dependencies.xml +++ b/resources/META-INF/lang-optional-dependencies.xml @@ -269,7 +269,20 @@ - + + + + + + + + + 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/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/actions/CopyFileToHybrisConsoleUtils.java b/src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java new file mode 100644 index 000000000..3d48a70ed --- /dev/null +++ b/src/com/intellij/idea/plugin/hybris/actions/CopyFileToHybrisConsoleUtils.java @@ -0,0 +1,172 @@ +/* + * 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.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; +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(@NotNull R obj, Class clazz) { + if (clazz.isAssignableFrom(obj.getClass())) { + return Optional.ofNullable((T) obj); + } else { + return Optional.empty(); + } + } + + 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) + ); + consoleDialog.show(() -> copyToHybrisConsole(project, consoleTitle, query)); + } else { + copyToHybrisConsole(project, consoleTitle, query); + } + } + } + + public static boolean isRequiredSingleFileExtension(Project project, String fileExtension) { + var fileExtensions = getFileExtensions(project); + 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 extensions = new ArrayList(); + for (var virtualFile : getSelectedFiles(project)) { + if (virtualFile.isDirectory()) { + return Collections.emptyList(); + } + extensions.add(virtualFile.getExtension()); + } + return extensions; + } + + 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(PsiElement::getText) + .collect(Collectors.joining(lineSeparator())); + } + + 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(CopyFileToHybrisConsoleUtils::getVirtualFile) + .filter(Optional::isPresent) + .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 ObjectUtils.getIfNull(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); + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..7937c87d3 --- /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.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.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 { + + @Override + public void update(@NotNull final AnActionEvent event) { + Project project = event.getProject(); + if (project != null) { + event.getPresentation() + .setEnabledAndVisible(ActionUtils.isHybrisContext(project) && isRequiredSingleFileExtension( + project, FLEXIBLE_SEARCH_FILE_EXTENSION)); + } + } + + @Override + public void actionPerformed(@NotNull final AnActionEvent event) { + Project project = event.getProject(); + if (project != null) { + copySelectedFilesToHybrisConsole(project, FLEXIBLE_SEARCH_CONSOLE_TITLE, 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 +
+ + + + + + + + + + + + + + + + + + + + + + + +
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..d39899f7e --- /dev/null +++ b/src/com/intellij/idea/plugin/hybris/toolwindow/CopyFileToHybrisConsoleDialog.java @@ -0,0 +1,52 @@ +/* + * 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 final class CopyFileToHybrisConsoleDialog extends DialogWrapper { + + private JPanel contentPane; + + public CopyFileToHybrisConsoleDialog( + @Nullable final Project project, + @Nullable final String dialogTitle + ) { + super(project, false, PROJECT); + setTitle(dialogTitle); + init(); + } + + @Override + protected @Nullable JComponent createCenterPanel() { + return contentPane; + } + + public void show(final Runnable runnable) { + if(showAndGet()) { + runnable.run(); + } + } +}