added point by point component creation

This commit is contained in:
bancika
2011-01-16 21:11:41 +00:00
parent 8186841851
commit 1d0f393931
20 changed files with 223 additions and 130 deletions
@@ -2,6 +2,7 @@ package org.diylc.common;
import javax.swing.Icon;
import org.diylc.core.CreationMethod;
import org.diylc.core.IDIYComponent;
/**
@@ -11,27 +12,29 @@ import org.diylc.core.IDIYComponent;
*
* @see IDIYComponent
*/
@SuppressWarnings("rawtypes")
public class ComponentType {
private String name;
private String description;
private CreationMethod creationMethod;
private String category;
private String namePrefix;
private String author;
private Icon icon;
@SuppressWarnings("unchecked")
private Class<? extends IDIYComponent> instanceClass;
private double zOrder;
private boolean stretchable;
private boolean sticky;
@SuppressWarnings("unchecked")
public ComponentType(String name, String description, String category, String namePrefix,
String author, Icon icon, Class<? extends IDIYComponent> instanceClass,
double zOrder, boolean stretchable, boolean sticky) {
public ComponentType(String name, String description, CreationMethod creationMethod,
String category, String namePrefix, String author, Icon icon,
Class<? extends IDIYComponent> instanceClass, double zOrder, boolean stretchable,
boolean sticky) {
super();
this.name = name;
this.description = description;
this.creationMethod = creationMethod;
this.category = category;
this.namePrefix = namePrefix;
this.author = author;
@@ -50,6 +53,10 @@ public class ComponentType {
return description;
}
public CreationMethod getCreationMethod() {
return creationMethod;
}
public String getCategory() {
return category;
}
@@ -66,7 +73,6 @@ public class ComponentType {
return icon;
}
@SuppressWarnings("unchecked")
public Class<? extends IDIYComponent> getInstanceClass() {
return instanceClass;
}
@@ -78,7 +84,7 @@ public class ComponentType {
public boolean isStretchable() {
return stretchable;
}
public boolean isSticky() {
return sticky;
}
@@ -31,10 +31,10 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
private static final long serialVersionUID = 1L;
public static Color LEAD_COLOR = Color.black;
public static Color LEAD_COLOR = Color.decode("#65909A");
public static Color LABEL_COLOR = Color.black;
public static Color LABEL_COLOR_SELECTED = Color.red;
public static Size LEAD_THICKNESS = new Size(0.3d, SizeUnit.mm);
public static Size LEAD_THICKNESS = new Size(0.8d, SizeUnit.mm);
public static Size DEFAULT_SIZE = new Size(1d, SizeUnit.in);
protected Size width;
@@ -91,12 +91,15 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
|| componentState == ComponentState.DRAGGING ? SELECTION_COLOR : borderColor);
g2d.draw(shape);
// Draw leads.
g2d.setStroke(new BasicStroke(getLeadThickness().convertToPixels()));
int leadThickness = getLeadThickness().convertToPixels();
g2d.setStroke(new BasicStroke(leadThickness));
g2d.setColor(getLeadColor(componentState));
g2d.drawLine((int) (shapeRect.width - distance) / 2, (int) shapeRect.height / 2,
(int) ((shapeRect.width - distance) / 2 + leadLenght), (int) shapeRect.height / 2);
(int) ((shapeRect.width - distance) / 2 + leadLenght) - leadThickness / 2,
(int) shapeRect.height / 2);
g2d.drawLine((int) (shapeRect.width + distance) / 2, (int) shapeRect.height / 2,
(int) ((shapeRect.width + distance) / 2 - leadLenght), (int) shapeRect.height / 2);
(int) ((shapeRect.width + distance) / 2 - leadLenght) + leadThickness / 2,
(int) shapeRect.height / 2);
// Do not track the label because that area was already covered.
drawingObserver.stopTracking();
// Draw label.
@@ -204,12 +207,12 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
public void setHeight(Size height) {
this.height = height;
}
@EditableProperty
public Display getDisplay() {
return display;
}
public void setDisplay(Display display) {
this.display = display;
}
@@ -0,0 +1,6 @@
package org.diylc.core;
public enum CreationMethod {
SINGLE_CLICK, POINT_BY_POINT;
}
@@ -3,6 +3,7 @@ package org.diylc.core.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.diylc.core.CreationMethod;
import org.diylc.core.IDIYComponent;
/**
@@ -24,6 +25,13 @@ public @interface ComponentDescriptor {
*/
String description();
/**
* @return method that should be used to create a component. If
* <code>CreationMethod.POINT_BY_POINT</code> is used, user will
* have to select ending points before the component is created.
*/
CreationMethod creationMethod() default CreationMethod.SINGLE_CLICK;
/**
* @return component category, e.g. "Passive", "Semiconductors", etc.
*/
@@ -17,6 +17,7 @@ import javax.swing.ImageIcon;
import org.apache.log4j.Logger;
import org.diylc.common.ComponentType;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.CreationMethod;
import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
@@ -54,6 +55,7 @@ public class ComponentProcessor {
public ComponentType createComponentTypeFrom(Class<? extends IDIYComponent<?>> clazz) {
String name;
String description;
CreationMethod creationMethod;
String category;
String namePrefix;
String author;
@@ -65,6 +67,7 @@ public class ComponentProcessor {
ComponentDescriptor annotation = clazz.getAnnotation(ComponentDescriptor.class);
name = annotation.name();
description = annotation.description();
creationMethod = annotation.creationMethod();
category = annotation.category();
namePrefix = annotation.instanceNamePrefix();
author = annotation.author();
@@ -74,6 +77,7 @@ public class ComponentProcessor {
} else {
name = clazz.getSimpleName();
description = "";
creationMethod = CreationMethod.SINGLE_CLICK;
category = "Uncategorized";
namePrefix = "Unknown";
author = "Unknown";
@@ -88,9 +92,7 @@ public class ComponentProcessor {
Image image = new BufferedImage(Presenter.ICON_SIZE, Presenter.ICON_SIZE,
java.awt.image.BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d
.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
componentInstance.drawIcon(g2d, Presenter.ICON_SIZE, Presenter.ICON_SIZE);
icon = new ImageIcon(image);
} catch (InstantiationException e) {
@@ -98,8 +100,8 @@ public class ComponentProcessor {
} catch (IllegalAccessException e) {
e.printStackTrace();
}
ComponentType componentType = new ComponentType(name, description, category, namePrefix,
author, icon, clazz, zOrder, stretchable, sticky);
ComponentType componentType = new ComponentType(name, description, creationMethod,
category, namePrefix, author, icon, clazz, zOrder, stretchable, sticky);
return componentType;
}
@@ -14,6 +14,7 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -38,6 +39,7 @@ import org.diylc.common.IComponentFiler;
import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.CreationMethod;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.measures.SizeUnit;
@@ -103,7 +105,10 @@ public class Presenter implements IPlugInPort {
private Point previousDragPoint = null;
private Project preDragProject = null;
// Component creation
private ComponentType componentSlot;
private Point controlPointSlot;
private Point potentialControlPoint;
private boolean snapToGrid = true;
@@ -307,8 +312,9 @@ public class Presenter implements IPlugInPort {
drawControlPoints.add(component);
}
}
projectPainter.draw(g2d, currentProject, drawOptions, filter, selectionRect,
selectedComponents, drawControlPoints, dragInProgress, zoomLevel);
projectPainter.drawProject(g2d, currentProject, drawOptions, filter, selectionRect,
selectedComponents, drawControlPoints, Arrays.asList(controlPointSlot,
potentialControlPoint), dragInProgress, zoomLevel);
}
@Override
@@ -347,13 +353,34 @@ public class Presenter implements IPlugInPort {
.format("mouseClicked(%s, %s, %s, %s)", point, ctrlDown, shiftDown, altDown));
Point scaledPoint = scalePoint(point);
if (componentSlot != null) {
try {
instantiateComponent(componentSlot, scaledPoint);
} catch (Exception e) {
LOG.error("Error instatiating component of type: "
+ componentSlot.getInstanceClass().getName(), e);
switch (componentSlot.getCreationMethod()) {
case SINGLE_CLICK:
try {
instantiateComponent(componentSlot, scaledPoint);
} catch (Exception e) {
LOG.error("Error instatiating component of type: "
+ componentSlot.getInstanceClass().getName(), e);
}
setNewComponentSlot(null);
break;
case POINT_BY_POINT:
if (controlPointSlot != null) {
try {
instantiateComponent(componentSlot, controlPointSlot, scaledPoint);
} catch (Exception e) {
LOG.error("Error instatiating component of type: "
+ componentSlot.getInstanceClass().getName(), e);
}
setNewComponentSlot(null);
} else {
controlPointSlot = scaledPoint;
snapPointToGrid(controlPointSlot);
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
break;
default:
LOG.error("Unknown creation method: " + componentSlot.getCreationMethod());
}
setNewComponentSlot(null);
} else {
List<IDIYComponent<?>> components = findComponentsAt(scaledPoint);
// If there's nothing under mouse cursor deselect all.
@@ -388,38 +415,46 @@ public class Presenter implements IPlugInPort {
public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) {
Map<IDIYComponent<?>, Set<Integer>> components = new HashMap<IDIYComponent<?>, Set<Integer>>();
Point scaledPoint = scalePoint(point);
// Go backwards so we take the highest z-order components first.
for (int i = currentProject.getComponents().size() - 1; i >= 0; i--) {
IDIYComponent<?> component = currentProject.getComponents().get(i);
ComponentType componentType = componentTypeMap.get(component.getClass().getName());
for (int pointIndex = 0; pointIndex < component.getControlPointCount(); pointIndex++) {
Point controlPoint = component.getControlPoint(pointIndex);
// Only consider selected components that are not grouped.
if (selectedComponents.contains(component) && componentType.isStretchable()
&& findAllGroupedComponents(component).size() == 1) {
try {
if (scaledPoint.distance(controlPoint) < ProjectPainter.CONTROL_POINT_SIZE) {
Set<Integer> indices = new HashSet<Integer>();
if (componentType.isStretchable()) {
indices.add(pointIndex);
} else {
for (int j = 0; j < component.getControlPointCount(); j++) {
indices.add(j);
if (componentSlot != null
&& componentSlot.getCreationMethod() == CreationMethod.POINT_BY_POINT) {
potentialControlPoint = scaledPoint;
snapPointToGrid(potentialControlPoint);
messageDispatcher.dispatchMessage(EventType.REPAINT);
} else {
// Go backwards so we take the highest z-order components first.
for (int i = currentProject.getComponents().size() - 1; i >= 0; i--) {
IDIYComponent<?> component = currentProject.getComponents().get(i);
ComponentType componentType = componentTypeMap.get(component.getClass().getName());
for (int pointIndex = 0; pointIndex < component.getControlPointCount(); pointIndex++) {
Point controlPoint = component.getControlPoint(pointIndex);
// Only consider selected components that are not grouped.
if (selectedComponents.contains(component) && componentType.isStretchable()
&& findAllGroupedComponents(component).size() == 1) {
try {
if (scaledPoint.distance(controlPoint) < ProjectPainter.CONTROL_POINT_SIZE) {
Set<Integer> indices = new HashSet<Integer>();
if (componentType.isStretchable()) {
indices.add(pointIndex);
} else {
for (int j = 0; j < component.getControlPointCount(); j++) {
indices.add(j);
}
}
components.put(component, indices);
break;
}
components.put(component, indices);
break;
} catch (Exception e) {
LOG.warn("Error reading control point for component of type: "
+ component.getClass().getName());
}
} catch (Exception e) {
LOG.warn("Error reading control point for component of type: "
+ component.getClass().getName());
}
}
// // If CTRL is pressed, we only care about the top most
// component.
// if (altDown && components.size() > 0) {
// break;
// }
}
// // If CTRL is pressed, we only care about the top most component.
// if (altDown && components.size() > 0) {
// break;
// }
}
messageDispatcher.dispatchMessage(EventType.MOUSE_MOVED, scaledPoint);
@@ -637,6 +672,16 @@ public class Presenter implements IPlugInPort {
return (Math.round(1f * x / grid) * grid);
}
private void snapPointToGrid(Point point) {
int x = point.x;
int y = point.y;
if (snapToGrid) {
x = roundToGrid(x);
y = roundToGrid(y);
}
point.setLocation(x, y);
}
@Override
public void dragEnded(Point point) {
LOG.debug(String.format("dragEnded(%s)", point));
@@ -719,7 +764,6 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
@SuppressWarnings("unchecked")
@Override
public void setSelectionDefaultPropertyValue(String propertyName, Object value) {
LOG.info(String.format("setSelectionDefaultPropertyValue(%s, %s)", propertyName, value));
@@ -887,8 +931,8 @@ public class Presenter implements IPlugInPort {
}
}
@SuppressWarnings("unchecked")
private void instantiateComponent(ComponentType componentType, Point point) throws Exception {
private void instantiateComponent(ComponentType componentType, Point... points)
throws Exception {
LOG.info("Instatiating component of type: " + componentType.getInstanceClass().getName());
Project oldProject = cloner.deepClone(currentProject);
@@ -919,17 +963,23 @@ public class Presenter implements IPlugInPort {
selectedComponents.add(component);
// Translate them to the desired location.
if (point != null) {
for (int j = 0; j < component.getControlPointCount(); j++) {
Point controlPoint = new Point(component.getControlPoint(j));
int x = controlPoint.x + point.x;
int y = controlPoint.y + point.y;
if (snapToGrid) {
x = roundToGrid(x);
y = roundToGrid(y);
if (points != null) {
switch (componentType.getCreationMethod()) {
case SINGLE_CLICK:
for (int j = 0; j < component.getControlPointCount(); j++) {
Point controlPoint = new Point(component.getControlPoint(j));
controlPoint.translate(points[0].x, points[0].y);
snapPointToGrid(controlPoint);
component.setControlPoint(controlPoint, j);
}
controlPoint.setLocation(x, y);
component.setControlPoint(controlPoint, j);
break;
case POINT_BY_POINT:
for (int j = 0; j < points.length; j++) {
Point point = points[j];
snapPointToGrid(point);
component.setControlPoint(point, j);
}
break;
}
}
@@ -1064,6 +1114,8 @@ public class Presenter implements IPlugInPort {
LOG.info(String.format("setNewComponentSlot(%s)", componentType == null ? null
: componentType.getName()));
this.componentSlot = componentType;
this.controlPointSlot = null;
this.potentialControlPoint = null;
selectedComponents.clear();
fireSelectionChanged();
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
@@ -1098,6 +1150,6 @@ public class Presenter implements IPlugInPort {
if (findAllGroupedComponents(component).size() > 1) {
return false;
}
return selectedComponents.contains(component);
return true;
}
}
@@ -16,7 +16,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.diylc.common.ComponentSelection;
import org.diylc.common.DrawOption;
import org.diylc.common.IComponentFiler;
@@ -33,9 +32,7 @@ import org.diylc.utils.Constants;
*/
public class ProjectPainter {
private static final Logger LOG = Logger.getLogger(ProjectPainter.class);
public static final int CONTROL_POINT_SIZE = 5;
public static final int CONTROL_POINT_SIZE = 7;
public static boolean ENABLE_ANTIALIASING = true;
public static boolean DEBUG_COMPONENT_AREAS = false;
@@ -51,9 +48,10 @@ public class ProjectPainter {
lastDrawnStateMap = new HashMap<IDIYComponent<?>, ComponentState>();
}
public void draw(Graphics2D g2d, Project project, Set<DrawOption> drawOptions,
public void drawProject(Graphics2D g2d, Project project, Set<DrawOption> drawOptions,
IComponentFiler filter, Rectangle selectionRect, ComponentSelection selectedComponents,
Set<IDIYComponent<?>> drawControlPoints, boolean dragInProgress, double zoomLevel) {
Set<IDIYComponent<?>> drawControlPoints, List<Point> controlPointSlot,
boolean dragInProgress, double zoomLevel) {
if (project == null) {
return;
}
@@ -93,57 +91,64 @@ public class ProjectPainter {
// g2dWrapper.resetTx();
List<IDIYComponent<?>> components = project.getComponents();
// componentAreaMap.clear();
if (components != null) {
for (IDIYComponent<?> component : components) {
// Do not draw the component if it's filtered out.
if (filter != null && !filter.testComponent(component)) {
continue;
}
ComponentState state = ComponentState.NORMAL;
if (drawOptions.contains(DrawOption.SELECTION)
&& selectedComponents.contains(component)) {
if (dragInProgress) {
state = ComponentState.DRAGGING;
} else {
state = ComponentState.SELECTED;
}
}
// Do not track the area if component is not invalidated and was
// drawn in the same state.
boolean trackArea = lastDrawnStateMap.get(component) != state;
g2dWrapper.startedDrawingComponent();
if (!trackArea) {
g2dWrapper.stopTracking();
}
// Draw the component through the g2dWrapper.
component.draw(g2dWrapper, state, project, g2dWrapper);
Area area = g2dWrapper.finishedDrawingComponent();
if (trackArea) {
componentAreaMap.put(component, area);
lastDrawnStateMap.put(component, state);
for (IDIYComponent<?> component : project.getComponents()) {
// Do not draw the component if it's filtered out.
if (filter != null && !filter.testComponent(component)) {
continue;
}
ComponentState state = ComponentState.NORMAL;
if (drawOptions.contains(DrawOption.SELECTION)
&& selectedComponents.contains(component)) {
if (dragInProgress) {
state = ComponentState.DRAGGING;
} else {
state = ComponentState.SELECTED;
}
}
// Draw control points.
for (IDIYComponent<?> component : components) {
if (drawOptions.contains(DrawOption.CONTROL_POINTS)) {
// Do not track the area if component is not invalidated and was
// drawn in the same state.
boolean trackArea = lastDrawnStateMap.get(component) != state;
g2dWrapper.startedDrawingComponent();
if (!trackArea) {
g2dWrapper.stopTracking();
}
// Draw the component through the g2dWrapper.
component.draw(g2dWrapper, state, project, g2dWrapper);
Area area = g2dWrapper.finishedDrawingComponent();
if (trackArea) {
componentAreaMap.put(component, area);
lastDrawnStateMap.put(component, state);
}
}
// Draw control points of the component in the slot.
if (controlPointSlot != null) {
for (Point point : controlPointSlot) {
if (point != null) {
g2dWrapper.setColor(Constants.SELECTED_CONTROL_POINT_COLOR);
g2dWrapper.fillOval(point.x - CONTROL_POINT_SIZE / 2, point.y
- CONTROL_POINT_SIZE / 2, CONTROL_POINT_SIZE, CONTROL_POINT_SIZE);
}
}
}
g2dWrapper.setColor(Constants.CONTROL_POINT_COLOR);
// Draw control points.
for (IDIYComponent<?> component : project.getComponents()) {
if (drawOptions.contains(DrawOption.CONTROL_POINTS)) {
if (drawControlPoints.contains(component)) {
boolean isSelected = selectedComponents.contains(component);
g2dWrapper.setColor(isSelected ? Constants.SELECTED_CONTROL_POINT_COLOR
: Constants.CONTROL_POINT_COLOR);
for (int i = 0; i < component.getControlPointCount(); i++) {
Point controlPoint = component.getControlPoint(i);
try {
if (drawControlPoints.contains(component)) {
g2dWrapper.setColor(Constants.CONTROL_POINT_COLOR);
g2dWrapper.setStroke(new BasicStroke(2));
// g2d.drawOval(controlPoint.x - 2,
// controlPoint.y - 2, 4, 4);
g2dWrapper.fillOval(controlPoint.x - CONTROL_POINT_SIZE / 2,
controlPoint.y - CONTROL_POINT_SIZE / 2,
CONTROL_POINT_SIZE, CONTROL_POINT_SIZE);
}
} catch (Exception e) {
LOG.error("Could not obtain control points for component of type "
+ component.getClass().getName());
int pointSize = CONTROL_POINT_SIZE;
if (!isSelected) {
pointSize -= 2;
}
g2dWrapper.fillOval(controlPoint.x - pointSize / 2, controlPoint.y
- pointSize / 2, pointSize, pointSize);
}
}
}
@@ -7,14 +7,16 @@ import java.awt.Stroke;
public class Constants {
public static final int PIXELS_PER_INCH = 100;//Toolkit.getDefaultToolkit().getScreenResolution();
public static final int PIXELS_PER_INCH = 120;//Toolkit.getDefaultToolkit().getScreenResolution();
public static int GRIDS_PER_INCH = 8;
// public static Size GRID_SIZE = new Size(1d / GRIDS_PER_INCH, SizeUnit.in);
// public static final int GRID = 20;
public static Color CANVAS_COLOR = Color.white;
public static Color GRID_COLOR = new Color(240, 240, 240);
public static Color CONTROL_POINT_COLOR = Color.blue;
public static Color CONTROL_POINT_COLOR = Color.black;
public static Color SELECTED_CONTROL_POINT_COLOR = Color.blue;
public static Color CREATION_POINT_COLOR = Color.decode("#32CD32");
public static Font LABEL_FONT = new Font("Tahoma", Font.BOLD, 11);