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