Search in sources :

Example 1 with PropertiesReferenceManager

use of com.intellij.lang.properties.PropertiesReferenceManager in project intellij-community by JetBrains.

the class I18nUtil method propertiesFilesByBundleName.

@NotNull
public static List<PropertiesFile> propertiesFilesByBundleName(final String resourceBundleName, final PsiElement context) {
    PsiFile containingFile = context.getContainingFile();
    PsiElement containingFileContext = InjectedLanguageManager.getInstance(containingFile.getProject()).getInjectionHost(containingFile);
    if (containingFileContext != null)
        containingFile = containingFileContext.getContainingFile();
    VirtualFile virtualFile = containingFile.getVirtualFile();
    if (virtualFile == null) {
        virtualFile = containingFile.getOriginalFile().getVirtualFile();
    }
    if (virtualFile != null) {
        Project project = containingFile.getProject();
        final Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(virtualFile);
        if (module != null) {
            PropertiesReferenceManager refManager = PropertiesReferenceManager.getInstance(project);
            return refManager.findPropertiesFiles(module, resourceBundleName);
        }
    }
    return Collections.emptyList();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) PropertiesReferenceManager(com.intellij.lang.properties.PropertiesReferenceManager) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PropertiesReferenceManager

use of com.intellij.lang.properties.PropertiesReferenceManager in project intellij-community by JetBrains.

the class FormEditingUtil method collectUsedLocales.

