mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
resource bundles: CombinePropertiesFilesAction improved to combine resource bundles with properties files to single resource bundle
This commit is contained in:
+7
@@ -26,6 +26,7 @@ import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
@@ -122,6 +123,12 @@ public class PropertiesImplUtil extends PropertiesUtil {
|
||||
return file instanceof PropertiesFile ? (PropertiesFile)file : XmlPropertiesFileImpl.getPropertiesFile(file);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PropertiesFile getPropertiesFile(@Nullable PsiElement element) {
|
||||
if (!(element instanceof PsiFile)) return null;
|
||||
return getPropertiesFile((PsiFile)element);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<IProperty> findPropertiesByKey(@NotNull final Project project, @NotNull final String key) {
|
||||
final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
|
||||
|
||||
+42
-19
@@ -32,11 +32,10 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.InputValidatorEx;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
@@ -49,7 +48,16 @@ public class CombinePropertiesFilesAction extends AnAction {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(final AnActionEvent e) {
|
||||
final List<PropertiesFile> propertiesFiles = getPropertiesFiles(e);
|
||||
final List<PropertiesFile> initialPropertiesFiles = getPropertiesFiles(e);
|
||||
final List<PropertiesFile> propertiesFiles = initialPropertiesFiles == null ? new ArrayList<PropertiesFile>()
|
||||
: new ArrayList<PropertiesFile>(initialPropertiesFiles);
|
||||
final List<ResourceBundle> resourceBundles = getResourceBundles(e);
|
||||
if (resourceBundles != null) {
|
||||
for (ResourceBundle bundle : resourceBundles) {
|
||||
propertiesFiles.addAll(bundle.getPropertiesFiles());
|
||||
}
|
||||
}
|
||||
|
||||
final String newBaseName = Messages.showInputDialog(propertiesFiles.get(0).getProject(),
|
||||
PropertiesBundle.message("combine.properties.files.prompt.text"),
|
||||
PropertiesBundle.message("combine.properties.files.title"),
|
||||
@@ -58,7 +66,20 @@ public class CombinePropertiesFilesAction extends AnAction {
|
||||
new MyInputValidator(propertiesFiles));
|
||||
if (newBaseName != null) {
|
||||
final Project project = propertiesFiles.get(0).getProject();
|
||||
ResourceBundleManager.getInstance(project).combineToResourceBundle(propertiesFiles, newBaseName);
|
||||
|
||||
final Set<ResourceBundle> uniqueBundlesToDissociate = new HashSet<ResourceBundle>();
|
||||
for (PropertiesFile file : propertiesFiles) {
|
||||
final ResourceBundle resourceBundle = file.getResourceBundle();
|
||||
if (resourceBundle.getPropertiesFiles().size() != 1) {
|
||||
uniqueBundlesToDissociate.add(resourceBundle);
|
||||
}
|
||||
}
|
||||
final ResourceBundleManager resourceBundleManager = ResourceBundleManager.getInstance(project);
|
||||
for (ResourceBundle resourceBundle : uniqueBundlesToDissociate) {
|
||||
resourceBundleManager.dissociateResourceBundle(resourceBundle);
|
||||
}
|
||||
|
||||
resourceBundleManager.combineToResourceBundle(propertiesFiles, newBaseName);
|
||||
final ResourceBundle resourceBundle = propertiesFiles.get(0).getResourceBundle();
|
||||
FileEditorManager.getInstance(project).openFile(new ResourceBundleAsVirtualFile(resourceBundle), true);
|
||||
ProjectView.getInstance(project).refresh();
|
||||
@@ -67,17 +88,22 @@ public class CombinePropertiesFilesAction extends AnAction {
|
||||
|
||||
@Override
|
||||
public void update(final AnActionEvent e) {
|
||||
final List<PropertiesFile> propertiesFiles = getPropertiesFiles(e);
|
||||
boolean isAvailable = propertiesFiles != null && propertiesFiles.size() > 1;
|
||||
if (isAvailable) {
|
||||
for (PropertiesFile propertiesFile : propertiesFiles) {
|
||||
if (propertiesFile.getResourceBundle().getPropertiesFiles().size() != 1) {
|
||||
isAvailable = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
final Collection<PropertiesFile> propertiesFiles = getPropertiesFiles(e);
|
||||
final List<ResourceBundle> resourceBundles = getResourceBundles(e);
|
||||
int elementCount = 0;
|
||||
if (propertiesFiles != null) {
|
||||
elementCount += propertiesFiles.size();
|
||||
}
|
||||
e.getPresentation().setVisible(isAvailable);
|
||||
if (resourceBundles != null) {
|
||||
elementCount += resourceBundles.size();
|
||||
}
|
||||
e.getPresentation().setEnabledAndVisible(elementCount > 1);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static List<ResourceBundle> getResourceBundles(AnActionEvent e) {
|
||||
final ResourceBundle[] resourceBundles = e.getData(ResourceBundle.ARRAY_DATA_KEY);
|
||||
return resourceBundles == null ? null : ContainerUtil.newArrayList(resourceBundles);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -88,10 +114,7 @@ public class CombinePropertiesFilesAction extends AnAction {
|
||||
}
|
||||
final List<PropertiesFile> files = new ArrayList<PropertiesFile>(psiElements.length);
|
||||
for (PsiElement psiElement : psiElements) {
|
||||
if (!(psiElement instanceof PsiFile)) {
|
||||
return null;
|
||||
}
|
||||
final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile((PsiFile)psiElement);
|
||||
final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(psiElement);
|
||||
if (propertiesFile == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
+7
-10
@@ -31,14 +31,13 @@ import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiFileSystemItem;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.HashSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Dmitry Batkovich
|
||||
@@ -116,13 +115,11 @@ public class DissociateResourceBundleAction extends AnAction {
|
||||
final PsiElement[] psiElements = event.getData(LangDataKeys.PSI_ELEMENT_ARRAY);
|
||||
if (psiElements != null) {
|
||||
for (PsiElement element : psiElements) {
|
||||
if (element instanceof PsiFile) {
|
||||
final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile((PsiFile)element);
|
||||
if (propertiesFile != null) {
|
||||
final ResourceBundle bundle = propertiesFile.getResourceBundle();
|
||||
if (bundle.getPropertiesFiles().size() > 1) {
|
||||
targetResourceBundles.add(bundle);
|
||||
}
|
||||
final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(element);
|
||||
if (propertiesFile != null) {
|
||||
final ResourceBundle bundle = propertiesFile.getResourceBundle();
|
||||
if (bundle.getPropertiesFiles().size() > 1) {
|
||||
targetResourceBundles.add(bundle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user