Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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 @@ -50,7 +50,7 @@ This file is part of Universal Gcode Sender (UGS).
* @author wwinder
*/
public class Utils {
public static final NumberFormat formatter = new DecimalFormat("#.###", Localization.dfs);
public static final NumberFormat formatter = new DecimalFormat("#.#####", Localization.dfs);
private static final Logger LOGGER = Logger.getLogger(Utils.class.getSimpleName());
private static final int MAX_WAIT_TIME_FOR_STATUS_REPORT = 1000;

Expand Down
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.getSettings());
zStepSizeSpinner = new StepSizeSpinner(backend.getSettings());

this.jogService = jogService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,45 @@ 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 com.willwinder.universalgcodesender.utils.Settings;
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() {
Settings settings = null;

public StepSizeSpinner(Settings settings) {
this.settings = settings;

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);

this.onBackendEvent(new SettingChangedEvent());
}
public void onBackendEvent(UGSEvent event) {
if (event instanceof SettingChangedEvent) {
if (settings != null) {
super.setEditor(new JSpinner.NumberEditor(this, settings.getMachineDecimalFormat()));
}
setValue(currentValue);
}
}

@Override
public Double getValue() {
try {
Expand All @@ -46,18 +65,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 +113,33 @@ private double bound(double val) {
return val;
}
}

private int getDecimalPlaces() {
if (settings == null) {
return 0;
}
switch (settings.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 @@ -37,6 +37,7 @@ This file is part of Universal Gcode Sender (UGS).
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -141,6 +142,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 @@ -602,7 +604,16 @@ 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;
this.changed();
}

public static class FileStats {
public Position minCoordinate;
public Position maxCoordinate;
Expand All @@ -620,4 +631,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,8 @@ 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 com.willwinder.universalgcodesender.utils.Settings;
import net.miginfocom.swing.MigLayout;
import org.openide.util.ImageUtilities;

Expand Down Expand Up @@ -103,8 +105,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,15 +128,20 @@ 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.getSettings());
feedRateSpinner.setMinimumSize(minimumSize);
xyStepSizeSpinner = new StepSizeSpinner();
xyStepSizeSpinner = new StepSizeSpinner(backend.getSettings());
xyStepSizeSpinner.setMinimumSize(minimumSize);
zStepSizeSpinner = new StepSizeSpinner();
backend.addUGSEventListener(xyStepSizeSpinner::onBackendEvent);

zStepSizeSpinner = new StepSizeSpinner(backend.getSettings());
zStepSizeSpinner.setMinimumSize(minimumSize);
abcStepSizeSpinner = new StepSizeSpinner();
backend.addUGSEventListener(zStepSizeSpinner::onBackendEvent);

abcStepSizeSpinner = new StepSizeSpinner(backend.getSettings());
abcStepSizeSpinner.setMinimumSize(minimumSize);

backend.addUGSEventListener(abcStepSizeSpinner::onBackendEvent);

// todo: could use a number of factory methods here to build similar stuff
feedRateLabel = createSettingLabel(font, Localization.getString("platform.plugin.jog.feedRate"));
feedRateLabel.setMinimumSize(new Dimension(0, 0));
Expand Down
Loading
Loading