Added "Common Node" component that ties all nodes with the same label together, e.g. B+, V+, GND, etc

This commit is contained in:
Bane Stojkovic
2022-10-25 23:06:52 +02:00
parent 544c5323d5
commit 573adc23ba
12 changed files with 338 additions and 72 deletions
Binary file not shown.
@@ -0,0 +1,206 @@
/*
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;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import org.diylc.awt.StringUtils;
import org.diylc.common.HorizontalAlignment;
import org.diylc.common.ObjectCache;
import org.diylc.common.VerticalAlignment;
import org.diylc.components.AbstractComponent;
import org.diylc.components.transform.TextTransformer;
import org.diylc.core.ComponentState;
import org.diylc.core.ICommonNode;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.BomPolicy;
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 {
public static String DEFAULT_TEXT = "X";
private static int BUFFER = 4;
private static final int STROKE_WIDTH = 2;
private static final Color BG_COLOR = Color.lightGray;
private static final long serialVersionUID = 1L;
private Point2D.Double point = new Point2D.Double(0, 0);
private String text = DEFAULT_TEXT;
private Font font = LABEL_FONT;
private Color color = LABEL_COLOR;
private Color bgColor = BG_COLOR;
@Override
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
IDrawingObserver drawingObserver) {
g2d.setFont(font.deriveFont(Font.BOLD));
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);
g2d.setColor(bgColor);
drawingObserver.startTrackingContinuityArea(true);
g2d.fill(rect);
drawingObserver.stopTrackingContinuityArea();
drawingObserver.stopTracking();
g2d.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED : color);
g2d.draw(rect);
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();
Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
int textHeight = (int) (rect.getHeight());
int textWidth = (int) (rect.getWidth());
// Center text horizontally and vertically.
int x = (width - textWidth) / 2 + 1;
int y = (height - textHeight) / 2 + fontMetrics.getAscent();
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.drawString(text, x, y);
}
@EditableProperty
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
@EditableProperty(name = "Font Size")
public int getFontSize() {
return font.getSize();
}
public void setFontSize(int size) {
font = font.deriveFont((float) size);
}
@Override
public int getControlPointCount() {
return 1;
}
@Override
public Point2D getControlPoint(int index) {
return point;
}
@Override
public boolean isControlPointSticky(int index) {
return false;
}
@Override
public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return VisibilityPolicy.WHEN_SELECTED;
}
@Override
public void setControlPoint(Point2D point, int index) {
this.point.setLocation(point);
}
@EditableProperty(name = "Label")
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
@EditableProperty(name = "Background")
public Color getBgColor() {
return bgColor;
}
public void setBgColor(Color bgColor) {
this.bgColor = bgColor;
}
@Deprecated
@Override
public String getName() {
return super.getName();
}
@EditableProperty
@Override
public String getValue() {
return text;
}
@Override
public void setValue(String value) {
this.text = value;
}
@Override
public String getControlPointNodeName(int index) {
return null;
}
@Override
public String getCommonNodeLabel() {
return text;
}
}
@@ -29,6 +29,7 @@ import org.diylc.common.ObjectCache;
import org.diylc.common.Orientation;
import org.diylc.components.AbstractComponent;
import org.diylc.core.ComponentState;
import org.diylc.core.ICommonNode;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
@@ -42,7 +43,7 @@ import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Ground", author = "Branislav Stojkovic", category = "Schematic Symbols",
instanceNamePrefix = "GND", description = "Ground schematic symbol",
zOrder = IDIYComponent.COMPONENT, bomPolicy = BomPolicy.NEVER_SHOW, autoEdit = false)
public class GroundSymbol extends AbstractComponent<Void> {
public class GroundSymbol extends AbstractComponent<Void> implements ICommonNode {
private static final long serialVersionUID = 1L;
@@ -161,14 +162,9 @@ public class GroundSymbol extends AbstractComponent<Void> {
public void setControlPoint(Point2D point, int index) {
this.point.setLocation(point);
}
//
// @Override
// public String getControlPointNodeName(int index) {
// return getName();
// }
@Override
public String getCommonPointName(int pointIndex) {
public String getCommonNodeLabel() {
return "GND";
}
@@ -1,50 +1,50 @@
/*
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;
import java.awt.Font;
import java.awt.FontMetrics;
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;
import org.diylc.components.AbstractComponent;
import org.diylc.components.transform.TextTransformer;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.BomPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
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;
import org.diylc.components.AbstractComponent;
import org.diylc.components.transform.TextTransformer;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
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,
description = "User defined label", instanceNamePrefix = "LAB", zOrder = IDIYComponent.TEXT, flexibleZOrder = true,
bomPolicy = BomPolicy.NEVER_SHOW, transformer = TextTransformer.class)
public class Label extends AbstractComponent<String> {
@@ -83,8 +83,8 @@ public class Label extends AbstractComponent<String> {
case _270:
g2d.rotate(Math.PI * 3 / 2, point.getX(), point.getY());
break;
}
}
StringUtils.drawCenteredText(g2d, text, x, y, getHorizontalAlignment(), getVerticalAlignment());
}
@@ -244,7 +244,7 @@ public class Label extends AbstractComponent<String> {
return super.getName();
}
@EditableProperty(name="Text", defaultable = false)
@EditableProperty(name="Text", defaultable = false)
@MultiLineText
@Override
public String getValue() {
@@ -254,10 +254,10 @@ public class Label extends AbstractComponent<String> {
@Override
public void setValue(String value) {
this.text = value;
}
@Override
public String getControlPointNodeName(int index) {
return null;
}
@Override
public String getControlPointNodeName(int index) {
return null;
}
}