abstract leaded component changes

This commit is contained in:
bancika
2011-01-04 22:19:24 +00:00
parent 2bd2ecd13d
commit cb5c95478f
8 changed files with 124 additions and 58 deletions
@@ -1,7 +1,15 @@
package org.diylc.components; 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; 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.IDIYComponent;
import org.diylc.core.VisibilityPolicy; import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentName; import org.diylc.core.annotations.ComponentName;
@@ -10,10 +18,10 @@ 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;
/** /**
* Base class for all leaded components such as resistors or capacitors. Has two * Base class for all leaded components such as resistors or capacitors. Has two
* control points for leads and one control point for the label. * 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 * @author Branislav Stojkovic
*/ */
@@ -21,41 +29,96 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static Color LEAD_COLOR = Color.black;
public static Color LABEL_COLOR = Color.black;
public static Font LABEL_FONT = new Font("Tahoma", Font.BOLD, 11);
protected Size width; protected Size width;
protected Size height; protected Size height;
protected Point point1 = new Point(0, 0); protected Point point1 = new Point(0, 0);
protected Point point2 = new Point((int) (Constants.GRID * 10), 0); protected Point point2 = new Point((int) (Constants.GRID * 10), 0);
protected Point labelPoint = new Point((int) (Constants.GRID * 5), -(int) Constants.GRID); protected String componentName = "New Component";
protected String name = "New Component";
protected AbstractLeadedDIYComponent() { protected AbstractLeadedDIYComponent() {
super(); super();
this.width = getDefaultWidth(); try {
this.height = getDefaultHeight(); this.width = getDefaultWidth().clone();
this.height = getDefaultHeight().clone();
} catch (CloneNotSupportedException e) {
// This should never happen because Size supports cloning.
}
} }
/** /**
* Returns the default component width. * @return default component width.
*
* @return
*/ */
protected abstract Size getDefaultWidth(); protected abstract Size getDefaultWidth();
/** /**
* Returns the default component height. * Returns default component height.
* *
* @return * @return
*/ */
protected abstract Size getDefaultHeight(); protected abstract Size getDefaultHeight();
@ComponentName /**
@EditableProperty * @return shape that represents component body. Shape should not be
public String getName() { * transformed and should be referenced to (0, 0).
return name; */
protected abstract Shape getComponentShape();
/**
* @return component body color.
*/
protected abstract Color getBodyColor();
/**
* @return component border color.
*/
protected abstract Color getBorderColor();
@Override
public void draw(Graphics2D g2d, ComponentState componentState) {
g2d.setColor(LEAD_COLOR);
g2d.setStroke(new BasicStroke(1));
double distance = point1.distance(point2);
Shape shape = getComponentShape();
Rectangle shapeRect = shape.getBounds();
double leadLenght = (distance - shapeRect.width) / 2;
Double theta;
theta = Math.atan2(point2.y - point1.y, point2.x - point1.x);
// Draw leads.
g2d.drawLine(point1.x, point1.y, (int) (point1.x + leadLenght * Math.cos(theta)),
(int) (point1.y + leadLenght * Math.sin(theta)));
g2d.drawLine(point2.x, point2.y, (int) (point2.x - leadLenght * Math.cos(theta)),
(int) (point2.y - leadLenght * Math.sin(theta)));
// Transform graphics to draw the body in the right place and at the
// right angle.
g2d.translate((point1.x + point2.x - shapeRect.width) / 2,
(point1.y + point2.y - shapeRect.height) / 2);
g2d.rotate(theta, shapeRect.width / 2, shapeRect.height / 2);
// Draw body.
g2d.setColor(getBodyColor());
g2d.fill(shape);
g2d.setColor(getBorderColor());
g2d.draw(shape);
// Draw label.
g2d.setFont(LABEL_FONT);
g2d.setColor(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,
(int) (shapeRect.height - textRect.getHeight()) / 2 + fontMetrics.getAscent());
} }
public void setName(String name) { @ComponentName
this.name = name; @EditableProperty
public String getComponentName() {
return componentName;
}
public void setComponentName(String componentName) {
this.componentName = componentName;
} }
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED) @ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED)
@@ -76,15 +139,6 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
this.point2 = point2; this.point2 = point2;
} }
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED)
public Point getLabelPoint() {
return labelPoint;
}
public void setLabelPoint(Point labelPoint) {
this.labelPoint = labelPoint;
}
@EditableProperty(defaultable = true) @EditableProperty(defaultable = true)
public Size getWidth() { public Size getWidth() {
return width; return width;
@@ -107,7 +161,7 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((componentName == null) ? 0 : componentName.hashCode());
return result; return result;
} }
@@ -120,10 +174,10 @@ public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
AbstractLeadedDIYComponent other = (AbstractLeadedDIYComponent) obj; AbstractLeadedDIYComponent other = (AbstractLeadedDIYComponent) obj;
if (name == null) { if (componentName == null) {
if (other.name != null) if (other.componentName != null)
return false; return false;
} else if (!name.equals(other.name)) } else if (!componentName.equals(other.componentName))
return false; return false;
return true; return true;
} }
@@ -1,10 +1,10 @@
package org.diylc.components; package org.diylc.components;
import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import org.diylc.core.ComponentState;
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;
@@ -17,8 +17,12 @@ public class Resistor extends AbstractLeadedDIYComponent {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static Size DEFAULT_WIDTH = new Size(1d / 2, SizeUnit.in);
public static Size DEFAULT_HEIGHT = new Size(1d / 8, SizeUnit.in);
public static Color BODY_COLOR = Color.decode("#82CFFD");
public static Color BORDER_COLOR = Color.decode("#5D92B1");
private Resistance r = new Resistance(123d, ResistanceUnit.K); private Resistance r = new Resistance(123d, ResistanceUnit.K);
private Color color = Color.green;
public Resistor() { public Resistor() {
super(); super();
@@ -29,30 +33,33 @@ public class Resistor extends AbstractLeadedDIYComponent {
return r; return r;
} }
@Override
public void draw(Graphics2D g2d, ComponentState componentState) {
g2d.setColor(componentState.equals(ComponentState.SELECTED) ? color : color.darker());
g2d.setStroke(new BasicStroke(1));
if (componentState == ComponentState.DRAGGING) {
g2d.setStroke(new BasicStroke(1));
}
g2d.drawLine(point1.x, point1.y, point2.x, point2.y);
g2d.rotate(Math.PI / 4, (point1.x + point2.x) / 2, (point1.y + point2.y) / 2);
g2d.fillRect((point1.x + point2.x) / 2 - 10, (point1.y + point2.y) / 2 - 10, 20, 20);
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) { public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.drawString("R", 10, 10); g2d.drawString("R", 10, 10);
} }
@Override @Override
protected Size getDefaultHeight() { protected Size getDefaultHeight() {
return new Size(3d, SizeUnit.mm); return DEFAULT_HEIGHT;
} }
@Override @Override
protected Size getDefaultWidth() { protected Size getDefaultWidth() {
return new Size(10d, SizeUnit.mm); return DEFAULT_WIDTH;
}
@Override
protected Color getBodyColor() {
return BODY_COLOR;
}
@Override
protected Color getBorderColor() {
return BORDER_COLOR;
}
@Override
protected Shape getComponentShape() {
return new Rectangle2D.Double(0f, 0f, getWidth().convertToPixels(), getHeight()
.convertToPixels());
} }
} }
@@ -27,7 +27,6 @@ import org.diylc.plugins.canvas.CanvasPlugin;
import org.diylc.plugins.clipboard.ClipboardManager; import org.diylc.plugins.clipboard.ClipboardManager;
import org.diylc.plugins.file.FileManager; import org.diylc.plugins.file.FileManager;
import org.diylc.plugins.help.HelpManager; import org.diylc.plugins.help.HelpManager;
import org.diylc.plugins.online.OnlineManager;
import org.diylc.plugins.statusbar.StatusBar; import org.diylc.plugins.statusbar.StatusBar;
import org.diylc.plugins.toolbox.ToolBox; import org.diylc.plugins.toolbox.ToolBox;
import org.diylc.presenter.Presenter; import org.diylc.presenter.Presenter;
@@ -65,7 +64,7 @@ public class MainFrame extends JFrame implements IView {
presenter.installPlugin(new ToolBox()); presenter.installPlugin(new ToolBox());
presenter.installPlugin(new FileManager()); presenter.installPlugin(new FileManager());
presenter.installPlugin(new ClipboardManager()); presenter.installPlugin(new ClipboardManager());
presenter.installPlugin(new OnlineManager()); // presenter.installPlugin(new OnlineManager());
presenter.installPlugin(new HelpManager()); presenter.installPlugin(new HelpManager());
presenter.installPlugin(new StatusBar()); presenter.installPlugin(new StatusBar());
@@ -33,6 +33,8 @@ public class StatusBar extends JPanel implements IPlugIn {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static String UPDATE_URL = "http://www.diy-fever.com/update.xml";
private JComboBox zoomBox; private JComboBox zoomBox;
private UpdateLabel updateLabel; private UpdateLabel updateLabel;
private MemoryBar memoryPanel; private MemoryBar memoryPanel;
@@ -70,8 +72,7 @@ public class StatusBar extends JPanel implements IPlugIn {
private UpdateLabel getUpdateLabel() { private UpdateLabel getUpdateLabel() {
if (updateLabel == null) { if (updateLabel == null) {
updateLabel = new UpdateLabel(plugInPort.getCurrentVersionNumber(), updateLabel = new UpdateLabel(plugInPort.getCurrentVersionNumber(), UPDATE_URL);
"http://www.diy-fever.com/update.xml");
// updateLabel.setBorder(BorderFactory.createCompoundBorder( // updateLabel.setBorder(BorderFactory.createCompoundBorder(
// BorderFactory.createEtchedBorder(), BorderFactory // BorderFactory.createEtchedBorder(), BorderFactory
// .createEmptyBorder(2, 4, 2, 4))); // .createEmptyBorder(2, 4, 2, 4)));
@@ -46,6 +46,7 @@ class G2DWrapper extends Graphics2D {
private Color originalColor; private Color originalColor;
private Composite originalComposite; private Composite originalComposite;
private AffineTransform originalTx; private AffineTransform originalTx;
private Font originalFont;
private AffineTransform currentTx; private AffineTransform currentTx;
private Area currentArea; private Area currentArea;
@@ -69,6 +70,7 @@ class G2DWrapper extends Graphics2D {
originalColor = canvasGraphics.getColor(); originalColor = canvasGraphics.getColor();
originalTx = canvasGraphics.getTransform(); originalTx = canvasGraphics.getTransform();
originalComposite = canvasGraphics.getComposite(); originalComposite = canvasGraphics.getComposite();
originalFont = canvasGraphics.getFont();
currentTx = new AffineTransform(); currentTx = new AffineTransform();
} }
@@ -83,6 +85,7 @@ class G2DWrapper extends Graphics2D {
canvasGraphics.setColor(originalColor); canvasGraphics.setColor(originalColor);
canvasGraphics.setTransform(originalTx); canvasGraphics.setTransform(originalTx);
canvasGraphics.setComposite(originalComposite); canvasGraphics.setComposite(originalComposite);
canvasGraphics.setFont(originalFont);
return currentArea; return currentArea;
} }
@@ -63,10 +63,11 @@ public class Presenter implements IPlugInPort {
private static final Logger LOG = Logger.getLogger(Presenter.class); private static final Logger LOG = Logger.getLogger(Presenter.class);
private static final VersionNumber CURRENT_VERSION = new VersionNumber(0, 0, 0); public static final VersionNumber CURRENT_VERSION = new VersionNumber(0, 0, 0);
private static final String DEFAULTS_KEY = "defaults"; public static final String DEFAULTS_KEY = "defaults";
private static final int CONTROL_POINT_SENSITIVITY = 4; public static final int CONTROL_POINT_SENSITIVITY = 4;
private static final int ICON_SIZE = 32; public static final int ICON_SIZE = 32;
public static boolean ENABLE_ANTIALIASING = true;
private double zoomLevel = 1; private double zoomLevel = 1;
private Map<IDIYComponent, Area> componentAreaMap; private Map<IDIYComponent, Area> componentAreaMap;
@@ -255,7 +256,7 @@ public class Presenter implements IPlugInPort {
} }
G2DWrapper g2dWrapper = new G2DWrapper(g2d); G2DWrapper g2dWrapper = new G2DWrapper(g2d);
if (drawOptions.contains(DrawOption.ANTIALIASING)) { if (drawOptions.contains(DrawOption.ANTIALIASING) && ENABLE_ANTIALIASING) {
g2d g2d
.setRenderingHint(RenderingHints.KEY_ANTIALIASING, .setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); RenderingHints.VALUE_ANTIALIAS_ON);
@@ -264,7 +265,7 @@ public class Presenter implements IPlugInPort {
AffineTransform initialTx = g2d.getTransform(); AffineTransform initialTx = g2d.getTransform();
Dimension d = getCanvasDimensions(drawOptions.contains(DrawOption.ZOOM)); Dimension d = getCanvasDimensions(drawOptions.contains(DrawOption.ZOOM));
g2dWrapper.setColor(Color.white); g2dWrapper.setColor(Constants.CANVAS_COLOR);
g2dWrapper.fillRect(0, 0, d.width, d.height); g2dWrapper.fillRect(0, 0, d.width, d.height);
if (drawOptions.contains(DrawOption.GRID)) { if (drawOptions.contains(DrawOption.GRID)) {
@@ -706,7 +707,7 @@ public class Presenter implements IPlugInPort {
// Instantiate the component. // Instantiate the component.
IDIYComponent component = componentClass.newInstance(); IDIYComponent component = componentClass.newInstance();
// Find the next available name for the component. // Find the next available componentName for the component.
int i = 0; int i = 0;
boolean exists = true; boolean exists = true;
while (exists) { while (exists) {
@@ -9,6 +9,7 @@ public class Constants {
public static final float GRID = Toolkit.getDefaultToolkit().getScreenResolution() / 10f; public static final float GRID = Toolkit.getDefaultToolkit().getScreenResolution() / 10f;
public static final Color CANVAS_COLOR = Color.white;
public static final Color GRID_COLOR = new Color(240, 240, 240); public static final Color GRID_COLOR = new Color(240, 240, 240);
public static final Stroke dashedStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, public static final Stroke dashedStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,