From 25b92f4536dc1ea7bcf9dc7bee1a0bb991e5ac2a Mon Sep 17 00:00:00 2001 From: Moemoea Fierin <moemoea.fierin@epita.fr> Date: Tue, 1 Oct 2019 13:42:08 +0200 Subject: [PATCH] [ZOOM] Issue #31: created function to check if text can be drawn - text wont be draw if its size is bigger than the box size and iconSize - "..." shall be printed if some attribute could not be printed due to lack of space --- .../ui/tmlcompd/TMLCPrimitiveComponent.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/ui/tmlcompd/TMLCPrimitiveComponent.java b/src/main/java/ui/tmlcompd/TMLCPrimitiveComponent.java index e0d17901bd..54b89c716a 100755 --- a/src/main/java/ui/tmlcompd/TMLCPrimitiveComponent.java +++ b/src/main/java/ui/tmlcompd/TMLCPrimitiveComponent.java @@ -138,6 +138,19 @@ public class TMLCPrimitiveComponent extends TGCScalableWithInternalComponent imp return new Color(201, 243, 188 - (getMyDepth() * 10), 200); } + private boolean canTextGoInTheBox(Graphics g, int fontSize, String text) + { + int txtWidth = g.getFontMetrics().stringWidth(text) + (textX * 2); + int spaceTakenByIcon = iconSize + textX; + return (fontSize + (textY * 2) < height) // enough space in height + && (txtWidth + spaceTakenByIcon < width) // enough space in width + ; + } + /** + * Function which is drawing the box, the text and icon + * Issue #31: Fixed zoom on texts and icon + made sure that if the text can't go into the box is does not get drawn + * @param g + */ @Override public void internalDrawing(Graphics g) { @@ -154,7 +167,7 @@ public class TMLCPrimitiveComponent extends TGCScalableWithInternalComponent imp int centerOfBox = (width - stringWidth) / 2; Font f = g.getFont(); currentFontSize = f.getSize(); - if (currentFontSize + (textY * 2) < height) + if (canTextGoInTheBox(g, currentFontSize, value)) { //put title in bold before drawing then set back to normal after g.setFont(f.deriveFont(Font.BOLD)); @@ -162,12 +175,12 @@ public class TMLCPrimitiveComponent extends TGCScalableWithInternalComponent imp g.setFont(f); } - // Icon - g.drawImage(scale(IconManager.imgic1200.getImage()), x + width - scale(iconSize) - textX, y + textX, null); + // Scaled ICON drawing + g.drawImage(scale(IconManager.imgic1200.getImage()), x + width - iconSize - textX, y + textX, null); if (isAttacker) - g.drawImage(scale(IconManager.imgic7008.getImage()), x + width - scale(2 * iconSize) - textX, y + 2 * textX, null); + g.drawImage(scale(IconManager.imgic7008.getImage()), x + width - 2 * iconSize - textX, y + 2 * textX, null); - // Attributes + // Attributes printing if (tdp.areAttributesVisible()) { //spaces permits the attributes to not override each other @@ -178,10 +191,15 @@ public class TMLCPrimitiveComponent extends TGCScalableWithInternalComponent imp { attribute = myAttributes.get(i); spaces += currentFontSize; - attributeStr = attribute.toString(); - g.drawString(attributeStr, x + textX, y + spaces); - drawVerification(g, x + textX, y + spaces, attribute.getConfidentialityVerification()); - } + attributeStr = attribute.toString(); + if (canTextGoInTheBox(g, spaces, attributeStr)) + { + g.drawString(attributeStr, x + textX, y + spaces); + drawVerification(g, x + textX, y + spaces, attribute.getConfidentialityVerification()); + } + else // if we could not display some attributes it will show a ... + g.drawString("...", x + textX, y + height - 15); + } } } /* -- GitLab