Search in sources :

Example 11 with KRectangle

use of de.cau.cs.kieler.klighd.krendering.KRectangle in project lingua-franca by lf-lang.

the class ModeDiagrams method handleModes.

public void handleModes(List<KNode> nodes, ReactorInstance reactor) {
    if (!reactor.modes.isEmpty()) {
        var modeNodes = new LinkedHashMap<ModeInstance, KNode>();
        var modeDefinitionMap = new LinkedHashMap<Mode, ModeInstance>();
        for (ModeInstance mode : reactor.modes) {
            var node = _kNodeExtensions.createNode();
            associateWith(node, mode.getDefinition());
            NamedInstanceUtil.linkInstance(node, mode);
            _utilityExtensions.setID(node, mode.uniqueID());
            modeNodes.put(mode, node);
            modeDefinitionMap.put(mode.getDefinition(), mode);
            if (mode.isInitial()) {
                DiagramSyntheses.setLayoutOption(node, LayeredOptions.LAYERING_LAYER_CONSTRAINT, LayerConstraint.FIRST);
            }
            DiagramSyntheses.setLayoutOption(node, LayeredOptions.CROSSING_MINIMIZATION_SEMI_INTERACTIVE, true);
            var expansionState = MemorizingExpandCollapseAction.getExpansionState(mode);
            DiagramSyntheses.setLayoutOption(node, KlighdProperties.EXPAND, expansionState != null ? expansionState : !this.getBooleanValue(INITIALLY_COLLAPSE_MODES));
            // Expanded Rectangle
            var expandFigure = addModeFigure(node, mode, true);
            expandFigure.setProperty(KlighdProperties.EXPANDED_RENDERING, true);
            _kRenderingExtensions.addDoubleClickAction(expandFigure, MemorizingExpandCollapseAction.ID);
            if (getBooleanValue(LinguaFrancaSynthesis.SHOW_HYPERLINKS)) {
                // Collapse button
                KText textButton = _linguaFrancaShapeExtensions.addTextButton(expandFigure, LinguaFrancaSynthesis.TEXT_HIDE_ACTION);
                _kRenderingExtensions.to(_kRenderingExtensions.from(_kRenderingExtensions.setGridPlacementData(textButton), _kRenderingExtensions.LEFT, 8, 0, _kRenderingExtensions.TOP, 0, 0), _kRenderingExtensions.RIGHT, 8, 0, _kRenderingExtensions.BOTTOM, 0, 0);
                _kRenderingExtensions.addSingleClickAction(textButton, MemorizingExpandCollapseAction.ID);
                _kRenderingExtensions.addDoubleClickAction(textButton, MemorizingExpandCollapseAction.ID);
            }
            _kContainerRenderingExtensions.addChildArea(expandFigure);
            // Collapse Rectangle
            var collapseFigure = addModeFigure(node, mode, false);
            collapseFigure.setProperty(KlighdProperties.COLLAPSED_RENDERING, true);
            if (this.hasContent(mode)) {
                _kRenderingExtensions.addDoubleClickAction(collapseFigure, MemorizingExpandCollapseAction.ID);
                if (getBooleanValue(LinguaFrancaSynthesis.SHOW_HYPERLINKS)) {
                    // Expand button
                    KText textButton = _linguaFrancaShapeExtensions.addTextButton(collapseFigure, LinguaFrancaSynthesis.TEXT_SHOW_ACTION);
                    _kRenderingExtensions.to(_kRenderingExtensions.from(_kRenderingExtensions.setGridPlacementData(textButton), _kRenderingExtensions.LEFT, 8, 0, _kRenderingExtensions.TOP, 0, 0), _kRenderingExtensions.RIGHT, 8, 0, _kRenderingExtensions.BOTTOM, 8, 0);
                    _kRenderingExtensions.addSingleClickAction(textButton, MemorizingExpandCollapseAction.ID);
                    _kRenderingExtensions.addDoubleClickAction(textButton, MemorizingExpandCollapseAction.ID);
                }
            }
        }
        var modeChildren = HashMultimap.<ModeInstance, KNode>create();
        var nodeModes = new HashMap<KNode, ModeInstance>();
        for (var node : nodes) {
            var instance = NamedInstanceUtil.getLinkedInstance(node);
            if (instance == null && node.getProperty(CoreOptions.COMMENT_BOX)) {
                var firstEdge = IterableExtensions.<KEdge>head(node.getOutgoingEdges());
                if (firstEdge != null && firstEdge.getTarget() != null) {
                    instance = NamedInstanceUtil.getLinkedInstance(firstEdge.getTarget());
                }
            }
            if (instance != null) {
                var mode = instance.getMode(true);
                modeChildren.put(mode, node);
                nodeModes.put(node, mode);
            } else {
                modeChildren.put(null, node);
            }
        }
        var modeContainer = _kNodeExtensions.createNode();
        modeContainer.getChildren().addAll(modeNodes.values());
        var fig = addModeContainerFigure(modeContainer);
        _kRenderingExtensions.addDoubleClickAction(fig, MemorizingExpandCollapseAction.ID);
        if (modeChildren.get(null).isEmpty()) {
            _kRenderingExtensions.setInvisible(fig, true);
            DiagramSyntheses.setLayoutOption(modeContainer, CoreOptions.PADDING, new ElkPadding());
        }
        DiagramSyntheses.setLayoutOption(modeContainer, CoreOptions.NODE_SIZE_CONSTRAINTS, SizeConstraint.minimumSizeWithPorts());
        DiagramSyntheses.setLayoutOption(modeContainer, CoreOptions.EDGE_ROUTING, EdgeRouting.SPLINES);
        DiagramSyntheses.setLayoutOption(modeContainer, CoreOptions.DIRECTION, Direction.DOWN);
        DiagramSyntheses.setLayoutOption(modeContainer, CoreOptions.PORT_CONSTRAINTS, PortConstraints.FIXED_ORDER);
        var modeContainerPorts = new HashMap<KPort, KPort>();
        for (var mode : ListExtensions.reverseView(reactor.modes)) {
            var modeNode = modeNodes.get(mode);
            var edges = new HashSet<KEdge>();
            // add children
            for (var child : modeChildren.get(mode)) {
                nodes.remove(child);
                modeNode.getChildren().add(child);
                edges.addAll(child.getIncomingEdges());
                edges.addAll(child.getOutgoingEdges());
            }
            // add transitions
            var representedTargets = new HashSet<Pair<ModeInstance, ModeInstance.ModeTransitionType>>();
            for (var transition : ListExtensions.reverseView(mode.transitions)) {
                if (!representedTargets.contains(new Pair<ModeInstance, ModeInstance.ModeTransitionType>(transition.target, transition.type))) {
                    var edge = _kEdgeExtensions.createEdge();
                    edge.setSource(modeNode);
                    edge.setTarget(modeNodes.get(transition.target));
                    addTransitionFigure(edge, transition);
                    if (getBooleanValue(SHOW_TRANSITION_LABELS)) {
                        associateWith(edge, transition.getDefinition());
                    } else {
                        // Bundle similar transitions
                        representedTargets.add(new Pair<ModeInstance, ModeInstance.ModeTransitionType>(transition.target, transition.type));
                    }
                }
            }
            // handle cross hierarchy edges
            var portCopies = new HashMap<KPort, KPort>();
            for (var edge : edges) {
                if (!edge.getProperty(CoreOptions.NO_LAYOUT)) {
                    var sourceNodeMode = nodeModes.get(edge.getSource());
                    if (sourceNodeMode == null) {
                        sourceNodeMode = nodeModes.get(edge.getSource().getParent());
                    }
                    var sourceIsInMode = sourceNodeMode != null;
                    var targetNodeMode = nodeModes.get(edge.getTarget());
                    if (targetNodeMode == null) {
                        targetNodeMode = nodeModes.get(edge.getTarget().getParent());
                    }
                    var targetIsInMode = targetNodeMode != null;
                    if (!sourceIsInMode || !targetIsInMode) {
                        var node = sourceIsInMode ? edge.getTarget() : edge.getSource();
                        var port = sourceIsInMode ? edge.getTargetPort() : edge.getSourcePort();
                        var isLocal = modeChildren.get(null).contains(node);
                        if (isLocal) {
                            // Add port to mode container
                            if (modeContainerPorts.containsKey(port)) {
                                node = modeContainer;
                                port = modeContainerPorts.get(port);
                            } else {
                                var containerPort = _kPortExtensions.createPort();
                                modeContainerPorts.put(port, containerPort);
                                modeContainer.getPorts().add(containerPort);
                                _kPortExtensions.setPortSize(containerPort, 8, 4);
                                KRectangle rect = _kRenderingExtensions.addRectangle(containerPort);
                                _kRenderingExtensions.setBackground(rect, Colors.BLACK);
                                DiagramSyntheses.setLayoutOption(containerPort, CoreOptions.PORT_BORDER_OFFSET, -4.0);
                                DiagramSyntheses.setLayoutOption(containerPort, CoreOptions.PORT_SIDE, sourceIsInMode ? PortSide.EAST : PortSide.WEST);
                                var source = _utilityExtensions.sourceElement(node);
                                var label = "";
                                if (source instanceof Action) {
                                    label = ((Action) source).getName();
                                } else if (source instanceof Timer) {
                                    label = ((Timer) source).getName();
                                }
                                _kLabelExtensions.addOutsidePortLabel(containerPort, label, 8);
                                // new connection
                                var copy = EcoreUtil.copy(edge);
                                if (sourceIsInMode) {
                                    copy.setSource(modeContainer);
                                    copy.setSourcePort(containerPort);
                                    copy.setTarget(edge.getTarget());
                                } else {
                                    copy.setTarget(modeContainer);
                                    copy.setTargetPort(containerPort);
                                    copy.setSource(edge.getSource());
                                }
                                node = modeContainer;
                                port = containerPort;
                            }
                        }
                        // Duplicate port
                        if (!portCopies.containsKey(port)) {
                            var copy = EcoreUtil.copy(port);
                            portCopies.put(port, copy);
                            var dummyNode = _kNodeExtensions.createNode();
                            var newID = mode.uniqueID() + "_";
                            if (!port.getLabels().isEmpty()) {
                                newID += IterableExtensions.head(port.getLabels()).getText();
                            }
                            _utilityExtensions.setID(dummyNode, newID);
                            _kRenderingExtensions.addInvisibleContainerRendering(dummyNode);
                            dummyNode.getPorts().add(copy);
                            DiagramSyntheses.setLayoutOption(dummyNode, LayeredOptions.LAYERING_LAYER_CONSTRAINT, port.getProperty(CoreOptions.PORT_SIDE) == PortSide.WEST ? LayerConstraint.FIRST : LayerConstraint.LAST);
                            modeNode.getChildren().add(dummyNode);
                        }
                        var newPort = portCopies.get(port);
                        if (sourceIsInMode) {
                            edge.setTarget(newPort.getNode());
                            edge.setTargetPort(newPort);
                        } else {
                            edge.setSource(newPort.getNode());
                            edge.setSourcePort(newPort);
                        }
                    }
                }
            }
        }
        nodes.add(modeContainer);
    }
}
Also used : ModeInstance(org.lflang.generator.ModeInstance) MemorizingExpandCollapseAction(org.lflang.diagram.synthesis.action.MemorizingExpandCollapseAction) Action(org.lflang.lf.Action) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) KEdge(de.cau.cs.kieler.klighd.kgraph.KEdge) KNode(de.cau.cs.kieler.klighd.kgraph.KNode) LinkedHashMap(java.util.LinkedHashMap) KRectangle(de.cau.cs.kieler.klighd.krendering.KRectangle) KText(de.cau.cs.kieler.klighd.krendering.KText) Timer(org.lflang.lf.Timer) ElkPadding(org.eclipse.elk.core.math.ElkPadding) HashSet(java.util.HashSet) ModeTransitionType(org.lflang.generator.ModeInstance.ModeTransitionType) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 12 with KRectangle

