Fix Checkstyle issues
[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.MicroService;
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
47     /**
48      * Constructor to create instance of Painter.
49      *
50      * @param svgGraphics2D svg graphics
51      * @param documentBuilder document builder
52      */
53     public Painter(SVGGraphics2D svgGraphics2D, DocumentBuilder documentBuilder) {
54         this.g2d = svgGraphics2D;
55         this.documentBuilder = documentBuilder;
56         this.canvasSize = DEFALUT_CANVAS_SIZE;
57     }
58
59     DocumentBuilder doPaint(String collector, List<MicroService> microServices, String policy) {
60         int numOfRectangles = 2 + microServices.size();
61         int numOfArrows = numOfRectangles + 1;
62         int baseLength = (canvasSize - 2 * CIRCLE_RADIUS) / (numOfArrows + numOfRectangles);
63         int rectHeight = (int) (baseLength / RECT_RATIO);
64
65         adjustGraphics2DProperties();
66
67         Point origin = new Point(0, rectHeight / 2);
68         ImageBuilder ib = new ImageBuilder(g2d, documentBuilder, origin, baseLength, rectHeight);
69
70         doTheActualDrawing(collector, microServices, policy, ib);
71
72         return ib.getDocumentBuilder();
73     }
74
75     private void doTheActualDrawing(String collector, List<MicroService> microServices, String policy,
76         ImageBuilder ib) {
77         ib.circle("start-circle", SLIM_LINE).arrow().rectangle(collector, RectTypes.COLECTOR, collector);
78
79         for (MicroService ms : microServices) {
80             ib.arrow().rectangle(ms.getMappedNameJpa(), RectTypes.MICROSERVICE, ms.getName());
81         }
82
83         ib.arrow().rectangle(policy, RectTypes.POLICY, policy).arrow().circle("stop-circle", THICK_LINE);
84     }
85
86     private void adjustGraphics2DProperties() {
87         g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
88         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
89         g2d.setStroke(new BasicStroke(SLIM_LINE));
90         g2d.setPaint(Color.BLACK);
91     }
92
93 }