11package dev.meanmail.prettifypython.settings
22
33import com.intellij.icons.AllIcons
4+ import com.intellij.openapi.actionSystem.ActionManager
45import com.intellij.openapi.actionSystem.ActionToolbarPosition
56import com.intellij.openapi.actionSystem.AnAction
67import com.intellij.openapi.actionSystem.AnActionEvent
8+ import com.intellij.openapi.actionSystem.impl.SimpleDataContext
79import com.intellij.openapi.ui.DialogWrapper
810import com.intellij.openapi.ui.Messages
911import com.intellij.ui.IdeBorderFactory
@@ -30,6 +32,7 @@ class PrettifySettingsComponent {
3032 private val treeModel: DefaultTreeModel
3133 private val rootNode = DefaultMutableTreeNode (" Mappings" )
3234 private val json = Json { prettyPrint = true }
35+ private var resetButton: AnAction ? = null
3336
3437 init {
3538 treeModel = DefaultTreeModel (rootNode)
@@ -71,7 +74,11 @@ class PrettifySettingsComponent {
7174 override fun actionPerformed (e : AnActionEvent ) {
7275 resetToDefault()
7376 }
74- })
77+
78+ override fun update (e : AnActionEvent ) {
79+ e.presentation.isEnabled = ! isDefaultMappings()
80+ }
81+ }.also { resetButton = it })
7582
7683 mainPanel = JPanel (BorderLayout ()).apply {
7784 add(toolbarDecorator.createPanel(), BorderLayout .CENTER )
@@ -101,7 +108,7 @@ class PrettifySettingsComponent {
101108 }
102109 addMappingToTree(updatedMapping)
103110 }
104- TreeUtil .expandAll(mappingsTree )
111+ expandAllNodes( )
105112 }
106113 }
107114
@@ -111,7 +118,7 @@ class PrettifySettingsComponent {
111118 if (dialog.isOK) {
112119 val mapping = dialog.getMapping()
113120 addMappingToTree(mapping)
114- TreeUtil .expandAll(mappingsTree )
121+ expandAllNodes( )
115122 }
116123 }
117124
@@ -303,11 +310,45 @@ class PrettifySettingsComponent {
303310 return mappings
304311 }
305312
313+ private fun expandAllNodes () {
314+ var row = 0
315+ while (row < mappingsTree.rowCount) {
316+ mappingsTree.expandRow(row)
317+ row++
318+ }
319+ }
320+
306321 fun setMappings (mappings : List <MappingEntry >) {
307322 rootNode.removeAllChildren()
308- treeModel.reload()
309323 mappings.forEach { addMappingToTree(it) }
310- TreeUtil .expandAll(mappingsTree)
324+ treeModel.reload()
325+ expandAllNodes()
326+ resetButton?.let { action ->
327+ val presentation = action.templatePresentation.clone()
328+ action.update(
329+ AnActionEvent (
330+ null ,
331+ SimpleDataContext .EMPTY_CONTEXT ,
332+ " PrettifySettingsComponent" ,
333+ presentation,
334+ ActionManager .getInstance(),
335+ 0
336+ )
337+ )
338+ }
339+ }
340+
341+ private fun isDefaultMappings (): Boolean {
342+ val currentMappings = getMappings().sortedBy { it.from }
343+ val defaultMappings = PrettifySettings ().getDefaultMappings().sortedBy { it.from }
344+
345+ if (currentMappings.size != defaultMappings.size) return false
346+
347+ return currentMappings.zip(defaultMappings) { current, default ->
348+ current.from == default.from &&
349+ current.to == default.to &&
350+ current.category == default.category
351+ }.all { it }
311352 }
312353}
313354
0 commit comments