Editable label color for leaded components

This commit is contained in:
bancika
2012-09-21 10:42:33 +00:00
parent e2412c223a
commit abf9faa05a
2 changed files with 139 additions and 95 deletions
@@ -33,7 +33,8 @@ import org.diylc.utils.Constants;
* *
* @author Branislav Stojkovic * @author Branislav Stojkovic
*/ */
public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComponent<T> { public abstract class AbstractLeadedComponent<T> extends
AbstractTransparentComponent<T> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -64,8 +65,9 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
} }
@Override @Override
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, public void draw(Graphics2D g2d, ComponentState componentState,
Project project, IDrawingObserver drawingObserver) { boolean outlineMode, Project project,
IDrawingObserver drawingObserver) {
double distance = points[0].distance(points[1]); double distance = points[0].distance(points[1]);
Shape shape = getBodyShape(); Shape shape = getBodyShape();
// If there's no body, just draw the line connecting the ending points. // If there's no body, just draw the line connecting the ending points.
@@ -76,23 +78,25 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
// When ending points are too close draw the component in standing // When ending points are too close draw the component in standing
// mode. // mode.
int width = getClosestOdd(this.width.convertToPixels()); int width = getClosestOdd(this.width.convertToPixels());
Shape body = new Ellipse2D.Double(points[0].x - width / 2, points[0].y - width / 2, Shape body = new Ellipse2D.Double(points[0].x - width / 2,
width, width); points[0].y - width / 2, width, width);
Composite oldComposite = g2d.getComposite(); Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) { if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha g2d.setComposite(AlphaComposite.getInstance(
/ MAX_ALPHA)); AlphaComposite.SRC_OVER, 1f * alpha / MAX_ALPHA));
} }
g2d.setColor(outlineMode ? Constants.TRANSPARENT_COLOR : getStandingBodyColor()); g2d.setColor(outlineMode ? Constants.TRANSPARENT_COLOR
: getStandingBodyColor());
g2d.fill(body); g2d.fill(body);
g2d.setComposite(oldComposite); g2d.setComposite(oldComposite);
Color finalBorderColor; Color finalBorderColor;
if (outlineMode) { if (outlineMode) {
Theme theme = (Theme) ConfigurationManager.getInstance().readObject( Theme theme = (Theme) ConfigurationManager.getInstance()
IPlugInPort.THEME_KEY, Constants.DEFAULT_THEME); .readObject(IPlugInPort.THEME_KEY,
Constants.DEFAULT_THEME);
finalBorderColor = componentState == ComponentState.SELECTED finalBorderColor = componentState == ComponentState.SELECTED
|| componentState == ComponentState.DRAGGING ? SELECTION_COLOR : theme || componentState == ComponentState.DRAGGING ? SELECTION_COLOR
.getOutlineColor(); : theme.getOutlineColor();
} else { } else {
finalBorderColor = componentState == ComponentState.SELECTED finalBorderColor = componentState == ComponentState.SELECTED
|| componentState == ComponentState.DRAGGING ? SELECTION_COLOR || componentState == ComponentState.DRAGGING ? SELECTION_COLOR
@@ -107,34 +111,45 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
} else { } else {
// Normal mode with component body in the center and two lead parts. // Normal mode with component body in the center and two lead parts.
Rectangle shapeRect = shape.getBounds(); Rectangle shapeRect = shape.getBounds();
Double theta = Math.atan2(points[1].y - points[0].y, points[1].x - points[0].x); Double theta = Math.atan2(points[1].y - points[0].y, points[1].x
- points[0].x);
// Go back to the original transformation to draw leads. // Go back to the original transformation to draw leads.
if (!outlineMode) { if (!outlineMode) {
AffineTransform textTransform = g2d.getTransform(); AffineTransform textTransform = g2d.getTransform();
// if (length.convertToPixels() > points[0].distance(points[1])) { // if (length.convertToPixels() > points[0].distance(points[1]))
// g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); // {
// } // g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
// 0.5f));
// }
int leadThickness = getClosestOdd(getLeadThickness()); int leadThickness = getClosestOdd(getLeadThickness());
double leadLength = (distance - calculatePinSpacing(shapeRect)) / 2 - leadThickness / 2; double leadLength = (distance - calculatePinSpacing(shapeRect))
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness)); / 2 - leadThickness / 2;
Color leadColor = shouldShadeLeads() ? getLeadColor(componentState).darker() g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(
: getLeadColor(componentState); leadThickness));
Color leadColor = shouldShadeLeads() ? getLeadColor(
componentState).darker() : getLeadColor(componentState);
g2d.setColor(leadColor); g2d.setColor(leadColor);
int endX = (int) (points[0].x + Math.cos(theta) * leadLength); int endX = (int) (points[0].x + Math.cos(theta) * leadLength);
int endY = (int) Math.round(points[0].y + Math.sin(theta) * leadLength); int endY = (int) Math.round(points[0].y + Math.sin(theta)
* leadLength);
g2d.drawLine(points[0].x, points[0].y, endX, endY); g2d.drawLine(points[0].x, points[0].y, endX, endY);
endX = (int) (points[1].x + Math.cos(theta - Math.PI) * leadLength); endX = (int) (points[1].x + Math.cos(theta - Math.PI)
endY = (int) Math.round(points[1].y + Math.sin(theta - Math.PI) * leadLength); * leadLength);
endY = (int) Math.round(points[1].y + Math.sin(theta - Math.PI)
* leadLength);
g2d.drawLine(points[1].x, points[1].y, endX, endY); g2d.drawLine(points[1].x, points[1].y, endX, endY);
if (shouldShadeLeads()) { if (shouldShadeLeads()) {
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness - 2)); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(
leadThickness - 2));
leadColor = getLeadColor(componentState); leadColor = getLeadColor(componentState);
g2d.setColor(leadColor); g2d.setColor(leadColor);
g2d.drawLine(points[0].x, points[0].y, (int) (points[0].x + Math.cos(theta) g2d.drawLine(points[0].x, points[0].y,
* leadLength), (int) (points[0].y + Math.sin(theta) * leadLength)); (int) (points[0].x + Math.cos(theta) * leadLength),
g2d.drawLine(points[1].x, points[1].y, (int) (points[1].x + Math.cos(theta (int) (points[0].y + Math.sin(theta) * leadLength));
- Math.PI) g2d.drawLine(points[1].x, points[1].y,
* leadLength), (int) (points[1].y + Math.sin(theta - Math.PI) (int) (points[1].x + Math.cos(theta - Math.PI)
* leadLength), (int) (points[1].y + Math
.sin(theta - Math.PI)
* leadLength)); * leadLength));
} }
g2d.setTransform(textTransform); g2d.setTransform(textTransform);
@@ -142,18 +157,19 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
// Transform graphics to draw the body in the right place and at the // Transform graphics to draw the body in the right place and at the
// right angle. // right angle.
AffineTransform oldTransform = g2d.getTransform(); AffineTransform oldTransform = g2d.getTransform();
g2d.translate((points[0].x + points[1].x - shapeRect.width) / 2, (points[0].y g2d.translate((points[0].x + points[1].x - shapeRect.width) / 2,
+ points[1].y - shapeRect.height) / 2); (points[0].y + points[1].y - shapeRect.height) / 2);
g2d.rotate(theta, shapeRect.width / 2, shapeRect.height / 2); g2d.rotate(theta, shapeRect.width / 2, shapeRect.height / 2);
// Draw body. // Draw body.
Composite oldComposite = g2d.getComposite(); Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) { if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha g2d.setComposite(AlphaComposite.getInstance(
/ MAX_ALPHA)); AlphaComposite.SRC_OVER, 1f * alpha / MAX_ALPHA));
} }
if (bodyColor != null) { if (bodyColor != null) {
if (bodyColor != null) { if (bodyColor != null) {
g2d.setColor(outlineMode ? Constants.TRANSPARENT_COLOR : bodyColor); g2d.setColor(outlineMode ? Constants.TRANSPARENT_COLOR
: bodyColor);
g2d.fill(shape); g2d.fill(shape);
} }
} }
@@ -162,11 +178,12 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1)); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1));
Color finalBorderColor; Color finalBorderColor;
if (outlineMode) { if (outlineMode) {
Theme theme = (Theme) ConfigurationManager.getInstance().readObject( Theme theme = (Theme) ConfigurationManager.getInstance()
IPlugInPort.THEME_KEY, Constants.DEFAULT_THEME); .readObject(IPlugInPort.THEME_KEY,
Constants.DEFAULT_THEME);
finalBorderColor = componentState == ComponentState.SELECTED finalBorderColor = componentState == ComponentState.SELECTED
|| componentState == ComponentState.DRAGGING ? SELECTION_COLOR : theme || componentState == ComponentState.DRAGGING ? SELECTION_COLOR
.getOutlineColor(); : theme.getOutlineColor();
} else { } else {
finalBorderColor = componentState == ComponentState.SELECTED finalBorderColor = componentState == ComponentState.SELECTED
|| componentState == ComponentState.DRAGGING ? SELECTION_COLOR || componentState == ComponentState.DRAGGING ? SELECTION_COLOR
@@ -175,50 +192,61 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
g2d.setColor(finalBorderColor); g2d.setColor(finalBorderColor);
g2d.draw(shape); g2d.draw(shape);
// // Go back to the original transformation to draw leads. // // Go back to the original transformation to draw leads.
// if (!outlineMode) { // if (!outlineMode) {
// AffineTransform textTransform = g2d.getTransform(); // AffineTransform textTransform = g2d.getTransform();
// g2d.setTransform(oldTransform); // g2d.setTransform(oldTransform);
// if (length.convertToPixels() > points[0].distance(points[1])) { // if (length.convertToPixels() > points[0].distance(points[1])) {
// g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); // g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
// } // 0.5f));
// int leadThickness = getClosestOdd(getLeadThickness()); // }
// double leadLength = (distance - calculatePinSpacing(shapeRect)) / 2 - leadThickness / 2; // int leadThickness = getClosestOdd(getLeadThickness());
// g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness)); // double leadLength = (distance - calculatePinSpacing(shapeRect)) /
// Color leadColor = shouldShadeLeads() ? getLeadColor(componentState).darker() // 2 - leadThickness / 2;
// : getLeadColor(componentState); // g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness));
// g2d.setColor(leadColor); // Color leadColor = shouldShadeLeads() ?
// int endX = (int) (points[0].x + Math.cos(theta) * leadLength); // getLeadColor(componentState).darker()
// int endY = (int) Math.round(points[0].y + Math.sin(theta) * leadLength); // : getLeadColor(componentState);
// g2d.drawLine(points[0].x, points[0].y, endX, endY); // g2d.setColor(leadColor);
// endX = (int) (points[1].x + Math.cos(theta - Math.PI) * leadLength); // int endX = (int) (points[0].x + Math.cos(theta) * leadLength);
// endY = (int) Math.round(points[1].y + Math.sin(theta - Math.PI) * leadLength); // int endY = (int) Math.round(points[0].y + Math.sin(theta) *
// g2d.drawLine(points[1].x, points[1].y, endX, endY); // leadLength);
// if (shouldShadeLeads()) { // g2d.drawLine(points[0].x, points[0].y, endX, endY);
// g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness - 2)); // endX = (int) (points[1].x + Math.cos(theta - Math.PI) *
// leadColor = getLeadColor(componentState); // leadLength);
// g2d.setColor(leadColor); // endY = (int) Math.round(points[1].y + Math.sin(theta - Math.PI) *
// g2d.drawLine(points[0].x, points[0].y, (int) (points[0].x + Math.cos(theta) // leadLength);
// * leadLength), (int) (points[0].y + Math.sin(theta) * leadLength)); // g2d.drawLine(points[1].x, points[1].y, endX, endY);
// g2d.drawLine(points[1].x, points[1].y, (int) (points[1].x + Math.cos(theta // if (shouldShadeLeads()) {
// - Math.PI) // g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness
// * leadLength), (int) (points[1].y + Math.sin(theta - Math.PI) // - 2));
// * leadLength)); // leadColor = getLeadColor(componentState);
// } // g2d.setColor(leadColor);
// g2d.setComposite(oldComposite); // g2d.drawLine(points[0].x, points[0].y, (int) (points[0].x +
// g2d.setTransform(textTransform); // Math.cos(theta)
// } // * leadLength), (int) (points[0].y + Math.sin(theta) *
// leadLength));
// g2d.drawLine(points[1].x, points[1].y, (int) (points[1].x +
// Math.cos(theta
// - Math.PI)
// * leadLength), (int) (points[1].y + Math.sin(theta - Math.PI)
// * leadLength));
// }
// g2d.setComposite(oldComposite);
// g2d.setTransform(textTransform);
// }
// Draw label. // Draw label.
g2d.setFont(LABEL_FONT); g2d.setFont(LABEL_FONT);
g2d.translate(shapeRect.x, shapeRect.y); g2d.translate(shapeRect.x, shapeRect.y);
Color finalLabelColor; Color finalLabelColor;
if (outlineMode) { if (outlineMode) {
Theme theme = (Theme) ConfigurationManager.getInstance().readObject( Theme theme = (Theme) ConfigurationManager.getInstance()
IPlugInPort.THEME_KEY, Constants.DEFAULT_THEME); .readObject(IPlugInPort.THEME_KEY,
Constants.DEFAULT_THEME);
finalLabelColor = componentState == ComponentState.SELECTED finalLabelColor = componentState == ComponentState.SELECTED
|| componentState == ComponentState.DRAGGING ? LABEL_COLOR_SELECTED : theme || componentState == ComponentState.DRAGGING ? LABEL_COLOR_SELECTED
.getOutlineColor(); : theme.getOutlineColor();
} else { } else {
finalLabelColor = componentState == ComponentState.SELECTED finalLabelColor = componentState == ComponentState.SELECTED
|| componentState == ComponentState.DRAGGING ? LABEL_COLOR_SELECTED || componentState == ComponentState.DRAGGING ? LABEL_COLOR_SELECTED
@@ -226,30 +254,35 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
} }
g2d.setColor(finalLabelColor); g2d.setColor(finalLabelColor);
FontMetrics fontMetrics = g2d.getFontMetrics(); FontMetrics fontMetrics = g2d.getFontMetrics();
String label = display == Display.NAME ? getName() : getValue().toString(); String label = display == Display.NAME ? getName() : getValue()
.toString();
Rectangle2D textRect = fontMetrics.getStringBounds(label, g2d); Rectangle2D textRect = fontMetrics.getStringBounds(label, g2d);
// Don't offset in outline mode. // Don't offset in outline mode.
int offset = outlineMode ? 0 : getLabelOffset((int) shapeRect.getWidth(), (int) textRect.getWidth()); int offset = outlineMode ? 0 : getLabelOffset((int) shapeRect
.getWidth(), (int) textRect.getWidth());
// Adjust label angle if needed to make sure that it's readable. // Adjust label angle if needed to make sure that it's readable.
if ((theta >= Math.PI / 2 && theta <= Math.PI) if ((theta >= Math.PI / 2 && theta <= Math.PI)
|| (theta < -Math.PI / 2 && theta > -Math.PI)) { || (theta < -Math.PI / 2 && theta > -Math.PI)) {
g2d.rotate(Math.PI, shapeRect.width / 2, shapeRect.height / 2); g2d.rotate(Math.PI, shapeRect.width / 2, shapeRect.height / 2);
offset = -offset; offset = -offset;
} }
g2d.drawString(label, (int) (shapeRect.width - textRect.getWidth()) / 2 + offset, g2d.drawString(label, (int) (shapeRect.width - textRect.getWidth())
calculateLabelYCoordinate(shapeRect, textRect, fontMetrics)); / 2 + offset, calculateLabelYCoordinate(shapeRect,
textRect, fontMetrics));
g2d.setTransform(oldTransform); g2d.setTransform(oldTransform);
} }
} }
private void drawLead(Graphics2D g2d, ComponentState componentState) { private void drawLead(Graphics2D g2d, ComponentState componentState) {
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(getLeadThickness())); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(
Color leadColor = shouldShadeLeads() ? getLeadColor(componentState).darker() getLeadThickness()));
: getLeadColor(componentState); Color leadColor = shouldShadeLeads() ? getLeadColor(componentState)
.darker() : getLeadColor(componentState);
g2d.setColor(leadColor); g2d.setColor(leadColor);
g2d.drawLine(points[0].x, points[0].y, points[1].x, points[1].y); g2d.drawLine(points[0].x, points[0].y, points[1].x, points[1].y);
if (shouldShadeLeads()) { if (shouldShadeLeads()) {
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(getLeadThickness() - 2)); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(
getLeadThickness() - 2));
leadColor = getLeadColor(componentState); leadColor = getLeadColor(componentState);
g2d.setColor(leadColor); g2d.setColor(leadColor);
g2d.drawLine(points[0].x, points[0].y, points[1].x, points[1].y); g2d.drawLine(points[0].x, points[0].y, points[1].x, points[1].y);
@@ -260,9 +293,10 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
// Do nothing. // Do nothing.
} }
protected int calculateLabelYCoordinate(Rectangle2D shapeRect, Rectangle2D textRect, protected int calculateLabelYCoordinate(Rectangle2D shapeRect,
FontMetrics fontMetrics) { Rectangle2D textRect, FontMetrics fontMetrics) {
return (int) (shapeRect.getHeight() - textRect.getHeight()) / 2 + fontMetrics.getAscent(); return (int) (shapeRect.getHeight() - textRect.getHeight()) / 2
+ fontMetrics.getAscent();
} }
protected boolean shouldShadeLeads() { protected boolean shouldShadeLeads() {
@@ -307,7 +341,8 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
*/ */
protected Color getLeadColor(ComponentState componentState) { protected Color getLeadColor(ComponentState componentState) {
return componentState == ComponentState.SELECTED return componentState == ComponentState.SELECTED
|| componentState == ComponentState.DRAGGING ? SELECTION_COLOR : LEAD_COLOR; || componentState == ComponentState.DRAGGING ? SELECTION_COLOR
: LEAD_COLOR;
} }
protected int calculatePinSpacing(Rectangle shapeRect) { protected int calculatePinSpacing(Rectangle shapeRect) {
@@ -387,4 +422,13 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
public void setDisplay(Display display) { public void setDisplay(Display display) {
this.display = display; this.display = display;
} }
@EditableProperty(name = "Label color")
public Color getLabelColor() {
return labelColor;
}
public void setLabelColor(Color labelColor) {
this.labelColor = labelColor;
}
} }
@@ -58,7 +58,7 @@ public class Presenter implements IPlugInPort {
private static final Logger LOG = Logger.getLogger(Presenter.class); private static final Logger LOG = Logger.getLogger(Presenter.class);
public static final VersionNumber CURRENT_VERSION = new VersionNumber(3, public static final VersionNumber CURRENT_VERSION = new VersionNumber(3,
16, 0); 17, 0);
public static final String DEFAULTS_KEY_PREFIX = "default."; public static final String DEFAULTS_KEY_PREFIX = "default.";
public static final List<IDIYComponent<?>> EMPTY_SELECTION = Collections public static final List<IDIYComponent<?>> EMPTY_SELECTION = Collections