update copyright
[fedora-idea.git] / plugins / properties / src / com / intellij / lang / properties / projectView / ResourceBundleGrouper.java
blobee8c396dab9eeedaf310c8943bb0f17b9351b9d9
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.lang.properties.projectView;
18 import com.intellij.ide.projectView.TreeStructureProvider;
19 import com.intellij.ide.projectView.ViewSettings;
20 import com.intellij.ide.util.treeView.AbstractTreeNode;
21 import com.intellij.lang.properties.ResourceBundle;
22 import com.intellij.lang.properties.editor.ResourceBundleAsVirtualFile;
23 import com.intellij.lang.properties.psi.PropertiesFile;
24 import com.intellij.openapi.actionSystem.DataConstants;
25 import com.intellij.openapi.actionSystem.ex.DataConstantsEx;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.project.DumbAware;
28 import com.intellij.psi.PsiElement;
29 import com.intellij.util.SmartList;
30 import gnu.trove.THashMap;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.Map;
37 public class ResourceBundleGrouper implements TreeStructureProvider, DumbAware {
38 private final Project myProject;
40 public ResourceBundleGrouper(Project project) {
41 myProject = project;
44 public Collection<AbstractTreeNode> modify(AbstractTreeNode parent, Collection<AbstractTreeNode> children, ViewSettings settings) {
45 if (parent instanceof ResourceBundleNode) return children;
47 Map<ResourceBundle,Collection<PropertiesFile>> childBundles = new THashMap<ResourceBundle, Collection<PropertiesFile>>();
48 for (AbstractTreeNode child : children) {
49 Object f = child.getValue();
50 if (f instanceof PropertiesFile) {
51 PropertiesFile propertiesFile = (PropertiesFile)f;
52 ResourceBundle bundle = propertiesFile.getResourceBundle();
53 Collection<PropertiesFile> files = childBundles.get(bundle);
54 if (files == null) {
55 files = new SmartList<PropertiesFile>();
56 childBundles.put(bundle, files);
58 files.add(propertiesFile);
62 List<AbstractTreeNode> result = new ArrayList<AbstractTreeNode>();
63 for (Map.Entry<ResourceBundle, Collection<PropertiesFile>> entry : childBundles.entrySet()) {
64 ResourceBundle resourceBundle = entry.getKey();
65 Collection<PropertiesFile> files = entry.getValue();
66 if (files.size() != 1) {
67 result.add(new ResourceBundleNode(myProject, resourceBundle, settings));
70 for (AbstractTreeNode child : children) {
71 Object f = child.getValue();
72 if (f instanceof PropertiesFile) {
73 PropertiesFile propertiesFile = (PropertiesFile)f;
74 ResourceBundle bundle = propertiesFile.getResourceBundle();
75 if (childBundles.get(bundle).size() != 1) {
76 continue;
79 result.add(child);
82 return result;
85 public Object getData(Collection<AbstractTreeNode> selected, String dataName) {
86 if (selected == null) return null;
87 for (AbstractTreeNode selectedElement : selected) {
88 Object element = selectedElement.getValue();
89 if (DataConstants.VIRTUAL_FILE.equals(dataName)) {
90 if (element instanceof ResourceBundle) {
91 return new ResourceBundleAsVirtualFile((ResourceBundle)element);
94 if (DataConstants.PSI_ELEMENT_ARRAY.equals(dataName)) {
95 if (element instanceof ResourceBundle) {
96 List<PropertiesFile> propertiesFiles = ((ResourceBundle)element).getPropertiesFiles(myProject);
97 return propertiesFiles.toArray(PsiElement.EMPTY_ARRAY);
100 if (DataConstants.DELETE_ELEMENT_PROVIDER.equals(dataName)) {
101 if (element instanceof ResourceBundle) {
102 return new ResourceBundleDeleteProvider((ResourceBundle)element);
106 if (DataConstantsEx.RESOURCE_BUNDLE_ARRAY.equals(dataName)) {
107 final List<ResourceBundle> selectedElements = new ArrayList<ResourceBundle>();
108 for (AbstractTreeNode node : selected) {
109 final Object value = node.getValue();
110 if (value instanceof ResourceBundle) {
111 selectedElements.add((ResourceBundle)value);
114 return selectedElements.isEmpty() ? null : selectedElements.toArray(new ResourceBundle[selectedElements.size()]);
116 return null;