From bd61c66418db3265d3ba603fcdd6438d74e90fc8 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Mon, 18 Aug 2014 11:34:36 +0400 Subject: [PATCH] resource bundle editor: delete properties on multiple selection fixed --- .../ResourceBundleEditorViewElement.java | 2 - .../properties/editor/NewPropertyAction.java | 2 +- .../editor/ResourceBundleEditor.java | 58 +++++++++++++------ .../ResourceBundleStructureViewComponent.java | 18 ++++-- ...ResourceBundleFromEditorRenameHandler.java | 2 +- 5 files changed, 55 insertions(+), 27 deletions(-) diff --git a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/editor/ResourceBundleEditorViewElement.java b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/editor/ResourceBundleEditorViewElement.java index 58932d1fd4c5..8a8d3b0390d4 100644 --- a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/editor/ResourceBundleEditorViewElement.java +++ b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/editor/ResourceBundleEditorViewElement.java @@ -16,9 +16,7 @@ package com.intellij.lang.properties.editor; -import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; -import org.jetbrains.annotations.NotNull; /** * @author Dmitry Batkovich diff --git a/plugins/properties/src/com/intellij/lang/properties/editor/NewPropertyAction.java b/plugins/properties/src/com/intellij/lang/properties/editor/NewPropertyAction.java index d25b780bd769..2218b7598884 100644 --- a/plugins/properties/src/com/intellij/lang/properties/editor/NewPropertyAction.java +++ b/plugins/properties/src/com/intellij/lang/properties/editor/NewPropertyAction.java @@ -60,7 +60,7 @@ class NewPropertyAction extends AnAction { prefix = null; separator = null; } else { - final ResourceBundleEditorViewElement selectedElement = resourceBundleEditor.getSelectedElement(); + final ResourceBundleEditorViewElement selectedElement = resourceBundleEditor.getSelectedElementIfOnlyOne(); if (selectedElement == null) { return; } diff --git a/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditor.java b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditor.java index e9eb9e9f72d2..0339b09b8c88 100644 --- a/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditor.java +++ b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleEditor.java @@ -57,7 +57,10 @@ import com.intellij.ui.JBColor; import com.intellij.ui.JBSplitter; import com.intellij.ui.components.JBScrollPane; import com.intellij.util.Alarm; +import com.intellij.util.Function; import com.intellij.util.IncorrectOperationException; +import com.intellij.util.NullableFunction; +import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.ContainerUtilRt; import com.intellij.util.containers.Stack; import com.intellij.util.ui.UIUtil; @@ -129,7 +132,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements FileEdit @Override public void valueChanged(TreeSelectionEvent e) { // filter out temp unselect/select events - if (getSelectedProperty() == null) return; + if (Comparing.equal(e.getNewLeadSelectionPath(), e.getOldLeadSelectionPath()) || getSelectedProperty() == null) return; if (!arePropertiesEquivalent(selectedProperty, getSelectedProperty()) || !Comparing.equal(selectedPropertiesFile, getSelectedPropertiesFile())) { @@ -570,13 +573,21 @@ public class ResourceBundleEditor extends UserDataHolderBase implements FileEdit document.replaceString(0, document.getTextLength(), text); } - @Nullable - private DefaultMutableTreeNode getSelectedNode() { + @NotNull + private Collection getSelectedNodes() { + if (!isValid()) { + return Collections.emptyList(); + } JTree tree = myStructureViewComponent.getTree(); - if (tree == null) return null; - TreePath selected = tree.getSelectionModel().getSelectionPath(); - if (selected == null) return null; - return (DefaultMutableTreeNode)selected.getLastPathComponent(); + if (tree == null) return Collections.emptyList(); + TreePath[] selected = tree.getSelectionModel().getSelectionPaths(); + if (selected == null || selected.length == 0) return Collections.emptyList(); + return ContainerUtil.map(selected, new Function() { + @Override + public DefaultMutableTreeNode fun(TreePath treePath) { + return (DefaultMutableTreeNode)treePath.getLastPathComponent(); + } + }); } @Nullable @@ -587,25 +598,34 @@ public class ResourceBundleEditor extends UserDataHolderBase implements FileEdit @Nullable private IProperty getSelectedProperty() { - final DefaultMutableTreeNode selectedNode = getSelectedNode(); - if (selectedNode == null) { + final Collection selectedNode = getSelectedNodes(); + if (selectedNode.isEmpty()) { return null; } - final ResourceBundleEditorViewElement element = getSelectedElement(selectedNode); + final ResourceBundleEditorViewElement element = getSelectedElement(ContainerUtil.getFirstItem(selectedNode)); return element instanceof ResourceBundlePropertyStructureViewElement ? ((ResourceBundlePropertyStructureViewElement)element).getProperty() : null; } + @NotNull + public Collection getSelectedElements() { + final Collection selectedNodes = getSelectedNodes(); + return ContainerUtil.mapNotNull(selectedNodes, new NullableFunction() { + @Nullable + @Override + public ResourceBundleEditorViewElement fun(DefaultMutableTreeNode selectedNode) { + Object userObject = selectedNode.getUserObject(); + if (!(userObject instanceof AbstractTreeNode)) return null; + Object value = ((AbstractTreeNode)userObject).getValue(); + return value instanceof ResourceBundleEditorViewElement ? (ResourceBundleEditorViewElement) value : null; + } + }); + } + @Nullable - public ResourceBundleEditorViewElement getSelectedElement() { - final DefaultMutableTreeNode selectedNode = getSelectedNode(); - if (selectedNode == null) { - return null; - } - Object userObject = selectedNode.getUserObject(); - if (!(userObject instanceof AbstractTreeNode)) return null; - Object value = ((AbstractTreeNode)userObject).getValue(); - return value instanceof ResourceBundleEditorViewElement ? (ResourceBundleEditorViewElement) value : null; + public ResourceBundleEditorViewElement getSelectedElementIfOnlyOne() { + final Collection selectedElements = getSelectedElements(); + return selectedElements.size() == 1 ? ContainerUtil.getFirstItem(selectedElements) : null; } @Override diff --git a/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleStructureViewComponent.java b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleStructureViewComponent.java index 0985f35a5e12..da376abe05b2 100644 --- a/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleStructureViewComponent.java +++ b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleStructureViewComponent.java @@ -25,7 +25,6 @@ import com.intellij.lang.properties.psi.Property; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.ide.CopyPasteManager; -import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiNamedElement; @@ -34,10 +33,13 @@ import com.intellij.refactoring.safeDelete.SafeDeleteHandler; import com.intellij.ui.PopupHandler; import com.intellij.usages.UsageTarget; import com.intellij.usages.UsageView; +import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import java.awt.datatransfer.StringSelection; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; /** @@ -74,9 +76,17 @@ class ResourceBundleStructureViewComponent extends PropertiesGroupingStructureVi } else if (PlatformDataKeys.FILE_EDITOR.is(dataId)) { return getFileEditor(); } else if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) { - final ResourceBundleEditorViewElement selectedElement = ((ResourceBundleEditor)getFileEditor()).getSelectedElement(); - if (selectedElement != null) { - return selectedElement.getPsiElements(); + final Collection selectedElements = ((ResourceBundleEditor)getFileEditor()).getSelectedElements(); + if (selectedElements.isEmpty()) { + return null; + } else if (selectedElements.size() == 1) { + return ContainerUtil.getFirstItem(selectedElements).getPsiElements(); + } else { + final List psiElements = new ArrayList(); + for (ResourceBundleEditorViewElement selectedElement : selectedElements) { + Collections.addAll(psiElements, selectedElement.getPsiElements()); + } + return psiElements.toArray(new PsiElement[psiElements.size()]); } } else if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) { final PsiElement[] psiElements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(this); diff --git a/plugins/properties/src/com/intellij/lang/properties/refactoring/rename/ResourceBundleFromEditorRenameHandler.java b/plugins/properties/src/com/intellij/lang/properties/refactoring/rename/ResourceBundleFromEditorRenameHandler.java index 0c04bec80620..e105195cb9b3 100644 --- a/plugins/properties/src/com/intellij/lang/properties/refactoring/rename/ResourceBundleFromEditorRenameHandler.java +++ b/plugins/properties/src/com/intellij/lang/properties/refactoring/rename/ResourceBundleFromEditorRenameHandler.java @@ -74,7 +74,7 @@ public class ResourceBundleFromEditorRenameHandler implements RenameHandler { public void invoke(final @NotNull Project project, Editor editor, final PsiFile file, DataContext dataContext) { final ResourceBundleEditor resourceBundleEditor = (ResourceBundleEditor)PlatformDataKeys.FILE_EDITOR.getData(dataContext); assert resourceBundleEditor != null; - final ResourceBundleEditorViewElement selectedElement = resourceBundleEditor.getSelectedElement(); + final ResourceBundleEditorViewElement selectedElement = resourceBundleEditor.getSelectedElementIfOnlyOne(); if (selectedElement != null) { CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() { @Override