public static Locale[] collectUsedLocales(final Module module, final IRootContainer rootContainer) {
    final Set<Locale> locales = new HashSet<>();
    final PropertiesReferenceManager propManager = PropertiesReferenceManager.getInstance(module.getProject());
    for (String bundleName : collectUsedBundleNames(rootContainer)) {
        List<PropertiesFile> propFiles = propManager.findPropertiesFiles(module, bundleName.replace('/', '.'));
        for (PropertiesFile propFile : propFiles) {
            locales.add(propFile.getLocale());
        }
    }
    return locales.toArray(new Locale[locales.size()]);
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PropertiesReferenceManager(com.intellij.lang.properties.PropertiesReferenceManager) HashSet(com.intellij.util.containers.HashSet)

Example 3 with PropertiesReferenceManager

use of com.intellij.lang.properties.PropertiesReferenceManager in project intellij-community by JetBrains.

the class PreviewFormAction method showPreviewFrame.

private static void showPreviewFrame(@NotNull final Module module, @NotNull final VirtualFile formFile, @Nullable final Locale stringDescriptorLocale) {
    final String tempPath;
    try {
        final File tempDirectory = FileUtil.createTempDirectory("FormPreview", "");
        tempPath = tempDirectory.getAbsolutePath();
        CopyResourcesUtil.copyFormsRuntime(tempPath, true);
    } catch (IOException e) {
        Messages.showErrorDialog(module.getProject(), UIDesignerBundle.message("error.cannot.preview.form", formFile.getPath().replace('/', File.separatorChar), e.toString()), CommonBundle.getErrorTitle());
        return;
    }
    final PathsList sources = OrderEnumerator.orderEntries(module).withoutSdk().withoutLibraries().withoutDepModules().getSourcePathsList();
    final String classPath = OrderEnumerator.orderEntries(module).recursively().getPathsList().getPathsString() + File.pathSeparator + sources.getPathsString() + File.pathSeparator + /* resources bundles */
    tempPath;
    final InstrumentationClassFinder finder = createClassFinder(classPath);
    try {
        final Document doc = FileDocumentManager.getInstance().getDocument(formFile);
        final LwRootContainer rootContainer;
        try {
            rootContainer = Utils.getRootContainer(doc.getText(), new CompiledClassPropertiesProvider(finder.getLoader()));
        } catch (Exception e) {
            Messages.showErrorDialog(module.getProject(), UIDesignerBundle.message("error.cannot.read.form", formFile.getPath().replace('/', File.separatorChar), e.getMessage()), CommonBundle.getErrorTitle());
            return;
        }
        if (rootContainer.getComponentCount() == 0) {
            Messages.showErrorDialog(module.getProject(), UIDesignerBundle.message("error.cannot.preview.empty.form", formFile.getPath().replace('/', File.separatorChar)), CommonBundle.getErrorTitle());
            return;
        }
        setPreviewBindings(rootContainer, CLASS_TO_BIND_NAME);
        // 2. Copy previewer class and all its superclasses into TEMP directory and instrument it.
        try {
            PreviewNestedFormLoader nestedFormLoader = new PreviewNestedFormLoader(module, tempPath, finder);
            final File tempFile = CopyResourcesUtil.copyClass(tempPath, CLASS_TO_BIND_NAME, true);
            //CopyResourcesUtil.copyClass(tempPath, CLASS_TO_BIND_NAME + "$1", true);
            CopyResourcesUtil.copyClass(tempPath, CLASS_TO_BIND_NAME + "$MyExitAction", true);
            CopyResourcesUtil.copyClass(tempPath, CLASS_TO_BIND_NAME + "$MyPackAction", true);
            CopyResourcesUtil.copyClass(tempPath, CLASS_TO_BIND_NAME + "$MySetLafAction", true);
            Locale locale = Locale.getDefault();
            if (locale.getCountry().length() > 0 && locale.getLanguage().length() > 0) {
                CopyResourcesUtil.copyProperties(tempPath, RUNTIME_BUNDLE_PREFIX + "_" + locale.getLanguage() + "_" + locale.getCountry() + PropertiesFileType.DOT_DEFAULT_EXTENSION);
            }
            if (locale.getLanguage().length() > 0) {
                CopyResourcesUtil.copyProperties(tempPath, RUNTIME_BUNDLE_PREFIX + "_" + locale.getLanguage() + PropertiesFileType.DOT_DEFAULT_EXTENSION);
            }
            CopyResourcesUtil.copyProperties(tempPath, RUNTIME_BUNDLE_PREFIX + "_" + locale.getLanguage() + PropertiesFileType.DOT_DEFAULT_EXTENSION);
            CopyResourcesUtil.copyProperties(tempPath, RUNTIME_BUNDLE_PREFIX + PropertiesFileType.DOT_DEFAULT_EXTENSION);
            final AsmCodeGenerator codeGenerator = new AsmCodeGenerator(rootContainer, finder, nestedFormLoader, true, new PsiClassWriter(module));
            codeGenerator.patchFile(tempFile);
            final FormErrorInfo[] errors = codeGenerator.getErrors();
            if (errors.length != 0) {
                Messages.showErrorDialog(module.getProject(), UIDesignerBundle.message("error.cannot.preview.form", formFile.getPath().replace('/', File.separatorChar), errors[0].getErrorMessage()), CommonBundle.getErrorTitle());
                return;
            }
        } catch (Exception e) {
            LOG.debug(e);
            Messages.showErrorDialog(module.getProject(), UIDesignerBundle.message("error.cannot.preview.form", formFile.getPath().replace('/', File.separatorChar), e.getMessage() != null ? e.getMessage() : e.toString()), CommonBundle.getErrorTitle());
            return;
        }
        // 2.5. Copy up-to-date properties files to the output directory.
        final HashSet<String> bundleSet = new HashSet<>();
        FormEditingUtil.iterateStringDescriptors(rootContainer, new FormEditingUtil.StringDescriptorVisitor<IComponent>() {

            public boolean visit(final IComponent component, final StringDescriptor descriptor) {
                if (descriptor.getBundleName() != null) {
                    bundleSet.add(descriptor.getDottedBundleName());
                }
                return true;
            }
        });
        if (bundleSet.size() > 0) {
            HashSet<VirtualFile> virtualFiles = new HashSet<>();
            HashSet<Module> modules = new HashSet<>();
            PropertiesReferenceManager manager = PropertiesReferenceManager.getInstance(module.getProject());
            for (String bundleName : bundleSet) {
                for (PropertiesFile propFile : manager.findPropertiesFiles(module, bundleName)) {
                    virtualFiles.add(propFile.getVirtualFile());
                    final Module moduleForFile = ModuleUtil.findModuleForFile(propFile.getVirtualFile(), module.getProject());
                    if (moduleForFile != null) {
                        modules.add(moduleForFile);
                    }
                }
            }
            FileSetCompileScope scope = new FileSetCompileScope(virtualFiles, modules.toArray(new Module[modules.size()]));
            CompilerManager.getInstance(module.getProject()).make(scope, new CompileStatusNotification() {

                public void finished(boolean aborted, int errors, int warnings, final CompileContext compileContext) {
                    if (!aborted && errors == 0) {
                        runPreviewProcess(tempPath, sources, module, formFile, stringDescriptorLocale);
                    }
                }
            });
        } else {
            runPreviewProcess(tempPath, sources, module, formFile, stringDescriptorLocale);
        }
    } finally {
        finder.releaseResources();
    }
}
Also used : Locale(java.util.Locale) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiClassWriter(com.intellij.compiler.PsiClassWriter) PreviewNestedFormLoader(com.intellij.uiDesigner.make.PreviewNestedFormLoader) Document(com.intellij.openapi.editor.Document) CompileContext(com.intellij.openapi.compiler.CompileContext) CompileStatusNotification(com.intellij.openapi.compiler.CompileStatusNotification) FormErrorInfo(com.intellij.uiDesigner.compiler.FormErrorInfo) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PropertiesReferenceManager(com.intellij.lang.properties.PropertiesReferenceManager) HashSet(com.intellij.util.containers.HashSet) InstrumentationClassFinder(com.intellij.compiler.instrumentation.InstrumentationClassFinder) IOException(java.io.IOException) ExecutionException(com.intellij.execution.ExecutionException) IOException(java.io.IOException) CantRunException(com.intellij.execution.CantRunException) PathsList(com.intellij.util.PathsList) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) File(java.io.File) AsmCodeGenerator(com.intellij.uiDesigner.compiler.AsmCodeGenerator) FormEditingUtil(com.intellij.uiDesigner.FormEditingUtil) FileSetCompileScope(com.intellij.compiler.impl.FileSetCompileScope)

