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);
}
}
}
Aggregations