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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.diagram.handler.layout;
\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
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
28 public class FlowLayout implements LayoutManager {
\r
30 /** Diagram hint, Row justification */
\r
31 public static enum Align {Left, Center, Right, Fill}
\r
34 * The align specifies the alignment of elements
\r
37 public static final Key ALIGN = new KeyOf(Align.class, "FlowLayout.ALIGN");
\r
40 * The horizontal gap specifies the space between
\r
41 * elements and between an element and borders.
\r
43 public static final Key HGAP = new KeyOf(Double.class, "FlowLayout.HGAP");
\r
46 * The vertical gap specifies the space between
\r
47 * elements and between an element and borders.
\r
49 public static final Key VGAP = new KeyOf(Double.class, "FlowLayout.VGAP");
\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
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
67 public Rectangle2D computeSize(IDiagram diagram, Double wHint, Double hHint)
\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
77 if (wHint==null) wHint = Double.MAX_VALUE;
\r
78 if (hHint==null) hHint = Double.MAX_VALUE;
\r
80 bounds.setFrame(0, 0, wHint, hHint);
\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
92 return new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);
\r
95 private Map<IElement, Rectangle2D> computePositions(IDiagram diagram, Rectangle2D bounds, Align align, double hgap, double vgap)
\r
97 double width = bounds.getWidth();
\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
105 List<List<IElement>> rows = new ArrayList<List<IElement>>();
\r
107 // Divide elements to rows
\r
108 List<IElement> row = new ArrayList<IElement>();
\r
110 double rowWidth = 0.0;
\r
111 for (IElement e : elements)
\r
113 Rectangle2D elementBounds = rects.get(e);
\r
115 if (row.isEmpty()) {
\r
117 rowWidth += elementBounds.getWidth();
\r
121 // Add element if it fits to the row
\r
122 if (rowWidth + elementBounds.getWidth() + hgap*(row.size()) < width)
\r
124 rowWidth += elementBounds.getWidth();
\r
129 // Create a new row
\r
130 rows.add( row = new ArrayList<IElement>() );
\r
132 rowWidth = elementBounds.getWidth();
\r
136 double y = bounds.getMinY();
\r
137 double hmargin = hgap;
\r
138 for (List<IElement> row_ : rows) {
\r
139 double rowHeight = 0;
\r
142 double rowContentWidth = (row.size()-1) * hgap;
\r
143 for (IElement e : row)
\r
144 rowContentWidth += rects.get(e).getWidth();
\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
153 _hgap = (width - hmargin - hmargin - rowContentWidth) / (row.size()+1);
\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
163 x = bounds.getMinX();
\r
164 y += rowHeight + vgap;
\r