Example 4 with PropertiesReferenceManager

use of com.intellij.lang.properties.PropertiesReferenceManager in project intellij-community by JetBrains.

the class InvalidPropertyKeyFormInspection method checkDescriptor.

@Nullable
private static String checkDescriptor(final StringDescriptor descriptor, final Module module) {
    final String bundleName = descriptor.getDottedBundleName();
    final String key = descriptor.getKey();
    if (bundleName == null && key == null)
        return null;
    if (bundleName == null) {
        return UIDesignerBundle.message("inspection.invalid.property.in.form.quickfix.error.bundle.not.specified");
    }
    if (key == null) {
        return UIDesignerBundle.message("inspection.invalid.property.in.form.quickfix.error.property.key.not.specified");
    }
    PropertiesReferenceManager manager = PropertiesReferenceManager.getInstance(module.getProject());
    List<PropertiesFile> propFiles = manager.findPropertiesFiles(module, bundleName);
    if (propFiles.size() == 0) {
        return UIDesignerBundle.message("inspection.invalid.property.in.form.quickfix.error.bundle.not.found", bundleName);
    }
    for (PropertiesFile propFile : propFiles) {
        final com.intellij.lang.properties.IProperty property = propFile.findPropertyByKey(key);
        if (property == null) {
            return UIDesignerBundle.message("inspection.invalid.property.in.form.quickfix.error.key.not.found", key, bundleName, propFile.getLocale().getDisplayName());
        }
    }
    return null;
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PropertiesReferenceManager(com.intellij.lang.properties.PropertiesReferenceManager) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PropertiesReferenceManager

use of com.intellij.lang.properties.PropertiesReferenceManager in project intellij-community by JetBrains.

the class StringEditorDialog method saveModifiedPropertyValue.

@Nullable
public static String saveModifiedPropertyValue(final Module module, final StringDescriptor descriptor, final Locale locale, final String editedValue, final PsiFile formFile) {
    final PropertiesReferenceManager manager = PropertiesReferenceManager.getInstance(module.getProject());
    final PropertiesFile propFile = manager.findPropertiesFile(module, descriptor.getDottedBundleName(), locale);
    if (propFile != null) {
        final IProperty propertyByKey = propFile.findPropertyByKey(descriptor.getKey());
        if (propertyByKey instanceof Property && !editedValue.equals(propertyByKey.getValue())) {
            final Collection<PsiReference> references = findPropertyReferences((Property) propertyByKey, module);
            String newKeyName = null;
            if (references.size() > 1) {
                final int rc = Messages.showYesNoCancelDialog(module.getProject(), UIDesignerBundle.message("edit.text.multiple.usages", propertyByKey.getUnescapedKey(), references.size()), UIDesignerBundle.message("edit.text.multiple.usages.title"), UIDesignerBundle.message("edit.text.change.all"), UIDesignerBundle.message("edit.text.make.unique"), CommonBundle.getCancelButtonText(), Messages.getWarningIcon());
                if (rc == Messages.CANCEL) {
                    return null;
                }
                if (rc == Messages.NO) {
                    newKeyName = promptNewKeyName(module.getProject(), propFile, descriptor.getKey());
                    if (newKeyName == null)
                        return null;
                }
            }
            final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler.getInstance(module.getProject()).ensureFilesWritable(propFile.getVirtualFile());
            if (operationStatus.hasReadonlyFiles()) {
                return null;
            }
            final String newKeyName1 = newKeyName;
            CommandProcessor.getInstance().executeCommand(module.getProject(), () -> {
                UndoUtil.markPsiFileForUndo(formFile);
                ApplicationManager.getApplication().runWriteAction(() -> {
                    PsiDocumentManager.getInstance(module.getProject()).commitAllDocuments();
                    try {
                        if (newKeyName1 != null) {
                            propFile.addProperty(newKeyName1, editedValue);
                        } else {
                            final IProperty propertyByKey1 = propFile.findPropertyByKey(descriptor.getKey());
                            if (propertyByKey1 != null) {
                                propertyByKey1.setValue(editedValue);
                            }
                        }
                    } catch (IncorrectOperationException e) {
                        LOG.error(e);
                    }
                });
            }, UIDesignerBundle.message("command.update.property"), null);
            return newKeyName;
        }
    }
    return null;
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PsiReference(com.intellij.psi.PsiReference) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PropertiesReferenceManager(com.intellij.lang.properties.PropertiesReferenceManager) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty) ReadonlyStatusHandler(com.intellij.openapi.vfs.ReadonlyStatusHandler) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PropertiesReferenceManager (com.intellij.lang.properties.PropertiesReferenceManager)5 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)4 Module (com.intellij.openapi.module.Module)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 HashSet (com.intellij.util.containers.HashSet)2 Nullable (org.jetbrains.annotations.Nullable)2 PsiClassWriter (com.intellij.compiler.PsiClassWriter)1 FileSetCompileScope (com.intellij.compiler.impl.FileSetCompileScope)1 InstrumentationClassFinder (com.intellij.compiler.instrumentation.InstrumentationClassFinder)1 CantRunException (com.intellij.execution.CantRunException)1 ExecutionException (com.intellij.execution.ExecutionException)1 IProperty (com.intellij.lang.properties.IProperty)1 Property (com.intellij.lang.properties.psi.Property)1 CompileContext (com.intellij.openapi.compiler.CompileContext)1 CompileStatusNotification (com.intellij.openapi.compiler.CompileStatusNotification)1 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 ReadonlyStatusHandler (com.intellij.openapi.vfs.ReadonlyStatusHandler)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1