Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ This file is part of Universal Gcode Sender (UGS).

public class JogPanel extends JPanel implements UGSEventListener {

private final StepSizeSpinner xyStepSizeSpinner = new StepSizeSpinner();
private final StepSizeSpinner zStepSizeSpinner = new StepSizeSpinner();
private StepSizeSpinner xyStepSizeSpinner = null;
private StepSizeSpinner zStepSizeSpinner = null;
private final JLabel stepSizeLabel = new JLabel(Localization.getString("mainWindow.swing.stepSizeLabel"));
private final JLabel stepSizeLabelZ = new JLabel(Localization.getString("mainWindow.swing.stepSizeZLabel"));

private final StepSizeSpinner feedRateSpinner = new StepSizeSpinner();
private final StepSizeSpinner feedRateSpinner = new StepSizeSpinner(null);
private final JLabel feedRateLabel = new JLabel(Localization.getString("mainWindow.swing.feedRateLabel"));

private final JButton unitButton = new JButton();
Expand All @@ -59,6 +59,8 @@ public class JogPanel extends JPanel implements UGSEventListener {
public JogPanel(BackendAPI backend, JogService jogService, boolean showKeyboardToggle) {
this.backend = backend;
this.showKeyboardToggle = showKeyboardToggle;
xyStepSizeSpinner = new StepSizeSpinner(backend);
zStepSizeSpinner = new StepSizeSpinner(backend);

this.jogService = jogService;

Expand Down

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to keep this component simple without the dependency to the backend? The component gets a bit "heavy" as it is now.

Not sure where the best place to put this setting, there is a place where these settings are read and updated today. The downside is that the setting needs to be "prop drilled" all the way down to the components that needs them. But we get rid of the rather large dependency to the backend:

https://github.com/winder/Universal-G-Code-Sender/blob/master/ugs-platform/ugs-platform-plugin-jog/src/main/java/com/willwinder/ugs/nbp/jog/JogTopComponent.java#L133

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the control itself becomes heavy if it depends on the backendAPI and catches all of its events.

If the "decimalPlaces" property changes the behavior of the output to the controller after a G20 has been sent then I feel as if it does belong to the backend as opposed to global settings.

As I am new on this project I am reluctant to create a new structure for data storage/broadcast before I understand the entire system.

Does reducing the scope of the dependency to com.willwinder.universalgcodesender.utils.Settings / SettingChangeListener seem acceptable ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the "decimalPlaces" property changes the behavior of the output to the controller after a G20 has been sent then I feel as if it does belong to the backend as opposed to global settings.

It does not change the output, but rather jog controller doesn't handle jog movements with numbers smaller than three decimals. That should be an easy fix which can be done at a later time.

The bigger problem as I see it is that the controller may be set to report its position in inches (the default is metric) or if the user is using metric then these new options will not work well at all. But since these new options are quite hidden we can ignore that for now.

So the only thing left is the dependency to the backend. I'd prefer if you tweak this to instead use the settings.

I would personally have expanded these places for that:
https://github.com/winder/Universal-G-Code-Sender/blob/master/ugs-platform/ugs-platform-plugin-jog/src/main/java/com/willwinder/ugs/nbp/jog/JogTopComponent.java#L133
https://github.com/winder/Universal-G-Code-Sender/blob/master/ugs-platform/ugs-platform-plugin-dro/src/main/java/com/willwinder/ugs/nbp/dro/panels/MachineStatusPanel.java#L223

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Updated Utils.formatter on a branch
  • Changed dependency on BE to Settings.
  • Leaving Review comment open for now.

Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,44 @@ This file is part of Universal Gcode Sender (UGS).
*/
package com.willwinder.universalgcodesender.uielements.jog;

import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.model.UGSEvent;
import com.willwinder.universalgcodesender.model.events.ControllerStateEvent;
import com.willwinder.universalgcodesender.model.events.SettingChangedEvent;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.text.DefaultFormatter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;


public class StepSizeSpinner extends JSpinner {

double currentValue = 0.0;

public StepSizeSpinner() {
BackendAPI backend = null;

public StepSizeSpinner(BackendAPI backend) {
this.backend = backend;

setModel(new StepSizeSpinnerModel());

// Make the editor fire update events when typing, not only after changing fields
JComponent comp = getEditor();
JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
formatter.setCommitsOnValidEdit(true);
formatter.setCommitsOnValidEdit(true);
backend.addUGSEventListener(this::onBackendEvent);
this.onBackendEvent(new SettingChangedEvent());
}
private void onBackendEvent(UGSEvent event) {
if (event instanceof SettingChangedEvent) {
if (backend != null) {
super.setEditor(new JSpinner.NumberEditor(this, backend.getSettings().getMachineDecimalFormat()));
}
setValue(currentValue);
}
}

@Override
public Double getValue() {
try {
Expand All @@ -46,18 +64,21 @@ public Double getValue() {
setValue(currentValue);
}

BigDecimal bd = new BigDecimal(super.getValue().toString()).setScale(3, RoundingMode.HALF_EVEN);
BigDecimal bd = new BigDecimal(super.getValue().toString()).setScale(getDecimalPlaces(), RoundingMode.HALF_EVEN);
return bd.doubleValue();
}

@Override
public void setValue(Object value) {
double val = Double.parseDouble(value.toString());
BigDecimal bd = new BigDecimal(val).setScale(3, RoundingMode.HALF_EVEN);
BigDecimal bd = new BigDecimal(val).setScale(getDecimalPlaces(), RoundingMode.HALF_EVEN);
currentValue = bd.doubleValue();

super.setValue(currentValue);
}


// todo: Add backend Settings Listener.
// todo: Apply formatting to formatter to enforce decimal places.
public void increaseStep() {
Object nextValue = getNextValue();
if (nextValue != null) {
Expand Down Expand Up @@ -91,4 +112,33 @@ private double bound(double val) {
return val;
}
}

private int getDecimalPlaces() {
if (backend == null) {
return 3;
}
switch (backend.getSettings().getMachineDecimalFormat()) {
case "0" -> {
return 0;
}
case "0.0" -> {
return 1;
}
case "0.00" -> {
return 2;
}
case "0.000" -> {
return 3;
}
case "0.0000" -> {
return 4;
}
case "0.00000" -> {
return 5;
}

default -> throw new AssertionError();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public class Settings {

private FxSettings fxSettings = new FxSettings();

private String machineDecimalFormat = "0.000";
/**
* The GSON deserialization doesn't do anything beyond initialize what's in the json document. Call finalizeInitialization() before using the Settings.
*/
Expand Down Expand Up @@ -188,7 +189,7 @@ public void setSettingChangeListener(SettingChangeListener listener) {
}
}

private void changed() {
public void changed() {
if (listener != null) {
listener.settingChanged();
}
Expand Down Expand Up @@ -602,7 +603,15 @@ public String getLastWorkingDirectory() {
public void setLastWorkingDirectory(String lastWorkingDirectory) {
this.lastWorkingDirectory = lastWorkingDirectory;
}

public String getMachineDecimalFormat() {
return this.machineDecimalFormat;
}

public void setMachineDecimalFormat(String aValue) {
this.machineDecimalFormat=aValue;
}

public static class FileStats {
public Position minCoordinate;
public Position maxCoordinate;
Expand All @@ -620,4 +629,5 @@ public FileStats(Position min, Position max, long num) {
this.numCommands = num;
}
}

}
3 changes: 3 additions & 0 deletions ugs-core/src/resources/MessagesBundle_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ platform.visualizer.popup.presets.left = Left
platform.visualizer.popup.presets.front = Front
platform.plugin.jog.useSeparateStepSize = Use separate step sizes for Z and XY
platform.plugin.jog.showABCStepSize = Show ABC step size
platform.plugin.jog.setMachineDecimalFormatThree=3 Decimal Places
platform.plugin.jog.setMachineDecimalFormatFour=4 Decimal Places
platform.plugin.jog.setMachineDecimalFormatFive=5 Decimal Places
platform.plugin.jog.feedRate = Feed rate
platform.plugin.jog.stepSize = Step size
platform.plugin.jog.stepSizeZ = Step size Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbp.core.actions.ResetZCoordinateToZeroAction;
import com.willwinder.ugs.nbp.dro.FontManager;
import com.willwinder.universalgcodesender.model.Axis;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.uielements.components.RoundedPanel;
import com.willwinder.universalgcodesender.uielements.helpers.MouseClickListener;
import com.willwinder.universalgcodesender.uielements.helpers.ThemeColors;
Expand All @@ -50,13 +51,16 @@ public class AxisPanel extends JPanel {
private static final int RADIUS = 7;
public static final int HIGHLIGHT_TIME = 300;
private final HighlightableLabel axisLabel = new HighlightableLabel();
private final CoordinateLabel workLabel = new CoordinateLabel(0.0);
private final CoordinateLabel machineLabel = new CoordinateLabel(0.0);
private CoordinateLabel workLabel = null;//new CoordinateLabel(0.0);
private CoordinateLabel machineLabel = null;//new CoordinateLabel(0.0);
private final Set<AxisPanelListener> axisPanelListenerList = new HashSet<>();
private transient ScheduledFuture<?> highlightLabelsFuture;

public AxisPanel(Axis axis, FontManager fontManager) {
public AxisPanel(Axis axis, FontManager fontManager, BackendAPI backend) {
super(new MigLayout("fill, inset 0", "[grow, fill]5[50]"));
workLabel = new CoordinateLabel(0.0,backend);
machineLabel = new CoordinateLabel(0.0,backend);

RoundedPanel axisPanel = new RoundedPanel(RADIUS);
axisPanel.setBackground(ThemeColors.VERY_DARK_GREY);
axisPanel.setForeground(ThemeColors.LIGHT_BLUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,28 @@ This file is part of Universal Gcode Sender (UGS).
*/
package com.willwinder.ugs.nbp.dro.panels;

import com.willwinder.universalgcodesender.listeners.UGSEventListener;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.model.UGSEvent;
import com.willwinder.universalgcodesender.model.events.ControllerStateEvent;
import com.willwinder.universalgcodesender.model.events.ControllerStatusEvent;
import com.willwinder.universalgcodesender.model.events.SettingChangedEvent;
import javax.swing.SwingConstants;
import java.text.DecimalFormat;

public class CoordinateLabel extends HighlightableLabel {
public class CoordinateLabel extends HighlightableLabel implements UGSEventListener {

private final DecimalFormat decimalFormatter = new DecimalFormat("0.000");
private DecimalFormat decimalFormatter = new DecimalFormat("0.000");
private double value = 0.0;
private final transient BackendAPI backend;

public CoordinateLabel(double value, BackendAPI backend) {
this.backend = backend;

if (this.backend != null) {
this.backend.addUGSEventListener(this);
}

public CoordinateLabel(double value) {
setValue(value);
setHorizontalAlignment(SwingConstants.RIGHT);
}
Expand All @@ -37,7 +50,16 @@ public double getValue() {

public void setValue(double value) {
this.value = value;
if (this.backend != null) {
String valueToUse = this.backend.getSettings().getMachineDecimalFormat();
decimalFormatter = new DecimalFormat(valueToUse);
}
String textValue = decimalFormatter.format(value);
setText(textValue);
}

@Override
public void UGSEvent(UGSEvent evt) {
setValue(this.value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class MachineStatusPanel extends JPanel implements UGSEventListener, Axis
private final JPanel axisPanel = new JPanel();
private Units units;
private final Map<Axis, AxisPanel> axisPanels = new EnumMap<>(Axis.class);
private final DecimalFormat decimalFormatter = new DecimalFormat("0.000");
private DecimalFormat decimalFormatter = new DecimalFormat("0.000");


public MachineStatusPanel(BackendAPI backend) {
Expand All @@ -102,8 +102,8 @@ public MachineStatusPanel(BackendAPI backend) {
setUnits(Units.MM);
} else {
setUnits(Units.INCH);
}

}
updateControls();
}

Expand Down Expand Up @@ -167,7 +167,7 @@ private void initComponents() {
}

private void initializeAxisPanel(Axis axis) {
AxisPanel panel = new AxisPanel(axis, fontManager);
AxisPanel panel = new AxisPanel(axis, fontManager, backend);
panel.setVisible(axis.isLinear());
panel.setEnabled(false);
axisPanels.put(axis, panel);
Expand Down Expand Up @@ -222,6 +222,11 @@ public void UGSEvent(UGSEvent evt) {
*/
private void updateControls() {
Settings settings = backend.getSettings();

if (this.backend != null) {
decimalFormatter = new DecimalFormat(this.backend.getSettings().getMachineDecimalFormat() );
}

if (!backend.isConnected()) {
axisPanels.forEach((key, value) -> {
value.setEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.uielements.jog.StepSizeSpinner;
import com.willwinder.universalgcodesender.utils.FontUtils;
import com.willwinder.universalgcodesender.listeners.LongPressMouseListener;
import com.willwinder.universalgcodesender.model.BackendAPI;
import net.miginfocom.swing.MigLayout;
import org.openide.util.ImageUtilities;

Expand Down Expand Up @@ -103,8 +104,11 @@ public class JogPanel extends JPanel implements SteppedSizeManager.SteppedSizeCh
private JButton unitToggleButton;
private JButton increaseStepSizeButton;
private JButton decreaseStepSizeButton;

public JogPanel() {

private BackendAPI backend;

public JogPanel(BackendAPI backend) {
this.backend = backend;
createComponents();
initPanels();
initListeners();
Expand All @@ -123,13 +127,13 @@ private void createComponents() {
// Create our buttons
Arrays.asList(JogPanelButtonEnum.values()).forEach(this::createJogButton);
Dimension minimumSize = new Dimension(80, 18);
feedRateSpinner = new StepSizeSpinner();
feedRateSpinner = new StepSizeSpinner(backend);
feedRateSpinner.setMinimumSize(minimumSize);
xyStepSizeSpinner = new StepSizeSpinner();
xyStepSizeSpinner = new StepSizeSpinner(backend);
xyStepSizeSpinner.setMinimumSize(minimumSize);
zStepSizeSpinner = new StepSizeSpinner();
zStepSizeSpinner = new StepSizeSpinner(backend);
zStepSizeSpinner.setMinimumSize(minimumSize);
abcStepSizeSpinner = new StepSizeSpinner();
abcStepSizeSpinner = new StepSizeSpinner(backend);
abcStepSizeSpinner.setMinimumSize(minimumSize);

// todo: could use a number of factory methods here to build similar stuff
Expand Down
Loading