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