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