reorganized packages

added toggle switch
This commit is contained in:
bancika
2011-01-08 14:02:21 +00:00
parent 8a115e5cbd
commit 746b7d4e97
12 changed files with 215 additions and 190 deletions
@@ -1,182 +0,0 @@
package org.diylc.components;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size;
import org.diylc.utils.Constants;
/**
* Base class for all leaded components such as resistors or capacitors. Has two
* control points and draws leads between them. Also, it positions and draws the
* shape of the component as specified by a child class.
*
* @author Branislav Stojkovic
*/
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;
protected Size width;
protected Size height;
protected Point[] points = new Point[] { new Point((int) (-Constants.GRID * 5), 0),
new Point((int) (Constants.GRID * 5), 0) };
protected String name = "New Component";
protected AbstractLeadedDIYComponent() {
super();
try {
this.width = getDefaultWidth().clone();
this.height = getDefaultHeight().clone();
} catch (CloneNotSupportedException e) {
// This should never happen because Size supports cloning.
}
}
/**
* @return default component width.
*/
protected abstract Size getDefaultWidth();
/**
* Returns default component height.
*
* @return
*/
protected abstract Size getDefaultHeight();
/**
* @return shape that represents component body. Shape should not be
* transformed and should be referenced to (0, 0).
*/
protected abstract Shape getComponentShape();
/**
* @return component body color.
*/
protected abstract Color getBodyColor();
/**
* @return component border color.
*/
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
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
g2d.setColor(LEAD_COLOR);
g2d.setStroke(new BasicStroke(1));
double distance = points[0].distance(points[1]);
Shape shape = getComponentShape();
Rectangle shapeRect = shape.getBounds();
double leadLenght = (distance - shapeRect.width) / 2;
Double theta;
theta = Math.atan2(points[1].y - points[0].y, points[1].x - points[0].x);
// Draw leads.
g2d.drawLine(points[0].x, points[0].y, (int) (points[0].x + leadLenght * Math.cos(theta)),
(int) (points[0].y + leadLenght * Math.sin(theta)));
g2d.drawLine(points[1].x, points[1].y, (int) (points[1].x - leadLenght * Math.cos(theta)),
(int) (points[1].y - leadLenght * Math.sin(theta)));
// Transform graphics to draw the body in the right place and at the
// right angle.
g2d.translate((points[0].x + points[1].x - shapeRect.width) / 2,
(points[0].y + points[1].y - shapeRect.height) / 2);
g2d.rotate(theta, shapeRect.width / 2, shapeRect.height / 2);
// Draw body.
if (componentState != ComponentState.DRAGGING) {
g2d.setColor(getBodyColor());
g2d.fill(shape);
}
g2d.setColor(getBorderColor());
g2d.draw(shape);
// Draw label.
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(getName(), g2d);
g2d.drawString(getName(), (int) (shapeRect.width - textRect.getWidth()) / 2,
(int) (shapeRect.height - textRect.getHeight()) / 2 + fontMetrics.getAscent());
}
@EditableProperty(defaultable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@EditableProperty(defaultable = true)
public Size getWidth() {
return width;
}
public void setWidth(Size width) {
this.width = width;
}
@EditableProperty(defaultable = true)
public Size getHeight() {
return height;
}
public void setHeight(Size height) {
this.height = height;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AbstractLeadedDIYComponent other = (AbstractLeadedDIYComponent) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
@@ -1,4 +1,4 @@
package org.diylc.components; package org.diylc.components.boards;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
@@ -1,4 +1,4 @@
package org.diylc.components; package org.diylc.components.boards;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
@@ -1,4 +1,4 @@
package org.diylc.components; package org.diylc.components.boards;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
@@ -0,0 +1,192 @@
package org.diylc.components.electromechanical;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.RoundRectangle2D;
import org.diylc.core.ComponentLayer;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Mini Toggle Switch", category = "Electromechanical", author = "Branislav Stojkovic", desciption = "", stretchable = false, componentLayer = ComponentLayer.COMPONENT, instanceNamePrefix = "SW")
public class MiniToggleSwitch implements IDIYComponent<ToggleSwitchType> {
private static final long serialVersionUID = 1L;
private static Size SPACING = new Size(0.1d, SizeUnit.in);
private static Size MARGIN = new Size(0.08d, SizeUnit.in);
private static Size CIRCLE_SIZE = new Size(0.09d, SizeUnit.in);
private static Size LUG_WIDTH = new Size(0.065d, SizeUnit.in);
private static Size LUG_HEIGHT = new Size(0.03d, SizeUnit.in);
private static Color BODY_COLOR = Color.decode("#3299CC");
private static Color BORDER_COLOR = BODY_COLOR.darker();
private static Color CIRCLE_COLOR = Color.red;
private static Color LUG_COLOR = Color.blue;
protected Point[] controlPoints = new Point[] { new Point(0, 0) };
protected RoundRectangle2D body;
protected String name;
protected ToggleSwitchType switchType = ToggleSwitchType.SPST;
public MiniToggleSwitch() {
super();
updateControlPoints();
}
private void updateControlPoints() {
Point firstPoint = controlPoints[0];
int margin = MARGIN.convertToPixels();
int spacing = SPACING.convertToPixels();
switch (switchType) {
case SPST:
controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin,
2 * margin, 2 * margin + spacing, margin, margin);
break;
case SPDT:
controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin,
2 * margin, 2 * margin + 2 * spacing, margin, margin);
break;
case DPDT:
controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + spacing, firstPoint.y),
new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + spacing, 2 * margin + 2 * spacing, margin, margin);
break;
case _3PDT:
controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + spacing, firstPoint.y),
new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + 2 * spacing, firstPoint.y),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + 2 * spacing, 2 * margin + 2 * spacing, margin, margin);
break;
case _4PDT:
controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + spacing, firstPoint.y),
new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + 2 * spacing, firstPoint.y),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + 3 * spacing, firstPoint.y),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + 3 * spacing, 2 * margin + 2 * spacing, margin, margin);
break;
case _5PDT:
controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + spacing, firstPoint.y),
new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + 2 * spacing, firstPoint.y),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + 3 * spacing, firstPoint.y),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + 4 * spacing, firstPoint.y),
new Point(firstPoint.x + 4 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 4 * spacing, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + 4 * spacing, 2 * margin + 2 * spacing, margin, margin);
break;
}
}
@Override
public Point getControlPoint(int index) {
return controlPoints[index];
}
@Override
public int getControlPointCount() {
return controlPoints.length;
}
@Override
public void setControlPoint(Point point, int index) {
controlPoints[index].setLocation(point);
updateControlPoints();
}
@EditableProperty
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@EditableProperty(name = "Type")
@Override
public ToggleSwitchType getValue() {
return switchType;
}
@Override
public void setValue(ToggleSwitchType value) {
this.switchType = value;
updateControlPoints();
}
@Override
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
if (body != null) {
if (componentState != ComponentState.DRAGGING) {
g2d.setColor(BODY_COLOR);
g2d.fill(body);
}
g2d.setStroke(new BasicStroke());
g2d.setColor(BORDER_COLOR);
g2d.draw(body);
}
if (componentState != ComponentState.DRAGGING) {
int circleRadius = CIRCLE_SIZE.convertToPixels() / 2;
int lugWidth = LUG_WIDTH.convertToPixels();
int lugHeight = LUG_HEIGHT.convertToPixels();
for (Point p : controlPoints) {
g2d.setColor(CIRCLE_COLOR);
g2d.fillOval(p.x - circleRadius, p.y - circleRadius, 2 * circleRadius,
2 * circleRadius);
g2d.setColor(LUG_COLOR);
g2d.fillRect(p.x - lugWidth / 2, p.y - lugHeight / 2, lugWidth, lugHeight);
}
}
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,11 @@
package org.diylc.components.electromechanical;
public enum ToggleSwitchType {
SPST, SPDT, DPDT, _3PDT, _4PDT, _5PDT;
@Override
public String toString() {
return name().replace("_", "");
}
}
@@ -1,10 +1,11 @@
package org.diylc.components; package org.diylc.components.passive;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Shape; import java.awt.Shape;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import org.diylc.components.AbstractLeadedDIYComponent;
import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Capacitance; import org.diylc.core.measures.Capacitance;
@@ -1,10 +1,11 @@
package org.diylc.components; package org.diylc.components.passive;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Shape; import java.awt.Shape;
import java.awt.geom.Ellipse2D; import java.awt.geom.Ellipse2D;
import org.diylc.components.AbstractLeadedDIYComponent;
import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Capacitance; import org.diylc.core.measures.Capacitance;
@@ -1,10 +1,11 @@
package org.diylc.components; package org.diylc.components.passive;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Shape; import java.awt.Shape;
import java.awt.geom.RoundRectangle2D; import java.awt.geom.RoundRectangle2D;
import org.diylc.components.AbstractLeadedDIYComponent;
import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Capacitance; import org.diylc.core.measures.Capacitance;
@@ -1,10 +1,11 @@
package org.diylc.components; package org.diylc.components.passive;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Shape; import java.awt.Shape;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import org.diylc.components.AbstractLeadedDIYComponent;
import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Resistance; import org.diylc.core.measures.Resistance;
@@ -1,4 +1,4 @@
package org.diylc.components; package org.diylc.components.semiconductors;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;