Allow multi-line text in label components

This commit is contained in:
bancika
2021-02-08 22:55:40 +01:00
parent 22f0950fcf
commit d2f9ef5ac2
3 changed files with 19 additions and 39 deletions
@@ -28,8 +28,10 @@ import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.diylc.common.HorizontalAlignment;
import org.diylc.common.VerticalAlignment;
@@ -70,7 +72,8 @@ public class StringUtils {
* @return a non-empty list of strings
*/
public static List<String> wrap(String str, FontMetrics fm, int maxWidth) {
List<String> lines = splitIntoLines(str);
String[] split = str.split("\\<br\\>");
List<String> lines = Arrays.stream(split).flatMap(s -> splitIntoLines(s).stream()).collect(Collectors.toList());
if (lines.size() == 0)
return lines;
@@ -203,7 +206,7 @@ public class StringUtils {
public static void drawCenteredText(Graphics2D g2d, String text, double x, double y, HorizontalAlignment horizontalAlignment,
VerticalAlignment verticalAlignment) {
String[] parts = text.split("\n");
String[] parts = text.split("\\<br\\>");
if (parts.length > 1) {
FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle stringBounds = fontMetrics.getStringBounds(parts[0], g2d).getBounds();
@@ -244,7 +247,7 @@ public class StringUtils {
case BOTTOM:
textY = y - visualBounds.y;
break;
}
}
g2d.drawString(text, (int)Math.round(textX), (int)Math.round(textY));
}
@@ -28,6 +28,7 @@ import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import org.diylc.awt.StringUtils;
import org.diylc.common.HorizontalAlignment;
import org.diylc.common.Orientation;
import org.diylc.common.VerticalAlignment;
@@ -40,7 +41,8 @@ import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.BomPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.annotations.MultiLineText;
@ComponentDescriptor(name = "Label", author = "Branislav Stojkovic", category = "Misc",
description = "User defined label", instanceNamePrefix = "L", zOrder = IDIYComponent.TEXT, flexibleZOrder = true,
@@ -68,40 +70,9 @@ public class Label extends AbstractComponent<String> {
IDrawingObserver drawingObserver) {
g2d.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED : color);
g2d.setFont(font);
FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
int textHeight = (int) rect.getHeight();
int textWidth = (int) rect.getWidth();
double x = point.getX();
double y = point.getY();
switch (getVerticalAlignment()) {
case CENTER:
y = point.getY() - textHeight / 2 + fontMetrics.getAscent();
break;
case TOP:
y = point.getY() - textHeight + fontMetrics.getAscent();
break;
case BOTTOM:
y = point.getY() + fontMetrics.getAscent();
break;
default:
throw new RuntimeException("Unexpected alignment: " + getVerticalAlignment());
}
switch (getHorizontalAlignment()) {
case CENTER:
x = point.getX() - textWidth / 2;
break;
case LEFT:
x = point.getX();
break;
case RIGHT:
x = point.getX() - textWidth;
break;
default:
throw new RuntimeException("Unexpected alignment: " + getHorizontalAlignment());
}
switch (getOrientation()) {
case _90:
@@ -113,8 +84,9 @@ public class Label extends AbstractComponent<String> {
case _270:
g2d.rotate(Math.PI * 3 / 2, point.getX(), point.getY());
break;
}
g2d.drawString(text, (int)x, (int)y);
}
StringUtils.drawCenteredText(g2d, text, x, y, getHorizontalAlignment(), getVerticalAlignment());
}
@Override
@@ -273,7 +245,8 @@ public class Label extends AbstractComponent<String> {
return super.getName();
}
@EditableProperty(name="Text", defaultable = false)
@EditableProperty(name="Text", defaultable = false)
@MultiLineText
@Override
public String getValue() {
return text;