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
@@ -3526,6 +3526,10 @@
<releaseDate>2021-01-17 00:00:00.000 CET</releaseDate> <releaseDate>2021-01-17 00:00:00.000 CET</releaseDate>
<name></name> <name></name>
<changes> <changes>
<org.diylc.appframework.update.Change>
<changeType>NEW_FEATURE</changeType>
<description>Allow multi-line text in label components</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change> <org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType> <changeType>IMPROVEMENT</changeType>
<description>Slightly fancier splash screen :)</description> <description>Slightly fancier splash screen :)</description>
@@ -28,8 +28,10 @@ import java.awt.Rectangle;
import java.awt.font.FontRenderContext; import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector; import java.awt.font.GlyphVector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import org.diylc.common.HorizontalAlignment; import org.diylc.common.HorizontalAlignment;
import org.diylc.common.VerticalAlignment; import org.diylc.common.VerticalAlignment;
@@ -70,7 +72,8 @@ public class StringUtils {
* @return a non-empty list of strings * @return a non-empty list of strings
*/ */
public static List<String> wrap(String str, FontMetrics fm, int maxWidth) { 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) if (lines.size() == 0)
return lines; return lines;
@@ -203,7 +206,7 @@ public class StringUtils {
public static void drawCenteredText(Graphics2D g2d, String text, double x, double y, HorizontalAlignment horizontalAlignment, public static void drawCenteredText(Graphics2D g2d, String text, double x, double y, HorizontalAlignment horizontalAlignment,
VerticalAlignment verticalAlignment) { VerticalAlignment verticalAlignment) {
String[] parts = text.split("\n"); String[] parts = text.split("\\<br\\>");
if (parts.length > 1) { if (parts.length > 1) {
FontMetrics fontMetrics = g2d.getFontMetrics(); FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle stringBounds = fontMetrics.getStringBounds(parts[0], g2d).getBounds(); Rectangle stringBounds = fontMetrics.getStringBounds(parts[0], g2d).getBounds();
@@ -244,7 +247,7 @@ public class StringUtils {
case BOTTOM: case BOTTOM:
textY = y - visualBounds.y; textY = y - visualBounds.y;
break; break;
} }
g2d.drawString(text, (int)Math.round(textX), (int)Math.round(textY)); 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.Point2D;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import org.diylc.awt.StringUtils;
import org.diylc.common.HorizontalAlignment; import org.diylc.common.HorizontalAlignment;
import org.diylc.common.Orientation; import org.diylc.common.Orientation;
import org.diylc.common.VerticalAlignment; import org.diylc.common.VerticalAlignment;
@@ -40,7 +41,8 @@ import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy; import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.BomPolicy; import org.diylc.core.annotations.BomPolicy;
import org.diylc.core.annotations.ComponentDescriptor; 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", @ComponentDescriptor(name = "Label", author = "Branislav Stojkovic", category = "Misc",
description = "User defined label", instanceNamePrefix = "L", zOrder = IDIYComponent.TEXT, flexibleZOrder = true, description = "User defined label", instanceNamePrefix = "L", zOrder = IDIYComponent.TEXT, flexibleZOrder = true,
@@ -68,40 +70,9 @@ public class Label extends AbstractComponent<String> {
IDrawingObserver drawingObserver) { IDrawingObserver drawingObserver) {
g2d.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED : color); g2d.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED : color);
g2d.setFont(font); 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 x = point.getX();
double y = point.getY(); 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()) { switch (getOrientation()) {
case _90: case _90:
@@ -113,8 +84,9 @@ public class Label extends AbstractComponent<String> {
case _270: case _270:
g2d.rotate(Math.PI * 3 / 2, point.getX(), point.getY()); g2d.rotate(Math.PI * 3 / 2, point.getX(), point.getY());
break; break;
} }
g2d.drawString(text, (int)x, (int)y);
StringUtils.drawCenteredText(g2d, text, x, y, getHorizontalAlignment(), getVerticalAlignment());
} }
@Override @Override
@@ -273,7 +245,8 @@ public class Label extends AbstractComponent<String> {
return super.getName(); return super.getName();
} }
@EditableProperty(name="Text", defaultable = false) @EditableProperty(name="Text", defaultable = false)
@MultiLineText
@Override @Override
public String getValue() { public String getValue() {
return text; return text;