changed control point api

This commit is contained in:
bancika
2011-01-05 19:21:59 +00:00
parent 8db24c2588
commit 51eee908c2
10 changed files with 106 additions and 348 deletions
@@ -11,8 +11,6 @@ import java.awt.Shape;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size; import org.diylc.core.measures.Size;
import org.diylc.utils.Constants; import org.diylc.utils.Constants;
@@ -34,8 +32,8 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
protected Size width; protected Size width;
protected Size height; protected Size height;
protected Point point1 = new Point(0, 0); protected Point[] points = new Point[] { new Point(0, 0),
protected Point point2 = new Point((int) (Constants.GRID * 10), 0); new Point((int) (Constants.GRID * 10), 0) };
protected String name = "New Component"; protected String name = "New Component";
protected AbstractLeadedDIYComponent() { protected AbstractLeadedDIYComponent() {
@@ -76,25 +74,40 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
*/ */
protected abstract Color getBorderColor(); protected abstract Color getBorderColor();
@Override
public int getControlPointCount() {
return points.length;
}
@Override
public Point getControlPoint(int index) {
return (Point) points[index].clone();
}
@Override
public void setControlPoint(Point point, int index) {
points[index].setLocation(point);
}
@Override @Override
public void draw(Graphics2D g2d, ComponentState componentState, Project project) { public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
g2d.setColor(LEAD_COLOR); g2d.setColor(LEAD_COLOR);
g2d.setStroke(new BasicStroke(1)); g2d.setStroke(new BasicStroke(1));
double distance = point1.distance(point2); double distance = points[0].distance(points[1]);
Shape shape = getComponentShape(); Shape shape = getComponentShape();
Rectangle shapeRect = shape.getBounds(); Rectangle shapeRect = shape.getBounds();
double leadLenght = (distance - shapeRect.width) / 2; double leadLenght = (distance - shapeRect.width) / 2;
Double theta; Double theta;
theta = Math.atan2(point2.y - point1.y, point2.x - point1.x); theta = Math.atan2(points[1].y - points[0].y, points[1].x - points[0].x);
// Draw leads. // Draw leads.
g2d.drawLine(point1.x, point1.y, (int) (point1.x + leadLenght * Math.cos(theta)), g2d.drawLine(points[0].x, points[0].y, (int) (points[0].x + leadLenght * Math.cos(theta)),
(int) (point1.y + leadLenght * Math.sin(theta))); (int) (points[0].y + leadLenght * Math.sin(theta)));
g2d.drawLine(point2.x, point2.y, (int) (point2.x - leadLenght * Math.cos(theta)), g2d.drawLine(points[1].x, points[1].y, (int) (points[1].x - leadLenght * Math.cos(theta)),
(int) (point2.y - leadLenght * Math.sin(theta))); (int) (points[1].y - leadLenght * Math.sin(theta)));
// Transform graphics to draw the body in the right place and at the // Transform graphics to draw the body in the right place and at the
// right angle. // right angle.
g2d.translate((point1.x + point2.x - shapeRect.width) / 2, g2d.translate((points[0].x + points[1].x - shapeRect.width) / 2,
(point1.y + point2.y - shapeRect.height) / 2); (points[0].y + points[1].y - shapeRect.height) / 2);
g2d.rotate(theta, shapeRect.width / 2, shapeRect.height / 2); g2d.rotate(theta, shapeRect.width / 2, shapeRect.height / 2);
// Draw body. // Draw body.
if (componentState != ComponentState.DRAGGING) { if (componentState != ComponentState.DRAGGING) {
@@ -123,24 +136,6 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
this.name = 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;
}
@EditableProperty(defaultable = true) @EditableProperty(defaultable = true)
public Size getWidth() { public Size getWidth() {
return width; return width;
@@ -9,9 +9,7 @@ import java.awt.geom.Rectangle2D;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.utils.Constants; import org.diylc.utils.Constants;
@@ -48,18 +46,23 @@ public class DIL_IC implements IDIYComponent<String> {
this.value = value; this.value = value;
} }
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED) @Override
public Point getPoint1() { public int getControlPointCount() {
return point1; return 2;
} }
public void setPoint1(Point point1) { @Override
this.point1 = point1; public Point getControlPoint(int index) {
return index == 0 ? point1 : point2;
} }
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED) @Override
public Point getPoint2() { public void setControlPoint(Point point, int index) {
return point2; if (index == 0) {
point1.setLocation(point);
} else {
point2.setLocation(point);
}
} }
public void setPoint2(Point point2) { public void setPoint2(Point point2) {
@@ -7,9 +7,7 @@ import java.awt.Point;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Capacitance; import org.diylc.core.measures.Capacitance;
import org.diylc.core.measures.CapacitanceUnit; import org.diylc.core.measures.CapacitanceUnit;
@@ -19,7 +17,7 @@ import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit; import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Mock", author = "bancika", category = "Sample", instanceNamePrefix = "M", desciption = "test") @ComponentDescriptor(name = "Mock", author = "bancika", category = "Sample", instanceNamePrefix = "M", desciption = "test")
public class MockDIYComponent implements IDIYComponent { public class MockDIYComponent implements IDIYComponent<Object> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -31,8 +29,8 @@ public class MockDIYComponent implements IDIYComponent {
private Point leftTopCorner = new Point(0, 0); private Point leftTopCorner = new Point(0, 0);
private String name = "something"; private String name = "something";
public MockDIYComponent(String testField, Capacitance c, Resistance r, Size s, public MockDIYComponent(String testField, Capacitance c, Resistance r, Size s, Color color,
Color color, Point leftTopCorner) { Point leftTopCorner) {
super(); super();
this.testField = testField; this.testField = testField;
this.c = c; this.c = c;
@@ -55,13 +53,19 @@ public class MockDIYComponent implements IDIYComponent {
this.name = name; this.name = name;
} }
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED) @Override
public Point getLeftTopCorner() { public int getControlPointCount() {
return 1;
}
@Override
public Point getControlPoint(int index) {
return leftTopCorner; return leftTopCorner;
} }
public void setLeftTopCorner(Point leftTopCorner) { @Override
this.leftTopCorner = leftTopCorner; public void setControlPoint(Point point, int index) {
leftTopCorner.setLocation(point);
} }
@EditableProperty(name = "Test Field") @EditableProperty(name = "Test Field")
@@ -1,120 +0,0 @@
package org.diylc.common;
import java.awt.Point;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.diylc.core.IDIYComponent;
import org.diylc.core.VisibilityPolicy;
/**
* Entity bean that represents a control point. Has methods for reading/writing
* {@link Point} value from/to a component.
*
* @author Branislav Stojkovic
*/
public class ControlPointWrapper {
private String name;
private Point value;
private Method setter;
private Method getter;
private boolean editable;
private boolean sticky;
private VisibilityPolicy visibilityPolicy;
public ControlPointWrapper(String name, Method getter, Method setter, boolean editable,
boolean sticky, VisibilityPolicy visibilityPolicy) {
super();
this.name = name;
this.getter = getter;
this.setter = setter;
this.editable = editable;
this.sticky = sticky;
this.visibilityPolicy = visibilityPolicy;
}
/**
* Reads value from the specified component by invoking it's
* <code>getter</code> method.
*
* @param component
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public void readFrom(IDIYComponent component) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
value = (Point) getter.invoke(component);
}
/**
* Writes the value into the specified component by invoking it's
* <code>setter</code> method.
*
* @param component
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public void writeTo(IDIYComponent component) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
setter.invoke(component, value);
}
public String getName() {
return name;
}
public boolean isEditable() {
return editable;
}
public boolean isSticky() {
return sticky;
}
public Point getValue() {
return value;
}
public void setValue(Point value) {
this.value = value;
}
public VisibilityPolicy getVisibilityPolicy() {
return visibilityPolicy;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ControlPointWrapper other = (ControlPointWrapper) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}
@@ -1,10 +1,10 @@
package org.diylc.core; package org.diylc.core;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point;
import java.io.Serializable; import java.io.Serializable;
import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
/** /**
@@ -16,7 +16,6 @@ import org.diylc.core.annotations.EditableProperty;
* <ul> * <ul>
* <li>Must have an empty constructor.</li> * <li>Must have an empty constructor.</li>
* <li>Class should be annotated with {@link ComponentDescriptor}.</li> * <li>Class should be annotated with {@link ComponentDescriptor}.</li>
* <li>Control point getters need to be annotated with {@link ControlPoint}.</li>
* <li>Getters for properties editable by users should be annotated with * <li>Getters for properties editable by users should be annotated with
* {@link EditableProperty}.</li> * {@link EditableProperty}.</li>
* <li>Component configuration should be stored int <code>public static</code> * <li>Component configuration should be stored int <code>public static</code>
@@ -37,6 +36,12 @@ public interface IDIYComponent<T> extends Serializable {
T getValue(); T getValue();
int getControlPointCount();
Point getControlPoint(int index);
void setControlPoint(Point point, int index);
/** /**
* Draws the component onto the {@link Graphics2D}. * Draws the component onto the {@link Graphics2D}.
* *
@@ -1,40 +0,0 @@
package org.diylc.core.annotations;
import java.awt.Point;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.diylc.core.VisibilityPolicy;
/**
* Annotation for control points. This annotation may be added fields of type
* {@link Point} only when both getters and setters exist. Annotation is added
* always on the getter and it must return {@link Point} values.
*
* @author Branislav Stojkovic
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ControlPoint {
/**
* Determines when should the control point be rendered on the screen.
*
* @return
*/
VisibilityPolicy visibilityPolicy() default VisibilityPolicy.ALWAYS;
/**
* If true, application will allow the user to edit the control point.
*
* @return
*/
boolean editable() default true;
/**
* If true, the control point may be joined with other components' control
* points.
*
* @return
*/
boolean sticky() default true;
}
@@ -19,7 +19,6 @@ import javax.swing.SwingUtilities;
import org.diylc.common.BadPositionException; import org.diylc.common.BadPositionException;
import org.diylc.common.ComponentType; import org.diylc.common.ComponentType;
import org.diylc.common.ControlPointWrapper;
import org.diylc.common.EventType; import org.diylc.common.EventType;
import org.diylc.common.IPlugIn; import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
@@ -166,7 +165,7 @@ public class StatusBar extends JPanel implements IPlugIn {
break; break;
case AVAILABLE_CTRL_POINTS_CHANGED: case AVAILABLE_CTRL_POINTS_CHANGED:
componentsUnderCursor = new ArrayList<IDIYComponent<?>>( componentsUnderCursor = new ArrayList<IDIYComponent<?>>(
((Map<IDIYComponent<?>, ControlPointWrapper>) params[0]).keySet()); ((Map<IDIYComponent<?>, Integer>) params[0]).keySet());
Collections.sort(componentsUnderCursor, ComparatorFactory.getInstance() Collections.sort(componentsUnderCursor, ComparatorFactory.getInstance()
.getComponentNameComparator()); .getComponentNameComparator());
refreshStatusText(); refreshStatusText();
@@ -1,6 +1,5 @@
package org.diylc.presenter; package org.diylc.presenter;
import java.awt.Point;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@@ -8,10 +7,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.diylc.common.ControlPointWrapper;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import com.rits.cloning.Cloner; import com.rits.cloning.Cloner;
@@ -28,7 +25,6 @@ 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<Class<?>, List<ControlPointWrapper>> controlPointCache;
private Cloner cloner; private Cloner cloner;
@@ -43,7 +39,6 @@ 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.controlPointCache = new HashMap<Class<?>, List<ControlPointWrapper>>();
} }
/** /**
@@ -86,56 +81,14 @@ public class ComponentProcessor {
return cloner.deepClone(properties); return cloner.deepClone(properties);
} }
/**
* Reads all control points from the specified component class. Note than
* control points are cached, so it may happen that control points returned
* already have their values populated. Always use
* {@link ControlPointWrapper#readFrom(IDIYComponent)} to update their state
* from an actual component.
*
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public List<ControlPointWrapper> extractControlPoints(Class<? extends IDIYComponent> clazz) {
if (controlPointCache.containsKey(clazz)) {
return cloner.deepClone(controlPointCache.get(clazz));
}
List<ControlPointWrapper> controlPoints = new ArrayList<ControlPointWrapper>();
for (Method method : clazz.getMethods()) {
if (method.getName().startsWith("get")) {
try {
Method setter = clazz.getMethod("set" + method.getName().substring(3), method
.getReturnType());
if (method.isAnnotationPresent(ControlPoint.class)) {
if (method.getReturnType().equals(Point.class)) {
ControlPoint annotation = method.getAnnotation(ControlPoint.class);
ControlPointWrapper controlPoint = new ControlPointWrapper(method
.getName().substring(3), method, setter, annotation.editable(),
annotation.sticky(), annotation.visibilityPolicy());
controlPoints.add(controlPoint);
} else {
LOG.debug("Control point return type must be java.awt.Point.");
}
}
} catch (NoSuchMethodException e) {
LOG.debug("No matching setter found for \"" + method.getName()
+ "\". Skipping...");
}
}
}
controlPointCache.put(clazz, controlPoints);
return cloner.deepClone(controlPoints);
}
/** /**
* Returns properties mutual for all the selected components. * Returns properties mutual for all the selected components.
* *
* @param selectedComponents * @param selectedComponents
* @return * @return
*/ */
public List<PropertyWrapper> getMutualSelectionProperties(List<IDIYComponent<?>> selectedComponents) { public List<PropertyWrapper> getMutualSelectionProperties(
List<IDIYComponent<?>> selectedComponents) {
if (selectedComponents.isEmpty()) { if (selectedComponents.isEmpty()) {
return null; return null;
} }
@@ -22,7 +22,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.Map.Entry;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.Icon; import javax.swing.Icon;
@@ -33,7 +32,6 @@ import org.apache.log4j.Logger;
import org.diylc.common.BadPositionException; import org.diylc.common.BadPositionException;
import org.diylc.common.ComponentSelection; import org.diylc.common.ComponentSelection;
import org.diylc.common.ComponentType; import org.diylc.common.ComponentType;
import org.diylc.common.ControlPointWrapper;
import org.diylc.common.DrawOption; import org.diylc.common.DrawOption;
import org.diylc.common.EventType; import org.diylc.common.EventType;
import org.diylc.common.IPlugIn; import org.diylc.common.IPlugIn;
@@ -43,7 +41,6 @@ import org.diylc.core.ComponentLayer;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.gui.IView; import org.diylc.gui.IView;
import org.diylc.utils.Constants; import org.diylc.utils.Constants;
@@ -79,7 +76,7 @@ public class Presenter implements IPlugInPort {
private ComponentSelection selectedComponents; private ComponentSelection selectedComponents;
// List of components that have at least one of their control points // List of components that have at least one of their control points
// under the last recorded mouse position. // under the last recorded mouse position.
private Map<IDIYComponent<?>, ControlPointWrapper> componentControlPointMap; private Map<IDIYComponent<?>, Integer> componentControlPointMap;
private Cloner cloner; private Cloner cloner;
@@ -322,16 +319,13 @@ public class Presenter implements IPlugInPort {
// Draw control points // Draw control points
if (drawOptions.contains(DrawOption.CONTROL_POINTS)) { if (drawOptions.contains(DrawOption.CONTROL_POINTS)) {
List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance() for (int i = 0; i < component.getControlPointCount(); i++) {
.extractControlPoints(component.getClass()); Point controlPoint = component.getControlPoint(i);
for (ControlPointWrapper controlPoint : controlPoints) {
try { try {
controlPoint.readFrom(component); if (shouldShowControlPointsFor(component)) {
if (shouldShowControlPoint(controlPoint, component)) {
g2d.setColor(Constants.CONTROL_POINT_COLOR); g2d.setColor(Constants.CONTROL_POINT_COLOR);
g2d.setStroke(new BasicStroke(2)); g2d.setStroke(new BasicStroke(2));
g2d.drawOval(controlPoint.getValue().x - 2, g2d.drawOval(controlPoint.x - 2, controlPoint.y - 2, 4, 4);
controlPoint.getValue().y - 2, 4, 4);
// g2d.fillOval(point.getValue().x - 2, point // g2d.fillOval(point.getValue().x - 2, point
// .getValue().y - 2, 5, 5); // .getValue().y - 2, 5, 5);
} }
@@ -439,17 +433,15 @@ public class Presenter implements IPlugInPort {
@Override @Override
public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) { public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) {
Map<IDIYComponent<?>, ControlPointWrapper> components = new HashMap<IDIYComponent<?>, ControlPointWrapper>(); Map<IDIYComponent<?>, Integer> components = new HashMap<IDIYComponent<?>, Integer>();
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
for (IDIYComponent<?> component : currentProject.getComponents()) { for (IDIYComponent<?> component : currentProject.getComponents()) {
List<ControlPointWrapper> currentPoints = ComponentProcessor.getInstance() for (int i = 0; i < component.getControlPointCount(); i++) {
.extractControlPoints(component.getClass()); Point controlPoint = component.getControlPoint(i);
for (ControlPointWrapper controlPoint : currentPoints) { if (shouldShowControlPointsFor(component)) {
if (shouldShowControlPoint(controlPoint, component)) {
try { try {
controlPoint.readFrom(component); if (scaledPoint.distance(controlPoint) < CONTROL_POINT_SENSITIVITY) {
if (scaledPoint.distance(controlPoint.getValue()) < CONTROL_POINT_SENSITIVITY) { components.put(component, i);
components.put(component, controlPoint);
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
@@ -525,37 +517,21 @@ public class Presenter implements IPlugInPort {
if (!componentControlPointMap.isEmpty()) { if (!componentControlPointMap.isEmpty()) {
// We're dragging control point(s). // We're dragging control point(s).
IDIYComponent<?> firstComponent = componentControlPointMap.keySet().iterator().next(); IDIYComponent<?> firstComponent = componentControlPointMap.keySet().iterator().next();
ControlPointWrapper controlPoint = componentControlPointMap.get(firstComponent); Point controlPoint = firstComponent.getControlPoint(componentControlPointMap
.get(firstComponent));
if (controlPoint == null) { if (controlPoint == null) {
LOG.warn("Control point not found in the map!"); LOG.warn("Control point not found in the map!");
} }
// Re-read the value just in case. int x = (int) (Math.round((controlPoint.x + scaledPoint.x - previousDragPoint.x)
try { / Constants.GRID) * Constants.GRID);
controlPoint.readFrom(firstComponent); int y = (int) (Math.round((controlPoint.y + scaledPoint.y - previousDragPoint.y)
} catch (Exception e) { / Constants.GRID) * Constants.GRID);
e.printStackTrace();
}
int x = (int) (Math
.round((controlPoint.getValue().x + scaledPoint.x - previousDragPoint.x)
/ Constants.GRID) * Constants.GRID);
int y = (int) (Math
.round((controlPoint.getValue().y + scaledPoint.y - previousDragPoint.y)
/ Constants.GRID) * Constants.GRID);
previousDragPoint.setLocation(x, y); previousDragPoint.setLocation(x, y);
for (Entry<IDIYComponent<?>, ControlPointWrapper> entry : componentControlPointMap // Update all points.
.entrySet()) { for (Map.Entry<IDIYComponent<?>, Integer> entry : componentControlPointMap.entrySet()) {
try { Point p = new Point(x, y);
controlPoint = entry.getValue(); entry.getKey().setControlPoint(p, entry.getValue());
IDIYComponent<?> component = entry.getKey();
controlPoint.readFrom(component);
if (controlPoint.isEditable()) {
controlPoint.getValue().setLocation(x, y);
}
controlPoint.writeTo(component);
} catch (Exception e) {
}
} }
// dragStartPoint = point; // dragStartPoint = point;
} else if (selectedComponents.isEmpty()) { } else if (selectedComponents.isEmpty()) {
@@ -616,26 +592,18 @@ public class Presenter implements IPlugInPort {
int dy = (int) (Math.round((toPoint.y - fromPoint.y) / zoomLevel / Constants.GRID) * Constants.GRID); int dy = (int) (Math.round((toPoint.y - fromPoint.y) / zoomLevel / Constants.GRID) * Constants.GRID);
fromPoint.translate(dx, dy); fromPoint.translate(dx, dy);
for (IDIYComponent<?> component : selectedComponents) { for (IDIYComponent<?> component : selectedComponents) {
List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance() for (int i = 0; i < component.getControlPointCount(); i++) {
.extractControlPoints(component.getClass()); Point controlPoint = component.getControlPoint(i);
for (ControlPointWrapper controlPoint : controlPoints) { translateControlPoint(controlPoint, dx, dy);
try { component.setControlPoint(controlPoint, i);
controlPoint.readFrom(component);
if (controlPoint.isEditable()) {
translateControlPoint(controlPoint, dx, dy);
}
controlPoint.writeTo(component);
} catch (Exception e) {
LOG.error("Could not translate control points: " + e.getMessage());
}
} }
} }
} }
private void translateControlPoint(ControlPointWrapper controlPoint, int dx, int dy) { private void translateControlPoint(Point controlPoint, int dx, int dy) {
int x = controlPoint.getValue().x + dx; int x = controlPoint.x + dx;
int y = controlPoint.getValue().y + dy; int y = controlPoint.y + dy;
controlPoint.getValue().setLocation(x, y); controlPoint.setLocation(x, y);
} }
@Override @Override
@@ -727,19 +695,16 @@ public class Presenter implements IPlugInPort {
// Add it to the project. // Add it to the project.
currentProject.getComponents().add(component); currentProject.getComponents().add(component);
// Extract control points.
List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance()
.extractControlPoints(componentClass);
// Translate them to the desired location. // Translate them to the desired location.
if (point != null) { if (point != null) {
for (ControlPointWrapper controlPoint : controlPoints) { for (int j = 0; j < component.getControlPointCount(); j++) {
controlPoint.readFrom(component); Point controlPoint = component.getControlPoint(j);
int x = controlPoint.getValue().x + point.x; int x = controlPoint.x + point.x;
int y = controlPoint.getValue().y + point.y; int y = controlPoint.y + point.y;
x = (int) (Math.round(x / Constants.GRID) * Constants.GRID); x = (int) (Math.round(x / Constants.GRID) * Constants.GRID);
y = (int) (Math.round(y / Constants.GRID) * Constants.GRID); y = (int) (Math.round(y / Constants.GRID) * Constants.GRID);
controlPoint.getValue().setLocation(x, y); controlPoint.setLocation(x, y);
controlPoint.writeTo(component); component.setControlPoint(controlPoint, j);
} }
} }
@@ -814,9 +779,13 @@ public class Presenter implements IPlugInPort {
(int) (point.y / zoomLevel)); (int) (point.y / zoomLevel));
} }
private boolean shouldShowControlPoint(ControlPointWrapper controlPoint, IDIYComponent<?> component) { private boolean shouldShowControlPointsFor(IDIYComponent<?> component) {
return controlPoint.getVisibilityPolicy().equals(VisibilityPolicy.ALWAYS) return selectedComponents.contains(component);
|| ((controlPoint.getVisibilityPolicy().equals(VisibilityPolicy.WHEN_SELECTED)) && (getSelectedComponents() // return
.contains(component))); // controlPoint.getVisibilityPolicy().equals(VisibilityPolicy.ALWAYS)
// ||
// ((controlPoint.getVisibilityPolicy().equals(VisibilityPolicy.WHEN_SELECTED))
// && (getSelectedComponents()
// .contains(component)));
} }
} }
@@ -5,10 +5,8 @@ import static org.junit.Assert.assertNotNull;
import java.util.List; import java.util.List;
import org.diylc.common.ControlPointWrapper;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
import org.diylc.components.MockDIYComponent; import org.diylc.components.MockDIYComponent;
import org.diylc.presenter.ComponentProcessor;
import org.junit.Test; import org.junit.Test;
@@ -21,12 +19,4 @@ public class ClassProcessorTest {
assertNotNull(properties); assertNotNull(properties);
assertEquals(4, properties.size()); assertEquals(4, properties.size());
}; };
@Test
public void testExtractControlPoints() {
List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance()
.extractControlPoints(MockDIYComponent.class);
assertNotNull(controlPoints);
assertEquals(1, controlPoints.size());
};
} }