View Javadoc

1   /**
2    * Copyright 2011-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.maven.plugins.graph.dot;
17  
18  import java.util.ArrayList;
19  
20  import org.kuali.maven.plugins.graph.pojo.Edge;
21  import org.kuali.maven.plugins.graph.pojo.GraphNode;
22  import org.kuali.maven.plugins.graph.pojo.MavenContext;
23  import org.kuali.maven.plugins.graph.pojo.Scope;
24  import org.kuali.maven.plugins.graph.pojo.State;
25  import org.kuali.maven.plugins.graph.pojo.Style;
26  import org.kuali.maven.plugins.graph.processor.StyleProcessor;
27  import org.kuali.maven.plugins.graph.tree.Node;
28  import org.kuali.maven.plugins.graph.tree.TreeHelper;
29  import org.kuali.maven.plugins.graph.util.Counter;
30  
31  /**
32   * <p>
33   * Draw one parent->child edge for each node in the graph.
34   * </p>
35   */
36  public class EdgeGenerator {
37      StyleProcessor sp = new StyleProcessor();
38      Counter counter = new Counter(1);
39  
40      /**
41       * <p>
42       * Add this edge to the GraphNode contained in this node. If there are no edges on the GraphNode yet, create a
43       * <code>List<Edge></code> and store it on the GraphNode
44       * </p>
45       */
46      public void addEdge(Node<MavenContext> node, Edge edge) {
47          MavenContext context = node.getObject();
48          GraphNode graphNode = context.getGraphNode();
49          if (graphNode.getEdges() == null) {
50              graphNode.setEdges(new ArrayList<Edge>());
51          }
52          graphNode.getEdges().add(edge);
53      }
54  
55      /**
56       * <p>
57       * Create an edge running from this node's parent to itself
58       * </p>
59       */
60      public Edge getParentChildEdge(Node<MavenContext> node) {
61          GraphNode parent = node.getParent().getObject().getGraphNode();
62          MavenContext context = node.getObject();
63          GraphNode child = context.getGraphNode();
64          boolean optional = context.isOptional();
65          State state = context.getState();
66          Scope scope = Scope.getScope(context.getArtifact().getScope());
67          return getStyledEdge(parent, child, optional, scope, state);
68      }
69  
70      /**
71       * <p>
72       * Create an edge running from this node's parent to the replacement node
73       * </p>
74       */
75      public Edge getParentChildEdge(Node<MavenContext> node, Node<MavenContext> replacement, State state) {
76          GraphNode parent = node.getParent().getObject().getGraphNode();
77          MavenContext context = node.getObject();
78          GraphNode child = replacement.getObject().getGraphNode();
79          boolean optional = context.isOptional();
80          Scope scope = Scope.getScope(context.getArtifact().getScope());
81          return getStyledEdge(parent, child, optional, scope, state);
82      }
83  
84      /**
85       * <p>
86       * Create an edge running from this node's parent to the replacement node
87       * </p>
88       */
89      public Edge getParentChildEdge(Node<MavenContext> node, Node<MavenContext> replacement) {
90          return getParentChildEdge(node, replacement, node.getObject().getState());
91      }
92  
93      /**
94       * <p>
95       * Create an edge between the indicated parent and child using styling for the indicated scope, state, and optional
96       * settings
97       * </p>
98       */
99      public Edge getStyledEdge(GraphNode parent, GraphNode child, boolean optional, Scope scope, State state) {
100         Style style = sp.getStyle(scope, optional, state);
101         int id = counter.increment();
102         String label = TreeHelper.getRelationshipLabel(scope, optional, state);
103         Edge edge = new Edge(parent, child);
104         edge.setId(id);
105         sp.copyStyleProperties(edge, style);
106         edge.setLabel(label);
107         return edge;
108     }
109 }