Add common node shape

This commit is contained in:
Bane Stojkovic
2022-10-26 11:21:12 +02:00
parent 4ee9bced0e
commit 1323e9e7c8
3 changed files with 107 additions and 45 deletions
Binary file not shown.
@@ -1,24 +1,21 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
*
* DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* DIYLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with DIYLC. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
package org.diylc.components.misc;
import java.awt.Color;
@@ -26,9 +23,13 @@ import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.util.Locale;
import org.diylc.awt.StringUtils;
import org.diylc.common.HorizontalAlignment;
import org.diylc.common.ObjectCache;
@@ -46,13 +47,16 @@ import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
@ComponentDescriptor(name = "Common Node", author = "Branislav Stojkovic", category = "Misc",
description = "Label that ties all nodes together", instanceNamePrefix = "CN", zOrder = IDIYComponent.TEXT, flexibleZOrder = true,
bomPolicy = BomPolicy.NEVER_SHOW, transformer = TextTransformer.class)
public class CommonNode extends AbstractComponent<String> implements ICommonNode {
description = "Label that ties all nodes together", instanceNamePrefix = "CN",
zOrder = IDIYComponent.TEXT, flexibleZOrder = true, bomPolicy = BomPolicy.NEVER_SHOW,
transformer = TextTransformer.class)
public class CommonNode extends AbstractComponent<String> implements ICommonNode {
public static String DEFAULT_TEXT = "X";
private static int BUFFER = 4;
private static int BUFFER_X = 6;
private static int BUFFER_Y = 4;
private static final int STROKE_WIDTH = 2;
private static final Color BG_COLOR = Color.lightGray;
@@ -63,24 +67,33 @@ public class CommonNode extends AbstractComponent<String> implements ICommonNode
private Font font = LABEL_FONT;
private Color color = LABEL_COLOR;
private Color bgColor = BG_COLOR;
private CommonNodeShape shape = CommonNodeShape.RECTANGLE;
@Override
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
IDrawingObserver drawingObserver) {
g2d.setFont(font.deriveFont(Font.BOLD));
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode,
Project project, IDrawingObserver drawingObserver) {
g2d.setFont(font);
double x = point.getX();
double y = point.getY();
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(STROKE_WIDTH));
FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle stringBounds = fontMetrics.getStringBounds(text, g2d).getBounds();
RoundRectangle2D rect = new RoundRectangle2D.Double(x - stringBounds.getWidth() / 2 - BUFFER,
y - stringBounds.getHeight() / 2 - BUFFER, stringBounds.getWidth() + 2 * BUFFER, stringBounds.getHeight() + 2 * BUFFER,
stringBounds.getHeight() / 2, stringBounds.getHeight() / 2);
Shape rect;
if (shape == CommonNodeShape.RECTANGLE) {
rect = new RoundRectangle2D.Double(x - stringBounds.getWidth() / 2 - BUFFER_X,
y - stringBounds.getHeight() / 2 - BUFFER_Y, stringBounds.getWidth() + 2 * BUFFER_X,
stringBounds.getHeight() + 2 * BUFFER_Y, stringBounds.getHeight() / 2,
stringBounds.getHeight() / 2);
} else {
double max = Math.max(stringBounds.getWidth(), stringBounds.getHeight());
rect = new Ellipse2D.Double(x - max / 2 - BUFFER_X, y - max / 2 - BUFFER_X,
max + 2 * BUFFER_X, max + 2 * BUFFER_X);
}
g2d.setColor(bgColor);
drawingObserver.startTrackingContinuityArea(true);
g2d.fill(rect);
@@ -88,15 +101,16 @@ public class CommonNode extends AbstractComponent<String> implements ICommonNode
drawingObserver.stopTracking();
g2d.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED : color);
g2d.draw(rect);
StringUtils.drawCenteredText(g2d, text, x, y, HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
StringUtils.drawCenteredText(g2d, text, x, y, HorizontalAlignment.CENTER,
VerticalAlignment.CENTER);
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.setFont(LABEL_FONT.deriveFont(13f * width / 32).deriveFont(Font.BOLD));
String text = "B+";
FontMetrics fontMetrics = g2d.getFontMetrics();
@@ -112,7 +126,7 @@ public class CommonNode extends AbstractComponent<String> implements ICommonNode
g2d.setColor(BG_COLOR);
g2d.fillRoundRect(4, 6, width - 8, height - 12, 6, 6);
g2d.setColor(LABEL_COLOR);
g2d.drawRoundRect(4, 6, width - 8, height - 12, 6, 6);
g2d.drawRoundRect(4, 6, width - 8, height - 12, 6, 6);
g2d.drawString(text, x, y);
}
@@ -125,6 +139,30 @@ public class CommonNode extends AbstractComponent<String> implements ICommonNode
this.font = font;
}
// Bold and italic fields are named to be alphabetically after Font. This is
// important!
@EditableProperty(name = "Font Bold")
public boolean getBold() {
return font.isBold();
}
public void setBold(boolean bold) {
if (bold) {
if (font.isItalic()) {
font = font.deriveFont(Font.BOLD + Font.ITALIC);
} else {
font = font.deriveFont(Font.BOLD);
}
} else {
if (font.isItalic()) {
font = font.deriveFont(Font.ITALIC);
} else {
font = font.deriveFont(Font.PLAIN);
}
}
}
@EditableProperty(name = "Font Size")
public int getFontSize() {
return font.getSize();
@@ -157,7 +195,7 @@ public class CommonNode extends AbstractComponent<String> implements ICommonNode
@Override
public void setControlPoint(Point2D point, int index) {
this.point.setLocation(point);
}
}
@EditableProperty(name = "Label")
public Color getColor() {
@@ -167,16 +205,25 @@ public class CommonNode extends AbstractComponent<String> implements ICommonNode
public void setColor(Color color) {
this.color = color;
}
@EditableProperty(name = "Background")
public Color getBgColor() {
return bgColor;
}
public void setBgColor(Color bgColor) {
this.bgColor = bgColor;
}
@EditableProperty
public CommonNodeShape getShape() {
return shape;
}
public void setShape(CommonNodeShape shape) {
this.shape = shape;
}
@Deprecated
@Override
public String getName() {
@@ -193,7 +240,7 @@ public class CommonNode extends AbstractComponent<String> implements ICommonNode
public void setValue(String value) {
this.text = value;
}
@Override
public String getControlPointNodeName(int index) {
return null;
@@ -203,4 +250,19 @@ public class CommonNode extends AbstractComponent<String> implements ICommonNode
public String getCommonNodeLabel() {
return text;
}
public static enum CommonNodeShape {
RECTANGLE("Rectangle"), CIRCLE("Circle");
private String label;
private CommonNodeShape(String label) {
this.label = label;
}
@Override
public String toString() {
return label;
}
}
}
Binary file not shown.