]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.diagram.handler.layout;\r
13 \r
14 import java.awt.geom.Rectangle2D;\r
15 import java.util.ArrayList;\r
16 import java.util.Collection;\r
17 import java.util.List;\r
18 import java.util.Map;\r
19 import java.util.Map.Entry;\r
20 \r
21 import org.simantics.g2d.diagram.IDiagram;\r
22 import org.simantics.g2d.diagram.handler.LayoutManager;\r
23 import org.simantics.g2d.element.ElementUtils;\r
24 import org.simantics.g2d.element.IElement;\r
25 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
26 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
27 \r
28 public class FlowLayout implements LayoutManager {\r
29 \r
30     /** Diagram hint, Row justification */\r
31     public static enum Align {Left, Center, Right, Fill}\r
32 \r
33     /**\r
34      * The align specifies the alignment of elements\r
35      * in a row.\r
36      */\r
37     public static final Key ALIGN = new KeyOf(Align.class, "FlowLayout.ALIGN");\r
38 \r
39     /**\r
40      * The horizontal gap specifies the space between\r
41      * elements and between an element and borders.\r
42      */\r
43     public static final Key HGAP = new KeyOf(Double.class, "FlowLayout.HGAP");\r
44 \r
45     /**\r
46      * The vertical gap specifies the space between\r
47      * elements and between an element and borders.\r
48      */\r
49     public static final Key VGAP = new KeyOf(Double.class, "FlowLayout.VGAP");\r
50 \r
51     @Override\r
52     public void layout(IDiagram diagram, Rectangle2D bounds) {\r
53         Align align = diagram.getHint(ALIGN);\r
54         Double hgap = diagram.getHint(HGAP);\r
55         Double vgap = diagram.getHint(VGAP);\r
56         if (align==null) align = Align.Left;\r
57         if (hgap==null) hgap = 0.;\r
58         if (vgap==null) vgap = 0.;\r
59 \r
60         Map<IElement, Rectangle2D> pos = computePositions(diagram, bounds, align, hgap, vgap);\r
61         for (Entry<IElement, Rectangle2D> e : pos.entrySet()) {\r
62             ElementUtils.setPos(e.getKey(), e.getValue().getMinX(), e.getValue().getMinY());\r
63         }\r
64     }\r
65 \r
66     @Override\r
67     public Rectangle2D computeSize(IDiagram diagram, Double wHint, Double hHint)\r
68     {\r
69         Rectangle2D bounds = new Rectangle2D.Double();\r
70         Align align = diagram.getHint(ALIGN);\r
71         Double hgap = diagram.getHint(HGAP);\r
72         Double vgap = diagram.getHint(VGAP);\r
73         if (align==null) align = Align.Left;\r
74         if (hgap==null) hgap = 0.;\r
75         if (vgap==null) vgap = 0.;\r
76 \r
77         if (wHint==null) wHint = Double.MAX_VALUE;\r
78         if (hHint==null) hHint = Double.MAX_VALUE;\r
79 \r
80         bounds.setFrame(0, 0, wHint, hHint);\r
81 \r
82         Map<IElement, Rectangle2D> pos = computePositions(diagram, bounds, align, hgap, vgap);\r
83         double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE, maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;\r
84         for (IElement e : pos.keySet()) {\r
85             Rectangle2D b = pos.get(e);\r
86             if (b.getMinX() < minX) minX = b.getMinX();\r
87             if (b.getMinY() < minY) minY = b.getMinY();\r
88             if (b.getMaxX() > maxX) maxX = b.getMaxX();\r
89             if (b.getMaxY() > maxY) maxY = b.getMaxY();\r
90         }\r
91 \r
92         return new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);\r
93     }\r
94 \r
95     private Map<IElement, Rectangle2D> computePositions(IDiagram diagram, Rectangle2D bounds, Align align, double hgap, double vgap)\r
96     {\r
97         double width = bounds.getWidth();\r
98 \r
99         // Must use diagram.getSnapshot since we are not guaranteed to be in the\r
100         // diagram's canvas context thread.\r
101         Collection<IElement> elements = diagram.getSnapshot();\r
102         Map<IElement, Rectangle2D> rects = ElementUtils.getElementBoundsOnDiagram(elements, null);\r
103 \r
104         // Rows\r
105         List<List<IElement>> rows = new ArrayList<List<IElement>>();\r
106 \r
107         // Divide elements to rows\r
108         List<IElement> row = new ArrayList<IElement>();\r
109         rows.add(row);\r
110         double rowWidth = 0.0;\r
111         for (IElement e : elements)\r
112         {\r
113             Rectangle2D elementBounds = rects.get(e);\r
114 \r
115             if (row.isEmpty()) {\r
116                 row.add(e);\r
117                 rowWidth += elementBounds.getWidth();\r
118                 continue;\r
119             }\r
120 \r
121             // Add element if it fits to the row\r
122             if (rowWidth + elementBounds.getWidth() + hgap*(row.size()) < width)\r
123             {\r
124                 rowWidth += elementBounds.getWidth();\r
125                 row.add(e);\r
126                 continue;\r
127             }\r
128 \r
129             // Create a new row\r
130             rows.add( row = new ArrayList<IElement>() );\r
131             row.add(e);\r
132             rowWidth = elementBounds.getWidth();\r
133         }\r
134 \r
135         // Flush rows\r
136         double y = bounds.getMinY();\r
137         double hmargin = hgap;\r
138         for (List<IElement> row_ : rows) {\r
139             double rowHeight = 0;\r
140 \r
141             // Calc row width\r
142             double rowContentWidth = (row.size()-1) * hgap;\r
143             for (IElement e : row)\r
144                 rowContentWidth += rects.get(e).getWidth();\r
145 \r
146             double x = hgap + bounds.getMinX();\r
147             double _hgap = hgap;\r
148             if (align == Align.Center) x = (width - hmargin - hmargin - rowContentWidth) / 2;\r
149             if (align == Align.Left) x = hmargin;\r
150             if (align == Align.Right) x = width-rowContentWidth-hmargin;\r
151             if (align == Align.Fill) {\r
152                 x = hmargin;\r
153                 _hgap = (width - hmargin - hmargin - rowContentWidth) / (row.size()+1);\r
154                 //_hgap = 2;\r
155             }\r
156             for (IElement e : row_) {\r
157                 Rectangle2D rect = rects.get(e);\r
158                 rect.setFrame(x, y, rect.getWidth(), rect.getHeight());\r
159                 x += rect.getWidth() + _hgap;\r
160                 rowHeight = Math.max(rowHeight, rect.getHeight());\r
161             }\r
162 \r
163             x = bounds.getMinX();\r
164             y += rowHeight + vgap;\r
165         }\r
166 \r
167         return rects;\r
168     }\r
169 \r
170 \r
171 }\r