11package dev.meanmail.prettifypython.settings
22
33import com.intellij.icons.AllIcons
4+ import com.intellij.ide.plugins.PluginManagerCore
45import com.intellij.openapi.actionSystem.ActionManager
56import com.intellij.openapi.actionSystem.ActionToolbarPosition
67import com.intellij.openapi.actionSystem.AnAction
78import com.intellij.openapi.actionSystem.AnActionEvent
89import com.intellij.openapi.actionSystem.impl.SimpleDataContext
10+ import com.intellij.openapi.application.ApplicationInfo
11+ import com.intellij.openapi.extensions.PluginId
912import com.intellij.openapi.ui.DialogWrapper
1013import com.intellij.openapi.ui.Messages
1114import com.intellij.ui.IdeBorderFactory
@@ -191,8 +194,6 @@ class PrettifySettingsComponent {
191194 else -> return false
192195 }
193196
194- // Remove old node
195- val oldParent = draggedNode.parent as DefaultMutableTreeNode
196197 treeModel.removeNodeFromParent(draggedNode)
197198
198199 // Create new mapping with target category
@@ -208,42 +209,69 @@ class PrettifySettingsComponent {
208209
209210 private fun importMappings () {
210211 val fileChooser = JFileChooser ().apply {
211- fileFilter = FileNameExtensionFilter (" JSON files" , " json" )
212212 dialogTitle = " Import Mappings"
213+ fileSelectionMode = JFileChooser .FILES_ONLY
214+ fileFilter = FileNameExtensionFilter (" JSON files" , " json" )
213215 }
214216
215217 if (fileChooser.showOpenDialog(mainPanel) == JFileChooser .APPROVE_OPTION ) {
216218 try {
217- val jsonContent = fileChooser.selectedFile.readText()
218- val importedMappings = json.decodeFromString<List <MappingEntry >>(jsonContent)
219- setMappings(importedMappings)
219+ val jsonString = fileChooser.selectedFile.readText()
220+ val mappingsData = json.decodeFromString<MappingsData >(jsonString)
221+ setMappings(mappingsData.mappings)
222+ Messages .showInfoMessage(
223+ mainPanel,
224+ " Mappings imported successfully" ,
225+ " Import Successful"
226+ )
220227 } catch (e: Exception ) {
221228 Messages .showErrorDialog(
222229 mainPanel,
223230 " Failed to import mappings: ${e.message} " ,
224- " Import Error "
231+ " Import Failed "
225232 )
226233 }
227234 }
228235 }
229236
230237 private fun exportMappings () {
231238 val fileChooser = JFileChooser ().apply {
232- fileFilter = FileNameExtensionFilter (" JSON files" , " json" )
233239 dialogTitle = " Export Mappings"
234- val timestamp = LocalDateTime .now().format(DateTimeFormatter .ofPattern(" yyyyMMdd_HHmmss" ))
235- selectedFile = File (" prettify_python_mappings_$timestamp .json" )
240+ fileSelectionMode = JFileChooser .FILES_ONLY
241+ fileFilter = FileNameExtensionFilter (" JSON files" , " json" )
242+ val now = LocalDateTime .now()
243+ val formatter = DateTimeFormatter .ofPattern(" yyyyMMdd_HHmmss" )
244+ selectedFile = File (" prettify_python_mappings_${now.format(formatter)} .json" )
236245 }
237246
238247 if (fileChooser.showSaveDialog(mainPanel) == JFileChooser .APPROVE_OPTION ) {
239248 try {
240- val jsonContent = json.encodeToString(getMappings())
241- fileChooser.selectedFile.writeText(jsonContent)
249+ val mappings = getMappings()
250+ val pluginId = PluginId .findId(" ru.meanmail.plugins.prettify-python" )
251+ ? : throw IllegalStateException (" Plugin ID not found" )
252+ val plugin = PluginManagerCore .getPlugin(pluginId)
253+ ? : throw IllegalStateException (" Plugin not found" )
254+
255+ val now = LocalDateTime .now()
256+ val mappingsData = MappingsData (
257+ mappings = mappings,
258+ ideVersion = ApplicationInfo .getInstance().fullVersion,
259+ pluginVersion = plugin.version,
260+ mappingsCount = mappings.size,
261+ exportDate = now.toString()
262+ )
263+ val jsonString = json.encodeToString(mappingsData)
264+ fileChooser.selectedFile.writeText(jsonString)
265+ Messages .showInfoMessage(
266+ mainPanel,
267+ " Mappings exported successfully" ,
268+ " Export Successful"
269+ )
242270 } catch (e: Exception ) {
243271 Messages .showErrorDialog(
244272 mainPanel,
245273 " Failed to export mappings: ${e.message} " ,
246- " Export Error "
274+ " Export Failed "
247275 )
248276 }
249277 }
0 commit comments