feat: implement SText shape
This commit is contained in:
79
src/main/java/ovh/gasser/newshapes/shapes/SText.java
Normal file
79
src/main/java/ovh/gasser/newshapes/shapes/SText.java
Normal file
@@ -0,0 +1,79 @@
|
||||
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) {
|
||||
super(new Rectangle(x, y, 0, 0));
|
||||
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) {
|
||||
getBounds().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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user