mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
auto-increment names
abstract leaded component
This commit is contained in:
+94
@@ -0,0 +1,94 @@
|
|||||||
|
package com.diyfever.diylc.components;
|
||||||
|
|
||||||
|
import java.awt.Point;
|
||||||
|
|
||||||
|
import com.diyfever.diylc.model.IComponentInstance;
|
||||||
|
import com.diyfever.diylc.model.VisibilityPolicy;
|
||||||
|
import com.diyfever.diylc.model.annotations.BomName;
|
||||||
|
import com.diyfever.diylc.model.annotations.ControlPoint;
|
||||||
|
import com.diyfever.diylc.model.annotations.EditableProperty;
|
||||||
|
import com.diyfever.diylc.model.measures.Size;
|
||||||
|
import com.diyfever.diylc.utils.Constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for all leaded components such as resistors or capacitors. Has two
|
||||||
|
* control points for leads and one control point for the label.
|
||||||
|
*
|
||||||
|
* @author Branislav Stojkovic
|
||||||
|
*/
|
||||||
|
public abstract class AbstractLeadedComponentInstance implements IComponentInstance {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
protected Size width;
|
||||||
|
protected Size height;
|
||||||
|
protected Point point1 = new Point(0, 0);
|
||||||
|
protected Point point2 = new Point(0, (int) (Constants.GRID * 10));
|
||||||
|
protected Point labelPoint = new Point((int) Constants.GRID, (int) (Constants.GRID * 5));
|
||||||
|
protected String name = "New Component";
|
||||||
|
|
||||||
|
protected AbstractLeadedComponentInstance() {
|
||||||
|
super();
|
||||||
|
this.width = getDefaultWidth();
|
||||||
|
this.height = getDefaultHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Size getDefaultWidth();
|
||||||
|
|
||||||
|
protected abstract Size getDefaultHeight();
|
||||||
|
|
||||||
|
@BomName
|
||||||
|
@EditableProperty
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED)
|
||||||
|
public Point getLabelPoint() {
|
||||||
|
return labelPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLabelPoint(Point labelPoint) {
|
||||||
|
this.labelPoint = labelPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EditableProperty(defaultable = true)
|
||||||
|
public Size getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWidth(Size width) {
|
||||||
|
this.width = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EditableProperty(defaultable = true)
|
||||||
|
public Size getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHeight(Size height) {
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -13,6 +13,11 @@ public class MockComponentType implements IComponentType {
|
|||||||
public Class<? extends IComponentInstance> getComponentInstanceClass() {
|
public Class<? extends IComponentInstance> getComponentInstanceClass() {
|
||||||
return MockComponentInstance.class;
|
return MockComponentInstance.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getInstanceNamePrefix() {
|
||||||
|
return "ABC";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ComponentLayer getComponentLayer() {
|
public ComponentLayer getComponentLayer() {
|
||||||
|
|||||||
+12
-53
@@ -20,7 +20,7 @@ import com.diyfever.diylc.model.measures.Size;
|
|||||||
import com.diyfever.diylc.model.measures.SizeUnit;
|
import com.diyfever.diylc.model.measures.SizeUnit;
|
||||||
import com.diyfever.diylc.utils.Constants;
|
import com.diyfever.diylc.utils.Constants;
|
||||||
|
|
||||||
public class ResistorInstance implements IComponentInstance {
|
public class ResistorInstance extends AbstractLeadedComponentInstance {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@@ -29,53 +29,11 @@ public class ResistorInstance implements IComponentInstance {
|
|||||||
private Resistance r = new Resistance(123d, ResistanceUnit.K);
|
private Resistance r = new Resistance(123d, ResistanceUnit.K);
|
||||||
private Size s = new Size(1d, SizeUnit.cm);
|
private Size s = new Size(1d, SizeUnit.cm);
|
||||||
private Color color = Color.green;
|
private Color color = Color.green;
|
||||||
private Point leftTopCorner = new Point(0, 0);
|
|
||||||
private Point otherCorner = new Point((int) (Constants.GRID * 10), (int) (Constants.GRID * 10));
|
|
||||||
private String name = "something";
|
|
||||||
|
|
||||||
public ResistorInstance(String testField, Capacitance c, Resistance r, Size s, Color color,
|
|
||||||
Point leftTopCorner) {
|
|
||||||
super();
|
|
||||||
this.testField = testField;
|
|
||||||
this.c = c;
|
|
||||||
this.r = r;
|
|
||||||
this.s = s;
|
|
||||||
this.color = color;
|
|
||||||
this.leftTopCorner = leftTopCorner;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ResistorInstance() {
|
public ResistorInstance() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@BomName
|
|
||||||
@EditableProperty
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@ControlPoint(visibilityPolicy = VisibilityPolicy.ALWAYS)
|
|
||||||
public Point getLeftTopCorner() {
|
|
||||||
return leftTopCorner;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLeftTopCorner(Point leftTopCorner) {
|
|
||||||
this.leftTopCorner = leftTopCorner;
|
|
||||||
}
|
|
||||||
|
|
||||||
@ControlPoint(visibilityPolicy = VisibilityPolicy.ALWAYS)
|
|
||||||
public Point getOtherCorner() {
|
|
||||||
return otherCorner;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOtherCorner(Point otherCorner) {
|
|
||||||
this.otherCorner = otherCorner;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EditableProperty(name = "Test Field")
|
@EditableProperty(name = "Test Field")
|
||||||
public String getTestField() {
|
public String getTestField() {
|
||||||
return testField;
|
return testField;
|
||||||
@@ -126,21 +84,22 @@ public class ResistorInstance implements IComponentInstance {
|
|||||||
@Override
|
@Override
|
||||||
public void draw(Graphics2D g2d, ComponentState componentState) {
|
public void draw(Graphics2D g2d, ComponentState componentState) {
|
||||||
g2d.setColor(componentState.equals(ComponentState.SELECTED) ? color : color.darker());
|
g2d.setColor(componentState.equals(ComponentState.SELECTED) ? color : color.darker());
|
||||||
g2d.setStroke(new BasicStroke(2));
|
g2d.setStroke(new BasicStroke(1));
|
||||||
if (componentState == ComponentState.DRAGGING) {
|
if (componentState == ComponentState.DRAGGING) {
|
||||||
g2d.setStroke(new BasicStroke(1));
|
g2d.setStroke(new BasicStroke(1));
|
||||||
}
|
}
|
||||||
g2d.drawLine(leftTopCorner.x, leftTopCorner.y, otherCorner.x, otherCorner.y);
|
g2d.drawLine(point1.x, point1.y, point2.x, point2.y);
|
||||||
g2d.rotate(Math.PI / 4, (leftTopCorner.x + otherCorner.x) / 2,
|
g2d.rotate(Math.PI / 4, (point1.x + point2.x) / 2, (point1.y + point2.y) / 2);
|
||||||
(leftTopCorner.y + otherCorner.y) / 2);
|
g2d.fillRect((point1.x + point2.x) / 2 - 10, (point1.y + point2.y) / 2 - 10, 20, 20);
|
||||||
g2d.fillRect((leftTopCorner.x + otherCorner.x) / 2 - 10,
|
|
||||||
(leftTopCorner.y + otherCorner.y) / 2 - 10, 20, 20);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResistorInstance clone() throws CloneNotSupportedException {
|
protected Size getDefaultHeight() {
|
||||||
ResistorInstance newInstance = new ResistorInstance(testField, c.clone(), r.clone(), s
|
return new Size(3d, SizeUnit.mm);
|
||||||
.clone(), color, (Point) leftTopCorner.clone());
|
}
|
||||||
return newInstance;
|
|
||||||
|
@Override
|
||||||
|
protected Size getDefaultWidth() {
|
||||||
|
return new Size(10d, SizeUnit.mm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
@@ -13,6 +13,11 @@ public class ResistorType implements IComponentType {
|
|||||||
public Class<? extends IComponentInstance> getComponentInstanceClass() {
|
public Class<? extends IComponentInstance> getComponentInstanceClass() {
|
||||||
return ResistorInstance.class;
|
return ResistorInstance.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getInstanceNamePrefix() {
|
||||||
|
return "R";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ComponentLayer getComponentLayer() {
|
public ComponentLayer getComponentLayer() {
|
||||||
|
|||||||
+8
@@ -18,6 +18,14 @@ public interface IComponentType {
|
|||||||
*/
|
*/
|
||||||
String getName();
|
String getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns component instance name prefix, e.g. "R" for resistors, "C" for
|
||||||
|
* capacitors, etc.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String getInstanceNamePrefix();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns component category.
|
* Returns component category.
|
||||||
*
|
*
|
||||||
|
|||||||
+13
@@ -173,6 +173,19 @@ public class ComponentProcessor {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void writeBomName(IComponentInstance component, String newBomName) {
|
||||||
|
for (Method method : component.getClass().getMethods()) {
|
||||||
|
if (method.isAnnotationPresent(BomName.class)) {
|
||||||
|
try {
|
||||||
|
Method setter = component.getClass().getMethod(
|
||||||
|
method.getName().replaceFirst("get", "set"), String.class);
|
||||||
|
setter.invoke(component, newBomName);
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public String extractBomValue(IComponentInstance component) {
|
public String extractBomValue(IComponentInstance component) {
|
||||||
String value = null;
|
String value = null;
|
||||||
for (Method method : component.getClass().getMethods()) {
|
for (Method method : component.getClass().getMethods()) {
|
||||||
|
|||||||
+30
-6
@@ -360,7 +360,8 @@ public class Presenter implements IPlugInPort {
|
|||||||
Point scaledPoint = scalePoint(point);
|
Point scaledPoint = scalePoint(point);
|
||||||
if (componentSlot != null) {
|
if (componentSlot != null) {
|
||||||
try {
|
try {
|
||||||
instantiateComponent(componentSlot.getComponentInstanceClass(), scaledPoint);
|
instantiateComponent(componentSlot.getComponentInstanceClass(), componentSlot
|
||||||
|
.getName(), componentSlot.getInstanceNamePrefix(), scaledPoint);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.error("Error instatiating component of type: "
|
LOG.error("Error instatiating component of type: "
|
||||||
+ componentSlot.getComponentInstanceClass().getName());
|
+ componentSlot.getComponentInstanceClass().getName());
|
||||||
@@ -447,9 +448,14 @@ public class Presenter implements IPlugInPort {
|
|||||||
Point scaledPoint = scalePoint(point);
|
Point scaledPoint = scalePoint(point);
|
||||||
previousDragPoint = scaledPoint;
|
previousDragPoint = scaledPoint;
|
||||||
List<IComponentInstance> components = findComponentsAt(scaledPoint);
|
List<IComponentInstance> components = findComponentsAt(scaledPoint);
|
||||||
if (!componentControlPointMap.isEmpty() || components.isEmpty()) {
|
if (!componentControlPointMap.isEmpty()) {
|
||||||
// If we're dragging control points or there are no components are
|
// If we're dragging control points reset selection.
|
||||||
// under the cursor, reset selection.
|
selectedComponents.clear();
|
||||||
|
selectedComponents.addAll(componentControlPointMap.keySet());
|
||||||
|
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
|
||||||
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
|
} else if (components.isEmpty()) {
|
||||||
|
// If there are no components are under the cursor, reset selection.
|
||||||
selectedComponents.clear();
|
selectedComponents.clear();
|
||||||
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
|
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
@@ -647,13 +653,31 @@ public class Presenter implements IPlugInPort {
|
|||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void instantiateComponent(Class<? extends IComponentInstance> componentClass,
|
private void instantiateComponent(Class<? extends IComponentInstance> componentClass,
|
||||||
Point point) throws Exception {
|
String componentTypeName, String namePrefix, Point point) throws Exception {
|
||||||
LOG.info("Instatiating component of type: " + componentClass.getName());
|
LOG.info("Instatiating component of type: " + componentClass.getName());
|
||||||
|
|
||||||
Project oldProject = cloner.deepClone(currentProject);
|
Project oldProject = cloner.deepClone(currentProject);
|
||||||
|
|
||||||
// Instantiate the component.
|
// Instantiate the component.
|
||||||
IComponentInstance component = componentClass.newInstance();
|
IComponentInstance component = componentClass.newInstance();
|
||||||
|
|
||||||
|
// Find the next available name for the component.
|
||||||
|
int i = 0;
|
||||||
|
boolean exists = true;
|
||||||
|
while (exists) {
|
||||||
|
i++;
|
||||||
|
String name = namePrefix + i;
|
||||||
|
exists = false;
|
||||||
|
for (IComponentInstance c : currentProject.getComponents()) {
|
||||||
|
String cName = ComponentProcessor.getInstance().extractBomName(c);
|
||||||
|
if (cName.equals(name)) {
|
||||||
|
exists = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ComponentProcessor.getInstance().writeBomName(component, namePrefix + i);
|
||||||
|
|
||||||
// Add it to the project.
|
// Add it to the project.
|
||||||
currentProject.getComponents().add(component);
|
currentProject.getComponents().add(component);
|
||||||
|
|
||||||
@@ -693,7 +717,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
// Notify the listeners.
|
// Notify the listeners.
|
||||||
if (!oldProject.equals(currentProject)) {
|
if (!oldProject.equals(currentProject)) {
|
||||||
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
||||||
.deepClone(currentProject), "Create");
|
.deepClone(currentProject), "Add " + componentTypeName);
|
||||||
}
|
}
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user