use of de.cau.cs.kieler.klighd.krendering.KRectangle in project lingua-franca by lf-lang.

the class ModeDiagrams method addModeFigure.

private KContainerRendering addModeFigure(KNode node, ModeInstance mode, boolean expanded) {
    int padding = getBooleanValue(LinguaFrancaSynthesis.SHOW_HYPERLINKS) ? 8 : 6;
    var figure = _kRenderingExtensions.addRoundedRectangle(node, 13, 13, 1);
    _kContainerRenderingExtensions.setGridPlacement(figure, 1);
    _kRenderingExtensions.setLineWidth(figure, mode.isInitial() ? 3f : 1.5f);
    _kRenderingExtensions.setForeground(figure, MODE_FG);
    _kRenderingExtensions.setBackground(figure, MODE_BG);
    var background = _kRenderingExtensions.getBackground(figure);
    background.setAlpha(MODE_BG_ALPHA);
    _linguaFrancaStyleExtensions.boldLineSelectionStyle(figure);
    // Invisible container
    KRectangle container = _kContainerRenderingExtensions.addRectangle(figure);
    _kRenderingExtensions.setInvisible(container, true);
    int bottomPadding = this.hasContent(mode) && expanded ? 4 : padding;
    var from = _kRenderingExtensions.from(_kRenderingExtensions.setGridPlacementData(container), _kRenderingExtensions.LEFT, padding, 0, _kRenderingExtensions.TOP, padding, 0);
    _kRenderingExtensions.to(from, _kRenderingExtensions.RIGHT, padding, 0, _kRenderingExtensions.BOTTOM, bottomPadding, 0);
    // Centered child container
    KRectangle childContainer = _kContainerRenderingExtensions.addRectangle(container);
    this._kRenderingExtensions.setInvisible(childContainer, true);
    this._kRenderingExtensions.setPointPlacementData(childContainer, _kRenderingExtensions.LEFT, 0, 0.5f, _kRenderingExtensions.TOP, 0, 0.5f, _kRenderingExtensions.H_CENTRAL, _kRenderingExtensions.V_CENTRAL, 0, 0, 0, 0);
    this._kContainerRenderingExtensions.setGridPlacement(childContainer, 1);
    KText text = _kContainerRenderingExtensions.addText(childContainer, mode.getName());
    DiagramSyntheses.suppressSelectability(text);
    _linguaFrancaStyleExtensions.underlineSelectionStyle(text);
    return figure;
}
Also used : KText(de.cau.cs.kieler.klighd.krendering.KText) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) LayerConstraint(org.eclipse.elk.alg.layered.options.LayerConstraint) KRectangle(de.cau.cs.kieler.klighd.krendering.KRectangle)

