improved importer for v1

This commit is contained in:
bancika
2011-01-24 18:54:04 +00:00
parent 6d361283a1
commit 2aa8aed874
6 changed files with 95 additions and 41 deletions
@@ -1,7 +1,6 @@
package org.diylc.presenter; package org.diylc.presenter;
import java.util.Comparator; import java.util.Comparator;
import java.util.List;
import org.diylc.common.ComponentType; import org.diylc.common.ComponentType;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
@@ -14,6 +13,7 @@ public class ComparatorFactory {
private Comparator<IDIYComponent<?>> componentNameComparator; private Comparator<IDIYComponent<?>> componentNameComparator;
private Comparator<ComponentType> componentTypeComparator; private Comparator<ComponentType> componentTypeComparator;
private Comparator<PropertyWrapper> propertyNameComparator; private Comparator<PropertyWrapper> propertyNameComparator;
private Comparator<IDIYComponent<?>> componentZOrderComparator;
public static ComparatorFactory getInstance() { public static ComparatorFactory getInstance() {
if (instance == null) { if (instance == null) {
@@ -53,20 +53,6 @@ public class ComparatorFactory {
return componentTypeComparator; return componentTypeComparator;
} }
public Comparator<IDIYComponent<?>> getComponentZOrderComparator(
final List<IDIYComponent<?>> components) {
Comparator<IDIYComponent<?>> componentZOrderComparator = new Comparator<IDIYComponent<?>>() {
@Override
public int compare(IDIYComponent<?> o1, IDIYComponent<?> o2) {
int index1 = components.indexOf(o1);
int index2 = components.indexOf(o2);
return new Integer(index1).compareTo(index2);
}
};
return componentZOrderComparator;
}
public Comparator<PropertyWrapper> getPropertyNameComparator() { public Comparator<PropertyWrapper> getPropertyNameComparator() {
if (propertyNameComparator == null) { if (propertyNameComparator == null) {
propertyNameComparator = new Comparator<PropertyWrapper>() { propertyNameComparator = new Comparator<PropertyWrapper>() {
@@ -80,4 +66,23 @@ public class ComparatorFactory {
} }
return propertyNameComparator; return propertyNameComparator;
} }
public Comparator<IDIYComponent<?>> getComponentZOrderComparator() {
if (componentZOrderComparator == null) {
componentZOrderComparator = new Comparator<IDIYComponent<?>>() {
@Override
public int compare(IDIYComponent<?> o1, IDIYComponent<?> o2) {
ComponentType type1 = ComponentProcessor.getInstance()
.extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) o1.getClass());
ComponentType type2 = ComponentProcessor.getInstance()
.extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) o2.getClass());
return new Double(type1.getZOrder()).compareTo(type2.getZOrder());
}
};
}
return componentZOrderComparator;
}
} }
@@ -36,6 +36,7 @@ public class ComponentProcessor {
private static ComponentProcessor instance; private static ComponentProcessor instance;
private Map<Class<?>, List<PropertyWrapper>> propertyCache; private Map<Class<?>, List<PropertyWrapper>> propertyCache;
private Map<String, ComponentType> componentTypeMap;
private Cloner cloner; private Cloner cloner;
@@ -50,9 +51,13 @@ public class ComponentProcessor {
super(); super();
this.cloner = new Cloner(); this.cloner = new Cloner();
this.propertyCache = new HashMap<Class<?>, List<PropertyWrapper>>(); this.propertyCache = new HashMap<Class<?>, List<PropertyWrapper>>();
this.componentTypeMap = new HashMap<String, ComponentType>();
} }
public ComponentType createComponentTypeFrom(Class<? extends IDIYComponent<?>> clazz) { public ComponentType extractComponentTypeFrom(Class<? extends IDIYComponent<?>> clazz) {
if (componentTypeMap.containsKey(clazz.getName())) {
return componentTypeMap.get(clazz.getName());
}
String name; String name;
String description; String description;
CreationMethod creationMethod; CreationMethod creationMethod;
@@ -101,6 +106,7 @@ public class ComponentProcessor {
} }
ComponentType componentType = new ComponentType(name, description, creationMethod, ComponentType componentType = new ComponentType(name, description, creationMethod,
category, namePrefix, author, icon, clazz, zOrder, stretchable); category, namePrefix, author, icon, clazz, zOrder, stretchable);
componentTypeMap.put(clazz.getName(), componentType);
return componentType; return componentType;
} }
@@ -16,7 +16,7 @@ public class ComponentZOrderFiler implements IComponentFiler {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public boolean testComponent(IDIYComponent<?> component) { public boolean testComponent(IDIYComponent<?> component) {
ComponentType componentType = ComponentProcessor.getInstance().createComponentTypeFrom( ComponentType componentType = ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass()); (Class<? extends IDIYComponent<?>>) component.getClass());
return Math.round(componentType.getZOrder()) == zOrder; return Math.round(componentType.getZOrder()) == zOrder;
} }
@@ -11,7 +11,6 @@ import java.awt.Rectangle;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.awt.geom.Area; import java.awt.geom.Area;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -283,14 +282,12 @@ public class DrawingManager {
public List<IDIYComponent<?>> findComponentsAt(Point point, Project project) { public List<IDIYComponent<?>> findComponentsAt(Point point, Project project) {
List<IDIYComponent<?>> components = new ArrayList<IDIYComponent<?>>(); List<IDIYComponent<?>> components = new ArrayList<IDIYComponent<?>>();
for (Map.Entry<IDIYComponent<?>, Area> entry : componentAreaMap.entrySet()) { for (int i = 0; i < project.getComponents().size(); i++) {
if (entry.getValue().contains(point)) { Area area = componentAreaMap.get(project.getComponents().get(i));
components.add(entry.getKey()); if (area != null && area.contains(point)) {
components.add(project.getComponents().get(i));
} }
} }
// Sort by z-order.
Collections.sort(components, ComparatorFactory.getInstance().getComponentZOrderComparator(
project.getComponents()));
return components; return components;
} }
@@ -59,7 +59,6 @@ public class Presenter implements IPlugInPort {
private Project currentProject; private Project currentProject;
private Map<String, List<ComponentType>> componentTypes; private Map<String, List<ComponentType>> componentTypes;
// Maps component class names to ComponentType objects. // Maps component class names to ComponentType objects.
private Map<String, ComponentType> componentTypeMap;
private List<IPlugIn> plugIns; private List<IPlugIn> plugIns;
private ComponentSelection selectedComponents; private ComponentSelection selectedComponents;
@@ -266,14 +265,12 @@ public class Presenter implements IPlugInPort {
if (componentTypes == null) { if (componentTypes == null) {
LOG.info("Loading component types."); LOG.info("Loading component types.");
componentTypes = new HashMap<String, List<ComponentType>>(); componentTypes = new HashMap<String, List<ComponentType>>();
componentTypeMap = new HashMap<String, ComponentType>();
List<Class<?>> componentTypeClasses = JarScanner.getInstance().scanFolder("library/", List<Class<?>> componentTypeClasses = JarScanner.getInstance().scanFolder("library/",
IDIYComponent.class); IDIYComponent.class);
for (Class<?> clazz : componentTypeClasses) { for (Class<?> clazz : componentTypeClasses) {
if (!Modifier.isAbstract(clazz.getModifiers())) { if (!Modifier.isAbstract(clazz.getModifiers())) {
ComponentType componentType = ComponentProcessor.getInstance() ComponentType componentType = ComponentProcessor.getInstance()
.createComponentTypeFrom((Class<? extends IDIYComponent<?>>) clazz); .extractComponentTypeFrom((Class<? extends IDIYComponent<?>>) clazz);
componentTypeMap.put(componentType.getInstanceClass().getName(), componentType);
List<ComponentType> nestedList; List<ComponentType> nestedList;
if (componentTypes.containsKey(componentType.getCategory())) { if (componentTypes.containsKey(componentType.getCategory())) {
nestedList = componentTypes.get(componentType.getCategory()); nestedList = componentTypes.get(componentType.getCategory());
@@ -456,7 +453,9 @@ public class Presenter implements IPlugInPort {
// Go backwards so we take the highest z-order components first. // Go backwards so we take the highest z-order components first.
for (int i = currentProject.getComponents().size() - 1; i >= 0; i--) { for (int i = currentProject.getComponents().size() - 1; i >= 0; i--) {
IDIYComponent<?> component = currentProject.getComponents().get(i); IDIYComponent<?> component = currentProject.getComponents().get(i);
ComponentType componentType = componentTypeMap.get(component.getClass().getName()); ComponentType componentType = ComponentProcessor.getInstance()
.extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass());
for (int pointIndex = 0; pointIndex < component.getControlPointCount(); pointIndex++) { for (int pointIndex = 0; pointIndex < component.getControlPointCount(); pointIndex++) {
Point controlPoint = component.getControlPoint(pointIndex); Point controlPoint = component.getControlPoint(pointIndex);
// Only consider selected components that are not grouped. // Only consider selected components that are not grouped.
@@ -590,7 +589,9 @@ public class Presenter implements IPlugInPort {
int oldSize = controlPointMap.size(); int oldSize = controlPointMap.size();
LOG.debug("Expanding selected component map"); LOG.debug("Expanding selected component map");
for (IDIYComponent<?> component : currentProject.getComponents()) { for (IDIYComponent<?> component : currentProject.getComponents()) {
ComponentType componentType = componentTypeMap.get(component.getClass().getName()); ComponentType componentType = ComponentProcessor.getInstance()
.extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass());
// Do not process a component if it's already in the map and if it's // Do not process a component if it's already in the map and if it's
// locked. // locked.
@@ -786,7 +787,8 @@ public class Presenter implements IPlugInPort {
.getGridSpacing().convertToPixels()); .getGridSpacing().convertToPixels());
component.setControlPoint(point, i); component.setControlPoint(point, i);
} }
addComponent(component, componentTypeMap.get(component.getClass().getName())); addComponent(component, ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass()));
} }
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
.deepClone(currentProject), "Add"); .deepClone(currentProject), "Add");
@@ -982,8 +984,10 @@ public class Presenter implements IPlugInPort {
private void addComponent(IDIYComponent<?> component, ComponentType componentType) { private void addComponent(IDIYComponent<?> component, ComponentType componentType) {
int index = 0; int index = 0;
while (index < currentProject.getComponents().size() while (index < currentProject.getComponents().size()
&& componentType.getZOrder() >= componentTypeMap.get( && componentType.getZOrder() >= ComponentProcessor.getInstance()
currentProject.getComponents().get(index).getClass().getName()).getZOrder()) { .extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) currentProject.getComponents()
.get(index).getClass()).getZOrder()) {
index++; index++;
} }
if (index < currentProject.getComponents().size()) { if (index < currentProject.getComponents().size()) {
@@ -1104,7 +1108,8 @@ public class Presenter implements IPlugInPort {
} }
private boolean isComponentLocked(IDIYComponent<?> component) { private boolean isComponentLocked(IDIYComponent<?> component) {
ComponentType componentType = componentTypeMap.get(component.getClass().getName()); ComponentType componentType = ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass());
return currentProject.getLockedLayers().contains( return currentProject.getLockedLayers().contains(
(int) Math.round(componentType.getZOrder())); (int) Math.round(componentType.getZOrder()));
} }
@@ -7,7 +7,9 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@@ -26,6 +28,7 @@ import org.diylc.components.connectivity.SolderPad;
import org.diylc.components.passive.RadialFilmCapacitor; import org.diylc.components.passive.RadialFilmCapacitor;
import org.diylc.components.passive.Resistor; import org.diylc.components.passive.Resistor;
import org.diylc.components.semiconductors.DIL_IC; import org.diylc.components.semiconductors.DIL_IC;
import org.diylc.components.semiconductors.DiodePlastic;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.core.measures.Capacitance; import org.diylc.core.measures.Capacitance;
@@ -49,6 +52,15 @@ public class ProjectFileManager {
private static final int V1_PIXELS_PER_INCH = 200; 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 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 = new XStream(new DomDriver()); private XStream xStream = new XStream(new DomDriver());
@@ -73,8 +85,8 @@ public class ProjectFileManager {
fireFileStatusChanged(); fireFileStatusChanged();
} }
public Project deserializeProjectFromFile(String fileName, List<String> warnings) throws SAXException, IOException, public Project deserializeProjectFromFile(String fileName, List<String> warnings)
ParserConfigurationException { throws SAXException, IOException, ParserConfigurationException {
LOG.info(String.format("loadProjectFromFile(%s)", fileName)); LOG.info(String.format("loadProjectFromFile(%s)", fileName));
Project project; Project project;
File file = new File(fileName); File file = new File(fileName);
@@ -176,6 +188,11 @@ public class ProjectFileManager {
Point point2 = null; Point point2 = null;
Integer x2Attr = null; Integer x2Attr = null;
Integer y2Attr = 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 if (node.getAttributes().getNamedItem("X2") != null
&& node.getAttributes().getNamedItem("Y2") != null) { && node.getAttributes().getNamedItem("Y2") != null) {
x2Attr = Integer.parseInt(node.getAttributes().getNamedItem("X2") x2Attr = Integer.parseInt(node.getAttributes().getNamedItem("X2")
@@ -187,18 +204,21 @@ public class ProjectFileManager {
IDIYComponent<?> component = null; IDIYComponent<?> component = null;
if (nodeName.equalsIgnoreCase("pad")) { if (nodeName.equalsIgnoreCase("pad")) {
LOG.debug("Recognized " + nodeName); LOG.debug("Recognized " + nodeName);
String colorAttr = node.getAttributes().getNamedItem("Color").getNodeValue();
SolderPad pad = new SolderPad(); SolderPad pad = new SolderPad();
pad.setName(nameAttr); pad.setName(nameAttr);
// pad.setColor(Color.getColor(colorAttr)); if (color != null) {
pad.setColor(color);
}
pad.setControlPoint(convertV1CoordinatesToV3Point(referencePoint, x1Attr, pad.setControlPoint(convertV1CoordinatesToV3Point(referencePoint, x1Attr,
y1Attr), 0); y1Attr), 0);
component = pad; component = pad;
} else if (nodeName.equalsIgnoreCase("trace")) { } else if (nodeName.equalsIgnoreCase("trace")) {
LOG.debug("Recognized " + nodeName); LOG.debug("Recognized " + nodeName);
String colorAttr = node.getAttributes().getNamedItem("Color").getNodeValue();
CopperTrace trace = new CopperTrace(); CopperTrace trace = new CopperTrace();
trace.setName(nameAttr); trace.setName(nameAttr);
if (color != null) {
trace.setLeadColor(color);
}
trace.setControlPoint(point1, 0); trace.setControlPoint(point1, 0);
trace.setControlPoint(point2, 1); trace.setControlPoint(point2, 1);
component = trace; component = trace;
@@ -237,6 +257,20 @@ public class ProjectFileManager {
capacitor.setControlPoint(point1, 0); capacitor.setControlPoint(point1, 0);
capacitor.setControlPoint(point2, 1); capacitor.setControlPoint(point2, 1);
component = capacitor; 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.setWidth(new Size(6d, SizeUnit.mm));
capacitor.setHeight(new Size(2d, SizeUnit.mm));
capacitor.setControlPoint(point1, 0);
capacitor.setControlPoint(point2, 1);
component = capacitor;
} else if (nodeName.equalsIgnoreCase("ic")) { } else if (nodeName.equalsIgnoreCase("ic")) {
LOG.debug("Recognized " + nodeName); LOG.debug("Recognized " + nodeName);
DIL_IC ic = new DIL_IC(); DIL_IC ic = new DIL_IC();
@@ -253,11 +287,16 @@ public class ProjectFileManager {
} }
} }
ic.setName(nameAttr); ic.setName(nameAttr);
ic.setControlPoint(point1, 0); // 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); ic.setValue(valueAttr);
component = ic; component = ic;
} else { } else {
String message = "Could not recognize component type " + nodeName; String message = "Could not recognize component type " + nodeName;
LOG.debug(message); LOG.debug(message);
if (!warnings.contains(message)) { if (!warnings.contains(message)) {
warnings.add(message); warnings.add(message);
@@ -268,6 +307,8 @@ public class ProjectFileManager {
} }
} }
} }
Collections.sort(project.getComponents(), ComparatorFactory.getInstance()
.getComponentZOrderComparator());
return project; return project;
} }