Slide switch added

This commit is contained in:
Bane Stojkovic
2022-04-23 20:13:19 +02:00
parent d50acb4911
commit c24187eeb6
6 changed files with 789 additions and 199 deletions
@@ -0,0 +1,473 @@
/*
*
* DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* DIYLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with DIYLC. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
package org.diylc.components.electromechanical;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.IPlugInPort;
import org.diylc.common.ObjectCache;
import org.diylc.common.OrientationHV;
import org.diylc.components.AbstractTransparentComponent;
import org.diylc.components.transform.MiniToggleSwitchTransformer;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.ISwitch;
import org.diylc.core.Project;
import org.diylc.core.Theme;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.annotations.PositiveNonZeroMeasureValidator;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
import org.diylc.utils.Constants;
@ComponentDescriptor(name = "Slide Switch", category = "Electro-Mechanical",
author = "Branislav Stojkovic", description = "Panel mounted slide switch",
zOrder = IDIYComponent.COMPONENT, instanceNamePrefix = "SW", autoEdit = false,
enableCache = true, transformer = MiniToggleSwitchTransformer.class)
public class SlideSwitch extends AbstractTransparentComponent<SlideSwitchType> implements ISwitch {
private static final long serialVersionUID = 1L;
private static Size ROW_SPACING = new Size(0.25d, SizeUnit.in);
private static Size COLUMN_SPACING = new Size(0.2d, SizeUnit.in);
private static Size MARGIN = new Size(0.08d, SizeUnit.in);
private static Size LUG_WIDTH = new Size(0.020d, SizeUnit.in);
private static Size LUG_THICKNESS = new Size(0.1d, SizeUnit.in);
private static Size HOLE_DIAMETER = new Size(3d, SizeUnit.mm);
private static Color BODY_COLOR = Color.decode("#CD8500");
private static Color BORDER_COLOR = Color.gray;
public static Color TERMINAL_COLOR = METAL_COLOR;
public static Color LABEL_COLOR = Color.white;
public static Color BRACKET_COLOR = Color.lightGray;
protected Point2D[] controlPoints = new Point2D[] {new Point2D.Double(0, 0)};
transient protected Shape[] body;
protected SlideSwitchType switchType = SlideSwitchType.DP3T;
private OrientationHV orientation = OrientationHV.VERTICAL;
private Size rowSpacing = ROW_SPACING;
private Size columnSpacing = COLUMN_SPACING;
private Color bodyColor = BODY_COLOR;
private Color borderColor = BORDER_COLOR;
private Color labelColor = LABEL_COLOR;
private Color bracketColor = BRACKET_COLOR;
private Boolean showBracket = true;
public SlideSwitch() {
super();
updateControlPoints();
}
private void updateControlPoints() {
Point2D firstPoint = controlPoints[0];
int rowSpacing = (int) getRowSpacing().convertToPixels();
int columnSpacing = (int) getColumnSpacing().convertToPixels();
switch (switchType) {
case SPDT:
controlPoints = new Point2D[] {firstPoint,
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + rowSpacing),
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + 2 * rowSpacing)};
break;
case DPDT:
controlPoints = new Point2D[] {firstPoint,
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + rowSpacing),
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + 2 * rowSpacing),
new Point2D.Double(firstPoint.getX() + columnSpacing, firstPoint.getY()),
new Point2D.Double(firstPoint.getX() + columnSpacing, firstPoint.getY() + rowSpacing),
new Point2D.Double(firstPoint.getX() + columnSpacing,
firstPoint.getY() + 2 * rowSpacing)};
break;
case DP3T:
controlPoints = new Point2D[] {firstPoint,
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + rowSpacing),
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + 2 * rowSpacing),
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + 3 * rowSpacing),
new Point2D.Double(firstPoint.getX() + columnSpacing, firstPoint.getY()),
new Point2D.Double(firstPoint.getX() + columnSpacing, firstPoint.getY() + rowSpacing),
new Point2D.Double(firstPoint.getX() + columnSpacing,
firstPoint.getY() + 2 * rowSpacing),
new Point2D.Double(firstPoint.getX() + columnSpacing,
firstPoint.getY() + 3 * rowSpacing)};
break;
}
AffineTransform xform =
AffineTransform.getRotateInstance(-Math.PI / 2, firstPoint.getX(), firstPoint.getY());
if (getOrientation() == OrientationHV.HORIZONTAL) {
for (int i = 1; i < controlPoints.length; i++) {
xform.transform(controlPoints[i], controlPoints[i]);
}
}
}
@Override
public Point2D getControlPoint(int index) {
return controlPoints[index];
}
@Override
public boolean isControlPointSticky(int index) {
return true;
}
@Override
public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return VisibilityPolicy.NEVER;
}
@Override
public int getControlPointCount() {
return controlPoints.length;
}
@Override
public void setControlPoint(Point2D point, int index) {
controlPoints[index].setLocation(point);
// Reset body shape.
body = null;
}
@EditableProperty(name = "Type")
@Override
public SlideSwitchType getValue() {
return switchType;
}
@Override
public void setValue(SlideSwitchType value) {
this.switchType = value;
updateControlPoints();
// Reset body shape.
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;
}
@EditableProperty(name = "Row Spacing", validatorClass = PositiveNonZeroMeasureValidator.class)
public Size getRowSpacing() {
return rowSpacing;
}
public void setRowSpacing(Size rowSpacing) {
this.rowSpacing = rowSpacing;
updateControlPoints();
// Reset body shape.
body = null;
}
@EditableProperty(name = "Column Spacing", validatorClass = PositiveNonZeroMeasureValidator.class)
public Size getColumnSpacing() {
return columnSpacing;
}
public void setColumnSpacing(Size columnSpacing) {
this.columnSpacing = columnSpacing;
updateControlPoints();
// Reset body shape.
body = null;
}
@Override
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode,
Project project, IDrawingObserver drawingObserver) {
if (checkPointsClipped(g2d.getClip())) {
return;
}
Shape[] body = getBody();
Theme theme = (Theme) ConfigurationManager.getInstance().readObject(IPlugInPort.THEME_KEY,
Constants.DEFAULT_THEME);
// Draw body if available.
if (body != null) {
Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) {
g2d.setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha / MAX_ALPHA));
}
if (getShowBracket()) {
g2d.setColor(getBracketColor());
g2d.fill(body[1]);
}
g2d.setColor(outlineMode ? Constants.TRANSPARENT_COLOR : getBodyColor());
g2d.fill(body[0]);
g2d.setComposite(oldComposite);
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1));
Color finalBorderColor;
if (outlineMode) {
finalBorderColor =
componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING
? SELECTION_COLOR
: theme.getOutlineColor();
} else {
finalBorderColor =
componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING
? SELECTION_COLOR
: getBorderColor();
}
g2d.setColor(finalBorderColor);
if (getShowBracket()) {
g2d.draw(body[1]);
}
g2d.draw(body[0]);
}
// Do not track these changes because the whole switch has been tracked
// so far.
drawingObserver.stopTracking();
// Draw lugs.
int lugWidth;
int lugHeight;
if (getOrientation() == OrientationHV.HORIZONTAL) {
lugHeight = getClosestOdd((int) LUG_WIDTH.convertToPixels());
lugWidth = getClosestOdd((int) LUG_THICKNESS.convertToPixels());
} else {
lugWidth = getClosestOdd((int) LUG_WIDTH.convertToPixels());
lugHeight = getClosestOdd((int) LUG_THICKNESS.convertToPixels());
}
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1));
for (Point2D p : controlPoints) {
if (outlineMode) {
g2d.setColor(theme.getOutlineColor());
g2d.drawRect((int) (p.getX() - lugWidth / 2), (int) (p.getY() - lugHeight / 2), lugWidth,
lugHeight);
} else {
g2d.setColor(TERMINAL_COLOR);
g2d.fillRect((int) (p.getX() - lugWidth / 2), (int) (p.getY() - lugHeight / 2), lugWidth,
lugHeight);
g2d.setColor(TERMINAL_COLOR.darker());
g2d.drawRect((int) (p.getX() - lugWidth / 2), (int) (p.getY() - lugHeight / 2), lugWidth,
lugHeight);
}
}
// g2d.setFont(project.getFont());
// g2d.setColor(getLabelColor());
//
// double labelOffsetX = 0;
// double labelOffsetY = 0;
// if (getValue() == SlideSwitchType.SPDT) {
// int rowSpacing = (int) getRowSpacing().convertToPixels();
// if (getOrientation() == OrientationHV.VERTICAL) {
// labelOffsetY = -rowSpacing / 2;
// } else {
// labelOffsetX = -rowSpacing / 2;
// }
// }
//
// Rectangle bounds = body[0].getBounds();
// StringUtils.drawCenteredText(g2d, getName(), bounds.getCenterX() + labelOffsetX,
// bounds.getCenterY() + labelOffsetY, HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
}
public Shape[] getBody() {
if (body == null) {
Point2D firstPoint = controlPoints[0];
int margin = (int) MARGIN.convertToPixels();
int rowSpacing = (int) getRowSpacing().convertToPixels();
int columnSpacing = (int) getColumnSpacing().convertToPixels();
int holeDiameter = (int) HOLE_DIAMETER.convertToPixels();
double width = 0;
double height = 0;
switch (switchType) {
case SPDT:
width = 2 * margin;
height = 2 * margin + 2 * rowSpacing;
break;
case DPDT:
width = 2 * margin + columnSpacing;
height = 2 * margin + 2 * rowSpacing;
break;
case DP3T:
width = 2 * margin + columnSpacing;
height = 2 * margin + 3 * rowSpacing;
break;
}
Area mainBody = new Area(new Rectangle2D.Double(firstPoint.getX() - margin,
firstPoint.getY() - margin, width, height));
Area bracket = new Area(new RoundRectangle2D.Double(firstPoint.getX() - margin,
firstPoint.getY() - margin - holeDiameter * 2, width, height + holeDiameter * 4,
holeDiameter / 2, holeDiameter / 2));
bracket.subtract(
new Area(new Ellipse2D.Double(firstPoint.getX() - margin + width / 2 - holeDiameter / 2,
firstPoint.getY() - margin - holeDiameter * 3 / 2, holeDiameter, holeDiameter)));
bracket.subtract(
new Area(new Ellipse2D.Double(firstPoint.getX() - margin + width / 2 - holeDiameter / 2,
firstPoint.getY() - margin + height + holeDiameter / 2, holeDiameter, holeDiameter)));
bracket.subtract(mainBody);
if (getOrientation() == OrientationHV.HORIZONTAL) {
AffineTransform xform =
AffineTransform.getRotateInstance(-Math.PI / 2, firstPoint.getX(), firstPoint.getY());
mainBody.transform(xform);
bracket.transform(xform);
}
body = new Shape[] {mainBody, bracket};
}
return body;
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
int circleSize = (int) (5f * width / 32);
g2d.setColor(BODY_COLOR);
g2d.fillRect(width / 8, 1, width * 6 / 8, height - 2);
g2d.setColor(BORDER_COLOR);
g2d.drawRect(width / 8, 1, width * 6 / 8, height - 2);
g2d.setColor(TERMINAL_COLOR);
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(2f * width / 32));
for (int i = 1; i <= 3; i++) {
g2d.drawLine(width / 4, i * height / 4 - circleSize / 2 - 1, width / 4,
i * height / 4 + circleSize / 2 - 1);
g2d.drawLine(width * 3 / 4, i * height / 4 - circleSize / 2 - 1, width * 3 / 4,
i * height / 4 + circleSize / 2 - 1);
}
}
@EditableProperty(name = "Body")
public Color getBodyColor() {
if (bodyColor == null)
bodyColor = BODY_COLOR;
return bodyColor;
}
public void setBodyColor(Color bodyColor) {
this.bodyColor = bodyColor;
}
@EditableProperty(name = "Border")
public Color getBorderColor() {
if (borderColor == null)
borderColor = BORDER_COLOR;
return borderColor;
}
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
// switch stuff
//
// @Override
// public String getControlPointNodeName(int index) {
// // we don't want the switch to produce any nodes, it just makes connections
// return null;
// }
@Override
public boolean canPointMoveFreely(int pointIndex) {
return false;
}
@Override
public int getPositionCount() {
switch (switchType) {
case SPDT:
case DPDT:
return 2;
case DP3T:
return 3;
}
return 2;
}
@Override
public String getPositionName(int position) {
return "ON" + Integer.toString(position + 1);
}
@Override
public boolean arePointsConnected(int index1, int index2, int position) {
switch (switchType) {
case SPDT:
return (index1 == position) && (index2 - index1 == 1);
case DPDT:
case DP3T:
return (index1 == position * 2 || index1 == position * 2 + 1) && (index2 - index1 == 2);
}
return false;
}
@EditableProperty(name = "Label")
public Color getLabelColor() {
return labelColor;
}
public void setLabelColor(Color labelColor) {
this.labelColor = labelColor;
}
@EditableProperty(name = "Show Bracket")
public Boolean getShowBracket() {
return showBracket;
}
public void setShowBracket(Boolean showBracket) {
this.showBracket = showBracket;
}
@EditableProperty(name = "Bracket")
public Color getBracketColor() {
return bracketColor;
}
public void setBracketColor(Color bracketColor) {
this.bracketColor = bracketColor;
}
@Override
public Rectangle2D getCachingBounds() {
int margin = 20;
Rectangle2D bounds = getBody()[1].getBounds2D();
return new Rectangle2D.Double(bounds.getX() - margin, bounds.getY() - margin,
bounds.getWidth() + 2 * margin, bounds.getHeight() + 2 * margin);
}
}
@@ -0,0 +1,17 @@
package org.diylc.components.electromechanical;
public enum SlideSwitchType {
SPDT, DPDT, DP3T;
@Override
public String toString() {
String name = name();
if (name.startsWith("_"))
name = name.substring(1);
name = name.replace("_", " ");
name = name.replace("mustang", "");
name = name.replace("off", " (Center OFF)");
return name;
}
}
@@ -1,36 +1,27 @@
/* /*
*
DIY Layout Creator (DIYLC). * DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
Copyright (c) 2009-2018 held jointly by the individual authors. *
* This file is part of DIYLC.
This file is part of DIYLC. *
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
DIYLC is free software: you can redistribute it and/or modify * General Public License as published by the Free Software Foundation, either version 3 of the
it under the terms of the GNU General Public License as published by * License, or (at your option) any later version.
the Free Software Foundation, either version 3 of the License, or *
(at your option) any later version. * DIYLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
DIYLC is distributed in the hope that it will be useful, * Public License for more details.
but WITHOUT ANY WARRANTY; without even the implied warranty of *
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * You should have received a copy of the GNU General Public License along with DIYLC. If not, see
GNU General Public License for more details. * <http://www.gnu.org/licenses/>.
*
You should have received a copy of the GNU General Public License */
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.netlist; package org.diylc.netlist;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Arrays; import java.util.Arrays;
import org.diylc.components.electromechanical.OpenJack1_4; import org.diylc.components.electromechanical.OpenJack1_4;
import org.diylc.components.guitar.HumbuckerPickup; import org.diylc.components.guitar.HumbuckerPickup;
import org.diylc.components.guitar.LPSwitch;
import org.diylc.components.guitar.LeverSwitch;
import org.diylc.components.guitar.LeverSwitch.LeverSwitchType;
import org.diylc.components.guitar.SingleCoilPickup; import org.diylc.components.guitar.SingleCoilPickup;
import org.diylc.components.passive.PotentiometerPanel; import org.diylc.components.passive.PotentiometerPanel;
import org.diylc.components.passive.RadialCeramicDiskCapacitor; import org.diylc.components.passive.RadialCeramicDiskCapacitor;
@@ -83,7 +74,8 @@ public class GuitarNetlistAnalyzerTests {
Netlist netlist = new Netlist(Arrays.asList(jack, pickup, vol, tone, cap)); Netlist netlist = new Netlist(Arrays.asList(jack, pickup, vol, tone, cap));
Group hotGroup = new Group().connect(pickup, 1).connect(vol, 0).connect(tone, 2); Group hotGroup = new Group().connect(pickup, 1).connect(vol, 0).connect(tone, 2);
Group tipGroup = new Group().connect(jack, 0).connect(vol, 1); Group tipGroup = new Group().connect(jack, 0).connect(vol, 1);
Group sleeveGroup = new Group().connect(jack, 1).connect(pickup, 2).connect(vol, 2).connect(cap, 0); Group sleeveGroup =
new Group().connect(jack, 1).connect(pickup, 2).connect(vol, 2).connect(cap, 0);
Group toneGroup = new Group().connect(tone, 1).connect(cap, 1); Group toneGroup = new Group().connect(tone, 1).connect(cap, 1);
netlist.add(tipGroup).add(tipGroup).add(sleeveGroup).add(hotGroup).add(toneGroup); netlist.add(tipGroup).add(tipGroup).add(sleeveGroup).add(hotGroup).add(toneGroup);
Tree tree = new GuitarDiagramAnalyzer().constructTree(netlist); Tree tree = new GuitarDiagramAnalyzer().constructTree(netlist);
@@ -108,16 +100,22 @@ public class GuitarNetlistAnalyzerTests {
cap1.setName("Cap1"); cap1.setName("Cap1");
RadialCeramicDiskCapacitor cap2 = new RadialCeramicDiskCapacitor(); RadialCeramicDiskCapacitor cap2 = new RadialCeramicDiskCapacitor();
cap2.setName("Cap2"); cap2.setName("Cap2");
Netlist netlist = new Netlist(Arrays.asList(jack, pickup1, pickup2, vol, tone1, tone2, cap1, cap2)); Netlist netlist =
Group hotGroup = new Group().connect(pickup1, 1).connect(vol, 0).connect(tone1, 2).connect(pickup2, 1).connect(tone2, 2); new Netlist(Arrays.asList(jack, pickup1, pickup2, vol, tone1, tone2, cap1, cap2));
Group hotGroup = new Group().connect(pickup1, 1).connect(vol, 0).connect(tone1, 2)
.connect(pickup2, 1).connect(tone2, 2);
Group tipGroup = new Group().connect(jack, 0).connect(vol, 1); Group tipGroup = new Group().connect(jack, 0).connect(vol, 1);
Group sleeveGroup = new Group().connect(jack, 1).connect(pickup1, 2).connect(pickup2, 2).connect(vol, 2).connect(cap1, 0).connect(cap2, 0); Group sleeveGroup = new Group().connect(jack, 1).connect(pickup1, 2).connect(pickup2, 2)
.connect(vol, 2).connect(cap1, 0).connect(cap2, 0);
Group tone1Group = new Group().connect(tone1, 1).connect(cap1, 1); Group tone1Group = new Group().connect(tone1, 1).connect(cap1, 1);
Group tone2Group = new Group().connect(tone2, 1).connect(cap2, 1); Group tone2Group = new Group().connect(tone2, 1).connect(cap2, 1);
netlist.add(tipGroup).add(tipGroup).add(sleeveGroup).add(hotGroup).add(tone1Group).add(tone2Group); netlist.add(tipGroup).add(tipGroup).add(sleeveGroup).add(hotGroup).add(tone1Group)
.add(tone2Group);
Tree tree = new GuitarDiagramAnalyzer().constructTree(netlist); Tree tree = new GuitarDiagramAnalyzer().constructTree(netlist);
String s = tree.toString(); String s = tree.toString();
assertEquals("(((Volume.1-2) + ((Pickup1.North<-) || (Pickup2.North<-) || (Tone1.2-3 + Cap1) || (Tone2.2-3 + Cap2))) || (Volume.2-3))", s); assertEquals(
"(((Volume.1-2) + ((Pickup1.North<-) || (Pickup2.North<-) || (Tone1.2-3 + Cap1) || (Tone2.2-3 + Cap2))) || (Volume.2-3))",
s);
} }
@Test @Test
@@ -264,73 +262,23 @@ public class GuitarNetlistAnalyzerTests {
cap1.setName("Cap1"); cap1.setName("Cap1");
RadialCeramicDiskCapacitor cap2 = new RadialCeramicDiskCapacitor(); RadialCeramicDiskCapacitor cap2 = new RadialCeramicDiskCapacitor();
cap2.setName("Cap2"); cap2.setName("Cap2");
Netlist netlist = new Netlist(Arrays.asList(jack, pickup1, pickup2, vol, tone1, tone2, cap1, cap2)); Netlist netlist =
Group hotGroup = new Group().connect(pickup1, 0).connect(vol, 0).connect(tone1, 2).connect(pickup2, 0).connect(tone2, 2); new Netlist(Arrays.asList(jack, pickup1, pickup2, vol, tone1, tone2, cap1, cap2));
Group hotGroup = new Group().connect(pickup1, 0).connect(vol, 0).connect(tone1, 2)
.connect(pickup2, 0).connect(tone2, 2);
Group tipGroup = new Group().connect(jack, 0).connect(vol, 1); Group tipGroup = new Group().connect(jack, 0).connect(vol, 1);
Group sleeveGroup = new Group().connect(jack, 1).connect(pickup1, 3).connect(pickup2, 3).connect(vol, 2).connect(cap1, 0).connect(cap2, 0); Group sleeveGroup = new Group().connect(jack, 1).connect(pickup1, 3).connect(pickup2, 3)
.connect(vol, 2).connect(cap1, 0).connect(cap2, 0);
Group tone1Group = new Group().connect(tone1, 1).connect(cap1, 1); Group tone1Group = new Group().connect(tone1, 1).connect(cap1, 1);
Group tone2Group = new Group().connect(tone2, 1).connect(cap2, 1); Group tone2Group = new Group().connect(tone2, 1).connect(cap2, 1);
Group coilTap1 = new Group().connect(pickup1, 1).connect(pickup1, 2); Group coilTap1 = new Group().connect(pickup1, 1).connect(pickup1, 2);
Group coilTap2 = new Group().connect(pickup2, 1).connect(pickup2, 2); Group coilTap2 = new Group().connect(pickup2, 1).connect(pickup2, 2);
netlist.add(tipGroup).add(tipGroup).add(sleeveGroup).add(hotGroup).add(tone1Group).add(tone2Group).add(coilTap1).add(coilTap2); netlist.add(tipGroup).add(tipGroup).add(sleeveGroup).add(hotGroup).add(tone1Group)
.add(tone2Group).add(coilTap1).add(coilTap2);
Tree tree = new GuitarDiagramAnalyzer().constructTree(netlist); Tree tree = new GuitarDiagramAnalyzer().constructTree(netlist);
String s = tree.toString(); String s = tree.toString();
assertEquals("(((Volume.1-2) + ((Pickup1.North<- + Pickup1.South<-) || (Pickup2.North<- + Pickup2.South<-) || (Tone1.2-3 + Cap1) || (Tone2.2-3 + Cap2))) || (Volume.2-3))", s); assertEquals(
} "(((Volume.1-2) + ((Pickup1.North<- + Pickup1.South<-) || (Pickup2.North<- + Pickup2.South<-) || (Tone1.2-3 + Cap1) || (Tone2.2-3 + Cap2))) || (Volume.2-3))",
s);
@Test
public void testLeverSwitch() {
LeverSwitch leverSwitch = new LeverSwitch();
leverSwitch.setType(LeverSwitchType.DP5T);
String[] validCombinations = new String[]{
"0,0,1", "0,6,11", "0,12,13", "0,18,23",
"1,0,2", "1,7,11", "1,12,14", "1,19,23",
"2,0,3", "2,8,11", "2,12,15", "2,20,23",
"3,0,4", "3,9,11", "3,12,16", "3,21,23",
"4,0,5", "4,10,11", "4,12,17", "4,22,23"};
Arrays.sort(validCombinations);
for (int p = 0; p < leverSwitch.getPositionCount(); p++)
for (int i = 0; i < leverSwitch.getControlPointCount(); i++)
for (int j = 0; j < leverSwitch.getControlPointCount(); j++) {
String test = p + "," + i + "," + j;
boolean isConnected = leverSwitch.arePointsConnected(i, j, p);
boolean isOk = (isConnected && Arrays.binarySearch(validCombinations, test) >= 0) ||
!isConnected && Arrays.binarySearch(validCombinations, test) < 0;
if (!isOk)
fail("Bad connection for p=" + p + ", i=" + i + ",j=" + j);
}
leverSwitch.setType(LeverSwitchType._4P5T);
Arrays.sort(validCombinations);
for (int p = 0; p < leverSwitch.getPositionCount(); p++)
for (int i = 0; i < leverSwitch.getControlPointCount(); i++)
for (int j = 0; j < leverSwitch.getControlPointCount(); j++) {
String test = p + "," + i + "," + j;
boolean isConnected = leverSwitch.arePointsConnected(i, j, p);
boolean isOk = (isConnected && Arrays.binarySearch(validCombinations, test) >= 0) ||
!isConnected && Arrays.binarySearch(validCombinations, test) < 0;
if (!isOk)
fail("Bad connection for p=" + p + ", i=" + i + ",j=" + j);
}
}
@Test
public void testLPSwitch() {
LPSwitch lpSwitch = new LPSwitch();
String[] validCombinations = new String[] {
"0,1,2", "1,1,2", "1,2,3", "1,1,3", "2,2,3"
};
Arrays.sort(validCombinations);
for (int p = 0; p < lpSwitch.getPositionCount(); p++)
for (int i = 0; i < lpSwitch.getControlPointCount(); i++)
for (int j = 0; j < lpSwitch.getControlPointCount(); j++) {
String test = p + "," + i + "," + j;
boolean isConnected = lpSwitch.arePointsConnected(i, j, p);
boolean isOk = (isConnected && Arrays.binarySearch(validCombinations, test) >= 0) ||
!isConnected && Arrays.binarySearch(validCombinations, test) < 0;
if (!isOk)
fail("Bad connection for p=" + p + ", i=" + i + ",j=" + j);
}
} }
} }
@@ -0,0 +1,87 @@
package org.diylc.netlist;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Arrays;
import org.diylc.components.electromechanical.SlideSwitch;
import org.diylc.components.electromechanical.SlideSwitchType;
import org.diylc.components.guitar.LPSwitch;
import org.diylc.components.guitar.LeverSwitch;
import org.diylc.components.guitar.LeverSwitch.LeverSwitchType;
import org.diylc.core.IDIYComponent;
import org.diylc.core.ISwitch;
import org.junit.Test;
public class SwitchTests {
@Test
public void testLeverSwitchDP5T() {
LeverSwitch leverSwitch = new LeverSwitch();
leverSwitch.setType(LeverSwitchType.DP5T);
String[] validCombinations = new String[] {"0,0,1", "0,6,11", "1,0,2", "1,7,11", "2,0,3",
"2,8,11", "3,0,4", "3,9,11", "4,0,5", "4,10,11"};
testSwitch(leverSwitch, validCombinations);
}
@Test
public void testLeverSwitch4P5T() {
LeverSwitch leverSwitch = new LeverSwitch();
String[] validCombinations = new String[] {"0,0,1", "0,6,11", "0,12,13", "0,18,23", "1,0,2",
"1,7,11", "1,12,14", "1,19,23", "2,0,3", "2,8,11", "2,12,15", "2,20,23", "3,0,4", "3,9,11",
"3,12,16", "3,21,23", "4,0,5", "4,10,11", "4,12,17", "4,22,23"};
leverSwitch.setType(LeverSwitchType._4P5T);
testSwitch(leverSwitch, validCombinations);
}
@Test
public void testLPSwitch() {
LPSwitch lpSwitch = new LPSwitch();
String[] validCombinations = new String[] {"0,1,2", "1,1,2", "1,2,3", "1,1,3", "2,2,3"};
testSwitch(lpSwitch, validCombinations);
}
@Test
public void testSlideSwitchSPDT() {
SlideSwitch slideSwitch = new SlideSwitch();
slideSwitch.setValue(SlideSwitchType.SPDT);
String[] validCombinations = new String[] {"0,0,1", "1,1,2"};
testSwitch(slideSwitch, validCombinations);
}
@Test
public void testSlideSwitchDPDT() {
SlideSwitch slideSwitch = new SlideSwitch();
slideSwitch.setValue(SlideSwitchType.DPDT);
String[] validCombinations = new String[] {"0,0,2", "0,1,3", "1,2,4", "1,3,5"};
testSwitch(slideSwitch, validCombinations);
}
@Test
public void testSlideSwitchDP3T() {
SlideSwitch slideSwitch = new SlideSwitch();
slideSwitch.setValue(SlideSwitchType.DP3T);
String[] validCombinations = new String[] {"0,0,2", "0,1,3", "1,2,4", "1,3,5", "2,4,6", "2,5,7"};
testSwitch(slideSwitch, validCombinations);
}
private void testSwitch(ISwitch sw, String[] validCombinations) {
Arrays.sort(validCombinations);
int count = 0;
for (int p = 0; p < sw.getPositionCount(); p++)
for (int i = 0; i < ((IDIYComponent<?>) sw).getControlPointCount(); i++)
for (int j = 0; j < ((IDIYComponent<?>) sw).getControlPointCount(); j++) {
String test = p + "," + i + "," + j;
boolean isConnected = sw.arePointsConnected(i, j, p);
if (isConnected)
count++;
boolean isOk = (isConnected && Arrays.binarySearch(validCombinations, test) >= 0)
|| !isConnected && Arrays.binarySearch(validCombinations, test) < 0;
if (!isOk)
fail("Bad connection for p=" + p + ", i=" + i + ",j=" + j);
}
assertEquals(validCombinations.length, count);
}
}
+5 -1
View File
@@ -10,7 +10,7 @@
<property name="jar.dir" value="${build.dir}/jar" /> <property name="jar.dir" value="${build.dir}/jar" />
<property name="report.dir" value="${build.dir}/junitreport" /> <property name="report.dir" value="${build.dir}/junitreport" />
<property name="main.class" value="org.diylc.DIYLCStarter" /> <property name="main.class" value="org.diylc.DIYLCStarter" />
<property name="diylc.version" value="4.22.2" /> <property name="diylc.version" value="4.23.0" />
<property name="osx.jre8.path" value="jre\osx\jre8u321\Contents\Home\" /> <property name="osx.jre8.path" value="jre\osx\jre8u321\Contents\Home\" />
@@ -139,6 +139,9 @@
<arg value="/Fdiylc-${diylc.version}-win"/> <arg value="/Fdiylc-${diylc.version}-win"/>
<arg value="install.iss"/> <arg value="install.iss"/>
</exec> </exec>
</target>
<target name="universal_installer" depends="jar">
<izpack input="${basedir}/izpack.xml" <izpack input="${basedir}/izpack.xml"
output="${build.dir}/diylc-${diylc.version}-setup.jar" output="${build.dir}/diylc-${diylc.version}-setup.jar"
installerType="standard" installerType="standard"
@@ -162,6 +165,7 @@
<zipfileset dir="icons" includes="*.png" prefix="icons" /> <zipfileset dir="icons" includes="*.png" prefix="icons" />
<zipfileset dir="flatpak" includes="com.diy_fever.DIYLayoutCreator.desktop" /> <zipfileset dir="flatpak" includes="com.diy_fever.DIYLayoutCreator.desktop" />
<zipfileset dir="flatpak" includes="com.diy_fever.DIYLayoutCreator.metainfo.xml" /> <zipfileset dir="flatpak" includes="com.diy_fever.DIYLayoutCreator.metainfo.xml" />
<zipfileset dir="jre/win/jre1.8.0_211" prefix="jre" />
</zip> </zip>
<checksum file="${build.dir}/diylc-${diylc.version}.zip" algorithm="SHA-256"/> <checksum file="${build.dir}/diylc-${diylc.version}.zip" algorithm="SHA-256"/>
</target> </target>
@@ -3,16 +3,13 @@ package org.diylc;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@@ -23,12 +20,9 @@ import java.util.stream.Stream;
import org.diylc.appframework.miscutils.ConfigurationManager; import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.appframework.miscutils.InMemoryConfigurationManager; import org.diylc.appframework.miscutils.InMemoryConfigurationManager;
import org.diylc.appframework.update.VersionNumber; import org.diylc.appframework.update.VersionNumber;
import org.diylc.common.INetlistAnalyzer;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.core.IView; import org.diylc.core.IView;
import org.diylc.netlist.Netlist; import org.diylc.netlist.Netlist;
import org.diylc.netlist.Summary;
import org.diylc.netlist.TreeException;
import org.diylc.presenter.Presenter; import org.diylc.presenter.Presenter;
import org.diylc.swing.plugins.file.ProjectDrawingProvider; import org.diylc.swing.plugins.file.ProjectDrawingProvider;
import org.diylc.swingframework.export.DrawingExporter; import org.diylc.swingframework.export.DrawingExporter;
@@ -232,61 +226,104 @@ public class RegressionTestRunner {
return res.failed("Images do not match!"); return res.failed("Images do not match!");
} }
List<INetlistAnalyzer> summarizers = presenter.getNetlistAnalyzers();
if (summarizers != null) {
List<Netlist> netlists = presenter.extractNetlists(true); List<Netlist> netlists = presenter.extractNetlists(true);
File netlistOutputDir = new File(
for (INetlistAnalyzer summarizer : summarizers) { file.getParentFile().getParentFile().getAbsolutePath() + File.separator + "netlist");
File summaryOutputDir = File netlistOutputFile = new File(netlistOutputDir.getAbsolutePath() + File.separator
new File(outputDir.getAbsolutePath() + File.separator + summarizer.getShortName());
File summaryOutputFile = new File(summaryOutputDir.getAbsolutePath() + File.separator
+ file.getName().replace(".diy", ".txt")); + file.getName().replace(".diy", ".txt"));
summaryOutputDir.mkdirs(); File netlistInputFile = new File(inputDir.getAbsolutePath() + File.separator + "netlist"
if (summaryOutputFile.exists()) { + File.separator + (file.getName().replace(".diy", ".txt")));
summaryOutputFile.delete();
}
File summaryInputDir = new File(file.getParentFile().getParentFile().getAbsolutePath()
+ File.separator + summarizer.getShortName());
File summaryInputFile = new File(summaryInputDir.getAbsolutePath() + File.separator
+ file.getName().replace(".diy", ".txt"));
if (!summaryInputFile.exists()) {
return res.failed("Summary input file not found for: " + summarizer.getShortName());
}
netlistOutputDir.mkdirs();
try { try {
List<String> outputLines = new ArrayList<String>(); if (netlistOutputFile.exists()) {
netlistOutputFile.delete();
}
try { try {
writeSummariesToFile(netlists, summarizer, summaryOutputFile); try (BufferedWriter writer = new BufferedWriter(
outputLines = Files.readAllLines(Paths.get(summaryOutputFile.getAbsolutePath())); new OutputStreamWriter(new FileOutputStream(netlistOutputFile, true), "UTF-8"))) {
for (Netlist netlist : netlists) {
writer.write(netlist.toString());
writer.newLine();
}
}
} catch (Exception e) { } catch (Exception e) {
try (BufferedWriter writer = new BufferedWriter( try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(summaryOutputFile, true), "UTF-8"))) { new OutputStreamWriter(new FileOutputStream(netlistOutputFile, true), "UTF-8"))) {
String line = e.getClass().getCanonicalName() + ": " + e.getMessage(); writer.write(e.getClass().getCanonicalName() + ": " + e.getMessage());
writer.write(line);
outputLines.add(line);
} }
} }
List<String> inputLines = List<String> inputLines = Files.readAllLines(Paths.get(netlistInputFile.getAbsolutePath()));
Files.readAllLines(Paths.get(summaryInputFile.getAbsolutePath())); List<String> outputLines = Files.readAllLines(Paths.get(netlistOutputFile.getAbsolutePath()));
if (inputLines.size() != outputLines.size()) { if (inputLines.size() != outputLines.size()) {
return res.failed( return res.failed("Netlists do not match");
"Summary input and output files do not match for: " + summarizer.getShortName());
} }
for (int i = 0; i < inputLines.size(); i++) { for (int i = 0; i < inputLines.size(); i++) {
if (!inputLines.get(i).equals(outputLines.get(i))) { if (!inputLines.get(i).equals(outputLines.get(i))) {
return res.failed( return res.failed("Netlists do not match");
"Summary input and output files do not match for: " + summarizer.getShortName());
} }
} }
} catch (Exception e) { } catch (IOException e) {
return res.failed(e.getMessage()); return res.failed(e.getMessage());
} }
}
} // List<INetlistAnalyzer> summarizers = presenter.getNetlistAnalyzers();
// if (summarizers != null) {
// List<Netlist> netlists = presenter.extractNetlists(true);
//
// for (INetlistAnalyzer summarizer : summarizers) {
// File summaryOutputDir =
// new File(outputDir.getAbsolutePath() + File.separator + summarizer.getShortName());
// File summaryOutputFile = new File(summaryOutputDir.getAbsolutePath() + File.separator
// + file.getName().replace(".diy", ".txt"));
// summaryOutputDir.mkdirs();
// if (summaryOutputFile.exists()) {
// summaryOutputFile.delete();
// }
//
// File summaryInputDir = new File(file.getParentFile().getParentFile().getAbsolutePath()
// + File.separator + summarizer.getShortName());
// File summaryInputFile = new File(summaryInputDir.getAbsolutePath() + File.separator
// + file.getName().replace(".diy", ".txt"));
//
// if (!summaryInputFile.exists()) {
// return res.failed("Summary input file not found for: " + summarizer.getShortName());
// }
//
// try {
// List<String> outputLines = new ArrayList<String>();
// try {
// writeSummariesToFile(netlists, summarizer, summaryOutputFile);
// outputLines = Files.readAllLines(Paths.get(summaryOutputFile.getAbsolutePath()));
// } catch (Exception e) {
// try (BufferedWriter writer = new BufferedWriter(
// new OutputStreamWriter(new FileOutputStream(summaryOutputFile, true), "UTF-8"))) {
// String line = e.getClass().getCanonicalName() + ": " + e.getMessage();
// writer.write(line);
// outputLines.add(line);
// }
// }
//
// List<String> inputLines =
// Files.readAllLines(Paths.get(summaryInputFile.getAbsolutePath()));
//
// if (inputLines.size() != outputLines.size()) {
// return res.failed(
// "Summary input and output files do not match for: " + summarizer.getShortName());
// }
// for (int i = 0; i < inputLines.size(); i++) {
// if (!inputLines.get(i).equals(outputLines.get(i))) {
// return res.failed(
// "Summary input and output files do not match for: " + summarizer.getShortName());
// }
// }
// } catch (Exception e) {
// return res.failed(e.getMessage());
// }
// }
// }
String message = null; String message = null;
if (!view.getMessages().isEmpty()) { if (!view.getMessages().isEmpty()) {
@@ -310,51 +347,75 @@ public class RegressionTestRunner {
// always export image to ensure that rendering is done and continuity areas are populated // always export image to ensure that rendering is done and continuity areas are populated
DrawingExporter.getInstance().exportPNG(drawingProvider, pngFile); DrawingExporter.getInstance().exportPNG(drawingProvider, pngFile);
List<INetlistAnalyzer> summarizers = plugInPort.getNetlistAnalyzers();
if (summarizers != null) {
List<Netlist> netlists = plugInPort.extractNetlists(true); List<Netlist> netlists = plugInPort.extractNetlists(true);
File netlistDir = new File(
file.getParentFile().getParentFile().getAbsolutePath() + File.separator + "netlist");
File netlistFile = new File(
netlistDir.getAbsolutePath() + File.separator + file.getName().replace(".diy", ".txt"));
for (INetlistAnalyzer summarizer : summarizers) { netlistDir.mkdirs();
File summaryDir = new File(file.getParentFile().getParentFile().getAbsolutePath() if (!netlistFile.exists()) {
+ File.separator + summarizer.getShortName());
File summaryFile = new File(summaryDir.getAbsolutePath() + File.separator
+ file.getName().replace(".diy", ".txt"));
summaryDir.mkdirs();
if (!summaryFile.exists()) {
try { try {
writeSummariesToFile(netlists, summarizer, summaryFile); try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(netlistFile, true), "UTF-8"))) {
for (Netlist netlist : netlists) {
writer.write(netlist.toString());
writer.newLine();
}
}
} catch (Exception e) { } catch (Exception e) {
try (BufferedWriter writer = new BufferedWriter( try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(summaryFile, true), "UTF-8"))) { new OutputStreamWriter(new FileOutputStream(netlistFile, true), "UTF-8"))) {
writer.write(e.getClass().getCanonicalName() + ": " + e.getMessage()); writer.write(e.getClass().getCanonicalName() + ": " + e.getMessage());
} }
} }
} }
}
} // List<INetlistAnalyzer> summarizers = plugInPort.getNetlistAnalyzers();
// if (summarizers != null) {
//
//
// for (INetlistAnalyzer summarizer : summarizers) {
// summaryDir = new File(file.getParentFile().getParentFile().getAbsolutePath()
// + File.separator + summarizer.getShortName());
// summaryFile = new File(summaryDir.getAbsolutePath() + File.separator
// + file.getName().replace(".diy", ".txt"));
// summaryDir.mkdirs();
// if (!summaryFile.exists()) {
// try {
// writeSummariesToFile(netlists, summarizer, summaryFile);
//
// } catch (Exception e) {
// try (BufferedWriter writer = new BufferedWriter(
// new OutputStreamWriter(new FileOutputStream(summaryFile, true), "UTF-8"))) {
// writer.write(e.getClass().getCanonicalName() + ": " + e.getMessage());
// }
// }
// }
// }
// }
} catch (Exception e) { } catch (Exception e) {
return res.failed(e.getMessage()); return res.failed(e.getMessage());
} }
return res.succedded(); return res.succedded();
} }
private static void writeSummariesToFile(List<Netlist> netlists, INetlistAnalyzer summarizer, // private static void writeSummariesToFile(List<Netlist> netlists, INetlistAnalyzer summarizer,
File summaryFile) // File summaryFile)
throws TreeException, IOException, UnsupportedEncodingException, FileNotFoundException { // throws TreeException, IOException, UnsupportedEncodingException, FileNotFoundException {
List<Summary> summaries = summarizer.summarize(netlists, null); // List<Summary> summaries = summarizer.summarize(netlists, null);
//
try (BufferedWriter writer = new BufferedWriter( // try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(summaryFile, true), "UTF-8"))) { // new OutputStreamWriter(new FileOutputStream(summaryFile, true), "UTF-8"))) {
for (Summary summary : summaries) { // for (Summary summary : summaries) {
writer.write("<b>Switch configuration: "); // writer.write("<b>Switch configuration: ");
writer.write(summary.getNetlist().getSwitchSetup().toString()); // writer.write(summary.getNetlist().getSwitchSetup().toString());
writer.write("</b><br><br>"); // writer.write("</b><br><br>");
writer.write(summary.getSummary()); // writer.write(summary.getSummary());
writer.newLine(); // writer.newLine();
} // }
} // }
} // }
static class RegressionTestResult { static class RegressionTestResult {
boolean ok; boolean ok;