d96c9e537f5f3010b75f2f7d34a0ba0613078957
[clamp.git] / src / main / java / org / onap / clamp / clds / util / drawing / Painter.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  * Modifications copyright (c) 2019 AT&T
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.util.drawing;
26
27 import java.awt.BasicStroke;
28 import java.awt.Color;
29 import java.awt.Point;
30 import java.awt.RenderingHints;
31 import java.util.List;
32
33 import org.apache.batik.svggen.SVGGraphics2D;
34 import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService;
35
36 public class Painter {
37     private final int canvasSize;
38     private final SVGGraphics2D g2d;
39     private final DocumentBuilder documentBuilder;
40
41     private static final int DEFALUT_CANVAS_SIZE = 900;
42     private static final int SLIM_LINE = 2;
43     private static final int THICK_LINE = 4;
44     private static final double RECT_RATIO = 3.0 / 2.0;
45     private static final int CIRCLE_RADIUS = 17;
46     private static final int MINIMUM_BASE_LENGTH = 120;
47
48     /**
49      * Constructor to create instance of Painter.
50      *
51      * @param svgGraphics2D   svg graphics
52      * @param documentBuilder document builder
53      */
54     public Painter(SVGGraphics2D svgGraphics2D, DocumentBuilder documentBuilder) {
55         this.g2d = svgGraphics2D;
56         this.documentBuilder = documentBuilder;
57         this.canvasSize = DEFALUT_CANVAS_SIZE;
58     }
59
60     DocumentBuilder doPaint(String collector, List<BlueprintMicroService> microServices, String policy) {
61         int numOfRectangles = 2 + microServices.size();
62         int numOfArrows = numOfRectangles + 1;
63         int baseLength = (canvasSize - 2 * CIRCLE_RADIUS) / (numOfArrows + numOfRectangles);
64         if (baseLength < MINIMUM_BASE_LENGTH) {
65             baseLength = MINIMUM_BASE_LENGTH;
66         }
67         int rectHeight = (int) (baseLength / RECT_RATIO);
68
69         adjustGraphics2DProperties();
70
71         Point origin = new Point(1, rectHeight / 2);
72         ImageBuilder ib = new ImageBuilder(g2d, documentBuilder, origin, baseLength, rectHeight);
73
74         doTheActualDrawing(collector, microServices, policy, ib);
75
76         return ib.getDocumentBuilder();
77     }
78
79     private void doTheActualDrawing(String collector, List<BlueprintMicroService> microServices, String policy,
80             ImageBuilder ib) {
81         ib.circle("start-circle", SLIM_LINE).arrow().rectangle(collector, RectTypes.COLECTOR, collector);
82
83         for (BlueprintMicroService ms : microServices) {
84             ib.arrow().rectangle(ms.getModelType(), RectTypes.MICROSERVICE, ms.getName());
85         }
86
87         ib.arrow().rectangle(policy, RectTypes.POLICY, policy).arrow().circle("stop-circle", THICK_LINE);
88     }
89
90     private void adjustGraphics2DProperties() {
91         g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
92         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
93         g2d.setStroke(new BasicStroke(SLIM_LINE));
94         g2d.setPaint(Color.BLACK);
95     }
96
97 }