|
| 1 | +package cc.unitmesh.devti.mcp.ui |
| 2 | + |
| 3 | +import com.intellij.openapi.project.Project |
| 4 | +import com.intellij.openapi.ui.popup.JBPopupFactory |
| 5 | +import com.intellij.ui.CheckboxTree |
| 6 | +import com.intellij.ui.CheckedTreeNode |
| 7 | +import com.intellij.ui.SimpleTextAttributes |
| 8 | +import com.intellij.ui.components.JBScrollPane |
| 9 | +import com.intellij.ui.components.JBTextField |
| 10 | +import com.intellij.util.ui.JBUI |
| 11 | +import kotlinx.coroutines.CoroutineScope |
| 12 | +import kotlinx.coroutines.Dispatchers |
| 13 | +import kotlinx.coroutines.launch |
| 14 | +import java.awt.BorderLayout |
| 15 | +import java.awt.Dimension |
| 16 | +import java.awt.event.KeyAdapter |
| 17 | +import java.awt.event.KeyEvent |
| 18 | +import javax.swing.* |
| 19 | +import javax.swing.tree.DefaultTreeModel |
| 20 | +import javax.swing.tree.TreePath |
| 21 | + |
| 22 | +class McpConfigPopup { |
| 23 | + companion object { |
| 24 | + fun show(component: JComponent?, project: Project) { |
| 25 | + val configService = project.getService(McpConfigService::class.java) |
| 26 | + val popup = McpConfigPopup() |
| 27 | + popup.createAndShow(component, project, configService) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private fun createAndShow(component: JComponent?, project: Project, configService: McpConfigService) { |
| 32 | + val mainPanel = JPanel(BorderLayout()).apply { |
| 33 | + preferredSize = Dimension(400, 500) |
| 34 | + border = JBUI.Borders.empty(8) |
| 35 | + } |
| 36 | + |
| 37 | + // Search field |
| 38 | + val searchField = JBTextField().apply { |
| 39 | + emptyText.text = "Search tools..." |
| 40 | + border = JBUI.Borders.empty(4) |
| 41 | + } |
| 42 | + |
| 43 | + // Tree for tool selection |
| 44 | + val rootNode = CheckedTreeNode("MCP Tools") |
| 45 | + val treeModel = DefaultTreeModel(rootNode) |
| 46 | + val tree = CheckboxTree(object : CheckboxTree.CheckboxTreeCellRenderer() { |
| 47 | + override fun customizeRenderer( |
| 48 | + tree: JTree?, |
| 49 | + value: Any?, |
| 50 | + selected: Boolean, |
| 51 | + expanded: Boolean, |
| 52 | + leaf: Boolean, |
| 53 | + row: Int, |
| 54 | + hasFocus: Boolean |
| 55 | + ) { |
| 56 | + if (value is ToolTreeNode) { |
| 57 | + textRenderer.append(value.tool.name, SimpleTextAttributes.REGULAR_ATTRIBUTES) |
| 58 | + value.tool.description?.let { desc -> |
| 59 | + if (desc.isNotEmpty()) { |
| 60 | + textRenderer.append(" - $desc", SimpleTextAttributes.GRAYED_ATTRIBUTES) |
| 61 | + } |
| 62 | + } |
| 63 | + } else if (value is ServerTreeNode) { |
| 64 | + textRenderer.append(value.serverName, SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES) |
| 65 | + } else { |
| 66 | + textRenderer.append(value.toString(), SimpleTextAttributes.REGULAR_ATTRIBUTES) |
| 67 | + } |
| 68 | + } |
| 69 | + }, rootNode).apply { |
| 70 | + isRootVisible = false |
| 71 | + showsRootHandles = true |
| 72 | + } |
| 73 | + |
| 74 | + val scrollPane = JBScrollPane(tree).apply { |
| 75 | + preferredSize = Dimension(380, 350) |
| 76 | + } |
| 77 | + |
| 78 | + // Button panel |
| 79 | + val buttonPanel = JPanel().apply { |
| 80 | + layout = BoxLayout(this, BoxLayout.X_AXIS) |
| 81 | + add(Box.createHorizontalGlue()) |
| 82 | + |
| 83 | + val applyButton = JButton("Apply").apply { |
| 84 | + addActionListener { |
| 85 | + saveSelectedTools(tree, configService) |
| 86 | + // Close popup - this will be handled by the popup framework |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + val cancelButton = JButton("Cancel") |
| 91 | + |
| 92 | + add(cancelButton) |
| 93 | + add(Box.createHorizontalStrut(8)) |
| 94 | + add(applyButton) |
| 95 | + } |
| 96 | + |
| 97 | + mainPanel.add(searchField, BorderLayout.NORTH) |
| 98 | + mainPanel.add(scrollPane, BorderLayout.CENTER) |
| 99 | + mainPanel.add(buttonPanel, BorderLayout.SOUTH) |
| 100 | + |
| 101 | + // Load tools asynchronously |
| 102 | + loadToolsIntoTree(project, configService, rootNode, treeModel, tree) |
| 103 | + |
| 104 | + // Search functionality |
| 105 | + searchField.addKeyListener(object : KeyAdapter() { |
| 106 | + override fun keyReleased(e: KeyEvent) { |
| 107 | + filterTree(tree, rootNode, searchField.text) |
| 108 | + } |
| 109 | + }) |
| 110 | + |
| 111 | + val popup = JBPopupFactory.getInstance() |
| 112 | + .createComponentPopupBuilder(mainPanel, searchField) |
| 113 | + .setTitle("Configure MCP Tools") |
| 114 | + .setResizable(true) |
| 115 | + .setMovable(true) |
| 116 | + .setRequestFocus(true) |
| 117 | + .createPopup() |
| 118 | + |
| 119 | + if (component != null) { |
| 120 | + popup.showUnderneathOf(component) |
| 121 | + } else { |
| 122 | + popup.showCenteredInCurrentWindow(project) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private fun loadToolsIntoTree( |
| 127 | + project: Project, |
| 128 | + configService: McpConfigService, |
| 129 | + rootNode: CheckedTreeNode, |
| 130 | + treeModel: DefaultTreeModel, |
| 131 | + tree: CheckboxTree |
| 132 | + ) { |
| 133 | + CoroutineScope(Dispatchers.IO).launch { |
| 134 | + // Use empty content for now, or you can pass actual content based on context |
| 135 | + val allTools = configService.getAllAvailableTools("") |
| 136 | + val selectedTools = configService.getSelectedTools() |
| 137 | + |
| 138 | + CoroutineScope(Dispatchers.IO).launch { |
| 139 | + rootNode.removeAllChildren() |
| 140 | + |
| 141 | + allTools.forEach { (serverName, tools) -> |
| 142 | + val serverNode = ServerTreeNode(serverName) |
| 143 | + rootNode.add(serverNode) |
| 144 | + |
| 145 | + tools.forEach { tool -> |
| 146 | + val toolNode = ToolTreeNode(serverName, tool) |
| 147 | + val isSelected = selectedTools[serverName]?.contains(tool.name) == true |
| 148 | + toolNode.isChecked = isSelected |
| 149 | + serverNode.add(toolNode) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + treeModel.reload() |
| 154 | + expandAllNodes(tree) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private fun saveSelectedTools(tree: CheckboxTree, configService: McpConfigService) { |
| 160 | + val selectedTools = mutableMapOf<String, MutableSet<String>>() |
| 161 | + |
| 162 | + val root = tree.model.root as CheckedTreeNode |
| 163 | + for (i in 0 until root.childCount) { |
| 164 | + val serverNode = root.getChildAt(i) as ServerTreeNode |
| 165 | + val serverName = serverNode.serverName |
| 166 | + |
| 167 | + for (j in 0 until serverNode.childCount) { |
| 168 | + val toolNode = serverNode.getChildAt(j) as ToolTreeNode |
| 169 | + if (toolNode.isChecked) { |
| 170 | + selectedTools.computeIfAbsent(serverName) { mutableSetOf() } |
| 171 | + .add(toolNode.tool.name) |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + configService.setSelectedTools(selectedTools) |
| 177 | + } |
| 178 | + |
| 179 | + private fun filterTree(tree: CheckboxTree, rootNode: CheckedTreeNode, searchText: String) { |
| 180 | + // Simple implementation - in a real scenario, you might want more sophisticated filtering |
| 181 | + tree.expandPath(TreePath(rootNode.path)) |
| 182 | + } |
| 183 | + |
| 184 | + private fun expandAllNodes(tree: CheckboxTree) { |
| 185 | + for (i in 0 until tree.rowCount) { |
| 186 | + tree.expandRow(i) |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments