Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActivityDiagramLayouter |
|
| 1.75;1.75 |
1 | /* $Id: ActivityDiagramLayouter.java 17852 2010-01-12 19:53:56Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
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 | * mvw | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 2005-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.diagram.activity.layout; | |
40 | ||
41 | import java.awt.Dimension; | |
42 | import java.awt.Point; | |
43 | import java.util.ArrayList; | |
44 | import java.util.Iterator; | |
45 | import java.util.List; | |
46 | ||
47 | import org.argouml.model.Model; | |
48 | import org.argouml.uml.diagram.ArgoDiagram; | |
49 | import org.argouml.uml.diagram.layout.LayoutedObject; | |
50 | import org.argouml.uml.diagram.layout.Layouter; | |
51 | import org.tigris.gef.presentation.Fig; | |
52 | ||
53 | /** | |
54 | * A simple layout class for Activity Diagrams. It lays out the states in a | |
55 | * single column, densely packed on the assumption that they'll be split into | |
56 | * one or more swim lanes by the user. The graph is traversed starting at the | |
57 | * initial state and states are placed in the position where they are first | |
58 | * encountered. Nothing sophisticated is done for cycles or nodes with multiple | |
59 | * incoming or outgoing edges. | |
60 | * | |
61 | * @author Tom Morris | |
62 | * | |
63 | */ | |
64 | 0 | public class ActivityDiagramLayouter implements Layouter { |
65 | ||
66 | /* | |
67 | * The diagram to be laid out. | |
68 | */ | |
69 | private ArgoDiagram diagram; | |
70 | ||
71 | /* | |
72 | * List of objects. | |
73 | * | |
74 | * NOTE: This methods which read/write this don't appear to be used. | |
75 | */ | |
76 | 0 | private List objects = new ArrayList(); |
77 | ||
78 | /* | |
79 | * Point at which to start layout (initial state goes here). The X | |
80 | * coordinate must be greater than half the width of the widest figure to | |
81 | * be placed (because figures are centered on this location). | |
82 | */ | |
83 | 0 | private static final Point STARTING_POINT = new Point(100, 10); |
84 | ||
85 | /* | |
86 | * Amount to increment Y position by for each node placed. We pack them | |
87 | * densely on the assumption that the user is going to split them into at | |
88 | * least two swimlanes. | |
89 | */ | |
90 | private static final int OFFSET_Y = 25; | |
91 | ||
92 | /* | |
93 | * FinalState element for ActivityDiagram | |
94 | */ | |
95 | 0 | private Object finalState = null; |
96 | ||
97 | /** | |
98 | * Construct a new layout engine for an ActivityDiagram. | |
99 | * @param d the ActivityDiagram to be laid out. | |
100 | */ | |
101 | 0 | public ActivityDiagramLayouter(ArgoDiagram d) { |
102 | 0 | this.diagram = d; |
103 | 0 | } |
104 | ||
105 | /* | |
106 | * @see org.argouml.uml.diagram.layout.Layouter#add(org.argouml.uml.diagram.layout.LayoutedObject) | |
107 | */ | |
108 | public void add(LayoutedObject object) { | |
109 | 0 | objects.add(object); |
110 | 0 | } |
111 | ||
112 | /* | |
113 | * @see org.argouml.uml.diagram.layout.Layouter#remove(org.argouml.uml.diagram.layout.LayoutedObject) | |
114 | */ | |
115 | public void remove(LayoutedObject object) { | |
116 | 0 | objects.remove(object); |
117 | 0 | } |
118 | ||
119 | /* | |
120 | * @see org.argouml.uml.diagram.layout.Layouter#getObjects() | |
121 | */ | |
122 | public LayoutedObject[] getObjects() { | |
123 | 0 | return (LayoutedObject[]) objects.toArray(); |
124 | } | |
125 | ||
126 | /* | |
127 | * @see org.argouml.uml.diagram.layout.Layouter#getObject(int) | |
128 | */ | |
129 | public LayoutedObject getObject(int index) { | |
130 | 0 | return (LayoutedObject) objects.get(index); |
131 | } | |
132 | ||
133 | /* | |
134 | * @see org.argouml.uml.diagram.layout.Layouter#layout() | |
135 | */ | |
136 | public void layout() { | |
137 | 0 | Object first = null; |
138 | // Find our Initial State | |
139 | 0 | for (Iterator it = diagram.getNodes().iterator(); it.hasNext();) { |
140 | 0 | Object node = it.next(); |
141 | 0 | if (Model.getFacade().isAPseudostate(node) |
142 | && Model.getDataTypesHelper().equalsINITIALKind( | |
143 | Model.getFacade().getKind(node))) { | |
144 | 0 | first = node; |
145 | 0 | break; |
146 | } | |
147 | 0 | } |
148 | 0 | assert first != null; |
149 | 0 | assert Model.getFacade().getIncomings(first).isEmpty(); |
150 | ||
151 | // Place all the nodes | |
152 | 0 | int lastIndex = placeNodes(new ArrayList(), first, 0); |
153 | ||
154 | // Place the final state last with a little separation | |
155 | 0 | Point location = new Point(STARTING_POINT); |
156 | 0 | location.y += OFFSET_Y * (lastIndex + 2); |
157 | 0 | diagram.getContainingFig(finalState).setLocation(location); |
158 | 0 | } |
159 | ||
160 | /* | |
161 | * Recursively place all nodes pointed to by outgoing transitions. | |
162 | * | |
163 | * Because of the recursive algorithm multiple outgoing transitions | |
164 | * will end up very lopsided because one entire subgraph will be done | |
165 | * before dealing with the other transition(s). | |
166 | * | |
167 | * @param seen set of nodes seen so far | |
168 | * @param node the node to collect neighbors for | |
169 | */ | |
170 | private int placeNodes(List seen, Object node, int index) { | |
171 | 0 | if (!seen.contains(node)) { |
172 | 0 | seen.add(node); |
173 | 0 | if (Model.getFacade().isAFinalState(node)) { |
174 | 0 | finalState = node; |
175 | } | |
176 | 0 | Fig fig = diagram.getContainingFig(node); |
177 | 0 | Point location = new Point(STARTING_POINT.x - fig.getWidth() / 2, |
178 | STARTING_POINT.y + OFFSET_Y * index++); | |
179 | //System.out.println("Setting location to " + location); | |
180 | 0 | fig.setLocation(location); |
181 | 0 | for (Iterator it = Model.getFacade().getOutgoings(node).iterator(); |
182 | 0 | it.hasNext();) { |
183 | 0 | index = placeNodes(seen, Model.getFacade().getTarget(it.next()), |
184 | index); | |
185 | } | |
186 | } | |
187 | 0 | return index; |
188 | } | |
189 | ||
190 | /* | |
191 | * @see org.argouml.uml.diagram.layout.Layouter#getMinimumDiagramSize() | |
192 | */ | |
193 | public Dimension getMinimumDiagramSize() { | |
194 | 0 | return new Dimension( |
195 | STARTING_POINT.x + 300, | |
196 | STARTING_POINT.y + OFFSET_Y * objects.size() | |
197 | ); | |
198 | } | |
199 | ||
200 | } |