2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
25 package org.onap.clamp.clds.util.drawing;
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;
33 import org.apache.batik.svggen.SVGGraphics2D;
34 import org.onap.clamp.clds.sdc.controller.installer.MicroService;
36 public class Painter {
37 private final int canvasSize;
38 private final SVGGraphics2D g2d;
39 private final DocumentBuilder documentBuilder;
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;
48 * Constructor to create instance of Painter.
50 * @param svgGraphics2D svg graphics
51 * @param documentBuilder document builder
53 public Painter(SVGGraphics2D svgGraphics2D, DocumentBuilder documentBuilder) {
54 this.g2d = svgGraphics2D;
55 this.documentBuilder = documentBuilder;
56 this.canvasSize = DEFALUT_CANVAS_SIZE;
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);
65 adjustGraphics2DProperties();
67 Point origin = new Point(0, rectHeight / 2);
68 ImageBuilder ib = new ImageBuilder(g2d, documentBuilder, origin, baseLength, rectHeight);
70 doTheActualDrawing(collector, microServices, policy, ib);
72 return ib.getDocumentBuilder();
75 private void doTheActualDrawing(String collector, List<MicroService> microServices, String policy,
77 ib.circle("start-circle", SLIM_LINE).arrow().rectangle(collector, RectTypes.COLECTOR, collector);
79 for (MicroService ms : microServices) {
80 ib.arrow().rectangle(ms.getMappedNameJpa(), RectTypes.MICROSERVICE, ms.getName());
83 ib.arrow().rectangle(policy, RectTypes.POLICY, policy).arrow().circle("stop-circle", THICK_LINE);
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);