4d76581c3d956cfbeabcccb9470c32aac473abe1
[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 import org.apache.batik.svggen.SVGGraphics2D;
33
34 public class ImageBuilder {
35
36     public static final int POLICY_LINE_RATIO = 2;
37     public static final int COLLECTOR_LINE_RATIO = 6;
38     public static final float MS_LINE_TO_HEIGHT_RATIO = 0.75f;
39
40     private Point currentPoint;
41     private final int baseLength;
42     private final int rectHeight;
43     private final SVGGraphics2D g2d;
44     private final DocumentBuilder documentBuilder;
45
46     private static final int LINE_THICKNESS = 2;
47     private static final int CIRCLE_RADIUS = 17;
48
49     ImageBuilder(SVGGraphics2D svgGraphics2D, DocumentBuilder documentBuilder,
50             Point startingPoint, int baseLength, int rectHeight) {
51         this.g2d = svgGraphics2D;
52         this.documentBuilder = documentBuilder;
53         this.currentPoint = new Point(startingPoint);
54         this.baseLength = baseLength;
55         this.rectHeight = rectHeight;
56     }
57
58     ImageBuilder rectangle(String dataElementId, RectTypes rectType, String text) {
59         Point next = new Point(currentPoint.x + baseLength, currentPoint.y);
60         Point p = coordinatesForRectangle(currentPoint, next);
61
62         handleBasedOnRectType(rectType, text, p, baseLength, rectHeight);
63
64         documentBuilder.pushChangestoDocument(g2d, dataElementId);
65         currentPoint = next;
66         return this;
67     }
68
69     ImageBuilder arrow() {
70         String dataElementId = "Arrow-" + UUID.randomUUID().toString();
71         Point to = new Point(currentPoint.x + baseLength, currentPoint.y);
72         AwtUtils.drawArrow(g2d, currentPoint, to, LINE_THICKNESS);
73         documentBuilder.pushChangestoDocument(g2d, dataElementId);
74         currentPoint = to;
75         return this;
76     }
77
78     ImageBuilder circle(String dataElementId, int lineThickness) {
79         Point to = new Point(currentPoint.x + 2 * CIRCLE_RADIUS, currentPoint.y);
80         Shape circleStart =
81             new Ellipse2D.Double(currentPoint.x, currentPoint.y - CIRCLE_RADIUS,
82                 2 * CIRCLE_RADIUS, 2 * 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         currentPoint = to;
90         return this;
91     }
92
93     DocumentBuilder getDocumentBuilder() {
94         return documentBuilder;
95     }
96
97     private void handleBasedOnRectType(RectTypes rectType, String text, Point p, int w, int h) {
98         AwtUtils.rectWithText(g2d, text, p, w, h);
99         switch (rectType) {
100             case COLECTOR:
101                 drawVerticalLineForCollector(p, w, h);
102                 break;
103             case MICROSERVICE:
104                 drawHorizontalLineForMicroService(p, w, h);
105                 break;
106             case POLICY:
107                 drawDiagonalLineForPolicy(p, w, h);
108                 break;
109         }
110     }
111
112     private void drawVerticalLineForCollector(Point p, int w, int h) {
113         g2d.drawLine(p.x + w / COLLECTOR_LINE_RATIO, p.y, p.x + w / COLLECTOR_LINE_RATIO, p.y + h);
114     }
115
116     private void drawHorizontalLineForMicroService(Point p, int w, int h) {
117         int y = calculateMsHorizontalLineYCoordinate(p,h);
118         g2d.drawLine(p.x, y, p.x + w, y);
119     }
120
121     private void drawDiagonalLineForPolicy(Point p, int w, int h) {
122         g2d.drawLine(p.x, p.y + h / POLICY_LINE_RATIO, p.x + w / POLICY_LINE_RATIO, p.y);
123     }
124
125     private int calculateMsHorizontalLineYCoordinate(Point p, int h) {
126         return (int)(p.y * h * MS_LINE_TO_HEIGHT_RATIO);
127     }
128
129     private Point coordinatesForRectangle(Point from, Point next) {
130         int x = from.x;
131         int y = from.y - next.y + LINE_THICKNESS / 2;
132         return new Point(x,y);
133     }
134
135 }