]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/handler/layout/FlowLayout.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / handler / layout / FlowLayout.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/handler/layout/FlowLayout.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/handler/layout/FlowLayout.java
new file mode 100644 (file)
index 0000000..8d84864
--- /dev/null
@@ -0,0 +1,171 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.diagram.handler.layout;\r
+\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Map.Entry;\r
+\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.handler.LayoutManager;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+public class FlowLayout implements LayoutManager {\r
+\r
+    /** Diagram hint, Row justification */\r
+    public static enum Align {Left, Center, Right, Fill}\r
+\r
+    /**\r
+     * The align specifies the alignment of elements\r
+     * in a row.\r
+     */\r
+    public static final Key ALIGN = new KeyOf(Align.class, "FlowLayout.ALIGN");\r
+\r
+    /**\r
+     * The horizontal gap specifies the space between\r
+     * elements and between an element and borders.\r
+     */\r
+    public static final Key HGAP = new KeyOf(Double.class, "FlowLayout.HGAP");\r
+\r
+    /**\r
+     * The vertical gap specifies the space between\r
+     * elements and between an element and borders.\r
+     */\r
+    public static final Key VGAP = new KeyOf(Double.class, "FlowLayout.VGAP");\r
+\r
+    @Override\r
+    public void layout(IDiagram diagram, Rectangle2D bounds) {\r
+        Align align = diagram.getHint(ALIGN);\r
+        Double hgap = diagram.getHint(HGAP);\r
+        Double vgap = diagram.getHint(VGAP);\r
+        if (align==null) align = Align.Left;\r
+        if (hgap==null) hgap = 0.;\r
+        if (vgap==null) vgap = 0.;\r
+\r
+        Map<IElement, Rectangle2D> pos = computePositions(diagram, bounds, align, hgap, vgap);\r
+        for (Entry<IElement, Rectangle2D> e : pos.entrySet()) {\r
+            ElementUtils.setPos(e.getKey(), e.getValue().getMinX(), e.getValue().getMinY());\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Rectangle2D computeSize(IDiagram diagram, Double wHint, Double hHint)\r
+    {\r
+        Rectangle2D bounds = new Rectangle2D.Double();\r
+        Align align = diagram.getHint(ALIGN);\r
+        Double hgap = diagram.getHint(HGAP);\r
+        Double vgap = diagram.getHint(VGAP);\r
+        if (align==null) align = Align.Left;\r
+        if (hgap==null) hgap = 0.;\r
+        if (vgap==null) vgap = 0.;\r
+\r
+        if (wHint==null) wHint = Double.MAX_VALUE;\r
+        if (hHint==null) hHint = Double.MAX_VALUE;\r
+\r
+        bounds.setFrame(0, 0, wHint, hHint);\r
+\r
+        Map<IElement, Rectangle2D> pos = computePositions(diagram, bounds, align, hgap, vgap);\r
+        double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE, maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;\r
+        for (IElement e : pos.keySet()) {\r
+            Rectangle2D b = pos.get(e);\r
+            if (b.getMinX() < minX) minX = b.getMinX();\r
+            if (b.getMinY() < minY) minY = b.getMinY();\r
+            if (b.getMaxX() > maxX) maxX = b.getMaxX();\r
+            if (b.getMaxY() > maxY) maxY = b.getMaxY();\r
+        }\r
+\r
+        return new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);\r
+    }\r
+\r
+    private Map<IElement, Rectangle2D> computePositions(IDiagram diagram, Rectangle2D bounds, Align align, double hgap, double vgap)\r
+    {\r
+        double width = bounds.getWidth();\r
+\r
+        // Must use diagram.getSnapshot since we are not guaranteed to be in the\r
+        // diagram's canvas context thread.\r
+        Collection<IElement> elements = diagram.getSnapshot();\r
+        Map<IElement, Rectangle2D> rects = ElementUtils.getElementBoundsOnDiagram(elements, null);\r
+\r
+        // Rows\r
+        List<List<IElement>> rows = new ArrayList<List<IElement>>();\r
+\r
+        // Divide elements to rows\r
+        List<IElement> row = new ArrayList<IElement>();\r
+        rows.add(row);\r
+        double rowWidth = 0.0;\r
+        for (IElement e : elements)\r
+        {\r
+            Rectangle2D elementBounds = rects.get(e);\r
+\r
+            if (row.isEmpty()) {\r
+                row.add(e);\r
+                rowWidth += elementBounds.getWidth();\r
+                continue;\r
+            }\r
+\r
+            // Add element if it fits to the row\r
+            if (rowWidth + elementBounds.getWidth() + hgap*(row.size()) < width)\r
+            {\r
+                rowWidth += elementBounds.getWidth();\r
+                row.add(e);\r
+                continue;\r
+            }\r
+\r
+            // Create a new row\r
+            rows.add( row = new ArrayList<IElement>() );\r
+            row.add(e);\r
+            rowWidth = elementBounds.getWidth();\r
+        }\r
+\r
+        // Flush rows\r
+        double y = bounds.getMinY();\r
+        double hmargin = hgap;\r
+        for (List<IElement> row_ : rows) {\r
+            double rowHeight = 0;\r
+\r
+            // Calc row width\r
+            double rowContentWidth = (row.size()-1) * hgap;\r
+            for (IElement e : row)\r
+                rowContentWidth += rects.get(e).getWidth();\r
+\r
+            double x = hgap + bounds.getMinX();\r
+            double _hgap = hgap;\r
+            if (align == Align.Center) x = (width - hmargin - hmargin - rowContentWidth) / 2;\r
+            if (align == Align.Left) x = hmargin;\r
+            if (align == Align.Right) x = width-rowContentWidth-hmargin;\r
+            if (align == Align.Fill) {\r
+                x = hmargin;\r
+                _hgap = (width - hmargin - hmargin - rowContentWidth) / (row.size()+1);\r
+                //_hgap = 2;\r
+            }\r
+            for (IElement e : row_) {\r
+                Rectangle2D rect = rects.get(e);\r
+                rect.setFrame(x, y, rect.getWidth(), rect.getHeight());\r
+                x += rect.getWidth() + _hgap;\r
+                rowHeight = Math.max(rowHeight, rect.getHeight());\r
+            }\r
+\r
+            x = bounds.getMinX();\r
+            y += rowHeight + vgap;\r
+        }\r
+\r
+        return rects;\r
+    }\r
+\r
+\r
+}\r