Text Inside Clamp box is not resizing 13/89213/2
authorPiotr Darosz <piotr.darosz@nokia.com>
Tue, 4 Jun 2019 07:18:04 +0000 (09:18 +0200)
committerPiotr Darosz <piotr.darosz@nokia.com>
Tue, 4 Jun 2019 08:33:14 +0000 (10:33 +0200)
Resizable text in Clamp boxes, better drawing space utilization.

Change-Id: If6c79b22729730a8c243845b3288724f1c611b8a
Issue-ID: CLAMP-402
Signed-off-by: Piotr Darosz <piotr.darosz@nokia.com>
src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java
src/main/java/org/onap/clamp/clds/util/drawing/ImageBuilder.java
src/main/java/org/onap/clamp/clds/util/drawing/Painter.java

index 1ece484..7a1f122 100755 (executable)
@@ -40,6 +40,7 @@ public class AwtUtils {
     private static final int FONT_STYLE = Font.PLAIN;
     private static final String FONT_FACE = "SansSerif";
     private static final Color TRANSPARENT = new Color(0.0f, 0.0f, 0.0f, 0.0f);
+    private  static final int TEXT_PADDING = 5;
 
     private AwtUtils() {
     }
@@ -51,7 +52,7 @@ public class AwtUtils {
         g2d.setColor(TRANSPARENT);
         g2d.fill(rect);
         g2d.setColor(oldColor);
-        addText(g2d, text, point.x + width / 2, point.y + height / 2);
+        addText(g2d, text, rect);
     }
 
     static void drawArrow(Graphics2D g2d, Point from, Point to, int lineThickness) {
@@ -61,17 +62,30 @@ public class AwtUtils {
         g2d.fillPolygon(new int[]{x2 - ARROW_W, x2 - ARROW_W, x2}, new int[]{to.y - ARROW_H, to.y + ARROW_H, to.y}, 3);
     }
 
-    private static void addText(Graphics2D g2d, String text, int abs, int ord) {
+    private static void addText(Graphics2D g2d, String text, Rectangle rect) {
+        int textBoundingBoxLimit = rect.width - 2* TEXT_PADDING;
         Font font = new Font(FONT_FACE, FONT_STYLE, FONT_SIZE);
-        g2d.setFont(font);
-
-        FontMetrics fm1 = g2d.getFontMetrics();
-        int w1 = fm1.stringWidth(text);
-        int x1 = abs - (w1 / 2);
+        font = scaleFontToFit(text, textBoundingBoxLimit, g2d, font);
+        Font oldFont = g2d.getFont();
 
         g2d.setFont(font);
         g2d.setColor(Color.BLACK);
-        g2d.drawString(text, x1, ord);
+        FontMetrics fm1 = g2d.getFontMetrics();
+        float x1 = rect.x + (float)(rect.width - fm1.stringWidth(text)) / 2;
+        float y1 = rect.y + (float)(rect.height - fm1.getHeight()) / 2 + fm1.getAscent();
+        g2d.drawString(text, x1, y1);
+
+        g2d.setFont(oldFont);
+    }
+
+    private static Font scaleFontToFit(String text, int width, Graphics2D g2d, Font pFont) {
+        float fontSize = pFont.getSize();
+        float fWidth = g2d.getFontMetrics(pFont).stringWidth(text);
+        if(fWidth <= width) {
+            return pFont;
+        }
+        fontSize = ((float)width / fWidth) * fontSize;
+        return pFont.deriveFont(fontSize);
     }
 
 }
index ce21c4c..5d37701 100644 (file)
@@ -36,6 +36,7 @@ public class ImageBuilder {
     public static final int POLICY_LINE_RATIO = 2;
     public static final int COLLECTOR_LINE_RATIO = 6;
     public static final float MS_LINE_TO_HEIGHT_RATIO = 0.75f;
+    public static final float ARROW_TO_BASELINE_RATIO = 0.75f;
 
     private Point currentPoint;
     private final int baseLength;
@@ -68,7 +69,7 @@ public class ImageBuilder {
 
     ImageBuilder arrow() {
         String dataElementId = "Arrow-" + UUID.randomUUID().toString();
-        Point to = new Point(currentPoint.x + baseLength, currentPoint.y);
+        Point to = new Point(currentPoint.x + (int)(baseLength*ARROW_TO_BASELINE_RATIO), currentPoint.y);
         AwtUtils.drawArrow(g2d, currentPoint, to, LINE_THICKNESS);
         documentBuilder.pushChangestoDocument(g2d, dataElementId);
         currentPoint = to;
index d88a17e..ebb267f 100755 (executable)
@@ -43,6 +43,7 @@ public class Painter {
     private static final int THICK_LINE = 4;
     private static final double RECT_RATIO = 3.0 / 2.0;
     private static final int CIRCLE_RADIUS = 17;
+    private static final int MINIMUM_BASE_LENGTH = 120;
 
     /**
      * Constructor to create instance of Painter.
@@ -60,6 +61,9 @@ public class Painter {
         int numOfRectangles = 2 + microServices.size();
         int numOfArrows = numOfRectangles + 1;
         int baseLength = (canvasSize - 2 * CIRCLE_RADIUS) / (numOfArrows + numOfRectangles);
+        if(baseLength < MINIMUM_BASE_LENGTH) {
+            baseLength = MINIMUM_BASE_LENGTH;
+        }
         int rectHeight = (int) (baseLength / RECT_RATIO);
 
         adjustGraphics2DProperties();