StarUML documentation
v6
v6
  • Introduction
  • User Guide
    • Introduction
    • Basic Concepts
    • Managing Project
    • Managing Diagrams
    • Diagram Editor
    • Editing Elements
    • Formatting Elements
    • Annotation Elements
    • Managing Extensions
    • User Interface
    • CLI (Command Line Interface)
    • Validation Rules
    • Keyboard Shortcuts
    • TouchBar (MacBook)
    • Customization
  • Working with UML Diagrams
    • Class Diagram
    • Package Diagram
    • Composite Structure Diagram
    • Object Diagram
    • Component Diagram
    • Deployment Diagram
    • Use Case Diagram
    • Sequence Diagram
    • Communication Diagram
    • Timing Diagram
    • Interaction Overview Diagram
    • Statechart Diagram
    • Activity Diagram
    • Information Flow Diagram
    • Profile Diagram
  • Working with SysML Diagrams
    • Requirement Diagram
    • Block Definition Diagram
    • Internal Block Diagram
    • Parametric Diagram
  • Working with Additional Diagrams
    • Entity-Relationship Diagram
    • Flowchart Diagram
    • Data Flow Diagram
    • C4 Diagram
    • BPMN Diagram
    • Mindmap Diagram
    • Wireframe Diagram
    • AWS Architecture Diagram
    • GCP Architecture Diagram
  • Developing Extensions
    • Getting Started
    • Commands
    • Menus
    • Keymaps
    • Toolbox
    • Accessing Elements
    • Creating, Deleting and Modifying Elements
    • Working with Selections
    • Defining Preferences
    • Using Dialogs
    • Registering to Extension Registry
Powered by GitBook
On this page
  • Getting selected elements
  • Enforce to select particular elements
  • Listening selection changed event
  1. Developing Extensions

Working with Selections

PreviousCreating, Deleting and Modifying ElementsNextDefining Preferences

Last updated 1 year ago

In this chapter, we're going to learn how to get selected elements, force to select particular elements, and do something by listening to selection changed events.

Getting selected elements

Users can select elements in a diagram or in Model Explorer. Sometimes we may need to access only the selected elements.

We need to distinguish between selected views and selected models. If you select the Book class in a diagram, then there is a selected view (UMLClassView) and a selected model (UMLClass). If you select the Author class in Model Explorer, then there is a selected model (UMLClass) and no selected views.

We can access selected elements using app.selections as following:

var selectedViews = app.selections.getSelectedViews()
var selectedModels = app.selections.getSelectedModels()
var selected = app.selections.getSelected() // === selectedModels[0]

Enforce to select particular elements

To select a model element in Model Explorer, use ModelExplorerView module. (Assume that Book.mdj used in were loaded)

var book = app.repository.select("Model::Book")[0]
app.modelExplorer.select(book)

To scroll automatically so as to show the element, pass true value as the second parameter.

app.modelExplorer.select(book, true)

To select a view element in diagram, use app.diagrams. You can find more functions about selection in .

var diagram = app.repository.select("@Diagram")[0]
var view1 = diagram.ownedViews[0]

app.diagrams.selectInDiagram(view1)

Listening selection changed event

Now we will show how to listen and handle a selection change event. An array of selected model elements and an array of selected view elements are passed to the second and the third parameters respectively to the callback function.

app.selections.on('selectionChanged', function (models, views) {
  console.log("Selected number of model elements: ", models.length)
  console.log("Selected number of view elements: ", views.length)
})
Accessing Elements
API Reference