project configuration: ux improvements in 'Convert Module Groups' dialog

Make dialog non-modal, ask for confirmation if users clicks 'Cancel' without saving changes.
This commit is contained in:
nik
2017-12-27 19:04:46 +03:00
parent 0056c5c62e
commit 5136509b06
3 changed files with 57 additions and 19 deletions
@@ -17,6 +17,8 @@ import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LineExtensionInfo
import com.intellij.openapi.editor.SpellCheckingEditorCustomizationProvider
import com.intellij.openapi.editor.event.DocumentEvent
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.editor.impl.EditorImpl
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
@@ -49,9 +51,11 @@ class ConvertModuleGroupsToQualifiedNamesDialog(val project: Project) : DialogWr
get() = editorArea.document
private lateinit var modules: List<Module>
private val recordPreviousNamesCheckBox: JCheckBox
private var modified = false
init {
title = ProjectBundle.message("convert.module.groups.dialog.title")
isModal = false
setOKButtonText(ProjectBundle.message("convert.module.groups.button.text"))
editorArea = EditorTextFieldProvider.getInstance().getEditorField(StdLanguages.TEXT, project, listOf(EditorCustomization {
it.settings.apply {
@@ -67,6 +71,11 @@ class ConvertModuleGroupsToQualifiedNamesDialog(val project: Project) : DialogWr
(it as? EditorImpl)?.registerLineExtensionPainter(this::generateLineExtension)
setupHighlighting(it)
}, MonospaceEditorCustomization.getInstance()))
document.addDocumentListener(object: DocumentListener {
override fun documentChanged(event: DocumentEvent?) {
modified = true
}
}, disposable)
recordPreviousNamesCheckBox = JCheckBox(ProjectBundle.message("convert.module.groups.record.previous.names.text"), true)
importRenamingScheme(emptyMap())
init()
@@ -121,6 +130,7 @@ class ConvertModuleGroupsToQualifiedNamesDialog(val project: Project) : DialogWr
runWriteAction {
document.setText(modules.joinToString("\n") { names[it]!! })
}
modified = false
}
fun getRenamingScheme(): Map<String, String> {
@@ -131,6 +141,24 @@ class ConvertModuleGroupsToQualifiedNamesDialog(val project: Project) : DialogWr
})
}
override fun doCancelAction() {
if (modified) {
val answer = Messages.showYesNoCancelDialog(project,
ProjectBundle.message("convert.module.groups.do.you.want.to.save.scheme"),
ProjectBundle.message("convert.module.groups.dialog.title"), null)
when (answer) {
Messages.CANCEL -> return
Messages.YES -> {
if (!saveModuleRenamingScheme(this)) {
return
}
}
}
}
super.doCancelAction()
}
override fun doOKAction() {
ModuleNamesListInspection.checkModuleNames(document.charsSequence.lines(), project) { line, message ->
Messages.showErrorDialog(project,
@@ -162,7 +190,7 @@ class ConvertModuleGroupsToQualifiedNamesDialog(val project: Project) : DialogWr
}
override fun createActions(): Array<Action> {
return arrayOf(okAction, SaveModuleRenamingSchemeAction(this),
return arrayOf(okAction, SaveModuleRenamingSchemeAction(this, { modified = false }),
LoadModuleRenamingSchemeAction(this), cancelAction)
}
}
@@ -60,33 +60,42 @@ class LoadModuleRenamingSchemeAction(private val dialog: ConvertModuleGroupsToQu
}
}
class SaveModuleRenamingSchemeAction(private val dialog: ConvertModuleGroupsToQualifiedNamesDialog) : AbstractAction() {
class SaveModuleRenamingSchemeAction(private val dialog: ConvertModuleGroupsToQualifiedNamesDialog, private val onSaved: () -> Unit) : AbstractAction() {
init {
UIUtil.setActionNameAndMnemonic(ProjectBundle.message("module.renaming.scheme.save.button.text"), this)
}
override fun actionPerformed(e: ActionEvent?) {
val project = dialog.project
val descriptor = FileSaverDescriptor(ProjectBundle.message("module.renaming.scheme.save.chooser.title"),
ProjectBundle.message("module.renaming.scheme.save.chooser.description"), "xml")
val baseDir = getDefaultRenamingSchemeFile(project)?.parent ?: project.baseDir
val fileWrapper = FileChooserFactory.getInstance().createSaveFileDialog(descriptor, project).save(baseDir, "module-renaming-scheme.xml")
if (fileWrapper != null) {
saveDefaultRenamingSchemeFilePath(project, FileUtil.toSystemIndependentName(fileWrapper.file.absolutePath))
val state = ModuleRenamingHistoryState()
state.oldToNewName.putAll(dialog.getRenamingScheme())
try {
XmlSerializer.serialize(state).write(fileWrapper.file.toPath())
}
catch (e: Exception) {
LOG.info(e)
Messages.showErrorDialog(project, CommonBundle.getErrorTitle(), ProjectBundle.message("module.renaming.scheme.cannot.save.error", e.message ?: ""))
}
if (saveModuleRenamingScheme(dialog)) {
onSaved()
}
}
}
internal fun saveModuleRenamingScheme(dialog: ConvertModuleGroupsToQualifiedNamesDialog): Boolean {
val project = dialog.project
val descriptor = FileSaverDescriptor(ProjectBundle.message("module.renaming.scheme.save.chooser.title"),
ProjectBundle.message("module.renaming.scheme.save.chooser.description"), "xml")
val baseDir = getDefaultRenamingSchemeFile(project)?.parent ?: project.baseDir
val fileWrapper = FileChooserFactory.getInstance().createSaveFileDialog(descriptor, project).save(baseDir, "module-renaming-scheme.xml")
if (fileWrapper != null) {
saveDefaultRenamingSchemeFilePath(project, FileUtil.toSystemIndependentName(fileWrapper.file.absolutePath))
val state = ModuleRenamingHistoryState()
state.oldToNewName.putAll(dialog.getRenamingScheme())
try {
XmlSerializer.serialize(state).write(fileWrapper.file.toPath())
return true
}
catch (e: Exception) {
LOG.info(e)
Messages.showErrorDialog(project, CommonBundle.getErrorTitle(),
ProjectBundle.message("module.renaming.scheme.cannot.save.error", e.message ?: ""))
}
}
return false
}
private val EXPORTED_PATH_PROPERTY = "module.renaming.scheme.file"
private fun saveDefaultRenamingSchemeFilePath(project: Project, filePath: String?) {
@@ -155,6 +155,7 @@ convert.module.groups.error.at.text=Error at line {0}: {1}
convert.module.groups.record.previous.names.tooltip=All changes in module names will be recorded. {0} will use this data to automatically \
replace module names in workspace settings when other team members update the project from VCS.
convert.module.groups.record.previous.names.text=&Record previous module names
convert.module.groups.do.you.want.to.save.scheme=Do you want to save the current scheme?
module.renaming.scheme.cannot.load.error=Failed to load renaming scheme from {0}:\n{1}
module.renaming.scheme.cannot.save.error=Failed to save renaming scheme:\n{0}