diff options
author | Tor Norbye <tnorbye@google.com> | 2014-08-21 00:31:02 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-08-16 04:55:08 +0000 |
commit | 9cde0e3c015174898df8b8f3672185941fad4786 (patch) | |
tree | 80a55c7b59c38377216daaada4e8bc47b69ceb9a /plugins/properties/src/com/intellij/lang | |
parent | 3b37877a2561bf9fbe072253a18688807d523505 (diff) | |
parent | d76e3920c56d37c942092b7dca20fcaded81c0a5 (diff) | |
download | idea-9cde0e3c015174898df8b8f3672185941fad4786.tar.gz |
Merge "Merge remote-tracking branch 'aosp/upstream-master' into merge"
Diffstat (limited to 'plugins/properties/src/com/intellij/lang')
6 files changed, 309 insertions, 27 deletions
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/ResourceBundleAsVirtualFile.java b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleAsVirtualFile.java new file mode 100644 index 000000000000..36915e7e6962 --- /dev/null +++ b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleAsVirtualFile.java @@ -0,0 +1,183 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @author Alexey + */ +package com.intellij.lang.properties.editor; + +import com.intellij.ide.presentation.Presentation; +import com.intellij.lang.properties.PropertiesImplUtil; +import com.intellij.lang.properties.PropertiesUtil; +import com.intellij.lang.properties.ResourceBundle; +import com.intellij.lang.properties.psi.PropertiesFile; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileSystem; +import com.intellij.psi.PsiManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +@Presentation(icon = "AllIcons.Nodes.ResourceBundle") +public class ResourceBundleAsVirtualFile extends VirtualFile { + private final VirtualFile myBasePropertiesFile; + + private ResourceBundleAsVirtualFile(@NotNull final VirtualFile basePropertiesFile) { + myBasePropertiesFile = basePropertiesFile; + } + + @NotNull + public static ResourceBundleAsVirtualFile fromResourceBundle(final @NotNull ResourceBundle resourceBundle) { + return new ResourceBundleAsVirtualFile(resourceBundle.getDefaultPropertiesFile().getVirtualFile()); + } + + @Nullable + public ResourceBundle getResourceBundle(final Project project) { + final PsiManager psiManager = PsiManager.getInstance(project); + final PropertiesFile file = PropertiesImplUtil.getPropertiesFile(psiManager.findFile(myBasePropertiesFile)); + if (file == null) { + return null; + } + return PropertiesImplUtil.getResourceBundle(file); + } + + @Override + @NotNull + public VirtualFileSystem getFileSystem() { + return LocalFileSystem.getInstance(); + } + + @Override + @NotNull + public String getPath() { + return getName(); + } + + @Override + @NotNull + public String getName() { + return PropertiesUtil.getBaseName(myBasePropertiesFile); + } + + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + final ResourceBundleAsVirtualFile resourceBundleAsVirtualFile = (ResourceBundleAsVirtualFile)o; + + if (!myBasePropertiesFile.equals(resourceBundleAsVirtualFile.myBasePropertiesFile)) return false; + + return true; + } + + public int hashCode() { + return myBasePropertiesFile.hashCode(); + } + + @Override + public void rename(Object requestor, @NotNull String newName) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isWritable() { + return true; + } + + @Override + public boolean isDirectory() { + return false; + } + + @Override + public boolean isValid() { + return true; + } + + @Override + public VirtualFile getParent() { + return myBasePropertiesFile.getParent(); + } + + @Override + public VirtualFile[] getChildren() { + return EMPTY_ARRAY; + } + + @NotNull + @Override + public VirtualFile createChildDirectory(Object requestor, @NotNull String name) throws IOException { + throw new UnsupportedOperationException(); + } + + @NotNull + @Override + public VirtualFile createChildData(Object requestor, @NotNull String name) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void delete(Object requestor) throws IOException { + //todo + } + + @Override + public void move(Object requestor, @NotNull VirtualFile newParent) throws IOException { + //todo + } + + @Override + public InputStream getInputStream() throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + @NotNull + public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + @NotNull + public byte[] contentsToByteArray() throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public long getModificationStamp() { + return 0; + } + + @Override + public long getTimeStamp() { + return 0; + } + + @Override + public long getLength() { + return 0; + } + + @Override + public void refresh(boolean asynchronous, boolean recursive, Runnable postRunnable) { + + } +} 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..39e9ed67e7b2 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())) { @@ -167,7 +170,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements FileEdit TreeElement[] children = myStructureViewComponent.getTreeModel().getRoot().getChildren(); if (children.length != 0) { TreeElement child = children[0]; - String propName = ((ResourceBundlePropertyStructureViewElement)child).getValue(); + String propName = ((ResourceBundlePropertyStructureViewElement)child).getValue().getUnescapedKey(); setState(new ResourceBundleEditorState(propName)); } myDataProviderPanel = new DataProviderPanel(splitPanel); @@ -245,7 +248,7 @@ public class ResourceBundleEditor extends UserDataHolderBase implements FileEdit DefaultMutableTreeNode node = toCheck.pop(); final ResourceBundleEditorViewElement element = getSelectedElement(node); String value = element instanceof ResourceBundlePropertyStructureViewElement - ? ((ResourceBundlePropertyStructureViewElement)element).getValue() + ? ((ResourceBundlePropertyStructureViewElement)element).getValue().getUnescapedKey() : null; if (propertyName.equals(value)) { nodeToSelect = node; @@ -570,13 +573,21 @@ public class ResourceBundleEditor extends UserDataHolderBase implements FileEdit document.replaceString(0, document.getTextLength(), text); } - @Nullable - private DefaultMutableTreeNode getSelectedNode() { + @NotNull + private Collection<DefaultMutableTreeNode> 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<TreePath, DefaultMutableTreeNode>() { + @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<DefaultMutableTreeNode> 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<ResourceBundleEditorViewElement> getSelectedElements() { + final Collection<DefaultMutableTreeNode> selectedNodes = getSelectedNodes(); + return ContainerUtil.mapNotNull(selectedNodes, new NullableFunction<DefaultMutableTreeNode, ResourceBundleEditorViewElement>() { + @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<ResourceBundleEditorViewElement> 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<ResourceBundleEditorViewElement> selectedElements = ((ResourceBundleEditor)getFileEditor()).getSelectedElements(); + if (selectedElements.isEmpty()) { + return null; + } else if (selectedElements.size() == 1) { + return ContainerUtil.getFirstItem(selectedElements).getPsiElements(); + } else { + final List<PsiElement> psiElements = new ArrayList<PsiElement>(); + 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/editor/ResourceBundleUtil.java b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleUtil.java new file mode 100644 index 000000000000..e4a35e7e677c --- /dev/null +++ b/plugins/properties/src/com/intellij/lang/properties/editor/ResourceBundleUtil.java @@ -0,0 +1,69 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.lang.properties.editor; + +import com.intellij.lang.properties.IProperty; +import com.intellij.lang.properties.ResourceBundle; +import com.intellij.lang.properties.psi.PropertiesFile; +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.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Denis Zhdanov + * @since 10/5/11 2:35 PM + */ +public class ResourceBundleUtil { + + private ResourceBundleUtil() { + } + + /** + * Tries to derive {@link com.intellij.lang.properties.ResourceBundle resource bundle} related to the given context. + * + * @param dataContext target context + * @return {@link com.intellij.lang.properties.ResourceBundle resource bundle} related to the given context if any; + * <code>null</code> otherwise + */ + @Nullable + public static ResourceBundle getResourceBundleFromDataContext(@NotNull DataContext dataContext) { + PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext); + if (element instanceof IProperty) return null; //rename property + final ResourceBundle[] bundles = ResourceBundle.ARRAY_DATA_KEY.getData(dataContext); + if (bundles != null && bundles.length == 1) return bundles[0]; + VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext); + if (virtualFile == null) { + return null; + } + final Project project = CommonDataKeys.PROJECT.getData(dataContext); + if (virtualFile instanceof ResourceBundleAsVirtualFile && project != null) { + return ((ResourceBundleAsVirtualFile)virtualFile).getResourceBundle(project); + } + if (project != null) { + final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile); + if (psiFile instanceof PropertiesFile) { + return ((PropertiesFile)psiFile).getResourceBundle(); + } + } + return null; + } +} 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 |