|
| 1 | +package com.idealista.togglehandler |
| 2 | + |
| 3 | +import com.intellij.openapi.project.Project |
| 4 | +import com.intellij.openapi.ui.ComboBox |
| 5 | +import com.intellij.openapi.ui.DialogWrapper |
| 6 | +import com.intellij.openapi.ui.Messages |
| 7 | +import com.intellij.psi.PsiFile |
| 8 | +import com.intellij.psi.PsiManager |
| 9 | +import com.intellij.psi.search.FilenameIndex |
| 10 | +import com.intellij.psi.search.GlobalSearchScope |
| 11 | +import com.intellij.util.ui.JBUI |
| 12 | +import java.awt.Dimension |
| 13 | +import java.awt.GridBagConstraints |
| 14 | +import java.awt.GridBagLayout |
| 15 | +import javax.swing.DefaultComboBoxModel |
| 16 | +import javax.swing.JComponent |
| 17 | +import javax.swing.JLabel |
| 18 | +import javax.swing.JPanel |
| 19 | + |
| 20 | +class DeleteToggleDialog(private val project: Project) : DialogWrapper(project) { |
| 21 | + private val fileModifier = ToggleFileModifier(project) |
| 22 | + private val toggleComboBox = ComboBox<String>() |
| 23 | + |
| 24 | + init { |
| 25 | + title = "Delete Toggle" |
| 26 | + init() |
| 27 | + loadAvailableToggles() |
| 28 | + } |
| 29 | + |
| 30 | + override fun createCenterPanel(): JComponent { |
| 31 | + val panel = JPanel(GridBagLayout()) |
| 32 | + val gbc = GridBagConstraints() |
| 33 | + |
| 34 | + gbc.insets = JBUI.insets(5) |
| 35 | + gbc.anchor = GridBagConstraints.WEST |
| 36 | + |
| 37 | + gbc.gridx = 0 |
| 38 | + gbc.gridy = 0 |
| 39 | + gbc.weightx = 0.0 |
| 40 | + panel.add(JLabel("Select a toggle:"), gbc) |
| 41 | + |
| 42 | + gbc.gridx = 1 |
| 43 | + gbc.weightx = 1.0 |
| 44 | + gbc.fill = GridBagConstraints.HORIZONTAL |
| 45 | + toggleComboBox.preferredSize = Dimension(300, toggleComboBox.preferredSize.height) |
| 46 | + panel.add(toggleComboBox, gbc) |
| 47 | + |
| 48 | + gbc.gridx = 0 |
| 49 | + gbc.gridy = 1 |
| 50 | + gbc.gridwidth = 2 |
| 51 | + gbc.weightx = 1.0 |
| 52 | + gbc.insets = JBUI.insets(15, 5, 5, 5) |
| 53 | + val infoLabel = JLabel("<html><i>This action will delete it from Toggle.kt and RemoteSettingsDefault,\nbut not from ServiceExtensions.kt</i></html>") |
| 54 | + panel.add(infoLabel, gbc) |
| 55 | + |
| 56 | + return panel |
| 57 | + } |
| 58 | + |
| 59 | + private fun loadAvailableToggles() { |
| 60 | + val toggleFiles = findToggleFiles() |
| 61 | + val toggleFile = toggleFiles.find { it.second == ToggleDefinitions.ToggleFile }?.first |
| 62 | + |
| 63 | + if (toggleFile != null) { |
| 64 | + val toggleNames = fileModifier.extractToggleNames(toggleFile).filter { it != "None" } |
| 65 | + toggleComboBox.model = DefaultComboBoxModel(toggleNames.toTypedArray()) |
| 66 | + } else { |
| 67 | + Messages.showErrorDialog( |
| 68 | + project, |
| 69 | + "Could not find file Toggle.kt", |
| 70 | + "Error" |
| 71 | + ) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + override fun doOKAction() { |
| 76 | + val selectedToggle = toggleComboBox.selectedItem as? String |
| 77 | + |
| 78 | + if (selectedToggle.isNullOrEmpty()) { |
| 79 | + Messages.showWarningDialog( |
| 80 | + project, |
| 81 | + "Please select a toggle to delete", |
| 82 | + "Required Action" |
| 83 | + ) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + val toggleData = ToggleDefinitions.ToggleData(selectedToggle) |
| 88 | + deleteToggle(toggleData) |
| 89 | + super.doOKAction() |
| 90 | + } |
| 91 | + |
| 92 | + private fun deleteToggle(toggleData: ToggleDefinitions.ToggleData) { |
| 93 | + val filesToModify = findToggleFiles() |
| 94 | + |
| 95 | + if (filesToModify.isEmpty()) { |
| 96 | + Messages.showErrorDialog( |
| 97 | + project, |
| 98 | + "No files found to modify", |
| 99 | + "Error" |
| 100 | + ) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + modifyFilesToRemoveToggle(filesToModify, toggleData) |
| 106 | + } catch (e: Exception) { |
| 107 | + Messages.showErrorDialog( |
| 108 | + project, |
| 109 | + "Error deleting toggle: ${e.message}", |
| 110 | + "Error" |
| 111 | + ) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private fun findToggleFiles(): List<Pair<PsiFile, Any>> { |
| 116 | + val foundFiles = mutableListOf<Pair<PsiFile, Any>>() |
| 117 | + val scope = GlobalSearchScope.projectScope(project) |
| 118 | + |
| 119 | + ToggleDefinitions.TARGET_FILES.forEach { (fileName, fileType) -> |
| 120 | + val files = FilenameIndex.getVirtualFilesByName(fileName, scope).mapNotNull { virtualFile -> |
| 121 | + PsiManager.getInstance(project).findFile(virtualFile) |
| 122 | + } |
| 123 | + |
| 124 | + files.forEach { file -> |
| 125 | + foundFiles.add(file to fileType) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return foundFiles |
| 130 | + } |
| 131 | + |
| 132 | + private fun modifyFilesToRemoveToggle(files: List<Pair<PsiFile, Any>>, toggleData: ToggleDefinitions.ToggleData) { |
| 133 | + files.forEach { (file, fileType) -> |
| 134 | + when (fileType) { |
| 135 | + ToggleDefinitions.ToggleFile -> { |
| 136 | + fileModifier.removeToggleDefinition(file, toggleData) |
| 137 | + } |
| 138 | + |
| 139 | + ToggleDefinitions.ServiceExtensionsFile -> { |
| 140 | + fileModifier.openServiceExtension(file) |
| 141 | + } |
| 142 | + |
| 143 | + ToggleDefinitions.RemoteSettingsDefaultsFile -> { |
| 144 | + fileModifier.removeRemoteSettingsDefaults(file, toggleData) |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments