auto-create component framework improvement

This commit is contained in:
Branislav Stojkovic
2016-10-30 00:31:09 +02:00
parent 06e2342d36
commit 5de31dd7bb
41 changed files with 382 additions and 171 deletions
@@ -4,8 +4,8 @@ import javax.swing.Icon;
import org.diylc.core.CreationMethod;
import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.AutoCreateTag;
import org.diylc.core.annotations.BomPolicy;
import org.diylc.core.annotations.IAutoCreator;
/**
* Entity class used to describe a component type.
@@ -30,12 +30,12 @@ public class ComponentType {
private BomPolicy bomPolicy;
private boolean autoEdit;
private boolean rotatable;
private AutoCreateTag autoCreateTag;
private IAutoCreator autoCreator;
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,
AutoCreateTag autoCreateTag) {
IAutoCreator autoCreator) {
super();
this.name = name;
this.description = description;
@@ -51,7 +51,7 @@ public class ComponentType {
this.bomPolicy = bomPolicy;
this.autoEdit = autoEdit;
this.rotatable = rotatable;
this.autoCreateTag = autoCreateTag;
this.autoCreator = autoCreator;
}
public String getName() {
@@ -110,8 +110,8 @@ public class ComponentType {
return rotatable;
}
public AutoCreateTag getAutoCreateTag() {
return autoCreateTag;
public IAutoCreator getAutoCreateor() {
return autoCreator;
}
@Override
@@ -36,8 +36,7 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyP
public static final String EXPORT_GRID_KEY = "exportGrid";
public static final String STICKY_POINTS_KEY = "stickyPoints";
public static final String METRIC_KEY = "metric";
public static final String SNAP_TO_GRID_KEY = "snapToGrid";
public static final String AUTO_PADS_KEY = "autoCreatePads";
public static final String SNAP_TO_GRID_KEY = "snapToGrid";
public static final String CONTINUOUS_CREATION_KEY = "continuousCreation";
public static final String AUTO_EDIT_KEY = "autoEdit";
public static final String ABNORMAL_EXIT_KEY = "abnormalExit";
@@ -1,5 +0,0 @@
package org.diylc.core.annotations;
public enum AutoCreateTag {
NONE, SOLDER_PAD
}
@@ -0,0 +1,9 @@
package org.diylc.core.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoCreator {
public Class<? extends IAutoCreator> creatorClass();
}
@@ -80,8 +80,6 @@ public @interface ComponentDescriptor {
*/
boolean rotatable() default true;
AutoCreateTag autoCreateTag() default AutoCreateTag.NONE;
KeywordPolicy keywordPolicy() default KeywordPolicy.NEVER_SHOW;
}
@@ -0,0 +1,10 @@
package org.diylc.core.annotations;
import java.util.List;
import org.diylc.core.IDIYComponent;
public interface IAutoCreator {
public List<IDIYComponent<?>> createIfNeeded(IDIYComponent<?> lastAdded);
}
@@ -20,10 +20,11 @@ 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.AutoCreateTag;
import org.diylc.core.annotations.AutoCreator;
import org.diylc.core.annotations.BomPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.annotations.IAutoCreator;
/**
* Utility class with component processing methods.
@@ -71,7 +72,7 @@ public class ComponentProcessor {
BomPolicy bomPolicy;
boolean autoEdit;
boolean rotatable;
AutoCreateTag autoCreateTag = null;
IAutoCreator autoCreator = null;
if (clazz.isAnnotationPresent(ComponentDescriptor.class)) {
ComponentDescriptor annotation = clazz.getAnnotation(ComponentDescriptor.class);
name = annotation.name();
@@ -86,7 +87,6 @@ public class ComponentProcessor {
bomPolicy = annotation.bomPolicy();
autoEdit = annotation.autoEdit();
rotatable = annotation.rotatable();
autoCreateTag = annotation.autoCreateTag();
} else {
name = clazz.getSimpleName();
description = "";
@@ -100,7 +100,14 @@ public class ComponentProcessor {
bomPolicy = BomPolicy.SHOW_ALL_NAMES;
autoEdit = true;
rotatable = true;
autoCreateTag = AutoCreateTag.NONE;
}
if (clazz.isAnnotationPresent(AutoCreator.class)) {
AutoCreator annotation = clazz.getAnnotation(AutoCreator.class);
try {
autoCreator = annotation.creatorClass().newInstance();
} catch (Exception e) {
LOG.error("Could not instantiate auto-creator for " + name, e);
}
}
icon = null;
// Draw component icon.
@@ -120,7 +127,7 @@ public class ComponentProcessor {
}
ComponentType componentType =
new ComponentType(name, description, creationMethod, category, namePrefix, author, icon, clazz, zOrder,
flexibleZOrder, stretchable, bomPolicy, autoEdit, rotatable, autoCreateTag);
flexibleZOrder, stretchable, bomPolicy, autoEdit, rotatable, autoCreator);
componentTypeMap.put(clazz.getName(), componentType);
return componentType;
}
@@ -50,7 +50,7 @@ import org.diylc.core.IView;
import org.diylc.core.Project;
import org.diylc.core.Template;
import org.diylc.core.Theme;
import org.diylc.core.annotations.AutoCreateTag;
import org.diylc.core.annotations.IAutoCreator;
import org.diylc.core.measures.SizeUnit;
import org.diylc.utils.Constants;
@@ -91,7 +91,7 @@ public class Presenter implements IPlugInPort {
/**
* {@link ComponentType}s that can be autocreated, like Solder Pads
*/
private Map<AutoCreateTag, ComponentType> autoCreateTypes;
private List<IAutoCreator> autoCreators;
// Maps component class names to ComponentType objects.
private List<IPlugIn> plugIns;
@@ -352,19 +352,20 @@ public class Presenter implements IPlugInPort {
return componentTypes;
}
private Map<AutoCreateTag, ComponentType> getAutoCreateTypes() {
if (autoCreateTypes == null) {
autoCreateTypes = new HashMap<AutoCreateTag, ComponentType>();
public List<IAutoCreator> getAutoCreators() {
if (autoCreators == null) {
autoCreators = new ArrayList<IAutoCreator>();
for (Map.Entry<String, List<ComponentType>> entry : getComponentTypes().entrySet()) {
for (ComponentType t : entry.getValue()) {
if (t.getAutoCreateTag() != null)
autoCreateTypes.put(t.getAutoCreateTag(), t);
if (t.getAutoCreateor() != null)
autoCreators.add(t.getAutoCreateor());
}
}
}
return autoCreateTypes;
return autoCreators;
}
@Override
public void draw(Graphics2D g2d, Set<DrawOption> drawOptions, IComponentFiler filter) {
if (currentProject == null) {
@@ -1623,7 +1624,7 @@ public class Presenter implements IPlugInPort {
* @param component
*/
@SuppressWarnings("unchecked")
private void addComponent(IDIYComponent<?> component, boolean canCreatePads) {
private void addComponent(IDIYComponent<?> component, boolean alowAutoCreate) {
int index = currentProject.getComponents().size();
while (index > 0
&& ComponentProcessor.getInstance()
@@ -1640,21 +1641,12 @@ public class Presenter implements IPlugInPort {
currentProject.getComponents().add(component);
}
ComponentType padType = getAutoCreateTypes().get(AutoCreateTag.SOLDER_PAD);
if (canCreatePads && ConfigurationManager.getInstance().readBoolean(IPlugInPort.AUTO_PADS_KEY, false)
&& padType != null && !(component.getClass().equals(padType.getInstanceClass()))) {
for (int i = 0; i < component.getControlPointCount(); i++) {
if (component.isControlPointSticky(i)) {
try {
IDIYComponent<?> pad =
instantiationManager.instantiateComponent(padType, null, component.getControlPoint(i), currentProject)
.get(0);
pad.setControlPoint(component.getControlPoint(i), 0);
addComponent(pad, false);
} catch (Exception e) {
LOG.warn("Could not auto-create solder pad", e);
}
}
// Check if we should auto-create something.
for (IAutoCreator creator : this.getAutoCreators()) {
List<IDIYComponent<?>> newComponents = creator.createIfNeeded(component);
if (newComponents != null) {
for(IDIYComponent<?> c : newComponents)
addComponent(c, false);
}
}
}