mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
simplified component framework
This commit is contained in:
+13
-17
@@ -2,7 +2,6 @@ package org.diylc.components;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
@@ -12,7 +11,6 @@ import java.awt.Shape;
|
||||
import org.diylc.core.ComponentState;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.core.VisibilityPolicy;
|
||||
import org.diylc.core.annotations.ComponentName;
|
||||
import org.diylc.core.annotations.ControlPoint;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
import org.diylc.core.measures.Size;
|
||||
@@ -25,20 +23,19 @@ import org.diylc.utils.Constants;
|
||||
*
|
||||
* @author Branislav Stojkovic
|
||||
*/
|
||||
public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
|
||||
public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static Color LEAD_COLOR = Color.black;
|
||||
public static Color LABEL_COLOR = Color.black;
|
||||
public static Color LABEL_COLOR_SELECTED = Color.red;
|
||||
public static Font LABEL_FONT = new Font("Tahoma", Font.BOLD, 11);
|
||||
|
||||
protected Size width;
|
||||
protected Size height;
|
||||
protected Point point1 = new Point(0, 0);
|
||||
protected Point point2 = new Point((int) (Constants.GRID * 10), 0);
|
||||
protected String componentName = "New Component";
|
||||
protected String name = "New Component";
|
||||
|
||||
protected AbstractLeadedDIYComponent() {
|
||||
super();
|
||||
@@ -106,24 +103,23 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
|
||||
g2d.setColor(getBorderColor());
|
||||
g2d.draw(shape);
|
||||
// Draw label.
|
||||
g2d.setFont(LABEL_FONT);
|
||||
g2d.setFont(Constants.LABEL_FONT);
|
||||
g2d
|
||||
.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED
|
||||
: LABEL_COLOR);
|
||||
FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
|
||||
java.awt.geom.Rectangle2D textRect = fontMetrics.getStringBounds(getComponentName(), g2d);
|
||||
g2d.drawString(getComponentName(), (int) (shapeRect.width - textRect.getWidth()) / 2,
|
||||
java.awt.geom.Rectangle2D textRect = fontMetrics.getStringBounds(getName(), g2d);
|
||||
g2d.drawString(getName(), (int) (shapeRect.width - textRect.getWidth()) / 2,
|
||||
(int) (shapeRect.height - textRect.getHeight()) / 2 + fontMetrics.getAscent());
|
||||
}
|
||||
|
||||
@ComponentName
|
||||
@EditableProperty
|
||||
public String getComponentName() {
|
||||
return componentName;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setComponentName(String componentName) {
|
||||
this.componentName = componentName;
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED)
|
||||
@@ -166,7 +162,7 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((componentName == null) ? 0 : componentName.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -179,10 +175,10 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
AbstractLeadedDIYComponent other = (AbstractLeadedDIYComponent) obj;
|
||||
if (componentName == null) {
|
||||
if (other.componentName != null)
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!componentName.equals(other.componentName))
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
+1
-3
@@ -6,7 +6,6 @@ import java.awt.Shape;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
import org.diylc.core.annotations.ComponentDescriptor;
|
||||
import org.diylc.core.annotations.ComponentValue;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
import org.diylc.core.measures.Capacitance;
|
||||
import org.diylc.core.measures.CapacitanceUnit;
|
||||
@@ -14,7 +13,7 @@ import org.diylc.core.measures.Size;
|
||||
import org.diylc.core.measures.SizeUnit;
|
||||
|
||||
@ComponentDescriptor(name = "Axial Film Capacitor", author = "bancika", category = "Passive", instanceNamePrefix = "C", desciption = "test")
|
||||
public class AxialFilmCapacitor extends AbstractLeadedDIYComponent {
|
||||
public class AxialFilmCapacitor extends AbstractLeadedDIYComponent<Capacitance> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -29,7 +28,6 @@ public class AxialFilmCapacitor extends AbstractLeadedDIYComponent {
|
||||
super();
|
||||
}
|
||||
|
||||
@ComponentValue
|
||||
@EditableProperty
|
||||
public Capacitance getValue() {
|
||||
return value;
|
||||
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package org.diylc.components;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
import org.diylc.core.ComponentState;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.core.VisibilityPolicy;
|
||||
import org.diylc.core.annotations.ComponentDescriptor;
|
||||
import org.diylc.core.annotations.ControlPoint;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
import org.diylc.utils.Constants;
|
||||
|
||||
@ComponentDescriptor(name = "DIL IC", author = "bancika", category = "Semiconductors", instanceNamePrefix = "IC", desciption = "test")
|
||||
public class DIL_IC implements IDIYComponent<String> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static Color BODY_COLOR = Color.gray;
|
||||
public static Color BORDER_COLOR = Color.black;
|
||||
public static Color LABEL_COLOR = Color.white;
|
||||
public static int EDGE_RADIUS = 6;
|
||||
|
||||
private Point point1 = new Point(0, 0);
|
||||
private Point point2 = new Point((int) (Constants.GRID) * 4, (int) (Constants.GRID) * 5);
|
||||
private String name = "New Component";
|
||||
private String value = "";
|
||||
|
||||
@EditableProperty
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@EditableProperty
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED)
|
||||
public Point getPoint1() {
|
||||
return point1;
|
||||
}
|
||||
|
||||
public void setPoint1(Point point1) {
|
||||
this.point1 = point1;
|
||||
}
|
||||
|
||||
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED)
|
||||
public Point getPoint2() {
|
||||
return point2;
|
||||
}
|
||||
|
||||
public void setPoint2(Point point2) {
|
||||
this.point2 = point2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, ComponentState componentState) {
|
||||
int width = ((int) (Math.abs(point1.x - point2.x) / Constants.GRID));
|
||||
if (width % 2 == 1) {
|
||||
width--;
|
||||
}
|
||||
width = (int) (width * Constants.GRID);
|
||||
int height = ((int) (Math.abs(point1.y - point2.y) / Constants.GRID));
|
||||
if (height % 2 == 1) {
|
||||
height--;
|
||||
}
|
||||
height = (int) (height * Constants.GRID);
|
||||
int x = (point1.x + point2.x - width) / 2;
|
||||
int y = (point1.y + point2.y - height) / 2;
|
||||
if (componentState != ComponentState.DRAGGING) {
|
||||
g2d.setColor(BODY_COLOR);
|
||||
g2d.fillRoundRect(x, y, width, height, EDGE_RADIUS, EDGE_RADIUS);
|
||||
}
|
||||
g2d.setColor(BORDER_COLOR);
|
||||
g2d.drawRoundRect(x, y, width, height, EDGE_RADIUS, EDGE_RADIUS);
|
||||
// Draw label.
|
||||
g2d.setFont(Constants.LABEL_FONT);
|
||||
g2d.setColor(LABEL_COLOR);
|
||||
FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
|
||||
Rectangle2D rect = fontMetrics.getStringBounds(getName(), g2d);
|
||||
int textHeight = (int) (rect.getHeight());
|
||||
int textWidth = (int) (rect.getWidth());
|
||||
// Center text horizontally and vertically
|
||||
x = (width - textWidth) / 2;
|
||||
y = (height - textHeight) / 2 + fontMetrics.getAscent();
|
||||
g2d.drawString(getName(), x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawIcon(Graphics2D g2d, int width, int height) {
|
||||
g2d.drawString("IC", 10, 10);
|
||||
}
|
||||
}
|
||||
+1
-5
@@ -7,8 +7,6 @@ import java.awt.Point;
|
||||
import org.diylc.core.ComponentState;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.core.VisibilityPolicy;
|
||||
import org.diylc.core.annotations.ComponentName;
|
||||
import org.diylc.core.annotations.ComponentValue;
|
||||
import org.diylc.core.annotations.ComponentDescriptor;
|
||||
import org.diylc.core.annotations.ControlPoint;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
@@ -47,7 +45,6 @@ public class MockDIYComponent implements IDIYComponent {
|
||||
super();
|
||||
}
|
||||
|
||||
@ComponentName
|
||||
@EditableProperty
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -75,9 +72,8 @@ public class MockDIYComponent implements IDIYComponent {
|
||||
this.testField = testField;
|
||||
}
|
||||
|
||||
@ComponentValue
|
||||
@EditableProperty(defaultable = true)
|
||||
public Capacitance getC() {
|
||||
public Capacitance getValue() {
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
+1
-3
@@ -6,7 +6,6 @@ import java.awt.Shape;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
|
||||
import org.diylc.core.annotations.ComponentDescriptor;
|
||||
import org.diylc.core.annotations.ComponentValue;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
import org.diylc.core.measures.Capacitance;
|
||||
import org.diylc.core.measures.CapacitanceUnit;
|
||||
@@ -14,7 +13,7 @@ import org.diylc.core.measures.Size;
|
||||
import org.diylc.core.measures.SizeUnit;
|
||||
|
||||
@ComponentDescriptor(name = "Ceramic Capacitor", author = "bancika", category = "Passive", instanceNamePrefix = "C", desciption = "test")
|
||||
public class RadialCeramicDiskCapacitor extends AbstractLeadedDIYComponent {
|
||||
public class RadialCeramicDiskCapacitor extends AbstractLeadedDIYComponent<Capacitance> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -29,7 +28,6 @@ public class RadialCeramicDiskCapacitor extends AbstractLeadedDIYComponent {
|
||||
super();
|
||||
}
|
||||
|
||||
@ComponentValue
|
||||
@EditableProperty
|
||||
public Capacitance getValue() {
|
||||
return value;
|
||||
|
||||
+1
-3
@@ -6,7 +6,6 @@ import java.awt.Shape;
|
||||
import java.awt.geom.RoundRectangle2D;
|
||||
|
||||
import org.diylc.core.annotations.ComponentDescriptor;
|
||||
import org.diylc.core.annotations.ComponentValue;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
import org.diylc.core.measures.Capacitance;
|
||||
import org.diylc.core.measures.CapacitanceUnit;
|
||||
@@ -14,7 +13,7 @@ import org.diylc.core.measures.Size;
|
||||
import org.diylc.core.measures.SizeUnit;
|
||||
|
||||
@ComponentDescriptor(name = "Radial Film Capacitor", author = "bancika", category = "Passive", instanceNamePrefix = "C", desciption = "test")
|
||||
public class RadialFilmCapacitor extends AbstractLeadedDIYComponent {
|
||||
public class RadialFilmCapacitor extends AbstractLeadedDIYComponent<Capacitance> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -29,7 +28,6 @@ public class RadialFilmCapacitor extends AbstractLeadedDIYComponent {
|
||||
super();
|
||||
}
|
||||
|
||||
@ComponentValue
|
||||
@EditableProperty
|
||||
public Capacitance getValue() {
|
||||
return value;
|
||||
|
||||
+2
-4
@@ -6,7 +6,6 @@ import java.awt.Shape;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
import org.diylc.core.annotations.ComponentDescriptor;
|
||||
import org.diylc.core.annotations.ComponentValue;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
import org.diylc.core.measures.Resistance;
|
||||
import org.diylc.core.measures.ResistanceUnit;
|
||||
@@ -14,7 +13,7 @@ import org.diylc.core.measures.Size;
|
||||
import org.diylc.core.measures.SizeUnit;
|
||||
|
||||
@ComponentDescriptor(name = "Resistor", author = "bancika", category = "Passive", instanceNamePrefix = "R", desciption = "test")
|
||||
public class Resistor extends AbstractLeadedDIYComponent {
|
||||
public class Resistor extends AbstractLeadedDIYComponent<Resistance> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -29,12 +28,11 @@ public class Resistor extends AbstractLeadedDIYComponent {
|
||||
super();
|
||||
}
|
||||
|
||||
@ComponentValue
|
||||
@EditableProperty
|
||||
public Resistance getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
public void setValue(Resistance value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user