Aggregations

KRectangle (de.cau.cs.kieler.klighd.krendering.KRectangle)12 KText (de.cau.cs.kieler.klighd.krendering.KText)8 KPolyline (de.cau.cs.kieler.klighd.krendering.KPolyline)6 KRoundedRectangle (de.cau.cs.kieler.klighd.krendering.KRoundedRectangle)6 KEdge (de.cau.cs.kieler.klighd.kgraph.KEdge)5 KNode (de.cau.cs.kieler.klighd.kgraph.KNode)5 KContainerRendering (de.cau.cs.kieler.klighd.krendering.KContainerRendering)5 KRendering (de.cau.cs.kieler.klighd.krendering.KRendering)5 Pair (org.eclipse.xtext.xbase.lib.Pair)5 KPort (de.cau.cs.kieler.klighd.kgraph.KPort)4 LineStyle (de.cau.cs.kieler.klighd.krendering.LineStyle)4 ViewSynthesisShared (de.cau.cs.kieler.klighd.krendering.ViewSynthesisShared)4 KContainerRenderingExtensions (de.cau.cs.kieler.klighd.krendering.extensions.KContainerRenderingExtensions)4 KEdgeExtensions (de.cau.cs.kieler.klighd.krendering.extensions.KEdgeExtensions)4 KRenderingExtensions (de.cau.cs.kieler.klighd.krendering.extensions.KRenderingExtensions)4 Iterables (com.google.common.collect.Iterables)3 SynthesisOption (de.cau.cs.kieler.klighd.SynthesisOption)3 Colors (de.cau.cs.kieler.klighd.krendering.Colors)3 HorizontalAlignment (de.cau.cs.kieler.klighd.krendering.HorizontalAlignment)3 KEllipse (de.cau.cs.kieler.klighd.krendering.KEllipse)3