mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +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
@@ -14,6 +14,11 @@ public class MockComponentType implements IComponentType {
|
||||
return MockComponentInstance.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceNamePrefix() {
|
||||
return "ABC";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComponentLayer getComponentLayer() {
|
||||
return ComponentLayer.BOARD;
|
||||
|
||||
+12
-53
@@ -20,7 +20,7 @@ import com.diyfever.diylc.model.measures.Size;
|
||||
import com.diyfever.diylc.model.measures.SizeUnit;
|
||||
import com.diyfever.diylc.utils.Constants;
|
||||
|
||||
public class ResistorInstance implements IComponentInstance {
|
||||
public class ResistorInstance extends AbstractLeadedComponentInstance {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -29,53 +29,11 @@ public class ResistorInstance implements IComponentInstance {
|
||||
private Resistance r = new Resistance(123d, ResistanceUnit.K);
|
||||
private Size s = new Size(1d, SizeUnit.cm);
|
||||
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() {
|
||||
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")
|
||||
public String getTestField() {
|
||||
return testField;
|
||||
@@ -126,21 +84,22 @@ public class ResistorInstance implements IComponentInstance {
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, ComponentState componentState) {
|
||||
g2d.setColor(componentState.equals(ComponentState.SELECTED) ? color : color.darker());
|
||||
g2d.setStroke(new BasicStroke(2));
|
||||
g2d.setStroke(new BasicStroke(1));
|
||||
if (componentState == ComponentState.DRAGGING) {
|
||||
g2d.setStroke(new BasicStroke(1));
|
||||
}
|
||||
g2d.drawLine(leftTopCorner.x, leftTopCorner.y, otherCorner.x, otherCorner.y);
|
||||
g2d.rotate(Math.PI / 4, (leftTopCorner.x + otherCorner.x) / 2,
|
||||
(leftTopCorner.y + otherCorner.y) / 2);
|
||||
g2d.fillRect((leftTopCorner.x + otherCorner.x) / 2 - 10,
|
||||
(leftTopCorner.y + otherCorner.y) / 2 - 10, 20, 20);
|
||||
g2d.drawLine(point1.x, point1.y, point2.x, point2.y);
|
||||
g2d.rotate(Math.PI / 4, (point1.x + point2.x) / 2, (point1.y + point2.y) / 2);
|
||||
g2d.fillRect((point1.x + point2.x) / 2 - 10, (point1.y + point2.y) / 2 - 10, 20, 20);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResistorInstance clone() throws CloneNotSupportedException {
|
||||
ResistorInstance newInstance = new ResistorInstance(testField, c.clone(), r.clone(), s
|
||||
.clone(), color, (Point) leftTopCorner.clone());
|
||||
return newInstance;
|
||||
protected Size getDefaultHeight() {
|
||||
return new Size(3d, SizeUnit.mm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Size getDefaultWidth() {
|
||||
return new Size(10d, SizeUnit.mm);
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -14,6 +14,11 @@ public class ResistorType implements IComponentType {
|
||||
return ResistorInstance.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceNamePrefix() {
|
||||
return "R";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComponentLayer getComponentLayer() {
|
||||
return ComponentLayer.COMPONENT;
|
||||
|
||||
+8
@@ -18,6 +18,14 @@ public interface IComponentType {
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* Returns component instance name prefix, e.g. "R" for resistors, "C" for
|
||||
* capacitors, etc.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getInstanceNamePrefix();
|
||||
|
||||
/**
|
||||
* Returns component category.
|
||||
*
|
||||
|
||||
+13
@@ -173,6 +173,19 @@ public class ComponentProcessor {
|
||||
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) {
|
||||
String value = null;
|
||||
for (Method method : component.getClass().getMethods()) {
|
||||
|
||||
+30
-6
@@ -360,7 +360,8 @@ public class Presenter implements IPlugInPort {
|
||||
Point scaledPoint = scalePoint(point);
|
||||
if (componentSlot != null) {
|
||||
try {
|
||||
instantiateComponent(componentSlot.getComponentInstanceClass(), scaledPoint);
|
||||
instantiateComponent(componentSlot.getComponentInstanceClass(), componentSlot
|
||||
.getName(), componentSlot.getInstanceNamePrefix(), scaledPoint);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error instatiating component of type: "
|
||||
+ componentSlot.getComponentInstanceClass().getName());
|
||||
@@ -447,9 +448,14 @@ public class Presenter implements IPlugInPort {
|
||||
Point scaledPoint = scalePoint(point);
|
||||
previousDragPoint = scaledPoint;
|
||||
List<IComponentInstance> components = findComponentsAt(scaledPoint);
|
||||
if (!componentControlPointMap.isEmpty() || components.isEmpty()) {
|
||||
// If we're dragging control points or there are no components are
|
||||
// under the cursor, reset selection.
|
||||
if (!componentControlPointMap.isEmpty()) {
|
||||
// If we're dragging control points 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();
|
||||
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
@@ -647,13 +653,31 @@ public class Presenter implements IPlugInPort {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
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());
|
||||
|
||||
Project oldProject = cloner.deepClone(currentProject);
|
||||
|
||||
// Instantiate the component.
|
||||
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.
|
||||
currentProject.getComponents().add(component);
|
||||
|
||||
@@ -693,7 +717,7 @@ public class Presenter implements IPlugInPort {
|
||||
// Notify the listeners.
|
||||
if (!oldProject.equals(currentProject)) {
|
||||
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
||||
.deepClone(currentProject), "Create");
|
||||
.deepClone(currentProject), "Add " + componentTypeName);
|
||||
}
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user