Add info in the SVG
[clamp.git] / src / main / java / org / onap / clamp / clds / util / drawing / ImageBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.util.drawing;
25
26 import java.awt.BasicStroke;
27 import java.awt.Point;
28 import java.awt.Shape;
29 import java.awt.Stroke;
30 import java.awt.geom.Ellipse2D;
31 import java.util.UUID;
32
33 import org.apache.batik.svggen.SVGGraphics2D;
34
35 public class ImageBuilder {
36
37     public static final int POLICY_LINE_RATIO = 2;
38     public static final int COLLECTOR_LINE_RATIO = 6;
39     public static final float MS_LINE_TO_HEIGHT_RATIO = 0.75f;
40     public static final float ARROW_TO_BASELINE_RATIO = 0.75f;
41
42     private Point currentPoint;
43     private final int baseLength;
44     private final int rectHeight;
45     private final SVGGraphics2D g2d;
46     private final DocumentBuilder documentBuilder;
47
48     private static final int LINE_THICKNESS = 2;
49     private static final int CIRCLE_RADIUS = 17;
50
51     ImageBuilder(SVGGraphics2D svgGraphics2D, DocumentBuilder documentBuilder, Point startingPoint, int baseLength,
52                  int rectHeight) {
53         this.g2d = svgGraphics2D;
54         this.documentBuilder = documentBuilder;
55         this.currentPoint = new Point(startingPoint);
56         this.baseLength = baseLength;
57         this.rectHeight = rectHeight;
58     }
59
60     ImageBuilder rectangle(String dataElementId, RectTypes rectType, String boxText, String groupingId, String uiData) {
61         Point next = new Point(currentPoint.x + baseLength, currentPoint.y);
62         Point point = coordinatesForRectangle(currentPoint, next);
63
64         handleBasedOnRectType(rectType, boxText, point, baseLength, rectHeight);
65
66         documentBuilder.pushChangestoDocument(g2d, dataElementId, groupingId, uiData);
67         currentPoint = next;
68         return this;
69     }
70
71     ImageBuilder arrow() {
72         String dataElementId = "Arrow-" + UUID.randomUUID().toString();
73         Point to = new Point(currentPoint.x + (int) (baseLength * ARROW_TO_BASELINE_RATIO), currentPoint.y);
74         AwtUtils.drawArrow(g2d, currentPoint, to, LINE_THICKNESS);
75         documentBuilder.pushChangestoDocument(g2d, dataElementId);
76         currentPoint = to;
77         return this;
78     }
79
80     ImageBuilder circle(String dataElementId, int lineThickness) {
81         Shape circleStart = new Ellipse2D.Double(currentPoint.x, ((double) currentPoint.y) - CIRCLE_RADIUS,
82                 2.0 * CIRCLE_RADIUS, 2.0 * CIRCLE_RADIUS);
83
84         Stroke oldStroke = g2d.getStroke();
85         g2d.setStroke(new BasicStroke(lineThickness));
86         g2d.draw(circleStart);
87         g2d.setStroke(oldStroke);
88         documentBuilder.pushChangestoDocument(g2d, dataElementId);
89         Point to = new Point(currentPoint.x + 2 * CIRCLE_RADIUS, currentPoint.y);
90         currentPoint = to;
91         return this;
92     }
93
94     DocumentBuilder getDocumentBuilder() {
95         return documentBuilder;
96     }
97
98     private void handleBasedOnRectType(RectTypes rectType, String text, Point point, int width, int height) {
99         AwtUtils.rectWithText(g2d, text, point, width, height);
100         switch (rectType) {
101             case COLECTOR:
102                 drawVerticalLineForCollector(point, width, height);
103                 break;
104             case MICROSERVICE:
105                 drawHorizontalLineForMicroService(point, width, height);
106                 break;
107             case POLICY:
108                 drawDiagonalLineForPolicy(point, width, height);
109                 break;
110             default:
111         }
112     }
113
114     private void drawVerticalLineForCollector(Point point, int width, int height) {
115         g2d.drawLine(point.x + width / COLLECTOR_LINE_RATIO, point.y, point.x + width / COLLECTOR_LINE_RATIO,
116                 point.y + height);
117     }
118
119     private void drawHorizontalLineForMicroService(Point point, int width, int height) {
120         int pointY = calculateMsHorizontalLineYCoordinate(point, height);
121         g2d.drawLine(point.x, pointY, point.x + width, pointY);
122     }
123
124     private void drawDiagonalLineForPolicy(Point point, int width, int height) {
125         g2d.drawLine(point.x, point.y + height / POLICY_LINE_RATIO, point.x + width / POLICY_LINE_RATIO, point.y);
126     }
127
128     private int calculateMsHorizontalLineYCoordinate(Point point, int height) {
129         return (int) (point.y * height * MS_LINE_TO_HEIGHT_RATIO);
130     }
131
132     private Point coordinatesForRectangle(Point from, Point next) {
133         int pointX = from.x;
134         int pointY = from.y - next.y + LINE_THICKNESS / 2;
135         return new Point(pointX, pointY);
136     }
137
138 }