Search in sources :

Example 1 with IProperty

use of org.eclipse.elk.graph.properties.IProperty in project lingua-franca by lf-lang.

the class ReactionPortAdjustment method modify.

@Override
public boolean modify(IStyleModifier.StyleModificationContext context) {
    try {
        KGraphElement node = context.getGraphElement();
        if (node instanceof KNode) {
            KNode knode = (KNode) node;
            // Find root node
            KNode parent = knode;
            while (parent.eContainer() != null) {
                parent = (KNode) parent.eContainer();
            }
            // Get viewer (this is a bit brittle because it fetches the viewer from some internal property)
            Map.Entry<IProperty<?>, Object> first = IterableExtensions.findFirst(parent.getAllProperties().entrySet(), it -> {
                return it.getKey().getId().equals("de.cau.cs.kieler.klighd.viewer") || it.getKey().getId().equals("klighd.layout.viewer");
            });
            Object viewer = first != null ? first.getValue() : null;
            ILayoutRecorder recorder = null;
            if (viewer instanceof IViewer) {
                recorder = ((IViewer) viewer).getViewContext().getLayoutRecorder();
            }
            if (!knode.getPorts().isEmpty()) {
                if (IterableExtensions.head(knode.getPorts()).getYpos() != 0 && !knode.getProperty(ReactionPortAdjustment.PROCESSED)) {
                    // important for incremental update animation
                    if (recorder != null) {
                        recorder.startRecording();
                    }
                    List<KPort> in = IterableExtensions.toList(IterableExtensions.sortBy(IterableExtensions.filter(knode.getPorts(), it -> {
                        return it.getProperty(CoreOptions.PORT_SIDE) == PortSide.WEST && !it.hasProperty(CoreOptions.PORT_BORDER_OFFSET);
                    }), it -> {
                        return it.getYpos();
                    }));
                    List<KPort> out = IterableExtensions.toList(IterableExtensions.sortBy(IterableExtensions.filter(knode.getPorts(), it -> {
                        return it.getProperty(CoreOptions.PORT_SIDE) == PortSide.EAST && !it.hasProperty(CoreOptions.PORT_BORDER_OFFSET);
                    }), it -> {
                        return it.getYpos();
                    }));
                    // Adjust
                    adjustPositions(IterableExtensions.indexed(in), in.size(), true);
                    adjustPositions(IterableExtensions.indexed(out), out.size(), false);
                    knode.setProperty(ReactionPortAdjustment.PROCESSED, true);
                    if (recorder != null) {
                        recorder.stopRecording(0);
                    }
                } else if (IterableExtensions.head(knode.getPorts()).getYpos() == 0) {
                    knode.setProperty(PROCESSED, false);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    // do not disturb rendering process
    }
    return false;
}
Also used : KGraphElement(de.cau.cs.kieler.klighd.kgraph.KGraphElement) KEdge(de.cau.cs.kieler.klighd.kgraph.KEdge) CoreOptions(org.eclipse.elk.core.options.CoreOptions) KNode(de.cau.cs.kieler.klighd.kgraph.KNode) KPort(de.cau.cs.kieler.klighd.kgraph.KPort) KGraphElement(de.cau.cs.kieler.klighd.kgraph.KGraphElement) Property(org.eclipse.elk.graph.properties.Property) PortSide(org.eclipse.elk.core.options.PortSide) KPoint(de.cau.cs.kieler.klighd.kgraph.KPoint) KGraphFactory(de.cau.cs.kieler.klighd.kgraph.KGraphFactory) LinguaFrancaShapeExtensions(org.lflang.diagram.synthesis.styles.LinguaFrancaShapeExtensions) Extension(org.eclipse.xtext.xbase.lib.Extension) IViewer(de.cau.cs.kieler.klighd.IViewer) List(java.util.List) IterableExtensions(org.eclipse.xtext.xbase.lib.IterableExtensions) IProperty(org.eclipse.elk.graph.properties.IProperty) Map(java.util.Map) IStyleModifier(de.cau.cs.kieler.klighd.IStyleModifier) Pair(org.eclipse.xtext.xbase.lib.Pair) ILayoutRecorder(de.cau.cs.kieler.klighd.internal.ILayoutRecorder) IViewer(de.cau.cs.kieler.klighd.IViewer) IProperty(org.eclipse.elk.graph.properties.IProperty) ILayoutRecorder(de.cau.cs.kieler.klighd.internal.ILayoutRecorder) KPort(de.cau.cs.kieler.klighd.kgraph.KPort) KNode(de.cau.cs.kieler.klighd.kgraph.KNode) Map(java.util.Map)

Example 2 with IProperty

use of org.eclipse.elk.graph.properties.IProperty in project elk by eclipse.

the class ComponentsProcessor method pack.

/**
 * Pack the given components into a single graph.
 *
 * @param components
 *            a list of components
 * @return a single graph that contains all components
 */
@SuppressWarnings("unchecked")
public TGraph pack(final List<TGraph> components) {
    if (components.size() == 1) {
        return components.get(0);
    } else if (components.size() <= 0) {
        return new TGraph();
    }
    // assign priorities and sizes
    for (TGraph graph : components) {
        int priority = 0;
        double minx = Integer.MAX_VALUE, miny = Integer.MAX_VALUE;
        double maxx = Integer.MIN_VALUE, maxy = Integer.MIN_VALUE;
        for (TNode node : graph.getNodes()) {
            priority += node.getProperty(MrTreeOptions.PRIORITY);
            minx = Math.min(minx, node.getPosition().x);
            miny = Math.min(miny, node.getPosition().y);
            maxx = Math.max(maxx, node.getPosition().x + node.getSize().x);
            maxy = Math.max(maxy, node.getPosition().y + node.getSize().y);
        }
        graph.setProperty(MrTreeOptions.PRIORITY, priority);
        graph.setProperty(InternalProperties.BB_UPLEFT, new KVector(minx, miny));
        graph.setProperty(InternalProperties.BB_LOWRIGHT, new KVector(maxx, maxy));
    }
    // sort the components by their priority and size
    Collections.sort(components, new Comparator<TGraph>() {

        public int compare(final TGraph graph1, final TGraph graph2) {
            int prio = graph2.getProperty(MrTreeOptions.PRIORITY) - graph1.getProperty(MrTreeOptions.PRIORITY);
            if (prio == 0) {
                KVector size1 = graph1.getProperty(InternalProperties.BB_LOWRIGHT).clone().sub(graph1.getProperty(InternalProperties.BB_UPLEFT));
                KVector size2 = graph2.getProperty(InternalProperties.BB_LOWRIGHT).clone().sub(graph2.getProperty(InternalProperties.BB_UPLEFT));
                return Double.compare(size1.x * size1.y, size2.x * size2.y);
            }
            return prio;
        }
    });
    TGraph result = new TGraph();
    result.copyProperties(components.get(0));
    // determine the maximal row width by the maximal box width and the total area
    double maxRowWidth = 0.0f;
    double totalArea = 0.0f;
    for (TGraph graph : components) {
        KVector size = graph.getProperty(InternalProperties.BB_LOWRIGHT).clone().sub(graph.getProperty(InternalProperties.BB_UPLEFT));
        maxRowWidth = Math.max(maxRowWidth, size.x);
        totalArea += size.x * size.y;
    }
    maxRowWidth = Math.max(maxRowWidth, (float) Math.sqrt(totalArea) * result.getProperty(MrTreeOptions.ASPECT_RATIO));
    double spacing = result.getProperty(MrTreeOptions.SPACING_NODE_NODE).doubleValue();
    // place nodes iteratively into rows
    double xpos = 0, ypos = 0, highestBox = 0, broadestRow = spacing;
    for (TGraph graph : components) {
        KVector size = graph.getProperty(InternalProperties.BB_LOWRIGHT).clone().sub(graph.getProperty(InternalProperties.BB_UPLEFT));
        if (xpos + size.x > maxRowWidth) {
            // place the graph into the next row
            xpos = 0;
            ypos += highestBox + spacing;
            highestBox = 0;
        }
        moveGraph(result, graph, xpos, ypos);
        broadestRow = Math.max(broadestRow, xpos + size.x);
        highestBox = Math.max(highestBox, size.y);
        xpos += size.x + spacing;
    }
    Map<IProperty<?>, Object> propMerge = new HashMap<IProperty<?>, Object>();
    Map<IProperty<?>, Object> debug = new HashMap<IProperty<?>, Object>();
    for (TGraph tGraph : components) {
        boolean debugMode = tGraph.getProperty(CoreOptions.DEBUG_MODE);
        Map<IProperty<?>, Object> propComp = tGraph.getAllProperties();
        for (Entry<IProperty<?>, Object> entry : propComp.entrySet()) {
            if (propMerge.containsKey(entry.getKey())) {
                if (entry.getKey().getDefault() != entry.getValue()) {
                    if (debugMode && debug.containsKey(entry.getKey())) {
                        System.err.println("Found different values for property " + entry.getKey().getId() + " in components.");
                    } else {
                        propMerge.put(entry.getKey(), entry.getValue());
                        result.setProperty((IProperty<Object>) entry.getKey(), entry.getValue());
                        if (debugMode) {
                            debug.put(entry.getKey(), entry.getValue());
                        }
                    }
                }
            } else {
                propMerge.put(entry.getKey(), entry.getValue());
                result.setProperty((IProperty<Object>) entry.getKey(), entry.getValue());
            }
        }
    }
    return result;
}
Also used : TNode(org.eclipse.elk.alg.mrtree.graph.TNode) IProperty(org.eclipse.elk.graph.properties.IProperty) HashMap(java.util.HashMap) KVector(org.eclipse.elk.core.math.KVector) TGraph(org.eclipse.elk.alg.mrtree.graph.TGraph)

Example 3 with IProperty

use of org.eclipse.elk.graph.properties.IProperty in project elk by eclipse.

the class EMapPropertyHolderImpl method copyProperties.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT
 */
public IPropertyHolder copyProperties(IPropertyHolder source) {
    if (source == null) {
        return this;
    }
    if (source instanceof EMapPropertyHolder) {
        EMapPropertyHolder other = (EMapPropertyHolder) source;
        EMap<IProperty<?>, Object> ourProps = this.getProperties();
        for (Map.Entry<IProperty<?>, Object> entry : other.getProperties()) {
            Object value = entry.getValue();
            if (value instanceof IPropertyValueProxy) {
                IPropertyValueProxy proxy = (IPropertyValueProxy) value;
                Object newValue = proxy.resolveValue(entry.getKey());
                if (newValue != null) {
                    entry.setValue(newValue);
                    value = newValue;
                }
            }
            ourProps.put(entry.getKey(), value);
        }
    } else {
        this.getProperties().putAll(source.getAllProperties());
    }
    return this;
}
Also used : IPropertyValueProxy(org.eclipse.elk.graph.properties.IPropertyValueProxy) IProperty(org.eclipse.elk.graph.properties.IProperty) InternalEObject(org.eclipse.emf.ecore.InternalEObject) EMapPropertyHolder(org.eclipse.elk.graph.EMapPropertyHolder) EMap(org.eclipse.emf.common.util.EMap) Map(java.util.Map) EcoreEMap(org.eclipse.emf.ecore.util.EcoreEMap)

Example 4 with IProperty

use of org.eclipse.elk.graph.properties.IProperty in project elk by eclipse.

the class ElkReflectTest method testSimpleProperty.

/**
 * Tests the simple {@link Property} class, in particular, that the {@link Property#getDefault()} method behaves
 * correctly.
 */
@Test
public void testSimpleProperty() throws IllegalAccessException {
    final Iterable<Class<?>> allKnownProperties = Iterables.concat(providers, otherPropertyFiles);
    for (Class<?> p : allKnownProperties) {
        Field[] resolverFields = p.getDeclaredFields();
        for (Field f : resolverFields) {
            if (f.getType().isAssignableFrom(IProperty.class)) {
                // required for private static fields
                f.setAccessible(true);
                // get the property
                IProperty<?> property = (IProperty<?>) f.get(null);
                checkPropertyDefault(property);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) IProperty(org.eclipse.elk.graph.properties.IProperty) BeforeClass(org.junit.BeforeClass) Test(org.junit.Test)

Example 5 with IProperty

use of org.eclipse.elk.graph.properties.IProperty in project elk by eclipse.

the class EMapPropertyHolderImpl method getAllProperties.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT
 */
public Map<IProperty<?>, Object> getAllProperties() {
    EMap<IProperty<?>, Object> props = getProperties();
    // check for unresolved properties
    for (Map.Entry<IProperty<?>, Object> entry : props) {
        if (entry.getValue() instanceof IPropertyValueProxy && entry.getKey() != null) {
            IPropertyValueProxy proxy = (IPropertyValueProxy) entry.getValue();
            // Try to resolve the proxy's value, maybe the layout option was
            // registered by now. If not, we preserve the proxy.
            Object value = proxy.resolveValue(entry.getKey());
            if (value != null) {
                entry.setValue(value);
            }
        }
    }
    return props.map();
}
Also used : IPropertyValueProxy(org.eclipse.elk.graph.properties.IPropertyValueProxy) IProperty(org.eclipse.elk.graph.properties.IProperty) InternalEObject(org.eclipse.emf.ecore.InternalEObject) EMap(org.eclipse.emf.common.util.EMap) Map(java.util.Map) EcoreEMap(org.eclipse.emf.ecore.util.EcoreEMap)

Aggregations

IProperty (org.eclipse.elk.graph.properties.IProperty)5 Map (java.util.Map)3 IPropertyValueProxy (org.eclipse.elk.graph.properties.IPropertyValueProxy)2 EMap (org.eclipse.emf.common.util.EMap)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2 EcoreEMap (org.eclipse.emf.ecore.util.EcoreEMap)2 IStyleModifier (de.cau.cs.kieler.klighd.IStyleModifier)1 IViewer (de.cau.cs.kieler.klighd.IViewer)1 ILayoutRecorder (de.cau.cs.kieler.klighd.internal.ILayoutRecorder)1 KEdge (de.cau.cs.kieler.klighd.kgraph.KEdge)1 KGraphElement (de.cau.cs.kieler.klighd.kgraph.KGraphElement)1 KGraphFactory (de.cau.cs.kieler.klighd.kgraph.KGraphFactory)1 KNode (de.cau.cs.kieler.klighd.kgraph.KNode)1 KPoint (de.cau.cs.kieler.klighd.kgraph.KPoint)1 KPort (de.cau.cs.kieler.klighd.kgraph.KPort)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 List (java.util.List)1 TGraph (org.eclipse.elk.alg.mrtree.graph.TGraph)1 TNode (org.eclipse.elk.alg.mrtree.graph.TNode)1