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;
47 public Painter(SVGGraphics2D svgGraphics2D, DocumentBuilder documentBuilder) {
48 this.g2d = svgGraphics2D;
49 this.documentBuilder = documentBuilder;
50 this.canvasSize = DEFALUT_CANVAS_SIZE;
53 DocumentBuilder doPaint(String collector, List<MicroService> microServices, String policy) {
54 int numOfRectangles = 2 + microServices.size();
55 int numOfArrows = numOfRectangles + 1;
56 int baseLength = (canvasSize - 2 * CIRCLE_RADIUS) / (numOfArrows + numOfRectangles);
57 int rectHeight = (int) (baseLength / RECT_RATIO);
59 adjustGraphics2DProperties();
61 Point origin = new Point(0, rectHeight / 2);
62 ImageBuilder ib = new ImageBuilder(g2d, documentBuilder, origin, baseLength, rectHeight);
64 doTheActualDrawing(collector, microServices, policy, ib);
66 return ib.getDocumentBuilder();
69 private void doTheActualDrawing(String collector, List<MicroService> microServices, String policy,
71 ib.circle("start-circle", SLIM_LINE).arrow().rectangle(collector, RectTypes.COLECTOR, collector);
73 for (MicroService ms : microServices) {
74 ib.arrow().rectangle(ms.getMappedNameJpa(), RectTypes.MICROSERVICE, ms.getName());
77 ib.arrow().rectangle(policy, RectTypes.POLICY, policy).arrow().circle("stop-circle", THICK_LINE);
80 private void adjustGraphics2DProperties() {
81 g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
82 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
83 g2d.setStroke(new BasicStroke(SLIM_LINE));
84 g2d.setPaint(Color.BLACK);