Convert Java to Kotlin action: minor improvements
Do not show "Do you want to backup?" dialog if no java files are selected Improve the code Change dialog title to Convert Java to Kotlin Actually cancel if user presses cancel
This commit is contained in:
@@ -19,54 +19,51 @@ package org.jetbrains.jet.plugin.actions;
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.PlatformDataKeys;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.Converter;
|
||||
import org.jetbrains.jet.j2k.J2kPackage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.openapi.ui.Messages.NO;
|
||||
import static com.intellij.openapi.ui.Messages.YES;
|
||||
import static org.jetbrains.jet.plugin.actions.JavaToKotlinActionUtil.*;
|
||||
|
||||
public class JavaToKotlinAction extends AnAction {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
VirtualFile[] virtualFiles = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
|
||||
VirtualFile[] virtualFiles = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
|
||||
assert virtualFiles != null;
|
||||
final Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());
|
||||
final Project project = CommonDataKeys.PROJECT.getData(e.getDataContext());
|
||||
assert project != null;
|
||||
final Converter converter = new Converter(project, J2kPackage.getPluginSettings());
|
||||
int result = Messages.showYesNoCancelDialog(project,
|
||||
"Would you like to backup Java files?",
|
||||
"Backup",
|
||||
Messages.getQuestionIcon());
|
||||
final boolean finalRemoveIt = needToRemoveFiles(result);
|
||||
final List<PsiFile> allJavaFiles = getAllJavaFiles(virtualFiles, project);
|
||||
|
||||
converter.clearClassIdentifiers();
|
||||
for (PsiFile f : allJavaFiles) {
|
||||
if (f.getFileType() instanceof JavaFileType) {
|
||||
setClassIdentifiers(converter, f);
|
||||
}
|
||||
final List<PsiFile> selectedJavaFiles = getAllJavaFiles(virtualFiles, project);
|
||||
if (selectedJavaFiles.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
final DialogResult userResponse = showDialog(project);
|
||||
if (userResponse == DialogResult.CANCEL) {
|
||||
return;
|
||||
}
|
||||
|
||||
final List<PsiFile> allJavaFilesNear = getAllJavaFiles(virtualFiles, project);
|
||||
final Converter converter = prepareConverter(project, selectedJavaFiles);
|
||||
CommandProcessor.getInstance().executeCommand(
|
||||
project,
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<VirtualFile> newFiles = convertFiles(converter, allJavaFilesNear);
|
||||
if (finalRemoveIt) {
|
||||
deleteFiles(allJavaFilesNear);
|
||||
List<VirtualFile> newFiles = convertFiles(converter, selectedJavaFiles);
|
||||
if (userResponse == DialogResult.DELETE_FILES) {
|
||||
deleteFiles(selectedJavaFiles);
|
||||
}
|
||||
else {
|
||||
renameFiles(allJavaFiles);
|
||||
else if (userResponse == DialogResult.BACKUP_FILES) {
|
||||
renameFiles(selectedJavaFiles);
|
||||
}
|
||||
reformatFiles(newFiles, project);
|
||||
for (VirtualFile vf : newFiles) {
|
||||
@@ -79,22 +76,43 @@ public class JavaToKotlinAction extends AnAction {
|
||||
);
|
||||
}
|
||||
|
||||
private static boolean needToRemoveFiles(int result) {
|
||||
boolean removeIt = false;
|
||||
switch (result) {
|
||||
case 0:
|
||||
removeIt = false;
|
||||
break;
|
||||
case 1:
|
||||
removeIt = true;
|
||||
break;
|
||||
@NotNull
|
||||
private static Converter prepareConverter(@NotNull Project project, @NotNull List<PsiFile> selectedJavaFiles) {
|
||||
Converter converter = new Converter(project, J2kPackage.getPluginSettings());
|
||||
converter.clearClassIdentifiers();
|
||||
for (PsiFile f : selectedJavaFiles) {
|
||||
if (f.getFileType() instanceof JavaFileType) {
|
||||
setClassIdentifiers(converter, f);
|
||||
}
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
private static enum DialogResult {
|
||||
BACKUP_FILES,
|
||||
DELETE_FILES,
|
||||
CANCEL
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static DialogResult showDialog(@NotNull Project project) {
|
||||
int result = Messages.showYesNoCancelDialog(project,
|
||||
"Would you like to backup Java files?",
|
||||
"Convert Java to Kotlin",
|
||||
Messages.getQuestionIcon());
|
||||
switch (result) {
|
||||
case YES:
|
||||
return DialogResult.BACKUP_FILES;
|
||||
case NO:
|
||||
return DialogResult.DELETE_FILES;
|
||||
default:
|
||||
return DialogResult.CANCEL;
|
||||
}
|
||||
return removeIt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(AnActionEvent e) {
|
||||
boolean enabled = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY) != null;
|
||||
boolean enabled = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY) != null;
|
||||
e.getPresentation().setVisible(enabled);
|
||||
e.getPresentation().setEnabled(enabled);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user