Search in sources :

Example 1 with JavaI18nizeQuickFixDialog

use of com.intellij.codeInspection.i18n.JavaI18nizeQuickFixDialog in project intellij-community by JetBrains.

the class I18nizeFormQuickFix method run.

public void run() {
    final StringDescriptor descriptor = getStringDescriptorValue();
    final Project project = myEditor.getProject();
    PsiFile psiFile = myEditor.getPsiFile();
    if (!JavaI18nizeQuickFixDialog.isAvailable(myEditor.getPsiFile())) {
        return;
    }
    String initialValue = StringUtil.escapeStringCharacters(descriptor.getValue());
    final JavaI18nizeQuickFixDialog dialog = new JavaI18nizeQuickFixDialog(project, psiFile, null, initialValue, null, false, false) {

        protected String getDimensionServiceKey() {
            return "#com.intellij.codeInsight.i18n.I18nizeQuickFixDialog_Form";
        }
    };
    if (!dialog.showAndGet()) {
        return;
    }
    if (!myEditor.ensureEditable()) {
        return;
    }
    final Collection<PropertiesFile> propertiesFiles = dialog.getAllPropertiesFiles();
    PropertiesFile aPropertiesFile = null;
    for (PropertiesFile file : propertiesFiles) {
        if (!FileModificationService.getInstance().prepareFileForWrite(file.getContainingFile()))
            return;
        if (aPropertiesFile == null) {
            aPropertiesFile = file;
        }
    }
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            JavaI18nUtil.createProperty(project, propertiesFiles, dialog.getKey(), dialog.getValue());
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }), CodeInsightBundle.message("quickfix.i18n.command.name"), project);
    // saving files is necessary to ensure correct reload of properties files by UI Designer
    for (PropertiesFile file : propertiesFiles) {
        FileDocumentManager.getInstance().saveDocument(PsiDocumentManager.getInstance(project).getDocument(file.getContainingFile()));
    }
    if (aPropertiesFile != null) {
        final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
        String packageName = fileIndex.getPackageNameByDirectory(aPropertiesFile.getVirtualFile().getParent());
        if (packageName != null) {
            String bundleName;
            if (packageName.length() > 0) {
                bundleName = packageName + "." + aPropertiesFile.getResourceBundle().getBaseName();
            } else {
                bundleName = aPropertiesFile.getResourceBundle().getBaseName();
            }
            bundleName = bundleName.replace('.', '/');
            try {
                setStringDescriptorValue(new StringDescriptor(bundleName, dialog.getKey()));
            } catch (Exception e) {
                LOG.error(e);
            }
            myEditor.refreshAndSave(true);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) StringDescriptor(com.intellij.uiDesigner.lw.StringDescriptor) PsiFile(com.intellij.psi.PsiFile) JavaI18nizeQuickFixDialog(com.intellij.codeInspection.i18n.JavaI18nizeQuickFixDialog) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

JavaI18nizeQuickFixDialog (com.intellij.codeInspection.i18n.JavaI18nizeQuickFixDialog)1 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)1 Project (com.intellij.openapi.project.Project)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 PsiFile (com.intellij.psi.PsiFile)1 StringDescriptor (com.intellij.uiDesigner.lw.StringDescriptor)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1