override fun actionPerformed()

in code/debugger/src/main/kotlin/org/fbme/debugger/plugin/SimulateExecutionAction.kt [45:101]


    override fun actionPerformed(event: AnActionEvent) {
        event.executeWriteActionInEditor {
            val project = event.project!!
            val mpsProject = project.getComponent(MPSProject::class.java)
            val toolWindowManager = ToolWindowManager.getInstance(project)
            val toolWindow = toolWindowManager.getToolWindow(DebuggerToolWindow.ID)!!
            val cell = event.cell!!
            val style = cell.style

            val typeDeclaration = style.get(RichEditorStyleAttributes.FB)?.type?.declaration
                ?: style.get(RichEditorStyleAttributes.NETWORK)?.container
                    ?.let { it as? FBTypeDeclaration ?: it as? ResourceDeclaration }
                ?: style.get(RichEditorStyleAttributes.ECC)?.container!!

            val snapshot: DeclarationSnapshot = DeclarationSnapshot.create(typeDeclaration)
            val snapshotDeclaration: Declaration = snapshot.snapshotDeclaration
            val typeName = snapshotDeclaration.name

            val simulator = when (snapshotDeclaration) {
                is BasicFBTypeDeclaration -> BasicFBSimulator(snapshotDeclaration)
                is CompositeFBTypeDeclaration -> CompositeFBSimulator(snapshotDeclaration)
                is ResourceDeclaration -> ResourceSimulator(snapshotDeclaration)
                else -> error("Unsupported declaration: $snapshotDeclaration")
            }

            val trace: ExecutionTrace = simulator.trace
            val explanationProducer = ExplanationProducer(trace, snapshotDeclaration)

            val inspector = (snapshotDeclaration as? DeclarationWithNetwork)?.let {
                val manager: InspectionManager = InspectionManagerImpl.getInstance(cell.editorComponent)!!
                val networkInstance: NetworkInstance = cell.style.get(RichEditorStyleAttributes.NETWORK_INSTANCE)
                if (networkInstance.networkDeclaration.container == typeDeclaration) {
                    manager.installInspector(networkInstance) { }!!
                } else null
            }

            val simulatorPanel = SimulatorPanel(
                project,
                mpsProject,
                trace,
                snapshotDeclaration,
                typeDeclaration,
                simulator,
                explanationProducer,
                inspector
            )
            val simulatorPanelComponent = simulatorPanel.toolWindowPanel

            val content = ContentFactory.SERVICE.getInstance().createContent(simulatorPanelComponent, typeName, false)
            content.isPinnable = true
            content.isPinned = true

            toolWindow.contentManager.addContent(content)
            toolWindow.contentManager.setSelectedContent(content, true)
            toolWindow.show()
        }
    }