simplified component framework

This commit is contained in:
bancika
2011-01-05 00:09:11 +00:00
parent d1892cae70
commit 69acfcff2f
17 changed files with 145 additions and 149 deletions
@@ -2,7 +2,6 @@ package org.diylc.components;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
@@ -12,7 +11,6 @@ import java.awt.Shape;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentName;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size;
@@ -25,20 +23,19 @@ import org.diylc.utils.Constants;
*
* @author Branislav Stojkovic
*/
public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T> {
private static final long serialVersionUID = 1L;
public static Color LEAD_COLOR = Color.black;
public static Color LABEL_COLOR = Color.black;
public static Color LABEL_COLOR_SELECTED = Color.red;
public static Font LABEL_FONT = new Font("Tahoma", Font.BOLD, 11);
protected Size width;
protected Size height;
protected Point point1 = new Point(0, 0);
protected Point point2 = new Point((int) (Constants.GRID * 10), 0);
protected String componentName = "New Component";
protected String name = "New Component";
protected AbstractLeadedDIYComponent() {
super();
@@ -106,24 +103,23 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
g2d.setColor(getBorderColor());
g2d.draw(shape);
// Draw label.
g2d.setFont(LABEL_FONT);
g2d.setFont(Constants.LABEL_FONT);
g2d
.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED
: LABEL_COLOR);
FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
java.awt.geom.Rectangle2D textRect = fontMetrics.getStringBounds(getComponentName(), g2d);
g2d.drawString(getComponentName(), (int) (shapeRect.width - textRect.getWidth()) / 2,
java.awt.geom.Rectangle2D textRect = fontMetrics.getStringBounds(getName(), g2d);
g2d.drawString(getName(), (int) (shapeRect.width - textRect.getWidth()) / 2,
(int) (shapeRect.height - textRect.getHeight()) / 2 + fontMetrics.getAscent());
}
@ComponentName
@EditableProperty
public String getComponentName() {
return componentName;
public String getName() {
return name;
}
public void setComponentName(String componentName) {
this.componentName = componentName;
public void setName(String name) {
this.name = name;
}
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED)
@@ -166,7 +162,7 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((componentName == null) ? 0 : componentName.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@@ -179,10 +175,10 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
if (getClass() != obj.getClass())
return false;
AbstractLeadedDIYComponent other = (AbstractLeadedDIYComponent) obj;
if (componentName == null) {
if (other.componentName != null)
if (name == null) {
if (other.name != null)
return false;
} else if (!componentName.equals(other.componentName))
} else if (!name.equals(other.name))
return false;
return true;
}
@@ -6,7 +6,6 @@ import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ComponentValue;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Capacitance;
import org.diylc.core.measures.CapacitanceUnit;
@@ -14,7 +13,7 @@ import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Axial Film Capacitor", author = "bancika", category = "Passive", instanceNamePrefix = "C", desciption = "test")
public class AxialFilmCapacitor extends AbstractLeadedDIYComponent {
public class AxialFilmCapacitor extends AbstractLeadedDIYComponent<Capacitance> {
private static final long serialVersionUID = 1L;
@@ -29,7 +28,6 @@ public class AxialFilmCapacitor extends AbstractLeadedDIYComponent {
super();
}
@ComponentValue
@EditableProperty
public Capacitance getValue() {
return value;
@@ -0,0 +1,105 @@
package org.diylc.components;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.utils.Constants;
@ComponentDescriptor(name = "DIL IC", author = "bancika", category = "Semiconductors", instanceNamePrefix = "IC", desciption = "test")
public class DIL_IC implements IDIYComponent<String> {
private static final long serialVersionUID = 1L;
public static Color BODY_COLOR = Color.gray;
public static Color BORDER_COLOR = Color.black;
public static Color LABEL_COLOR = Color.white;
public static int EDGE_RADIUS = 6;
private Point point1 = new Point(0, 0);
private Point point2 = new Point((int) (Constants.GRID) * 4, (int) (Constants.GRID) * 5);
private String name = "New Component";
private String value = "";
@EditableProperty
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@EditableProperty
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@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;
}
@Override
public void draw(Graphics2D g2d, ComponentState componentState) {
int width = ((int) (Math.abs(point1.x - point2.x) / Constants.GRID));
if (width % 2 == 1) {
width--;
}
width = (int) (width * Constants.GRID);
int height = ((int) (Math.abs(point1.y - point2.y) / Constants.GRID));
if (height % 2 == 1) {
height--;
}
height = (int) (height * Constants.GRID);
int x = (point1.x + point2.x - width) / 2;
int y = (point1.y + point2.y - height) / 2;
if (componentState != ComponentState.DRAGGING) {
g2d.setColor(BODY_COLOR);
g2d.fillRoundRect(x, y, width, height, EDGE_RADIUS, EDGE_RADIUS);
}
g2d.setColor(BORDER_COLOR);
g2d.drawRoundRect(x, y, width, height, EDGE_RADIUS, EDGE_RADIUS);
// Draw label.
g2d.setFont(Constants.LABEL_FONT);
g2d.setColor(LABEL_COLOR);
FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
Rectangle2D rect = fontMetrics.getStringBounds(getName(), g2d);
int textHeight = (int) (rect.getHeight());
int textWidth = (int) (rect.getWidth());
// Center text horizontally and vertically
x = (width - textWidth) / 2;
y = (height - textHeight) / 2 + fontMetrics.getAscent();
g2d.drawString(getName(), x, y);
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.drawString("IC", 10, 10);
}
}
@@ -7,8 +7,6 @@ import java.awt.Point;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentName;
import org.diylc.core.annotations.ComponentValue;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty;
@@ -47,7 +45,6 @@ public class MockDIYComponent implements IDIYComponent {
super();
}
@ComponentName
@EditableProperty
public String getName() {
return name;
@@ -75,9 +72,8 @@ public class MockDIYComponent implements IDIYComponent {
this.testField = testField;
}
@ComponentValue
@EditableProperty(defaultable = true)
public Capacitance getC() {
public Capacitance getValue() {
return c;
}
@@ -6,7 +6,6 @@ import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ComponentValue;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Capacitance;
import org.diylc.core.measures.CapacitanceUnit;
@@ -14,7 +13,7 @@ import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Ceramic Capacitor", author = "bancika", category = "Passive", instanceNamePrefix = "C", desciption = "test")
public class RadialCeramicDiskCapacitor extends AbstractLeadedDIYComponent {
public class RadialCeramicDiskCapacitor extends AbstractLeadedDIYComponent<Capacitance> {
private static final long serialVersionUID = 1L;
@@ -29,7 +28,6 @@ public class RadialCeramicDiskCapacitor extends AbstractLeadedDIYComponent {
super();
}
@ComponentValue
@EditableProperty
public Capacitance getValue() {
return value;
@@ -6,7 +6,6 @@ import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ComponentValue;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Capacitance;
import org.diylc.core.measures.CapacitanceUnit;
@@ -14,7 +13,7 @@ import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Radial Film Capacitor", author = "bancika", category = "Passive", instanceNamePrefix = "C", desciption = "test")
public class RadialFilmCapacitor extends AbstractLeadedDIYComponent {
public class RadialFilmCapacitor extends AbstractLeadedDIYComponent<Capacitance> {
private static final long serialVersionUID = 1L;
@@ -29,7 +28,6 @@ public class RadialFilmCapacitor extends AbstractLeadedDIYComponent {
super();
}
@ComponentValue
@EditableProperty
public Capacitance getValue() {
return value;
@@ -6,7 +6,6 @@ import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ComponentValue;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Resistance;
import org.diylc.core.measures.ResistanceUnit;
@@ -14,7 +13,7 @@ import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Resistor", author = "bancika", category = "Passive", instanceNamePrefix = "R", desciption = "test")
public class Resistor extends AbstractLeadedDIYComponent {
public class Resistor extends AbstractLeadedDIYComponent<Resistance> {
private static final long serialVersionUID = 1L;
@@ -29,7 +28,6 @@ public class Resistor extends AbstractLeadedDIYComponent {
super();
}
@ComponentValue
@EditableProperty
public Resistance getValue() {
return value;
@@ -4,8 +4,6 @@ import java.awt.Graphics2D;
import java.io.Serializable;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ComponentName;
import org.diylc.core.annotations.ComponentValue;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty;
@@ -18,8 +16,6 @@ import org.diylc.core.annotations.EditableProperty;
* <ul>
* <li>Must have an empty constructor.</li>
* <li>Class should be annotated with {@link ComponentDescriptor}.</li>
* <li>Exactly one getter should be annotated with {@link ComponentName}.</li>
* <li>Exactly one getter should be annotated with {@link ComponentValue}.</li>
* <li>Control point getters need to be annotated with {@link ControlPoint}.</li>
* <li>Getters for properties editable by users should be annotated with
* {@link EditableProperty}.</li>
@@ -28,8 +24,18 @@ import org.diylc.core.annotations.EditableProperty;
* </ul>
*
* @author Branislav Stojkovic
*
* @param <T>
* type of component values, e.g. Resistance for resistors or String
* for transistors.
*/
public interface IDIYComponent extends Serializable {
public interface IDIYComponent<T> extends Serializable {
String getName();
void setName(String name);
T getValue();
/**
* Draws the component onto the {@link Graphics2D}.
@@ -1,16 +0,0 @@
package org.diylc.core.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Annotates getter for the component name that will be shown in the BOM. Only
* one getter per class may be tagged with this annotation and, must return
* string type and there has to be a matching setter.
*
* @author Branislav Stojkovic
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ComponentName {
}
@@ -1,15 +0,0 @@
package org.diylc.core.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Annotates getter for the component value that will be shown in the BOM. Only
* one getter per class may be tagged with this annotation.
*
* @author Branislav Stojkovic
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ComponentValue {
}
@@ -8,8 +8,6 @@ import java.util.List;
import java.util.Map;
import org.diylc.core.IDIYComponent;
import org.diylc.presenter.ComponentProcessor;
public class BomMaker {
@@ -28,8 +26,8 @@ public class BomMaker {
public List<BomEntry> createBom(List<IDIYComponent> components) {
Map<String, BomEntry> entryMap = new HashMap<String, BomEntry>();
for (IDIYComponent component : components) {
String name = ComponentProcessor.getInstance().extractComponentName(component);
String value = ComponentProcessor.getInstance().extractComponentValue(component);
String name = component.getName();
String value = component.getValue() == null ? "" : component.getValue().toString();
if ((name != null) && (value != null)) {
String key = name + "|" + value;
if (entryMap.containsKey(key)) {
@@ -25,7 +25,6 @@ import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.core.IDIYComponent;
import org.diylc.presenter.ComparatorFactory;
import org.diylc.presenter.ComponentProcessor;
import com.diyfever.gui.MemoryBar;
import com.diyfever.gui.miscutils.PercentageListCellRenderer;
@@ -186,8 +185,7 @@ public class StatusBar extends JPanel implements IPlugIn {
if (n > 1) {
formattedNames += ", ";
}
formattedNames += ComponentProcessor.getInstance().extractComponentName(
component);//
formattedNames += component.getName();//
// +
// " (<b><font color=\"blue\">"
// +
@@ -23,8 +23,8 @@ public class ComparatorFactory {
@Override
public int compare(IDIYComponent o1, IDIYComponent o2) {
String name1 = ComponentProcessor.getInstance().extractComponentName(o1);
String name2 = ComponentProcessor.getInstance().extractComponentName(o2);
String name1 = o1.getName();
String name2 = o2.getName();
if (name1 == null || name2 == null) {
return 0;
}
@@ -11,8 +11,6 @@ import org.apache.log4j.Logger;
import org.diylc.common.ControlPointWrapper;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.ComponentName;
import org.diylc.core.annotations.ComponentValue;
import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty;
@@ -174,68 +172,4 @@ public class ComponentProcessor {
}
return properties;
}
/**
* Finds the method annotated with {@link ComponentName} annotations and
* calls it to retrieve component name.
*
* @param component
* @return
*/
public String extractComponentName(IDIYComponent component) {
String name = null;
for (Method method : component.getClass().getMethods()) {
if (method.isAnnotationPresent(ComponentName.class)) {
try {
name = method.invoke(component).toString();
} catch (Exception e) {
name = null;
}
}
}
return name;
}
/**
* Finds the method annotated with {@link ComponentName} and it's matching
* setter and calls the setter to update the component name.
*
* @param component
* @param newName
*/
public void writeComponentName(IDIYComponent component, String newName) {
for (Method method : component.getClass().getMethods()) {
if (method.isAnnotationPresent(ComponentName.class)) {
try {
Method setter = component.getClass().getMethod(
method.getName().replaceFirst("get", "set"), String.class);
setter.invoke(component, newName);
} catch (Exception e) {
LOG.warn("Could not update component name on a component of type "
+ component.getClass().getName());
}
}
}
}
/**
* Finds the method annotated with {@link ComponentValue} annotations and
* calls it to retrieve component value.
*
* @param component
* @return
*/
public String extractComponentValue(IDIYComponent component) {
String value = null;
for (Method method : component.getClass().getMethods()) {
if (method.isAnnotationPresent(ComponentValue.class)) {
try {
value = method.invoke(component).toString();
} catch (Exception e) {
value = null;
}
}
}
return value;
}
}
@@ -716,14 +716,13 @@ public class Presenter implements IPlugInPort {
String name = namePrefix + i;
exists = false;
for (IDIYComponent c : currentProject.getComponents()) {
String cName = ComponentProcessor.getInstance().extractComponentName(c);
if (cName.equals(name)) {
if (c.getName().equals(name)) {
exists = true;
break;
}
}
}
ComponentProcessor.getInstance().writeComponentName(component, namePrefix + i);
component.setName(namePrefix + i);
// Add it to the project.
currentProject.getComponents().add(component);
@@ -2,6 +2,7 @@ package org.diylc.utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Stroke;
import java.awt.Toolkit;
@@ -12,6 +13,8 @@ public class Constants {
public static final Color CANVAS_COLOR = Color.white;
public static final Color GRID_COLOR = new Color(240, 240, 240);
public static Font LABEL_FONT = new Font("Tahoma", Font.BOLD, 11);
public static final Stroke dashedStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 2.0f, new float[] { 2.0f }, 0.0f);
}