Component templates save shape of the component (for wires and such)

This commit is contained in:
bancika
2012-10-04 13:50:35 +00:00
parent ac0da8667c
commit 5d2c4e0fc9
3 changed files with 43 additions and 3 deletions
@@ -1,16 +1,20 @@
package org.diylc.core;
import java.awt.Point;
import java.util.List;
import java.util.Map;
public class Template {
private String name;
private Map<String, Object> values;
private List<Point> points;
public Template(String name, Map<String, Object> values) {
public Template(String name, Map<String, Object> values, List<Point> points) {
super();
this.name = name;
this.values = values;
this.points = points;
}
public String getName() {
@@ -29,11 +33,20 @@ public class Template {
this.values = values;
}
public List<Point> getPoints() {
return points;
}
public void setPoints(List<Point> points) {
this.points = points;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((points == null) ? 0 : points.hashCode());
result = prime * result + ((values == null) ? 0 : values.hashCode());
return result;
}
@@ -52,6 +65,11 @@ public class Template {
return false;
} else if (!name.equals(other.name))
return false;
if (points == null) {
if (other.points != null)
return false;
} else if (!points.equals(other.points))
return false;
if (values == null) {
if (other.values != null)
return false;
@@ -233,6 +233,16 @@ public class InstantiationManager {
component.setControlPoint(controlPoint, j);
}
}
// Make the component into shape from the template
if (template != null && template.getPoints() != null
&& template.getPoints().size() >= component
.getControlPointCount()) {
for (int i = 0; i < component.getControlPointCount(); i++) {
Point p = new Point(component.getControlPoint(0));
p.translate(template.getPoints().get(i).x, template.getPoints().get(i).y);
component.setControlPoint(p, i);
}
}
fillWithDefaultProperties(component, template);
@@ -1741,7 +1741,7 @@ public class Presenter implements IPlugInPort {
templateName));
if (selectedComponents.size() != 1) {
throw new RuntimeException(
"Can only save a single component at a time.");
"Can only save a single component as a template at once.");
}
IDIYComponent<?> component = selectedComponents.iterator().next();
ComponentType type = ComponentProcessor.getInstance()
@@ -1772,7 +1772,19 @@ public class Presenter implements IPlugInPort {
} catch (Exception e) {
}
}
Template template = new Template(templateName, values);
List<Point> points = new ArrayList<Point>();
for (int i = 0; i< component.getControlPointCount(); i++) {
Point p = new Point(component.getControlPoint(i));
points.add(p);
}
int x = points.iterator().next().x;
int y = points.iterator().next().y;
for (Point point : points) {
point.translate(-x, -y);
}
Template template = new Template(templateName, values, points);
boolean exists = false;
for (Template t : templates) {
if (t.getName().equalsIgnoreCase(templateName)) {