Skip to content

Commit

Permalink
Add notification user if block uses in several places
Browse files Browse the repository at this point in the history
  • Loading branch information
Sdmitrioul committed Jun 1, 2023
1 parent 17a2d61 commit 37226ff
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ interface FunctionBlockDeclarationBase : Declaration, ContainedElement {
return result
}

fun getAllPorts(): List<FBPortDescriptor> {
return type.eventInputPorts
fun getAllPorts(): List<FBPortDescriptor> =
type.eventInputPorts
.union(type.eventOutputPorts)
.union(type.dataInputPorts)
.union(type.dataOutputPorts)
.union(type.socketPorts)
.union(type.plugPorts)
.toList()
}

var x: Int
var y: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import jetbrains.mps.editor.runtime.style.CellAlign
import jetbrains.mps.editor.runtime.style.Measure
import jetbrains.mps.editor.runtime.style.Padding
import jetbrains.mps.editor.runtime.style.StyleAttributes
import jetbrains.mps.findUsages.FindUsagesManager
import jetbrains.mps.nodeEditor.MPSColors
import jetbrains.mps.nodeEditor.cellLayout.CellLayout_Vertical
import jetbrains.mps.nodeEditor.cells.EditorCell
Expand All @@ -20,6 +19,9 @@ import org.fbme.ide.richediting.adapters.fbnetwork.fb.FBTypeCellComponent
import org.fbme.ide.richediting.adapters.fbnetwork.fb.EditableFBTypeCell
import org.fbme.ide.richediting.adapters.fbnetwork.port.PortActionFactory
import org.fbme.ide.richediting.editor.RichEditorStyleAttributes
import org.fbme.ide.richediting.utils.Notifier
import org.fbme.ide.richediting.utils.ProjectProvider
import org.fbme.ide.richediting.utils.fb.FBUsageFinder
import org.fbme.ide.richediting.viewmodel.*
import org.fbme.lib.iec61499.DeclarationsScope
import org.fbme.lib.iec61499.IEC61499Factory
Expand Down Expand Up @@ -324,7 +326,21 @@ class FunctionBlockController(
override fun executeInCommand(): Boolean = true

override fun canExecute(context: EditorContext): Boolean {
//FindUsagesManager
val project = ProjectProvider.getInstance(context)!!
val res = FBUsageFinder
.findUsages(project, view.component.type.declaration!!)
.filter {
it.key.identifier != networkInstance.declaration.identifier
}.map {
it.key
}
if (res.isNotEmpty()) {
Notifier.showWarning(
"This function block uses also in " +
res.joinToString { it.name } + "!",
project.project
)
}
return true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.fbme.ide.richediting.utils.fb

import jetbrains.mps.findUsages.FindUsagesManager
import jetbrains.mps.project.MPSProject
import org.fbme.ide.iec61499.repository.PlatformElement
import org.fbme.ide.iec61499.repository.PlatformRepositoryProvider
import org.fbme.lib.common.Declaration
import org.jetbrains.mps.openapi.model.SNode

object FBUsageFinder {
fun findUsages(project: MPSProject, fbBlock: Declaration)
: Map<Declaration, SNode> {
val manager = project.getComponent(FindUsagesManager::class.java)
val platformElement = (fbBlock as PlatformElement)
val nodes = manager.findUsages(project.scope, mutableSetOf(platformElement.node), null)
val repository = PlatformRepositoryProvider.getInstance(project)

val result: MutableMap<Declaration, SNode> = HashMap()
nodes.mapNotNull {
it.sourceNode.parent
}.forEach {
val declaration = repository.getAdapter(it, Declaration::class.java)

if (declaration != null) {
result[declaration] = it
}
}

return result
}
}

0 comments on commit 37226ff

Please sign in to comment.