1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.flowchart.layout;
15  
16  import y.base.DataProvider;
17  import y.base.Edge;
18  import y.base.Graph;
19  import y.base.GraphInterface;
20  import y.base.Node;
21  
22  /**
23   * Provides type constants and corresponding <code>isXYZType()</code> methods for Flowchart symbols. These constants and
24   * methods are used by {@link FlowchartLayouter} and its associated classes to identify specific nodes and handle them
25   * appropriately.
26   *
27   * @noinspection ImplicitNumericConversion
28   */
29  public class FlowchartElements {
30    /**
31     * Type constant for an invalid type.
32     */
33    public static final byte TYPE_INVALID = 0;
34    /**
35     * Type constant for an event type.
36     */
37    public static final byte NODE_TYPE_EVENT = 1;
38    /**
39     * Type constant for a start event type.
40     */
41    public static final byte NODE_TYPE_START_EVENT = 7;
42    /**
43     * Type constant for a end event type.
44     */
45    public static final byte NODE_TYPE_END_EVENT = 9;
46    /**
47     * Type constant for a decision type.
48     */
49    public static final byte NODE_TYPE_DECISION = 2;
50    /**
51     * Type constant for a process type.
52     */
53    public static final byte NODE_TYPE_PROCESS = 3;
54    /**
55     * Type constant for a group type.
56     */
57    public static final byte NODE_TYPE_GROUP = 8;
58    /**
59     * Type constant for a annotation type.
60     */
61    public static final byte NODE_TYPE_ANNOTATION = 10;
62    /**
63     * Type constant for a pool type.
64     */
65    public static final byte NODE_TYPE_POOL = 12;
66    /**
67     * Type constant for a data type.
68     */
69    public static final byte NODE_TYPE_DATA = 11;
70  
71    /**
72     * Type constant for a connection type (sequence flow).
73     */
74    public static final byte EDGE_TYPE_SEQUENCE_FLOW = 4;
75  
76    /**
77     * Type constant for a connection type (message flow).
78     */
79    public static final byte EDGE_TYPE_MESSAGE_FLOW = 5;
80  
81    /**
82     * Type constant for a connection type (association).
83     */
84    public static final byte EDGE_TYPE_ASSOCIATION = 6;
85  
86    /**
87     * Returns true for activity nodes. For Flowcharts, this are Process, Data, and Group. For BPMN, this are Task and
88     * Sub-Process.
89     */
90    static boolean isActivity(final Graph graph, final Node node) {
91      final byte type = getType(graph, node);
92      return (type == NODE_TYPE_PROCESS) || (type == NODE_TYPE_DATA) || (type == NODE_TYPE_GROUP);
93    }
94  
95    /**
96     * Returns true for group nodes. For BPMN, this is Sub-Process.
97     */
98    static boolean isGroup(final Graph graph, final Node node) {
99      return getType(graph, node) == NODE_TYPE_GROUP;
100   }
101 
102   /**
103    * Returns true for annotation nodes.
104    */
105   static boolean isAnnotation(final Graph graph, final Node node) {
106     return getType(graph, node) == NODE_TYPE_ANNOTATION;
107   }
108 
109   /**
110    * Returns true for event nodes. For Flowchart, this are start and terminator, delay, display, manual operation and
111    * preparation. For BPMN, this are start, end and other events.
112    */
113   static boolean isEvent(final Graph graph, final Node node) {
114     final byte type = getType(graph, node);
115     return (type == NODE_TYPE_START_EVENT) || (type == NODE_TYPE_EVENT) || (type == NODE_TYPE_END_EVENT);
116   }
117 
118   /**
119    * Returns true for start event nodes.
120    */
121   static boolean isStartEvent(final Graph graph, final Node node) {
122     return getType(graph, node) == NODE_TYPE_START_EVENT;
123   }
124 
125   /**
126    * Returns true for end event nodes.
127    */
128   static boolean isEndEvent(final Graph graph, final Node node) {
129     return getType(graph, node) == NODE_TYPE_END_EVENT;
130   }
131 
132   /**
133    * Returns true for decision nodes. For BPMN, this are all Gateways.
134    */
135   static boolean isDecision(final Graph graph, final Node node) {
136     return getType(graph, node) == NODE_TYPE_DECISION;
137   }
138 
139   static boolean isUndefined(final Graph graph, final Edge edge) {
140     return getType(graph, edge) == TYPE_INVALID;
141   }
142 
143   static boolean isRegularEdge(final Graph graph, final Edge edge) {
144     return getType(graph, edge) == EDGE_TYPE_SEQUENCE_FLOW;
145   }
146 
147   static boolean isMessageFlow(final Graph graph, final Edge edge) {
148     return getType(graph, edge) == EDGE_TYPE_MESSAGE_FLOW;
149   }
150 
151   static byte getType(final GraphInterface graph, final Edge dataHolder) {
152     final DataProvider dataProvider = graph.getDataProvider(FlowchartLayouter.EDGE_TYPE_DPKEY);
153     return dataProvider == null ? TYPE_INVALID : (byte) dataProvider.getInt(dataHolder);
154   }
155 
156   static byte getType(final GraphInterface graph, final Node dataHolder) {
157     final DataProvider dataProvider = graph.getDataProvider(FlowchartLayouter.NODE_TYPE_DPKEY);
158     return dataProvider == null ? TYPE_INVALID : (byte) dataProvider.getInt(dataHolder);
159   }
160 
161   private FlowchartElements() {
162   }
163 }
164