mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
refactor old version parsing
remove dependency from core to library validate SolderPad size
This commit is contained in:
@@ -29,10 +29,11 @@ public class ComponentType {
|
||||
private BomPolicy bomPolicy;
|
||||
private boolean autoEdit;
|
||||
private boolean rotatable;
|
||||
private String autoCreateName;
|
||||
|
||||
public ComponentType(String name, String description, CreationMethod creationMethod, String category,
|
||||
String namePrefix, String author, Icon icon, Class<? extends IDIYComponent<?>> instanceClass, double zOrder,
|
||||
boolean flexibleZOrder, boolean stretchable, BomPolicy bomPolicy, boolean autoEdit, boolean rotatable) {
|
||||
boolean flexibleZOrder, boolean stretchable, BomPolicy bomPolicy, boolean autoEdit, boolean rotatable, String autoCreateName) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
@@ -48,6 +49,7 @@ public class ComponentType {
|
||||
this.bomPolicy = bomPolicy;
|
||||
this.autoEdit = autoEdit;
|
||||
this.rotatable = rotatable;
|
||||
this.autoCreateName = autoCreateName;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@@ -105,6 +107,10 @@ public class ComponentType {
|
||||
public boolean isRotatable() {
|
||||
return rotatable;
|
||||
}
|
||||
|
||||
public String getAutoCreateName() {
|
||||
return autoCreateName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.diylc.core.annotations;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.diylc.core.IDIYComponent;
|
||||
|
||||
/**
|
||||
* Annotation for {@link IDIYComponent} implementation. Tells the core which component should be auto-created as a solder pad.
|
||||
*
|
||||
* @author Branislav Stojkovic
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AutoCreated {
|
||||
/**
|
||||
* @return auto-create type name.
|
||||
*/
|
||||
String name();
|
||||
}
|
||||
@@ -11,7 +11,7 @@ public class PositiveMeasureValidator implements IPropertyValidator {
|
||||
if (value == null)
|
||||
return;
|
||||
if (value instanceof AbstractMeasure) {
|
||||
AbstractMeasure measure = (AbstractMeasure) value;
|
||||
AbstractMeasure<?> measure = (AbstractMeasure<?>) value;
|
||||
if (measure.getValue() < 0) {
|
||||
throw new ValidationException("must be greater than zero.");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.diylc.parsing;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.diylc.core.Project;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public interface IOldFileParser {
|
||||
|
||||
boolean canParse(String version);
|
||||
|
||||
Project parseFile(Element root, List<String> warnings);
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import org.diylc.common.PropertyWrapper;
|
||||
import org.diylc.core.CreationMethod;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.core.IPropertyValidator;
|
||||
import org.diylc.core.annotations.AutoCreated;
|
||||
import org.diylc.core.annotations.BomPolicy;
|
||||
import org.diylc.core.annotations.ComponentDescriptor;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
@@ -70,6 +71,7 @@ public class ComponentProcessor {
|
||||
BomPolicy bomPolicy;
|
||||
boolean autoEdit;
|
||||
boolean rotatable;
|
||||
String autoCreateName = null;
|
||||
if (clazz.isAnnotationPresent(ComponentDescriptor.class)) {
|
||||
ComponentDescriptor annotation = clazz.getAnnotation(ComponentDescriptor.class);
|
||||
name = annotation.name();
|
||||
@@ -98,6 +100,10 @@ public class ComponentProcessor {
|
||||
autoEdit = true;
|
||||
rotatable = true;
|
||||
}
|
||||
if (clazz.isAnnotationPresent(AutoCreated.class)) {
|
||||
AutoCreated annotation = clazz.getAnnotation(AutoCreated.class);
|
||||
autoCreateName = annotation.name();
|
||||
}
|
||||
icon = null;
|
||||
// Draw component icon.
|
||||
try {
|
||||
@@ -116,7 +122,7 @@ public class ComponentProcessor {
|
||||
}
|
||||
ComponentType componentType =
|
||||
new ComponentType(name, description, creationMethod, category, namePrefix, author, icon, clazz, zOrder,
|
||||
flexibleZOrder, stretchable, bomPolicy, autoEdit, rotatable);
|
||||
flexibleZOrder, stretchable, bomPolicy, autoEdit, rotatable, autoCreateName);
|
||||
componentTypeMap.put(clazz.getName(), componentType);
|
||||
return componentType;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class InstantiationManager {
|
||||
|
||||
private static final ComponentType clipboardType = new ComponentType("Clipboard contents",
|
||||
"Components from the clipboard", CreationMethod.SINGLE_CLICK, "Multi", "", "", null, null, 0, false, false, null,
|
||||
false, true);
|
||||
false, true, null);
|
||||
|
||||
public InstantiationManager() {}
|
||||
|
||||
|
||||
@@ -44,7 +44,6 @@ import org.diylc.common.IPlugInPort;
|
||||
import org.diylc.common.Orientation;
|
||||
import org.diylc.common.OrientationHV;
|
||||
import org.diylc.common.PropertyWrapper;
|
||||
import org.diylc.components.connectivity.SolderPad;
|
||||
import org.diylc.core.ExpansionMode;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.core.IView;
|
||||
@@ -64,6 +63,8 @@ import com.thoughtworks.xstream.io.xml.DomDriver;
|
||||
*/
|
||||
public class Presenter implements IPlugInPort {
|
||||
|
||||
private static final String SOLDER_PAD = "SolderPad";
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(Presenter.class);
|
||||
|
||||
public static VersionNumber CURRENT_VERSION = new VersionNumber(3, 0, 0);
|
||||
@@ -72,6 +73,7 @@ public class Presenter implements IPlugInPort {
|
||||
try {
|
||||
BufferedInputStream in = new BufferedInputStream(new FileInputStream("update.xml"));
|
||||
XStream xStream = new XStream(new DomDriver());
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Version> allVersions = (List<Version>) xStream.fromXML(in);
|
||||
in.close();
|
||||
CURRENT_VERSION = allVersions.get(allVersions.size() - 1).getVersionNumber();
|
||||
@@ -87,6 +89,10 @@ public class Presenter implements IPlugInPort {
|
||||
|
||||
private Project currentProject;
|
||||
private Map<String, List<ComponentType>> componentTypes;
|
||||
/**
|
||||
* {@link ComponentType}s that can be autocreated, like Solder Pads
|
||||
*/
|
||||
private Map<String, ComponentType> autoCreateTypes;
|
||||
// Maps component class names to ComponentType objects.
|
||||
private List<IPlugIn> plugIns;
|
||||
|
||||
@@ -347,6 +353,19 @@ public class Presenter implements IPlugInPort {
|
||||
return componentTypes;
|
||||
}
|
||||
|
||||
private Map<String, ComponentType> getAutoCreateTypes() {
|
||||
if (autoCreateTypes == null) {
|
||||
autoCreateTypes = new HashMap<String, ComponentType>();
|
||||
for (Map.Entry<String, List<ComponentType>> entry : getComponentTypes().entrySet()) {
|
||||
for (ComponentType t : entry.getValue()) {
|
||||
if (t.getAutoCreateName() != null)
|
||||
autoCreateTypes.put(t.getAutoCreateName(), t);
|
||||
}
|
||||
}
|
||||
}
|
||||
return autoCreateTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, Set<DrawOption> drawOptions, IComponentFiler filter) {
|
||||
if (currentProject == null) {
|
||||
@@ -670,6 +689,7 @@ public class Presenter implements IPlugInPort {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) {
|
||||
|
||||
@@ -746,6 +766,7 @@ public class Presenter implements IPlugInPort {
|
||||
return selectedComponents;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void selectAll(int layer) {
|
||||
LOG.info("selectAll()");
|
||||
@@ -852,6 +873,7 @@ public class Presenter implements IPlugInPort {
|
||||
*
|
||||
* @param controlPointMap
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void includeStuckComponents(Map<IDIYComponent<?>, Set<Integer>> controlPointMap) {
|
||||
int oldSize = controlPointMap.size();
|
||||
LOG.trace("Expanding selected component map");
|
||||
@@ -1054,6 +1076,7 @@ public class Presenter implements IPlugInPort {
|
||||
*
|
||||
* @param direction 1 for clockwise, -1 for counter-clockwise
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void rotateComponents(List<IDIYComponent<?>> components, int direction, boolean snapToGrid) {
|
||||
Point center = getCenterOf(components, snapToGrid);
|
||||
|
||||
@@ -1286,6 +1309,7 @@ public class Presenter implements IPlugInPort {
|
||||
messageDispatcher.dispatchMessage(EventType.LAYER_STATE_CHANGED, currentProject.getLockedLayers());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void sendSelectionToBack() {
|
||||
LOG.info("sendSelectionToBack()");
|
||||
@@ -1316,6 +1340,7 @@ public class Presenter implements IPlugInPort {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void bringSelectionToFront() {
|
||||
LOG.info("bringSelectionToFront()");
|
||||
@@ -1363,6 +1388,7 @@ public class Presenter implements IPlugInPort {
|
||||
drawingManager.setTheme(theme);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void renumberSelectedComponents(final boolean xAxisFirst) {
|
||||
LOG.info("renumberSelectedComponents(" + xAxisFirst + ")");
|
||||
@@ -1453,6 +1479,7 @@ public class Presenter implements IPlugInPort {
|
||||
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents, controlPointMap.keySet());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void expandSelection(ExpansionMode expansionMode) {
|
||||
LOG.info(String.format("expandSelection(%s)", expansionMode));
|
||||
@@ -1596,6 +1623,7 @@ public class Presenter implements IPlugInPort {
|
||||
*
|
||||
* @param component
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void addComponent(IDIYComponent<?> component, boolean canCreatePads) {
|
||||
int index = currentProject.getComponents().size();
|
||||
while (index > 0
|
||||
@@ -1612,9 +1640,10 @@ public class Presenter implements IPlugInPort {
|
||||
} else {
|
||||
currentProject.getComponents().add(component);
|
||||
}
|
||||
|
||||
ComponentType padType = getAutoCreateTypes().get(SOLDER_PAD);
|
||||
if (canCreatePads && ConfigurationManager.getInstance().readBoolean(IPlugInPort.AUTO_PADS_KEY, false)
|
||||
&& !(component instanceof SolderPad)) {
|
||||
ComponentType padType = ComponentProcessor.getInstance().extractComponentTypeFrom(SolderPad.class);
|
||||
&& padType != null && !(component.getClass().equals(padType.getInstanceClass()))) {
|
||||
for (int i = 0; i < component.getControlPointCount(); i++) {
|
||||
if (component.isControlPointSticky(i)) {
|
||||
try {
|
||||
@@ -1626,11 +1655,6 @@ public class Presenter implements IPlugInPort {
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Could not auto-create solder pad", e);
|
||||
}
|
||||
// SolderPad pad = new SolderPad();
|
||||
// pad.setControlPoint(component.getControlPoint(i), 0);
|
||||
// addComponent(pad,
|
||||
// ComponentProcessor.getInstance().extractComponentTypeFrom(
|
||||
// SolderPad.class), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1742,6 +1766,7 @@ public class Presenter implements IPlugInPort {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void saveSelectedComponentAsTemplate(String templateName) {
|
||||
LOG.info(String.format("saveSelectedComponentAsTemplate(%s)", templateName));
|
||||
@@ -1818,6 +1843,7 @@ public class Presenter implements IPlugInPort {
|
||||
ConfigurationManager.getInstance().writeValue(TEMPLATES_KEY, templateMap);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<Template> getTemplatesFor(String categoryName, String componentTypeName) {
|
||||
Map<String, List<Template>> templateMap =
|
||||
@@ -1828,6 +1854,7 @@ public class Presenter implements IPlugInPort {
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<Template> getTemplatesForSelection() {
|
||||
if (this.selectedComponents.isEmpty()) {
|
||||
@@ -1872,6 +1899,7 @@ public class Presenter implements IPlugInPort {
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void deleteTemplate(String categoryName, String componentTypeName, String templateName) {
|
||||
Map<String, List<Template>> templateMap =
|
||||
@@ -1900,6 +1928,7 @@ public class Presenter implements IPlugInPort {
|
||||
return lockedComponents;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private boolean isComponentLocked(IDIYComponent<?> component) {
|
||||
ComponentType componentType =
|
||||
ComponentProcessor.getInstance().extractComponentTypeFrom(
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.diylc.presenter;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Point;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
@@ -10,54 +8,23 @@ import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.diylc.appframework.miscutils.Utils;
|
||||
import org.diylc.appframework.simplemq.MessageDispatcher;
|
||||
import org.diylc.common.Display;
|
||||
import org.diylc.common.EventType;
|
||||
import org.diylc.common.HorizontalAlignment;
|
||||
import org.diylc.common.Orientation;
|
||||
import org.diylc.common.VerticalAlignment;
|
||||
import org.diylc.components.AbstractLeadedComponent;
|
||||
import org.diylc.components.AbstractTransparentComponent;
|
||||
import org.diylc.components.boards.AbstractBoard;
|
||||
import org.diylc.components.boards.BlankBoard;
|
||||
import org.diylc.components.boards.PerfBoard;
|
||||
import org.diylc.components.boards.VeroBoard;
|
||||
import org.diylc.components.connectivity.CopperTrace;
|
||||
import org.diylc.components.connectivity.HookupWire;
|
||||
import org.diylc.components.connectivity.Jumper;
|
||||
import org.diylc.components.connectivity.SolderPad;
|
||||
import org.diylc.components.misc.Label;
|
||||
import org.diylc.components.passive.PotentiometerPanel;
|
||||
import org.diylc.components.passive.RadialElectrolytic;
|
||||
import org.diylc.components.passive.RadialFilmCapacitor;
|
||||
import org.diylc.components.passive.Resistor;
|
||||
import org.diylc.components.passive.Taper;
|
||||
import org.diylc.components.semiconductors.DIL_IC;
|
||||
import org.diylc.components.semiconductors.DiodePlastic;
|
||||
import org.diylc.components.semiconductors.LED;
|
||||
import org.diylc.components.semiconductors.TransistorTO92;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.core.Project;
|
||||
import org.diylc.core.measures.Capacitance;
|
||||
import org.diylc.core.measures.Resistance;
|
||||
import org.diylc.core.measures.Size;
|
||||
import org.diylc.core.measures.SizeUnit;
|
||||
import org.diylc.utils.Constants;
|
||||
import org.diylc.parsing.IOldFileParser;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
@@ -68,18 +35,6 @@ public class ProjectFileManager {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ProjectFileManager.class);
|
||||
|
||||
// private static final int V1_PIXELS_PER_INCH = 200;
|
||||
private static final Size V1_GRID_SPACING = new Size(0.1d, SizeUnit.in);
|
||||
private static final Map<String, Color> V1_COLOR_MAP = new HashMap<String, Color>();
|
||||
static {
|
||||
V1_COLOR_MAP.put("red", Color.red);
|
||||
V1_COLOR_MAP.put("blue", Color.blue);
|
||||
V1_COLOR_MAP.put("white", Color.white);
|
||||
V1_COLOR_MAP.put("green", Color.green.darker());
|
||||
V1_COLOR_MAP.put("black", Color.black);
|
||||
V1_COLOR_MAP.put("yellow", Color.yellow);
|
||||
}
|
||||
|
||||
private XStream xStream;
|
||||
// Legacy deserializer for 3.0.1 through 3.0.7, loads Points referenced in
|
||||
// pixels.
|
||||
@@ -90,6 +45,8 @@ public class ProjectFileManager {
|
||||
|
||||
private MessageDispatcher<EventType> messageDispatcher;
|
||||
|
||||
private List<IOldFileParser> parsers;
|
||||
|
||||
public ProjectFileManager(MessageDispatcher<EventType> messageDispatcher) {
|
||||
super();
|
||||
this.xStream = new XStream(new DomDriver("UTF-8"));
|
||||
@@ -106,6 +63,25 @@ public class ProjectFileManager {
|
||||
fireFileStatusChanged();
|
||||
}
|
||||
|
||||
public List<IOldFileParser> getParsers() {
|
||||
if (parsers == null) {
|
||||
parsers = new ArrayList<IOldFileParser>();
|
||||
Set<Class<?>> componentTypeClasses = null;
|
||||
try {
|
||||
componentTypeClasses = Utils.getClasses("org.diylc.parsing");
|
||||
for (Class<?> clazz : componentTypeClasses) {
|
||||
if (!Modifier.isAbstract(clazz.getModifiers()) && IOldFileParser.class.isAssignableFrom(clazz)) {
|
||||
IOldFileParser instance = (IOldFileParser) clazz.newInstance();
|
||||
parsers.add(instance);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Could not find old version parsers", e);
|
||||
}
|
||||
}
|
||||
return parsers;
|
||||
}
|
||||
|
||||
public synchronized void serializeProjectToFile(Project project, String fileName, boolean isBackup)
|
||||
throws IOException {
|
||||
if (!isBackup) {
|
||||
@@ -127,7 +103,7 @@ public class ProjectFileManager {
|
||||
public Project deserializeProjectFromFile(String fileName, List<String> warnings) throws SAXException, IOException,
|
||||
ParserConfigurationException {
|
||||
LOG.info(String.format("loadProjectFromFile(%s)", fileName));
|
||||
Project project;
|
||||
Project project = null;
|
||||
File file = new File(fileName);
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
@@ -140,15 +116,16 @@ public class ProjectFileManager {
|
||||
throw new IllegalArgumentException("Could not open DIY file. Root node is not named 'Layout'.");
|
||||
}
|
||||
String formatVersion = doc.getDocumentElement().getAttribute("formatVersion");
|
||||
if (formatVersion == null || formatVersion.trim().isEmpty()) {
|
||||
LOG.debug("Detected v1 file.");
|
||||
project = parseV1File(doc.getDocumentElement(), warnings);
|
||||
} else if (formatVersion.equals("2.0")) {
|
||||
LOG.debug("Detected v2 file.");
|
||||
project = parseV2File(doc.getDocumentElement());
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unknown file format version: " + formatVersion);
|
||||
|
||||
// try to find a parser for an older version
|
||||
List<IOldFileParser> parsers = getParsers();
|
||||
for (int i = 0; i < parsers.size(); i++) {
|
||||
if (parsers.get(i).canParse(formatVersion))
|
||||
project = parsers.get(i).parseFile(doc.getDocumentElement(), warnings);
|
||||
}
|
||||
|
||||
if (project == null)
|
||||
throw new IllegalArgumentException("Unknown file format version: " + formatVersion);
|
||||
}
|
||||
Collections.sort(warnings);
|
||||
this.currentFileName = fileName;
|
||||
@@ -173,364 +150,6 @@ public class ProjectFileManager {
|
||||
messageDispatcher.dispatchMessage(EventType.FILE_STATUS_CHANGED, getCurrentFileName(), isModified());
|
||||
}
|
||||
|
||||
private Project parseV1File(Element root, List<String> warnings) {
|
||||
Project project = new Project();
|
||||
project.setTitle(root.getAttribute("Project"));
|
||||
project.setAuthor(root.getAttribute("Credits"));
|
||||
project.setGridSpacing(V1_GRID_SPACING);
|
||||
String type = root.getAttribute("Type");
|
||||
|
||||
// Create the board.
|
||||
int width = Integer.parseInt(root.getAttribute("Width")) + 1;
|
||||
int height = Integer.parseInt(root.getAttribute("Height")) + 1;
|
||||
int boardWidth = (int) (width * Constants.PIXELS_PER_INCH * V1_GRID_SPACING.getValue());
|
||||
int boardHeight = (int) (height * Constants.PIXELS_PER_INCH * V1_GRID_SPACING.getValue());
|
||||
int projectWidth = (int) project.getWidth().convertToPixels();
|
||||
int projectHeight = (int) project.getHeight().convertToPixels();
|
||||
int x = (projectWidth - boardWidth) / 2;
|
||||
int y = (projectHeight - boardHeight) / 2;
|
||||
AbstractBoard board;
|
||||
if (type.equalsIgnoreCase("pcb")) {
|
||||
board = new BlankBoard();
|
||||
board.setBoardColor(Color.white);
|
||||
board.setBorderColor(Color.black);
|
||||
} else if (type.equalsIgnoreCase("perfboard")) {
|
||||
board = new PerfBoard();
|
||||
} else if (type.equalsIgnoreCase("stripboard")) {
|
||||
board = new VeroBoard();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unrecognized board type: " + type);
|
||||
}
|
||||
board.setName("Main board");
|
||||
Point referencePoint =
|
||||
new Point(CalcUtils.roundToGrid(x, V1_GRID_SPACING), CalcUtils.roundToGrid(y, V1_GRID_SPACING));
|
||||
board.setControlPoint(referencePoint, 0);
|
||||
board.setControlPoint(
|
||||
new Point(CalcUtils.roundToGrid(x + boardWidth, V1_GRID_SPACING), CalcUtils.roundToGrid(y + boardHeight,
|
||||
V1_GRID_SPACING)), 1);
|
||||
project.getComponents().add(board);
|
||||
|
||||
NodeList childNodes = root.getChildNodes();
|
||||
for (int i = 0; i < childNodes.getLength(); i++) {
|
||||
Node node = childNodes.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
String nodeName = node.getNodeName();
|
||||
String nameAttr = node.getAttributes().getNamedItem("Name").getNodeValue();
|
||||
Node valueNode = node.getAttributes().getNamedItem("Value");
|
||||
String valueAttr = valueNode == null ? null : valueNode.getNodeValue();
|
||||
int x1Attr = Integer.parseInt(node.getAttributes().getNamedItem("X1").getNodeValue());
|
||||
int y1Attr = Integer.parseInt(node.getAttributes().getNamedItem("Y1").getNodeValue());
|
||||
Point point1 = convertV1CoordinatesToV3Point(referencePoint, x1Attr, y1Attr);
|
||||
Point point2 = null;
|
||||
Integer x2Attr = null;
|
||||
Integer y2Attr = null;
|
||||
Color color = null;
|
||||
if (node.getAttributes().getNamedItem("Color") != null) {
|
||||
String colorAttr = node.getAttributes().getNamedItem("Color").getNodeValue();
|
||||
color = V1_COLOR_MAP.get(colorAttr.toLowerCase());
|
||||
}
|
||||
if (node.getAttributes().getNamedItem("X2") != null && node.getAttributes().getNamedItem("Y2") != null) {
|
||||
x2Attr = Integer.parseInt(node.getAttributes().getNamedItem("X2").getNodeValue());
|
||||
y2Attr = Integer.parseInt(node.getAttributes().getNamedItem("Y2").getNodeValue());
|
||||
point2 = convertV1CoordinatesToV3Point(referencePoint, x2Attr, y2Attr);
|
||||
}
|
||||
IDIYComponent<?> component = null;
|
||||
if (nodeName.equalsIgnoreCase("text")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
Label label = new Label();
|
||||
label.setName(nameAttr);
|
||||
if (color != null) {
|
||||
label.setColor(color);
|
||||
}
|
||||
label.setText(valueAttr);
|
||||
label.setHorizontalAlignment(HorizontalAlignment.LEFT);
|
||||
label.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
label.setControlPoint(convertV1CoordinatesToV3Point(referencePoint, x1Attr, y1Attr), 0);
|
||||
component = label;
|
||||
} else if (nodeName.equalsIgnoreCase("pad")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
SolderPad pad = new SolderPad();
|
||||
pad.setName(nameAttr);
|
||||
if (color != null) {
|
||||
pad.setColor(color);
|
||||
}
|
||||
pad.setControlPoint(convertV1CoordinatesToV3Point(referencePoint, x1Attr, y1Attr), 0);
|
||||
component = pad;
|
||||
} else if (nodeName.equalsIgnoreCase("trace")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
CopperTrace trace = new CopperTrace();
|
||||
trace.setName(nameAttr);
|
||||
if (color != null) {
|
||||
trace.setLeadColor(color);
|
||||
}
|
||||
trace.setControlPoint(point1, 0);
|
||||
trace.setControlPoint(point2, 1);
|
||||
component = trace;
|
||||
} else if (nodeName.equalsIgnoreCase("jumper")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
Jumper jumper = new Jumper();
|
||||
jumper.setName(nameAttr);
|
||||
jumper.setControlPoint(point1, 0);
|
||||
jumper.setControlPoint(point2, 1);
|
||||
component = jumper;
|
||||
} else if (nodeName.equalsIgnoreCase("wire")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
HookupWire wire = new HookupWire();
|
||||
Point midPoint = new Point((point1.x + point2.x) / 2, (point1.y + point2.y) / 2);
|
||||
wire.setName(nameAttr);
|
||||
String colorAttr = node.getAttributes().getNamedItem("Color").getNodeValue();
|
||||
wire.setColor(parseV1Color(colorAttr));
|
||||
wire.setControlPoint(point1, 0);
|
||||
wire.setControlPoint(midPoint, 1);
|
||||
wire.setControlPoint(midPoint, 2);
|
||||
wire.setControlPoint(point2, 3);
|
||||
component = wire;
|
||||
} else if (nodeName.equalsIgnoreCase("resistor")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
Resistor resistor = new Resistor();
|
||||
resistor.setName(nameAttr);
|
||||
try {
|
||||
resistor.setValue(Resistance.parseResistance(valueAttr));
|
||||
} catch (Exception e) {
|
||||
LOG.debug("Could not set value of " + nameAttr);
|
||||
}
|
||||
resistor.setLength(new Size(6.35d, SizeUnit.mm));
|
||||
resistor.setWidth(new Size(2.2d, SizeUnit.mm));
|
||||
resistor.setControlPoint(point1, 0);
|
||||
resistor.setControlPoint(point2, 1);
|
||||
component = resistor;
|
||||
} else if (nodeName.equalsIgnoreCase("capacitor")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
RadialFilmCapacitor capacitor = new RadialFilmCapacitor();
|
||||
capacitor.setName(nameAttr);
|
||||
try {
|
||||
capacitor.setValue(Capacitance.parseCapacitance(valueAttr));
|
||||
} catch (Exception e) {
|
||||
LOG.debug("Could not set value of " + nameAttr);
|
||||
}
|
||||
capacitor.setLength(new Size(6d, SizeUnit.mm));
|
||||
capacitor.setWidth(new Size(2d, SizeUnit.mm));
|
||||
capacitor.setControlPoint(point1, 0);
|
||||
capacitor.setControlPoint(point2, 1);
|
||||
component = capacitor;
|
||||
} else if (nodeName.equalsIgnoreCase("electrolyte")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
RadialElectrolytic capacitor = new RadialElectrolytic();
|
||||
capacitor.setName(nameAttr);
|
||||
try {
|
||||
capacitor.setValue(Capacitance.parseCapacitance(valueAttr));
|
||||
} catch (Exception e) {
|
||||
LOG.debug("Could not set value of " + nameAttr);
|
||||
}
|
||||
try {
|
||||
String sizeAttr = node.getAttributes().getNamedItem("Size").getNodeValue();
|
||||
if (sizeAttr.equalsIgnoreCase("small")) {
|
||||
capacitor.setLength(new Size(3.5d, SizeUnit.mm));
|
||||
} else if (sizeAttr.equalsIgnoreCase("medium")) {
|
||||
capacitor.setLength(new Size(5d, SizeUnit.mm));
|
||||
} else if (sizeAttr.equalsIgnoreCase("large")) {
|
||||
capacitor.setLength(new Size(7d, SizeUnit.mm));
|
||||
} else {
|
||||
capacitor.setLength(new Size(4d, SizeUnit.mm));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
capacitor.setLength(new Size(5d, SizeUnit.mm));
|
||||
LOG.debug("Could not set size of " + nameAttr);
|
||||
}
|
||||
capacitor.setControlPoint(point1, 0);
|
||||
capacitor.setControlPoint(point2, 1);
|
||||
component = capacitor;
|
||||
} else if (nodeName.equalsIgnoreCase("diode")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
DiodePlastic capacitor = new DiodePlastic();
|
||||
capacitor.setName(nameAttr);
|
||||
try {
|
||||
capacitor.setValue(valueAttr);
|
||||
} catch (Exception e) {
|
||||
LOG.debug("Could not set value of " + nameAttr);
|
||||
}
|
||||
capacitor.setLength(new Size(6d, SizeUnit.mm));
|
||||
capacitor.setWidth(new Size(2d, SizeUnit.mm));
|
||||
capacitor.setControlPoint(point1, 0);
|
||||
capacitor.setControlPoint(point2, 1);
|
||||
component = capacitor;
|
||||
} else if (nodeName.equalsIgnoreCase("led")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
LED led = new LED();
|
||||
led.setName(nameAttr);
|
||||
led.setValue(valueAttr);
|
||||
led.setBodyColor(Color.red);
|
||||
led.setBorderColor(Color.red.darker());
|
||||
led.setLength(new Size(3d, SizeUnit.mm));
|
||||
led.setControlPoint(point1, 0);
|
||||
led.setControlPoint(point2, 1);
|
||||
component = led;
|
||||
} else if (nodeName.equalsIgnoreCase("transistor")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
TransistorTO92 transistor = new TransistorTO92();
|
||||
transistor.setName(nameAttr);
|
||||
try {
|
||||
transistor.setValue(valueAttr);
|
||||
} catch (Exception e) {
|
||||
LOG.debug("Could not set value of " + nameAttr);
|
||||
}
|
||||
transistor.setControlPoint(point1, 0);
|
||||
if (point1.y > point2.y) {
|
||||
transistor.setOrientation(Orientation._180);
|
||||
} else if (point1.y < point2.y) {
|
||||
transistor.setOrientation(Orientation.DEFAULT);
|
||||
} else if (point1.x < point2.x) {
|
||||
transistor.setOrientation(Orientation._270);
|
||||
} else if (point1.x > point2.x) {
|
||||
transistor.setOrientation(Orientation._90);
|
||||
}
|
||||
// capacitor.setControlPoint(point2, 1);
|
||||
component = transistor;
|
||||
} else if (nodeName.equalsIgnoreCase("ic")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
DIL_IC ic = new DIL_IC();
|
||||
int pinCount = 8;
|
||||
int rowSpace = 3;
|
||||
if (x1Attr < x2Attr && y1Attr < y2Attr) {
|
||||
pinCount = (y2Attr - y1Attr + 1) * 2;
|
||||
rowSpace = x2Attr - x1Attr;
|
||||
ic.setOrientation(Orientation.DEFAULT);
|
||||
} else if (x1Attr > x2Attr && y1Attr < y2Attr) {
|
||||
pinCount = (x1Attr - x2Attr + 1) * 2;
|
||||
rowSpace = y2Attr - y1Attr;
|
||||
ic.setOrientation(Orientation._90);
|
||||
} else if (x1Attr > x2Attr && y1Attr > y2Attr) {
|
||||
rowSpace = x1Attr - x2Attr;
|
||||
pinCount = (y1Attr - y2Attr + 1) * 2;
|
||||
ic.setOrientation(Orientation._180);
|
||||
} else if (x1Attr < x2Attr && y1Attr > y2Attr) {
|
||||
rowSpace = y1Attr - y2Attr;
|
||||
pinCount = (x2Attr - x1Attr + 1) * 2;
|
||||
ic.setOrientation(Orientation._270);
|
||||
}
|
||||
ic.setRowSpacing(new Size(0.1 * rowSpace, SizeUnit.in));
|
||||
ic.setPinCount(DIL_IC.PinCount.valueOf("_" + pinCount));
|
||||
ic.setName(nameAttr);
|
||||
// Translate control points.
|
||||
for (int j = 0; j < ic.getControlPointCount(); j++) {
|
||||
Point p = new Point(ic.getControlPoint(j));
|
||||
p.translate(point1.x, point1.y);
|
||||
ic.setControlPoint(p, j);
|
||||
}
|
||||
ic.setValue(valueAttr);
|
||||
component = ic;
|
||||
} else if (nodeName.equalsIgnoreCase("pot")) {
|
||||
LOG.debug("Recognized " + nodeName);
|
||||
PotentiometerPanel pot = new PotentiometerPanel();
|
||||
pot.setBodyDiameter(new Size(14d, SizeUnit.mm));
|
||||
pot.setSpacing(new Size(0.2, SizeUnit.in));
|
||||
pot.setName(nameAttr);
|
||||
try {
|
||||
pot.setValue(Resistance.parseResistance(valueAttr));
|
||||
} catch (Exception e) {
|
||||
LOG.debug("Could not set value of " + nameAttr);
|
||||
}
|
||||
String taperAttr = node.getAttributes().getNamedItem("Taper").getNodeValue();
|
||||
if ("Linear".equals(taperAttr)) {
|
||||
pot.setTaper(Taper.LIN);
|
||||
} else if ("Audio".equals(taperAttr)) {
|
||||
pot.setTaper(Taper.LOG);
|
||||
} else if ("Reverse Audio".equals(taperAttr)) {
|
||||
pot.setTaper(Taper.REV_LOG);
|
||||
}
|
||||
// Pin spacing, we'll need to move pot around a bit.
|
||||
int delta = Constants.PIXELS_PER_INCH / 5;
|
||||
if (x1Attr < x2Attr) {
|
||||
pot.setOrientation(Orientation.DEFAULT);
|
||||
for (int j = 0; j < pot.getControlPointCount(); j++) {
|
||||
Point p = new Point(pot.getControlPoint(j));
|
||||
p.translate(point1.x - delta, point1.y);
|
||||
pot.setControlPoint(p, j);
|
||||
}
|
||||
} else if (x1Attr > x2Attr) {
|
||||
pot.setOrientation(Orientation._180);
|
||||
for (int j = 0; j < pot.getControlPointCount(); j++) {
|
||||
Point p = new Point(pot.getControlPoint(j));
|
||||
p.translate(point1.x + delta, point1.y);
|
||||
pot.setControlPoint(p, j);
|
||||
}
|
||||
} else if (y1Attr < y2Attr) {
|
||||
pot.setOrientation(Orientation._90);
|
||||
for (int j = 0; j < pot.getControlPointCount(); j++) {
|
||||
Point p = new Point(pot.getControlPoint(j));
|
||||
p.translate(point1.x, point1.y - delta);
|
||||
pot.setControlPoint(p, j);
|
||||
}
|
||||
} else if (y1Attr > y2Attr) {
|
||||
pot.setOrientation(Orientation._270);
|
||||
for (int j = 0; j < pot.getControlPointCount(); j++) {
|
||||
Point p = new Point(pot.getControlPoint(j));
|
||||
p.translate(point1.x, point1.y + delta);
|
||||
pot.setControlPoint(p, j);
|
||||
};
|
||||
}
|
||||
component = pot;
|
||||
} else {
|
||||
String message = "Could not recognize component type " + nodeName;
|
||||
LOG.debug(message);
|
||||
if (!warnings.contains(message)) {
|
||||
warnings.add(message);
|
||||
}
|
||||
}
|
||||
if (component != null) {
|
||||
if (component instanceof AbstractLeadedComponent<?>) {
|
||||
((AbstractLeadedComponent<?>) component).setDisplay(Display.NAME);
|
||||
}
|
||||
if (component instanceof AbstractTransparentComponent<?>) {
|
||||
((AbstractTransparentComponent<?>) component).setAlpha((byte) 100);
|
||||
}
|
||||
project.getComponents().add(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
Collections.sort(project.getComponents(), ComparatorFactory.getInstance().getComponentZOrderComparator());
|
||||
return project;
|
||||
}
|
||||
|
||||
private Point convertV1CoordinatesToV3Point(Point reference, int x, int y) {
|
||||
Point point = new Point(reference);
|
||||
point.translate((int) (x * Constants.PIXELS_PER_INCH * V1_GRID_SPACING.getValue()), (int) (y
|
||||
* Constants.PIXELS_PER_INCH * V1_GRID_SPACING.getValue()));
|
||||
return point;
|
||||
}
|
||||
|
||||
private Color parseV1Color(String color) {
|
||||
if ("brown".equals(color.toLowerCase()))
|
||||
return new Color(139, 69, 19);
|
||||
try {
|
||||
Field field = Color.class.getDeclaredField(color.toLowerCase());
|
||||
return (Color) field.get(null);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Could not parse color \"" + color + "\"", e);
|
||||
return Color.black;
|
||||
}
|
||||
}
|
||||
|
||||
private Project parseV2File(Element root) {
|
||||
Project project = new Project();
|
||||
String projectName = root.getAttribute("projectName");
|
||||
String credits = root.getAttribute("credits");
|
||||
String width = root.getAttribute("width");
|
||||
String height = root.getAttribute("height");
|
||||
NodeList childNodes = root.getChildNodes();
|
||||
for (int i = 0; i < childNodes.getLength(); i++) {
|
||||
Node node = childNodes.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
if (node.getNodeName().equalsIgnoreCase("component")) {
|
||||
LOG.debug(node.getAttributes().getNamedItem("name").getNodeValue());
|
||||
} else {
|
||||
LOG.debug("Unrecognized node name found: " + node.getNodeName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return project;
|
||||
}
|
||||
|
||||
private Project parseV3File(String fileName) throws IOException {
|
||||
Project project;
|
||||
FileInputStream fis = new FileInputStream(fileName);
|
||||
|
||||
Reference in New Issue
Block a user