mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +10:00
Allow multi-line text in label components
This commit is contained in:
@@ -3526,6 +3526,10 @@
|
||||
<releaseDate>2021-01-17 00:00:00.000 CET</releaseDate>
|
||||
<name></name>
|
||||
<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>
|
||||
<changeType>IMPROVEMENT</changeType>
|
||||
<description>Slightly fancier splash screen :)</description>
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
@@ -41,6 +42,7 @@ 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.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:
|
||||
@@ -114,7 +85,8 @@ public class Label extends AbstractComponent<String> {
|
||||
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
|
||||
@@ -274,6 +246,7 @@ public class Label extends AbstractComponent<String> {
|
||||
}
|
||||
|
||||
@EditableProperty(name="Text", defaultable = false)
|
||||
@MultiLineText
|
||||
@Override
|
||||
public String getValue() {
|
||||
return text;
|
||||
|
||||
Reference in New Issue
Block a user