package ovh.gasser.newshapes.shapes; import ovh.gasser.newshapes.ShapeVisitor; import ovh.gasser.newshapes.attributes.ColorAttributes; import ovh.gasser.newshapes.attributes.SelectionAttributes; import java.awt.*; public class SText extends AbstractShape { public static final String PLACEHOLDER_TEXT = "Text"; public static final int DEFAULT_FONT_SIZE = 16; public static final String DEFAULT_FONT_NAME = "SansSerif"; public static final int DEFAULT_FONT_STYLE = Font.PLAIN; private final String text; private final int fontSize; private final String fontName; private final int fontStyle; private SText(int x, int y, String text, int fontSize, String fontName, int fontStyle) { // Initialize with a reasonable default width/height so hit testing works reliably super(new Rectangle(x, y, 100, 20)); this.text = normalizeText(text); this.fontSize = fontSize; this.fontName = fontName; this.fontStyle = fontStyle; } public static SText create(int x, int y, String text) { var shape = new SText(x, y, text, DEFAULT_FONT_SIZE, DEFAULT_FONT_NAME, DEFAULT_FONT_STYLE); shape.addAttributes(new SelectionAttributes()); shape.addAttributes(new ColorAttributes(true, false, Color.BLACK, Color.BLACK)); return shape; } private static String normalizeText(String input) { if (input == null || input.isBlank()) { return PLACEHOLDER_TEXT; } return input; } public String getText() { return text; } public int getFontSize() { return fontSize; } public String getFontName() { return fontName; } public int getFontStyle() { return fontStyle; } public void updateMeasuredBounds(int width, int height) { bounds.setSize(Math.max(width, 0), Math.max(height, 0)); } @Override public void accept(ShapeVisitor visitor) { visitor.visitText(this); } @Override public Shape clone() { var copy = new SText(getBounds().x, getBounds().y, text, fontSize, fontName, fontStyle); copy.updateMeasuredBounds(getBounds().width, getBounds().height); copy.addAttributes(new SelectionAttributes()); var attrs = (ColorAttributes) getAttributes(ColorAttributes.ID); if (attrs != null) { copy.addAttributes(new ColorAttributes(attrs.filled, attrs.stroked, attrs.filledColor, attrs.strokedColor)); } return copy; } }