Ability to rotate toggle switch

This commit is contained in:
bancika
2012-08-31 13:49:48 +00:00
parent 5abf2ebaa3
commit e85ffd1483
2 changed files with 120 additions and 52 deletions
@@ -6,11 +6,14 @@ import java.awt.Composite;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.Shape; import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D; import java.awt.geom.RoundRectangle2D;
import org.diylc.appframework.miscutils.ConfigurationManager; import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.common.ObjectCache; import org.diylc.common.ObjectCache;
import org.diylc.common.OrientationHV;
import org.diylc.components.AbstractTransparentComponent; import org.diylc.components.AbstractTransparentComponent;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
@@ -25,7 +28,8 @@ import org.diylc.core.measures.SizeUnit;
import org.diylc.utils.Constants; import org.diylc.utils.Constants;
@ComponentDescriptor(name = "Mini Toggle Switch", category = "Electromechanical", author = "Branislav Stojkovic", description = "Panel mounted mini toggle switch", stretchable = false, zOrder = IDIYComponent.COMPONENT, instanceNamePrefix = "SW", autoEdit = false) @ComponentDescriptor(name = "Mini Toggle Switch", category = "Electromechanical", author = "Branislav Stojkovic", description = "Panel mounted mini toggle switch", stretchable = false, zOrder = IDIYComponent.COMPONENT, instanceNamePrefix = "SW", autoEdit = false)
public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchType> { public class MiniToggleSwitch extends
AbstractTransparentComponent<ToggleSwitchType> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -40,9 +44,10 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
private static Color CIRCLE_COLOR = Color.decode("#FFFFAA"); private static Color CIRCLE_COLOR = Color.decode("#FFFFAA");
protected Point[] controlPoints = new Point[] { new Point(0, 0) }; protected Point[] controlPoints = new Point[] { new Point(0, 0) };
transient protected RoundRectangle2D body; transient protected Shape body;
protected String name; protected String name;
protected ToggleSwitchType switchType = ToggleSwitchType.DPDT; protected ToggleSwitchType switchType = ToggleSwitchType.DPDT;
private OrientationHV orientation = OrientationHV.VERTICAL;
public MiniToggleSwitch() { public MiniToggleSwitch() {
super(); super();
@@ -63,56 +68,83 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
new Point(firstPoint.x, firstPoint.y + 2 * spacing) }; new Point(firstPoint.x, firstPoint.y + 2 * spacing) };
break; break;
case DPDT: case DPDT:
controlPoints = new Point[] { firstPoint, controlPoints = new Point[] {
firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing), new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing), new Point(firstPoint.x, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + spacing, firstPoint.y), new Point(firstPoint.x + spacing, firstPoint.y),
new Point(firstPoint.x + spacing, firstPoint.y + spacing), new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * spacing) }; new Point(firstPoint.x + spacing, firstPoint.y + 2
* spacing) };
break; break;
case _3PDT: case _3PDT:
controlPoints = new Point[] { firstPoint, controlPoints = new Point[] {
firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing), new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing), new Point(firstPoint.x, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + spacing, firstPoint.y), new Point(firstPoint.x + spacing, firstPoint.y),
new Point(firstPoint.x + spacing, firstPoint.y + spacing), new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * 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),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + spacing), new Point(firstPoint.x + 2 * spacing, firstPoint.y
new Point(firstPoint.x + 2 * spacing, firstPoint.y + 2 * spacing) }; + spacing),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + 2
* spacing) };
break; break;
case _4PDT: case _4PDT:
controlPoints = new Point[] { firstPoint, controlPoints = new Point[] {
firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing), new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing), new Point(firstPoint.x, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + spacing, firstPoint.y), new Point(firstPoint.x + spacing, firstPoint.y),
new Point(firstPoint.x + spacing, firstPoint.y + spacing), new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * 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),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + spacing), new Point(firstPoint.x + 2 * spacing, firstPoint.y
new Point(firstPoint.x + 2 * spacing, firstPoint.y + 2 * spacing), + 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),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + spacing), new Point(firstPoint.x + 3 * spacing, firstPoint.y
new Point(firstPoint.x + 3 * spacing, firstPoint.y + 2 * spacing) }; + spacing),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + 2
* spacing) };
break; break;
case _5PDT: case _5PDT:
controlPoints = new Point[] { firstPoint, controlPoints = new Point[] {
firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing), new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing), new Point(firstPoint.x, firstPoint.y + 2 * spacing),
new Point(firstPoint.x + spacing, firstPoint.y), new Point(firstPoint.x + spacing, firstPoint.y),
new Point(firstPoint.x + spacing, firstPoint.y + spacing), new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * 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),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + spacing), new Point(firstPoint.x + 2 * spacing, firstPoint.y
new Point(firstPoint.x + 2 * spacing, firstPoint.y + 2 * spacing), + 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),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + spacing), new Point(firstPoint.x + 3 * spacing, firstPoint.y
new Point(firstPoint.x + 3 * spacing, firstPoint.y + 2 * spacing), + 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),
new Point(firstPoint.x + 4 * spacing, firstPoint.y + spacing), new Point(firstPoint.x + 4 * spacing, firstPoint.y
new Point(firstPoint.x + 4 * spacing, firstPoint.y + 2 * spacing) }; + spacing),
new Point(firstPoint.x + 4 * spacing, firstPoint.y + 2
* spacing) };
break; break;
} }
AffineTransform xform = AffineTransform.getRotateInstance(-Math.PI / 2,
firstPoint.x, firstPoint.y);
if (getOrientation() == OrientationHV.HORIZONTAL) {
for (int i = 1; i < controlPoints.length; i++) {
xform.transform(controlPoints[i], controlPoints[i]);
}
}
} }
@Override @Override
@@ -167,9 +199,25 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
body = null; body = null;
} }
@EditableProperty
public OrientationHV getOrientation() {
if (orientation == null) {
orientation = OrientationHV.VERTICAL;
}
return orientation;
}
public void setOrientation(OrientationHV orientation) {
this.orientation = orientation;
updateControlPoints();
// Reset body shape.
body = null;
}
@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) {
if (checkPointsClipped(g2d.getClip())) { if (checkPointsClipped(g2d.getClip())) {
return; return;
} }
@@ -178,20 +226,23 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
if (body != null) { if (body != null) {
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 : BODY_COLOR); g2d
.setColor(outlineMode ? Constants.TRANSPARENT_COLOR
: BODY_COLOR);
g2d.fill(body); g2d.fill(body);
g2d.setComposite(oldComposite); g2d.setComposite(oldComposite);
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
@@ -210,46 +261,60 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
for (Point p : controlPoints) { for (Point p : controlPoints) {
if (!outlineMode) { if (!outlineMode) {
g2d.setColor(CIRCLE_COLOR); g2d.setColor(CIRCLE_COLOR);
g2d.fillOval(p.x - circleDiameter / 2, p.y - circleDiameter / 2, circleDiameter, g2d.fillOval(p.x - circleDiameter / 2,
p.y - circleDiameter / 2, circleDiameter,
circleDiameter); circleDiameter);
} }
g2d.setColor(METAL_COLOR); g2d.setColor(METAL_COLOR);
g2d.fillRect(p.x - lugWidth / 2, p.y - lugHeight / 2, lugWidth, lugHeight); g2d.fillRect(p.x - lugWidth / 2, p.y - lugHeight / 2, lugWidth,
lugHeight);
} }
} }
public RoundRectangle2D getBody() { public Shape getBody() {
if (body == null) { if (body == null) {
Point firstPoint = controlPoints[0]; Point firstPoint = controlPoints[0];
int margin = (int) MARGIN.convertToPixels(); int margin = (int) MARGIN.convertToPixels();
int spacing = (int) SPACING.convertToPixels(); int spacing = (int) SPACING.convertToPixels();
switch (switchType) { switch (switchType) {
case SPST: case SPST:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, body = new RoundRectangle2D.Double(firstPoint.x - margin,
2 * margin, 2 * margin + spacing, margin, margin); firstPoint.y - margin, 2 * margin,
2 * margin + spacing, margin, margin);
break; break;
case SPDT: case SPDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, body = new RoundRectangle2D.Double(firstPoint.x - margin,
2 * margin, 2 * margin + 2 * spacing, margin, margin); firstPoint.y - margin, 2 * margin, 2 * margin + 2
* spacing, margin, margin);
break; break;
case DPDT: case DPDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2 body = new RoundRectangle2D.Double(firstPoint.x - margin,
* margin + spacing, 2 * margin + 2 * spacing, margin, margin); firstPoint.y - margin, 2 * margin + spacing, 2 * margin
+ 2 * spacing, margin, margin);
break; break;
case _3PDT: case _3PDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2 body = new RoundRectangle2D.Double(firstPoint.x - margin,
* margin + 2 * spacing, 2 * margin + 2 * spacing, margin, margin); firstPoint.y - margin, 2 * margin + 2 * spacing, 2
* margin + 2 * spacing, margin, margin);
break; break;
case _4PDT: case _4PDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2 body = new RoundRectangle2D.Double(firstPoint.x - margin,
* margin + 3 * spacing, 2 * margin + 2 * spacing, margin, margin); firstPoint.y - margin, 2 * margin + 3 * spacing, 2
* margin + 2 * spacing, margin, margin);
break; break;
case _5PDT: case _5PDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2 body = new RoundRectangle2D.Double(firstPoint.x - margin,
* margin + 4 * spacing, 2 * margin + 2 * spacing, margin, margin); firstPoint.y - margin, 2 * margin + 4 * spacing, 2
* margin + 2 * spacing, margin, margin);
break; break;
} }
} if (getOrientation() == OrientationHV.HORIZONTAL) {
AffineTransform xform = AffineTransform.getRotateInstance(-Math.PI / 2,
firstPoint.x, firstPoint.y);
body = new Area(body);
((Area)body).transform(xform);
}
}
return body; return body;
} }
@@ -257,15 +322,18 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
public void drawIcon(Graphics2D g2d, int width, int height) { public void drawIcon(Graphics2D g2d, int width, int height) {
int circleSize = 5 * width / 32; int circleSize = 5 * width / 32;
g2d.setColor(BODY_COLOR); g2d.setColor(BODY_COLOR);
g2d.fillRoundRect(width / 4, 1, width / 2, height - 2, circleSize, circleSize); g2d.fillRoundRect(width / 4, 1, width / 2, height - 2, circleSize,
circleSize);
g2d.setColor(BORDER_COLOR); g2d.setColor(BORDER_COLOR);
g2d.drawRoundRect(width / 4, 1, width / 2, height - 2, circleSize, circleSize); g2d.drawRoundRect(width / 4, 1, width / 2, height - 2, circleSize,
circleSize);
for (int i = 1; i <= 3; i++) { for (int i = 1; i <= 3; i++) {
g2d.setColor(CIRCLE_COLOR); g2d.setColor(CIRCLE_COLOR);
g2d.fillOval(width / 2 - circleSize / 2, i * height / 4 - 3, circleSize, circleSize); g2d.fillOval(width / 2 - circleSize / 2, i * height / 4 - 3,
circleSize, circleSize);
g2d.setColor(METAL_COLOR); g2d.setColor(METAL_COLOR);
g2d.drawLine(width / 2 - circleSize / 2 + 1, i * height / 4 - 1, width / 2 + circleSize g2d.drawLine(width / 2 - circleSize / 2 + 1, i * height / 4 - 1,
/ 2 - 1, i * height / 4 - 1); width / 2 + circleSize / 2 - 1, i * height / 4 - 1);
} }
} }
} }