From 0010064c690bb92c4ef5f713a03dbf527bde35c9 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Mon, 27 May 2024 07:56:10 +0200 Subject: [PATCH] Fixed selection bug overwriting settings when selecting in the entities tree. Also made the designer settings central and not a part of the design file. --- .../entities/controls/SelectionControl.java | 11 +- .../nbp/designer/entities/cuttable/Group.java | 18 +-- .../entities/selection/SelectionManager.java | 17 +++ .../nbp/designer/gui/DrawingContainer.java | 7 +- .../ugs/nbp/designer/gui/PanelButton.java | 9 +- .../ugs/nbp/designer/gui/ToolButton.java | 80 +++++++++++++ .../nbp/designer/gui/ToolSettingsPanel.java | 26 ++--- .../SelectionSettingsModel.java | 25 ++++ .../SelectionSettingsPanel.java | 39 ++----- .../nbp/designer/gui/tree/EntitiesTree.java | 13 ++- .../nbp/designer/io/ugsd/UgsDesignWriter.java | 55 ++++----- .../ugs/nbp/designer/io/ugsd/v1/DesignV1.java | 15 +-- .../nbp/designer/io/ugsd/v1/SettingsV1.java | 108 ------------------ .../ugs/nbp/designer/logic/Controller.java | 1 - .../ugs/nbp/designer/model/Design.java | 10 -- .../ugs/nbp/designer/model/Settings.java | 5 - .../platform/DesignerTopComponent.java | 10 +- .../designer/platform/SettingsAdapter.java | 71 ++++++++++++ .../src/main/nbm/manifest.mf | 1 + .../ugs/nbp/designer/platform/layer.xml | 13 +++ .../designer/entities/cuttable/GroupTest.java | 22 ++++ .../designer/io/ugsd/UgsDesignReaderTest.java | 45 ++++---- 22 files changed, 341 insertions(+), 260 deletions(-) create mode 100644 ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/ToolButton.java delete mode 100644 ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/v1/SettingsV1.java create mode 100644 ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/SettingsAdapter.java create mode 100644 ugs-platform/ugs-platform-plugin-designer/src/main/resources/com/willwinder/ugs/nbp/designer/platform/layer.xml diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/controls/SelectionControl.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/controls/SelectionControl.java index fe440576e8..e5604afa09 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/controls/SelectionControl.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/controls/SelectionControl.java @@ -74,8 +74,7 @@ public boolean isWithin(Point2D point) { @Override public void onEvent(EntityEvent entityEvent) { - if (entityEvent instanceof MouseEntityEvent) { - MouseEntityEvent mouseEntityEvent = (MouseEntityEvent) entityEvent; + if (entityEvent instanceof MouseEntityEvent mouseEntityEvent) { Point2D mousePosition = mouseEntityEvent.getCurrentMousePosition(); if (mouseEntityEvent.getType() == EventType.MOUSE_PRESSED) { @@ -109,12 +108,11 @@ private void selectIntersection(Shape shape, boolean selectMultiple) { .filter(e -> e != this) .filter(e -> !(e instanceof Control)) .filter(e -> !(e instanceof Cuttable && ((Cuttable) e).isHidden())) + .filter(e -> e != controller.getSelectionManager()) .collect(Collectors.toSet()); if (selectMultiple) { - if (!entitiesIntersecting.isEmpty()) { - entitiesIntersecting.forEach(e -> controller.getSelectionManager().toggleSelection(e)); - } + controller.getSelectionManager().toggleSelection(entitiesIntersecting); } else { controller.getSelectionManager().setSelection(new ArrayList<>(entitiesIntersecting)); } @@ -127,6 +125,7 @@ private void selectOne(Point2D mousePosition, boolean selectMultiple) { .filter(e -> e != this) .filter(e -> !(e instanceof Control)) .filter(e -> !(e instanceof Cuttable && ((Cuttable) e).isHidden())) + .filter(e -> e != controller.getSelectionManager()) .collect(Collectors.toSet()); if (selectMultiple) { @@ -136,7 +135,7 @@ private void selectOne(Point2D mousePosition, boolean selectMultiple) { .sorted(Comparator.comparingDouble(e -> e.getBounds().getWidth() * e.getBounds().getHeight())) .limit(1) .filter(e -> !controller.getSelectionManager().isSelected(e)) - .collect(Collectors.toList()); + .toList(); controller.getSelectionManager().setSelection(selection); } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/cuttable/Group.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/cuttable/Group.java index 1e4d8016a8..2b48399bd9 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/cuttable/Group.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/cuttable/Group.java @@ -22,7 +22,6 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.ugs.nbp.designer.entities.EntityGroup; import com.willwinder.ugs.nbp.designer.entities.EntitySetting; -import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Stream; @@ -202,18 +201,9 @@ public Optional getFirstChild() { @Override public List getSettings() { - return Arrays.asList( - EntitySetting.ANCHOR, - EntitySetting.POSITION_X, - EntitySetting.POSITION_Y, - EntitySetting.WIDTH, - EntitySetting.HEIGHT, - EntitySetting.CUT_TYPE, - EntitySetting.START_DEPTH, - EntitySetting.TARGET_DEPTH, - EntitySetting.SPINDLE_SPEED, - EntitySetting.PASSES, - EntitySetting.FEED_RATE - ); + return getCuttableStream() + .flatMap(cuttable -> cuttable.getSettings().stream()) + .distinct() + .toList(); } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/selection/SelectionManager.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/selection/SelectionManager.java index 18d3421c41..afc7cb9a46 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/selection/SelectionManager.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/entities/selection/SelectionManager.java @@ -120,6 +120,14 @@ public void removeSelection(Entity entity) { fireSelectionEvent(new SelectionEvent()); } + public void removeSelection(List nodes) { + nodes.forEach(e -> { + entityGroup.removeChild(e); + e.removeListener(this); + }); + fireSelectionEvent(new SelectionEvent()); + } + public void addSelectionListener(SelectionListener selectionListener) { this.listeners.add(selectionListener); } @@ -225,6 +233,15 @@ public void toggleSelection(Entity entity) { } } + public void toggleSelection(Set entitiesIntersecting) { + entitiesIntersecting.stream().filter(c -> entityGroup.getChildren().contains(c)).forEach(e -> { + entityGroup.removeChild(e); + e.removeListener(this); + }); + entitiesIntersecting.stream().filter(c -> !entityGroup.getChildren().contains(c)).forEach(entityGroup::addChild); + fireSelectionEvent(new SelectionEvent()); + } + @Override public void onEvent(EntityEvent entityEvent) { notifyEvent(entityEvent); diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/DrawingContainer.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/DrawingContainer.java index da5265f3fe..0378c7ffad 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/DrawingContainer.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/DrawingContainer.java @@ -1,5 +1,5 @@ /* - Copyright 2021 Will Winder + Copyright 2021-2024 Will Winder This file is part of Universal Gcode Sender (UGS). @@ -33,10 +33,10 @@ This file is part of Universal Gcode Sender (UGS). * centered. * * @author Alex Lagerstedt + * @author Joacim Breiler */ public class DrawingContainer extends JPanel implements ComponentListener, MouseWheelListener { - private static final long serialVersionUID = 0; private final transient Controller controller; private JScrollPane scrollPane; private JPanel buttonPanel; @@ -86,10 +86,9 @@ protected void processMouseWheelEvent(MouseWheelEvent e) { buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); buttonPanel.setOpaque(false); - PanelButton toolButton = new PanelButton("Tool", controller.getSettings().getToolDescription()); + ToolButton toolButton = new ToolButton(controller); toolButton.setMinimumSize(new Dimension(60, 40)); toolButton.setMaximumSize(new Dimension(100, 40)); - controller.getSettings().addListener(() -> toolButton.setText(controller.getSettings().getToolDescription())); toolButton.addActionListener(new OpenToolSettingsAction(controller)); buttonPanel.add(toolButton); diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/PanelButton.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/PanelButton.java index 41b72107c2..57277c83f6 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/PanelButton.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/PanelButton.java @@ -29,17 +29,18 @@ public class PanelButton extends JButton { public static final float TITLE_SCALE = 0.6f; public static final float TEXT_SCALE = 1.0f; private final JLabel textLabel; + private final JLabel titleLabel; public PanelButton(String title, String text) { super(); setLayout(new BorderLayout()); setMaximumSize(new Dimension(100, 100)); - JLabel titleLabel = new JLabel(title); + titleLabel = new JLabel(title); Font font = titleLabel.getFont(); font = font.deriveFont(font.getSize() * TITLE_SCALE); titleLabel.setFont(font); - add(titleLabel, BorderLayout.NORTH); + textLabel = new JLabel(text); font = textLabel.getFont(); font = font.deriveFont(font.getSize() * TEXT_SCALE); @@ -51,4 +52,8 @@ public PanelButton(String title, String text) { public void setText(String text) { this.textLabel.setText(text); } + + public void setTitle(String title) { + this.titleLabel.setText(title); + } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/ToolButton.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/ToolButton.java new file mode 100644 index 0000000000..08feb690e3 --- /dev/null +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/ToolButton.java @@ -0,0 +1,80 @@ +/* + Copyright 2024 Will Winder + + This file is part of Universal Gcode Sender (UGS). + + UGS 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. + + UGS 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 UGS. If not, see . + */ +package com.willwinder.ugs.nbp.designer.gui; + +import com.willwinder.ugs.nbp.designer.entities.cuttable.CutType; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Cuttable; +import com.willwinder.ugs.nbp.designer.logic.Controller; +import com.willwinder.universalgcodesender.Utils; +import com.willwinder.universalgcodesender.model.UnitUtils; + +/** + * A button that displays the currently used tool in the design + * + * @author Joacim Breiler + */ +public class ToolButton extends PanelButton { + private final transient Controller controller; + + public ToolButton(Controller controller) { + super("", ""); + this.controller = controller; + controller.getSettings().addListener(this::updateText); + controller.getDrawing().addListener(e -> updateText()); + controller.getDrawing().getRootEntity().addListener(e -> updateText()); + updateText(); + } + + private static boolean isMillOperation(Cuttable c) { + return c.getCutType() == CutType.ON_PATH || c.getCutType() == CutType.CENTER_DRILL || c.getCutType() == CutType.INSIDE_PATH || c.getCutType() == CutType.OUTSIDE_PATH || c.getCutType() == CutType.POCKET; + } + + private static boolean isLaserOperation(Cuttable c) { + return c.getCutType() == CutType.LASER_FILL || c.getCutType() == CutType.LASER_ON_PATH; + } + + private void updateText() { + setTitle("Tool"); + boolean hasLaserOperations = controller.getDrawing().getEntities().stream() + .filter(Cuttable.class::isInstance) + .map(e -> (Cuttable) e) + .anyMatch(ToolButton::isLaserOperation); + + boolean hasMillOperations = controller.getDrawing().getEntities().stream() + .filter(Cuttable.class::isInstance) + .map(e -> (Cuttable) e) + .anyMatch(ToolButton::isMillOperation); + + + boolean hasMixedOperations = hasLaserOperations && hasMillOperations; + + if (hasMixedOperations) { + setText("Mixed"); + } else if (hasLaserOperations) { + setText("Laser"); + } else { + setText(getMillToolDescription()); + } + } + + public String getMillToolDescription() { + double scale = UnitUtils.scaleUnits(UnitUtils.Units.MM, controller.getSettings().getPreferredUnits()); + return Utils.formatter.format(controller.getSettings().getToolDiameter() * scale) + " " + controller.getSettings().getPreferredUnits().abbreviation; + } +} diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/ToolSettingsPanel.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/ToolSettingsPanel.java index 22fe4eecf2..b5760ba4a6 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/ToolSettingsPanel.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/ToolSettingsPanel.java @@ -1,5 +1,5 @@ /* - Copyright 2021 Will Winder + Copyright 2021-2024 Will Winder This file is part of Universal Gcode Sender (UGS). @@ -37,6 +37,7 @@ This file is part of Universal Gcode Sender (UGS). * @author Joacim Breiler */ public class ToolSettingsPanel extends JPanel { + public static final String TOOL_FIELD_CONSTRAINT = "grow, wrap"; private final transient Controller controller; private JTextField toolDiameter; private JTextField feedSpeed; @@ -60,44 +61,43 @@ private void initComponents() { add(new JLabel("Tool diameter" )); toolDiameter = new TextFieldWithUnit(TextFieldUnit.MM, 3, controller.getSettings().getToolDiameter()); - add(toolDiameter, "grow, wrap" ); + add(toolDiameter, TOOL_FIELD_CONSTRAINT); add(new JLabel("Feed speed" )); feedSpeed = new TextFieldWithUnit(TextFieldUnit.MM_PER_MINUTE, 0, controller.getSettings().getFeedSpeed()); - add(feedSpeed, "grow, wrap" ); + add(feedSpeed, TOOL_FIELD_CONSTRAINT); add(new JLabel("Plunge speed" )); plungeSpeed = new TextFieldWithUnit(TextFieldUnit.MM_PER_MINUTE, 0, controller.getSettings().getPlungeSpeed()); - add(plungeSpeed, "grow, wrap" ); + add(plungeSpeed, TOOL_FIELD_CONSTRAINT); add(new JLabel("Depth per pass" )); depthPerPass = new TextFieldWithUnit(TextFieldUnit.MM, 2, controller.getSettings().getDepthPerPass()); - add(depthPerPass, "grow, wrap" ); + add(depthPerPass, TOOL_FIELD_CONSTRAINT); add(new JLabel("Step over" )); stepOver = new TextFieldWithUnit(TextFieldUnit.PERCENT, 2, controller.getSettings().getToolStepOver()); - add(stepOver, "grow, wrap" ); + add(stepOver, TOOL_FIELD_CONSTRAINT); add(new JLabel("Safe height" )); safeHeight = new TextFieldWithUnit(TextFieldUnit.MM, 2, controller.getSettings().getSafeHeight()); - add(safeHeight, "grow, wrap" ); + add(safeHeight, TOOL_FIELD_CONSTRAINT); - add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap" ); + add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap, hmin 2" ); add(new JLabel("Spindle speed" )); spindleSpeed = new TextFieldWithUnit(TextFieldUnit.ROTATIONS_PER_MINUTE, 0, controller.getSettings().getSpindleSpeed()); - add(spindleSpeed, "grow, wrap" ); + add(spindleSpeed, TOOL_FIELD_CONSTRAINT); - add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap" ); add(new JLabel("Max spindle speed" )); maxSpindleSpeed = new TextFieldWithUnit(TextFieldUnit.ROTATIONS_PER_MINUTE, 0, controller.getSettings().getMaxSpindleSpeed()); - add(maxSpindleSpeed, "grow, wrap" ); + add(maxSpindleSpeed, TOOL_FIELD_CONSTRAINT); - add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap" ); + add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap, hmin 2" ); add(new JLabel("Laser diameter" )); laserDiameter = new TextFieldWithUnit(TextFieldUnit.MM, 3, controller.getSettings().getLaserDiameter()); - add(laserDiameter, "grow, wrap" ); + add(laserDiameter, TOOL_FIELD_CONSTRAINT); } public double getToolDiameter() { diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/selectionsettings/SelectionSettingsModel.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/selectionsettings/SelectionSettingsModel.java index 4926fa1fd6..edcbbf2749 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/selectionsettings/SelectionSettingsModel.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/selectionsettings/SelectionSettingsModel.java @@ -22,6 +22,8 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.ugs.nbp.designer.entities.Anchor; import com.willwinder.ugs.nbp.designer.entities.EntitySetting; import com.willwinder.ugs.nbp.designer.entities.cuttable.CutType; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Group; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Text; import java.awt.Font; import java.io.Serializable; @@ -268,4 +270,27 @@ public void setLockRatio(boolean lockRatio) { notifyListeners(EntitySetting.LOCK_RATIO); } } + + public void updateFromEntity(Group selectionGroup) { + if(selectionGroup.getChildren().size() > 1) { + return; + } + boolean isTextCuttable = selectionGroup.getChildren().get(0) instanceof Text; + if (isTextCuttable) { + Text textEntity = (Text) selectionGroup.getChildren().get(0); + setText(textEntity.getText()); + setFontFamily(textEntity.getFontFamily()); + } + setPositionX(selectionGroup.getPosition(getAnchor()).getX()); + setPositionY(selectionGroup.getPosition(getAnchor()).getY()); + setWidth(selectionGroup.getSize().getWidth()); + setHeight(selectionGroup.getSize().getHeight()); + setRotation(selectionGroup.getRotation()); + setStartDepth(selectionGroup.getStartDepth()); + setTargetDepth(selectionGroup.getTargetDepth()); + setCutType(selectionGroup.getCutType()); + setSpindleSpeed(selectionGroup.getSpindleSpeed()); + setPasses(selectionGroup.getPasses()); + setFeedRate(selectionGroup.getFeedRate()); + } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/selectionsettings/SelectionSettingsPanel.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/selectionsettings/SelectionSettingsPanel.java index cb2d9f5057..69494e7c5b 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/selectionsettings/SelectionSettingsPanel.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/selectionsettings/SelectionSettingsPanel.java @@ -52,6 +52,7 @@ This file is part of Universal Gcode Sender (UGS). * @author Joacim Breiler */ public class SelectionSettingsPanel extends JPanel implements SelectionListener, EntityListener, SelectionSettingsModelListener { + private static final String FIELD_CONSTRAINTS = "grow, wrap"; private final SelectionSettingsModel model = new SelectionSettingsModel(); private final transient FieldEventDispatcher fieldEventDispatcher; private transient Controller controller; @@ -134,7 +135,7 @@ private JLabel createAndAddLabel(EntitySetting entitySetting) { private TextFieldWithUnit createAndAddField(EntitySetting setting, TextFieldUnit units, boolean wrap) { TextFieldWithUnit field = new TextFieldWithUnit(units, 4, 0); fieldEventDispatcher.registerListener(setting, field); - add(field, wrap ? "grow, wrap" : "grow" ); + add(field, wrap ? FIELD_CONSTRAINTS : "grow" ); return field; } @@ -144,13 +145,13 @@ private void addCutFields() { JLabel cutTypeLabel = new JLabel("Cut type", SwingConstants.RIGHT); add(cutTypeLabel, "grow" ); - add(cutTypeComboBox, "grow, wrap" ); + add(cutTypeComboBox, FIELD_CONSTRAINTS); feedRateLabel = new JLabel("Feed rate", SwingConstants.RIGHT); add(feedRateLabel, "grow" ); feedRateSpinner = new UnitSpinner(50, TextFieldUnit.MM_PER_MINUTE, 50d, 10000d, 10d); - add(feedRateSpinner, "grow, wrap" ); + add(feedRateSpinner, FIELD_CONSTRAINTS); fieldEventDispatcher.registerListener(EntitySetting.FEED_RATE, feedRateSpinner); spindleSpeedLabel = new JLabel("Power", SwingConstants.RIGHT); @@ -162,7 +163,7 @@ private void addCutFields() { spindleSpeedSlider.setMinorTickSpacing(5); spindleSpeedSlider.setMajorTickSpacing(20); - add(spindleSpeedSlider, "grow, wrap" ); + add(spindleSpeedSlider, FIELD_CONSTRAINTS); fieldEventDispatcher.registerListener(EntitySetting.SPINDLE_SPEED, spindleSpeedSlider); laserPassesLabel = new JLabel("Passes", SwingConstants.RIGHT); @@ -174,7 +175,7 @@ private void addCutFields() { passesSlider.setMinorTickSpacing(1); passesSlider.setMajorTickSpacing(5); - add(passesSlider, "grow, wrap" ); + add(passesSlider, FIELD_CONSTRAINTS); fieldEventDispatcher.registerListener(EntitySetting.PASSES, passesSlider); startDepthLabel = new JLabel("Start depth", SwingConstants.RIGHT); @@ -183,7 +184,7 @@ private void addCutFields() { startDepthSpinner = new UnitSpinner(0, TextFieldUnit.MM, null, null, 0.1d); startDepthSpinner.setPreferredSize(startDepthSpinner.getPreferredSize()); fieldEventDispatcher.registerListener(EntitySetting.START_DEPTH, startDepthSpinner); - add(startDepthSpinner, "grow, wrap" ); + add(startDepthSpinner, FIELD_CONSTRAINTS); targetDepthLabel = new JLabel("Target depth", SwingConstants.RIGHT); add(targetDepthLabel, "grow" ); @@ -192,7 +193,7 @@ private void addCutFields() { targetDepthSpinner.setPreferredSize(targetDepthSpinner.getPreferredSize()); fieldEventDispatcher.registerListener(EntitySetting.TARGET_DEPTH, targetDepthSpinner); - add(targetDepthSpinner, "grow, wrap" ); + add(targetDepthSpinner, FIELD_CONSTRAINTS); setEnabled(false); } @@ -210,7 +211,7 @@ private void addTextSettingFields() { textTextField = new JTextField(); textTextField.setVisible(false); fieldEventDispatcher.registerListener(EntitySetting.TEXT, textTextField); - add(textTextField, "grow, wrap" ); + add(textTextField, FIELD_CONSTRAINTS); fontLabel = new JLabel("Font", SwingConstants.RIGHT); fontLabel.setVisible(false); @@ -219,7 +220,7 @@ private void addTextSettingFields() { fontDropDown = new FontCombo(); fieldEventDispatcher.registerListener(EntitySetting.FONT_FAMILY, fontDropDown); fontDropDown.setVisible(false); - add(fontDropDown, "grow, wrap" ); + add(fontDropDown, FIELD_CONSTRAINTS); fontSeparator = new JSeparator(SwingConstants.HORIZONTAL); fontSeparator.setVisible(false); @@ -241,7 +242,7 @@ public void setEnabled(boolean enabled) { @Override public void onSelectionEvent(SelectionEvent selectionEvent) { - // Temporarily disable the field event dispatcher so that it won't triggers updates on select + // Temporarily disable the field event dispatcher so that it won't trigger updates on select fieldEventDispatcher.setEnabled(false); onEvent(new EntityEvent(controller.getSelectionManager(), EventType.SELECTED)); fieldEventDispatcher.setEnabled(true); @@ -257,23 +258,7 @@ public void onEvent(EntityEvent entityEvent) { } setEnabled(true); - boolean isTextCuttable = selectionGroup.getChildren().get(0) instanceof Text; - if (isTextCuttable) { - Text textEntity = (Text) selectionGroup.getChildren().get(0); - model.setText(textEntity.getText()); - model.setFontFamily(textEntity.getFontFamily()); - } - model.setPositionX(selectionGroup.getPosition(model.getAnchor()).getX()); - model.setPositionY(selectionGroup.getPosition(model.getAnchor()).getY()); - model.setWidth(selectionGroup.getSize().getWidth()); - model.setHeight(selectionGroup.getSize().getHeight()); - model.setRotation(selectionGroup.getRotation()); - model.setStartDepth(selectionGroup.getStartDepth()); - model.setTargetDepth(selectionGroup.getTargetDepth()); - model.setCutType(selectionGroup.getCutType()); - model.setSpindleSpeed(selectionGroup.getSpindleSpeed()); - model.setPasses(selectionGroup.getPasses()); - model.setFeedRate(selectionGroup.getFeedRate()); + model.updateFromEntity(selectionGroup); controller.getDrawing().invalidate(); } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/tree/EntitiesTree.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/tree/EntitiesTree.java index 899af5a33e..ef5fa9420b 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/tree/EntitiesTree.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/tree/EntitiesTree.java @@ -1,5 +1,5 @@ /* - Copyright 2021-2023 Will Winder + Copyright 2021-2024 Will Winder This file is part of Universal Gcode Sender (UGS). @@ -31,6 +31,7 @@ This file is part of Universal Gcode Sender (UGS). import javax.swing.event.TreeSelectionListener; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -75,14 +76,20 @@ public void release() { @Override public void valueChanged(TreeSelectionEvent e) { + List nodesToAdd = new ArrayList<>(); + List nodesToRemove = new ArrayList<>(); + Arrays.asList(e.getPaths()).forEach(p -> { Entity entity = (Entity) p.getLastPathComponent(); if (e.isAddedPath(p) && !controller.getSelectionManager().isSelected(entity)) { - controller.getSelectionManager().addSelection(entity); + nodesToAdd.add(entity); } else if (!e.isAddedPath(p) && controller.getSelectionManager().isSelected(entity)) { - controller.getSelectionManager().removeSelection(entity); + nodesToRemove.add(entity); } }); + + controller.getSelectionManager().removeSelection(nodesToRemove); + controller.getSelectionManager().addSelection(nodesToAdd); } @Override diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/UgsDesignWriter.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/UgsDesignWriter.java index 03cf8a1d99..3000f8451e 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/UgsDesignWriter.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/UgsDesignWriter.java @@ -4,18 +4,32 @@ import com.google.gson.GsonBuilder; import com.willwinder.ugs.nbp.designer.entities.Entity; import com.willwinder.ugs.nbp.designer.entities.EntityGroup; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Cuttable; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Ellipse; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Path; import com.willwinder.ugs.nbp.designer.entities.cuttable.Point; import com.willwinder.ugs.nbp.designer.entities.cuttable.Rectangle; -import com.willwinder.ugs.nbp.designer.entities.cuttable.*; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Text; import com.willwinder.ugs.nbp.designer.io.AffineTransformSerializer; import com.willwinder.ugs.nbp.designer.io.DesignWriter; import com.willwinder.ugs.nbp.designer.io.DesignWriterException; -import com.willwinder.ugs.nbp.designer.io.ugsd.v1.*; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.CutTypeV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.CuttableEntityV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.DesignV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.EntityEllipseV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.EntityGroupV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.EntityPathSegmentV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.EntityPathTypeV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.EntityPathV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.EntityPointV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.EntityRectangleV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.EntityTextV1; +import com.willwinder.ugs.nbp.designer.io.ugsd.v1.EntityV1; import com.willwinder.ugs.nbp.designer.logic.Controller; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; -import java.awt.*; +import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.PathIterator; import java.io.File; @@ -26,7 +40,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; /** * Writes the design to a stream or file in the latest design format. @@ -48,10 +61,9 @@ public void write(OutputStream outputStream, Controller controller) { try { Gson gson = createGsonParser(); DesignV1 design = new DesignV1(); - design.setSettings(convertSettings(controller)); EntityGroup rootEntity = controller.getDrawing().getRootEntity(); - design.setEntities(rootEntity.getChildren().stream().map(this::convertToEntity).collect(Collectors.toList())); + design.setEntities(rootEntity.getChildren().stream().map(this::convertToEntity).toList()); IOUtils.write(gson.toJson(design), outputStream, StandardCharsets.UTF_8); } catch (Exception e) { throw new DesignWriterException("Could not write to file", e); @@ -67,8 +79,8 @@ private static Gson createGsonParser() { private EntityV1 convertToEntity(Entity entity) { EntityV1 result; - if (entity instanceof EntityGroup) { - result = parseGroup((EntityGroup) entity); + if (entity instanceof EntityGroup entityGroup) { + result = parseGroup(entityGroup); } else if (entity instanceof Rectangle) { result = parseRectangle(entity); } else if (entity instanceof Ellipse) { @@ -77,8 +89,8 @@ private EntityV1 convertToEntity(Entity entity) { result = parsePath(entity); } else if (entity instanceof Point) { result = parsePoint(entity); - } else if (entity instanceof Text) { - result = parseText((Text) entity); + } else if (entity instanceof Text text) { + result = parseText(text); } else { return null; } @@ -87,9 +99,7 @@ private EntityV1 convertToEntity(Entity entity) { result.setName(entity.getName()); } - if (entity instanceof Cuttable && result instanceof CuttableEntityV1) { - Cuttable cuttable = (Cuttable) entity; - CuttableEntityV1 cuttableEntity = ((CuttableEntityV1) result); + if (entity instanceof Cuttable cuttable && result instanceof CuttableEntityV1 cuttableEntity) { cuttableEntity.setStartDepth(cuttable.getStartDepth()); cuttableEntity.setCutDepth(cuttable.getTargetDepth()); cuttableEntity.setCutType(CutTypeV1.fromCutType(cuttable.getCutType())); @@ -140,7 +150,7 @@ private EntityV1 parseGroup(EntityGroup entity) { .filter(Cuttable.class::isInstance) .map(Cuttable.class::cast) .map(this::convertToEntity) - .collect(Collectors.toList())); + .toList()); return group; } @@ -158,21 +168,6 @@ private List convertPathToSegments(Entity entity) { return segments; } - private SettingsV1 convertSettings(Controller controller) { - SettingsV1 settings = new SettingsV1(); - settings.setFeedSpeed(controller.getSettings().getFeedSpeed()); - settings.setDepthPerPass(controller.getSettings().getDepthPerPass()); - settings.setStockThickness(controller.getSettings().getStockThickness()); - settings.setPlungeSpeed(controller.getSettings().getPlungeSpeed()); - settings.setPreferredUnits(controller.getSettings().getPreferredUnits()); - settings.setSafeHeight(controller.getSettings().getSafeHeight()); - settings.setToolDiameter(controller.getSettings().getToolDiameter()); - settings.setToolStepOver(controller.getSettings().getToolStepOver()); - settings.setSpindleSpeed(controller.getSettings().getSpindleSpeed()); - settings.setLaserDiameter(controller.getSettings().getLaserDiameter()); - return settings; - } - private EntityPathSegmentV1 convertCoordinatesToPathSegment(int segmentType, double[] coordinates) { EntityPathSegmentV1 segment = new EntityPathSegmentV1(); List coordinateList = new ArrayList<>(); @@ -203,6 +198,6 @@ public String serialize(List entities) { Gson gson = createGsonParser(); return gson.toJson(entities.stream() .map(this::convertToEntity) - .collect(Collectors.toList())); + .toList()); } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/v1/DesignV1.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/v1/DesignV1.java index 64eb345e1c..f04e50cef4 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/v1/DesignV1.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/v1/DesignV1.java @@ -1,19 +1,17 @@ package com.willwinder.ugs.nbp.designer.io.ugsd.v1; import com.willwinder.ugs.nbp.designer.entities.Entity; -import com.willwinder.ugs.nbp.designer.model.Design; import com.willwinder.ugs.nbp.designer.io.ugsd.common.UgsDesign; +import com.willwinder.ugs.nbp.designer.model.Design; import java.io.Serializable; import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; public class DesignV1 extends UgsDesign implements Serializable { public static final String VERSION = "1"; - private SettingsV1 settings = new SettingsV1(); private List entities = Collections.emptyList(); public DesignV1() { @@ -35,18 +33,9 @@ public Design toInternal() { List internalEntities = getEntities().stream() .filter(Objects::nonNull) .map(EntityV1::toInternal) - .collect(Collectors.toList()); + .toList(); design.setEntities(internalEntities); } - design.setSettings(settings.toInternal()); return design; } - - public SettingsV1 getSettings() { - return settings; - } - - public void setSettings(SettingsV1 settings) { - this.settings = settings; - } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/v1/SettingsV1.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/v1/SettingsV1.java deleted file mode 100644 index 43480b2ffc..0000000000 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/io/ugsd/v1/SettingsV1.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.willwinder.ugs.nbp.designer.io.ugsd.v1; - -import com.willwinder.ugs.nbp.designer.model.Settings; -import com.willwinder.universalgcodesender.model.UnitUtils; - -import java.io.Serializable; - -public class SettingsV1 implements Serializable { - private int feedSpeed = 1000; - private int plungeSpeed = 400; - private double toolDiameter = 3d; - private double stockThickness = 20; - private double safeHeight = 10; - private UnitUtils.Units preferredUnits = UnitUtils.Units.MM; - private double toolStepOver = 0.3; - private double depthPerPass = 1; - private double spindleSpeed = 0; - private double laserDiameter = 0.2; - - public int getPlungeSpeed() { - return plungeSpeed; - } - - public void setPlungeSpeed(int plungeSpeed) { - this.plungeSpeed = plungeSpeed; - } - - public double getToolDiameter() { - return toolDiameter; - } - - public void setToolDiameter(double toolDiameter) { - this.toolDiameter = toolDiameter; - } - - public double getStockThickness() { - return stockThickness; - } - - public void setStockThickness(double stockThickness) { - this.stockThickness = stockThickness; - } - - public double getSafeHeight() { - return safeHeight; - } - - public void setSafeHeight(double safeHeight) { - this.safeHeight = safeHeight; - } - - public UnitUtils.Units getPreferredUnits() { - return preferredUnits; - } - - public void setPreferredUnits(UnitUtils.Units preferredUnits) { - this.preferredUnits = preferredUnits; - } - - public double getToolStepOver() { - return toolStepOver; - } - - public void setToolStepOver(double toolStepOver) { - this.toolStepOver = toolStepOver; - } - - public void setFeedSpeed(int feedSpeed) { - this.feedSpeed = feedSpeed; - } - - public int getFeedSpeed() { - return feedSpeed; - } - - public void setDepthPerPass(double depthPerPass) { - this.depthPerPass = depthPerPass; - } - - public double getDepthPerPass() { - return depthPerPass; - } - - public double getSpindleSpeed() { - return spindleSpeed; - } - - public void setSpindleSpeed(double spindleSpeed) { - this.spindleSpeed = spindleSpeed; - } - public void setLaserDiameter(double laserDiameter) { - this.laserDiameter = laserDiameter; - } - public Settings toInternal() { - Settings settings = new Settings(); - settings.setSafeHeight(safeHeight); - settings.setPreferredUnits(preferredUnits); - settings.setToolStepOver(toolStepOver); - settings.setToolDiameter(toolDiameter); - settings.setStockThickness(stockThickness); - settings.setPlungeSpeed(plungeSpeed); - settings.setDepthPerPass(depthPerPass); - settings.setFeedSpeed(feedSpeed); - settings.setSpindleSpeed(spindleSpeed); - settings.setLaserDiameter(laserDiameter); - return settings; - } -} diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/logic/Controller.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/logic/Controller.java index c6f40aebb0..cc3f3033f4 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/logic/Controller.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/logic/Controller.java @@ -113,7 +113,6 @@ public UndoManager getUndoManager() { public void setDesign(Design design) { newDrawing(); getDrawing().insertEntities(design.getEntities()); - settings.applySettings(design.getSettings()); getDrawing().repaint(); setTool(Tool.SELECT); } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/model/Design.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/model/Design.java index 0c68a51e5f..bd6dc7a921 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/model/Design.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/model/Design.java @@ -29,8 +29,6 @@ This file is part of Universal Gcode Sender (UGS). public class Design { private List entities = Collections.emptyList(); - private Settings settings = new Settings(); - public List getEntities() { return entities; } @@ -38,12 +36,4 @@ public List getEntities() { public void setEntities(List entities) { this.entities = entities; } - - public Settings getSettings() { - return settings; - } - - public void setSettings(Settings settings) { - this.settings = settings; - } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/model/Settings.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/model/Settings.java index b3ab7c2250..cd34089fe3 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/model/Settings.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/model/Settings.java @@ -154,11 +154,6 @@ public String getStockSizeDescription() { return Utils.formatter.format(getStockThickness() * scale) + " " + getPreferredUnits().abbreviation; } - public String getToolDescription() { - double scale = UnitUtils.scaleUnits(UnitUtils.Units.MM, getPreferredUnits()); - return Utils.formatter.format(getToolDiameter() * scale) + " " + getPreferredUnits().abbreviation; - } - public double getDepthPerPass() { return depthPerPass; } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/DesignerTopComponent.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/DesignerTopComponent.java index 14d38133ac..5c8c25ade4 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/DesignerTopComponent.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/DesignerTopComponent.java @@ -65,6 +65,7 @@ public DesignerTopComponent(UgsDataObject dataObject) { this.dataObject = dataObject; backend = CentralLookup.getDefault().lookup(BackendAPI.class); controller = ControllerFactory.getController(); + initSettingsAdapter(); // We need to reuse the drawing container for each loaded file if (drawingContainer == null) { @@ -81,6 +82,14 @@ public DesignerTopComponent(UgsDataObject dataObject) { PlatformUtils.registerActions(getActionMap(), this); } + private void initSettingsAdapter() { + // Load settings from the platform configuration + controller.getSettings().applySettings(SettingsAdapter.loadSettings()); + + // Add a settings listener to sync settings to the platform configuration + controller.getSettings().addListener(() -> SettingsAdapter.saveSettings(controller.getSettings())); + } + private void loadDesign(UgsDataObject dataObject) { try { File file = new File(dataObject.getPrimaryFile().getPath()); @@ -160,7 +169,6 @@ public UndoRedo getUndoRedo() { @Override public void onChanged() { dataObject.setModified(true); - } @Override diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/SettingsAdapter.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/SettingsAdapter.java new file mode 100644 index 0000000000..bf729b115c --- /dev/null +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/SettingsAdapter.java @@ -0,0 +1,71 @@ +package com.willwinder.ugs.nbp.designer.platform; +/* + Copyright 2024 Will Winder + + This file is part of Universal Gcode Sender (UGS). + + UGS 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. + + UGS 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 UGS. If not, see . + */ + +import com.willwinder.ugs.nbp.designer.model.Settings; +import org.openide.util.NbPreferences; + +import java.util.prefs.Preferences; + +/** + * Saves/Loads the designer settings into the netbeans platform. + */ +public class SettingsAdapter { + private static final Preferences preferences = NbPreferences.forModule(DesignerTopComponent.class); + private static final String MAX_SPINDLE_SPEED = "maxSpindleSpeed"; + private static final String LASER_DIAMETER = "laserDiameter"; + private static final String FEED_SPEED = "feedSpeed"; + private static final String PLUNGE_SPEED = "plungeSpeed"; + private static final String TOOL_DIAMETER = "toolDiameter"; + private static final String SAFE_HEIGHT = "safeHeight"; + private static final String TOOL_STEP_OVER = "toolStepOver"; + private static final String DEPTH_PER_PASS = "depthPerPass"; + private static final String SPINDLE_SPEED = "spindleSpeed"; + private static final String STOCK_THICKNESS = "stockThickness"; + + private SettingsAdapter() {} + + public static Settings loadSettings() { + Settings settings = new Settings(); + settings.setMaxSpindleSpeed(preferences.getInt(MAX_SPINDLE_SPEED, 255)); + settings.setLaserDiameter(preferences.getDouble(LASER_DIAMETER, 0.2d)); + settings.setDepthPerPass(preferences.getDouble(DEPTH_PER_PASS, 1d)); + settings.setFeedSpeed(preferences.getInt(FEED_SPEED, 1000)); + settings.setPlungeSpeed(preferences.getInt(PLUNGE_SPEED, 400)); + settings.setToolDiameter(preferences.getDouble(TOOL_DIAMETER, 3d)); + settings.setSafeHeight(preferences.getDouble(SAFE_HEIGHT, 5d)); + settings.setToolStepOver(preferences.getDouble(TOOL_STEP_OVER, 0.3)); + settings.setSpindleSpeed(preferences.getDouble(SPINDLE_SPEED, 0.3)); + settings.setStockThickness(preferences.getDouble(STOCK_THICKNESS, 10)); + return settings; + } + + public static void saveSettings(Settings settings) { + preferences.putInt(MAX_SPINDLE_SPEED, settings.getMaxSpindleSpeed()); + preferences.putDouble(LASER_DIAMETER, settings.getLaserDiameter()); + preferences.putDouble(DEPTH_PER_PASS, settings.getDepthPerPass()); + preferences.putInt(FEED_SPEED, settings.getFeedSpeed()); + preferences.putInt(PLUNGE_SPEED, settings.getPlungeSpeed()); + preferences.putDouble(TOOL_DIAMETER, settings.getToolDiameter()); + preferences.putDouble(SAFE_HEIGHT, settings.getSafeHeight()); + preferences.putDouble(TOOL_STEP_OVER, settings.getToolStepOver()); + preferences.putDouble(SPINDLE_SPEED, settings.getSpindleSpeed()); + preferences.putDouble(STOCK_THICKNESS, settings.getStockThickness()); + } +} diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/nbm/manifest.mf b/ugs-platform/ugs-platform-plugin-designer/src/main/nbm/manifest.mf index c429ccdf7d..ad58aae8b8 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/nbm/manifest.mf +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/nbm/manifest.mf @@ -1,4 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module-Localizing-Bundle: com/willwinder/ugs/nbp/designer/platform/Bundle.properties +OpenIDE-Module-Layer: com/willwinder/ugs/nbp/designer/platform/layer.xml OpenIDE-Module-Specification-Version: ${ugs.modules.specification.version} AutoUpdate-Essential-Module: true diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/resources/com/willwinder/ugs/nbp/designer/platform/layer.xml b/ugs-platform/ugs-platform-plugin-designer/src/main/resources/com/willwinder/ugs/nbp/designer/platform/layer.xml new file mode 100644 index 0000000000..10276dd292 --- /dev/null +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/resources/com/willwinder/ugs/nbp/designer/platform/layer.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/ugs-platform/ugs-platform-plugin-designer/src/test/java/com/willwinder/ugs/nbp/designer/entities/cuttable/GroupTest.java b/ugs-platform/ugs-platform-plugin-designer/src/test/java/com/willwinder/ugs/nbp/designer/entities/cuttable/GroupTest.java index 8b163f9681..32c759ce4d 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/test/java/com/willwinder/ugs/nbp/designer/entities/cuttable/GroupTest.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/test/java/com/willwinder/ugs/nbp/designer/entities/cuttable/GroupTest.java @@ -1,8 +1,11 @@ package com.willwinder.ugs.nbp.designer.entities.cuttable; +import com.willwinder.ugs.nbp.designer.entities.EntitySetting; import static org.junit.Assert.assertEquals; import org.junit.Test; +import java.util.List; + public class GroupTest { @Test public void setLaserPowerShoouldSetTheLaserPowerOnAllChildren() { @@ -29,4 +32,23 @@ public void getLaserPowerShouldGetTheHighestValue() { assertEquals(11, group.getSpindleSpeed(), 0.1); } + + @Test + public void getEntitySettingsShouldReturnACombinedListOfSettings() { + Point point1 = new Point(); + Point point2 = new Point(); + + Group group = new Group(); + assertEquals(List.of(), group.getSettings()); + + group.addChild(point1); + assertEquals(List.of(EntitySetting.POSITION_X, EntitySetting.POSITION_Y, EntitySetting.CUT_TYPE, EntitySetting.START_DEPTH, EntitySetting.TARGET_DEPTH), group.getSettings()); + + group.addChild(point2); + assertEquals(List.of(EntitySetting.POSITION_X, EntitySetting.POSITION_Y, EntitySetting.CUT_TYPE, EntitySetting.START_DEPTH, EntitySetting.TARGET_DEPTH), group.getSettings()); + + Rectangle rectangle = new Rectangle(); + group.addChild(rectangle); + assertEquals(List.of(EntitySetting.POSITION_X, EntitySetting.POSITION_Y, EntitySetting.CUT_TYPE, EntitySetting.START_DEPTH, EntitySetting.TARGET_DEPTH, EntitySetting.ANCHOR, EntitySetting.WIDTH, EntitySetting.HEIGHT, EntitySetting.SPINDLE_SPEED, EntitySetting.PASSES, EntitySetting.FEED_RATE), group.getSettings()); + } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/test/java/com/willwinder/ugs/nbp/designer/io/ugsd/UgsDesignReaderTest.java b/ugs-platform/ugs-platform-plugin-designer/src/test/java/com/willwinder/ugs/nbp/designer/io/ugsd/UgsDesignReaderTest.java index d8061b1171..072cedcd42 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/test/java/com/willwinder/ugs/nbp/designer/io/ugsd/UgsDesignReaderTest.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/test/java/com/willwinder/ugs/nbp/designer/io/ugsd/UgsDesignReaderTest.java @@ -2,27 +2,35 @@ import com.willwinder.ugs.nbp.designer.entities.Entity; import com.willwinder.ugs.nbp.designer.entities.EntityGroup; -import com.willwinder.ugs.nbp.designer.entities.cuttable.*; +import com.willwinder.ugs.nbp.designer.entities.cuttable.CutType; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Cuttable; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Ellipse; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Group; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Path; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Point; +import com.willwinder.ugs.nbp.designer.entities.cuttable.Rectangle; import com.willwinder.ugs.nbp.designer.gui.Drawing; import com.willwinder.ugs.nbp.designer.logic.Controller; import com.willwinder.ugs.nbp.designer.model.Design; import com.willwinder.ugs.nbp.designer.model.Settings; import com.willwinder.ugs.nbp.designer.model.Size; -import com.willwinder.universalgcodesender.model.UnitUtils; import org.apache.commons.io.IOUtils; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import static org.mockito.Mockito.when; import org.mockito.MockitoAnnotations; import java.awt.geom.Point2D; import java.io.ByteArrayOutputStream; +import java.nio.charset.Charset; import java.util.List; import java.util.Optional; -import static org.junit.Assert.*; -import static org.mockito.Mockito.when; - public class UgsDesignReaderTest { @Mock @@ -39,35 +47,26 @@ public void setUp() { @Test public void readEmptyFileShouldReturnEmptyDesign() { UgsDesignReader reader = new UgsDesignReader(); - Optional design = reader.read(IOUtils.toInputStream("")); + Optional design = reader.read(IOUtils.toInputStream("", Charset.defaultCharset())); assertFalse(design.isPresent()); } @Test(expected = RuntimeException.class) public void readFaultyFormatShouldThrowException() { UgsDesignReader reader = new UgsDesignReader(); - reader.read(IOUtils.toInputStream("{}")); + reader.read(IOUtils.toInputStream("{}", Charset.defaultCharset())); } @Test(expected = RuntimeException.class) public void readFaultyVersionShouldThrowException() { UgsDesignReader reader = new UgsDesignReader(); - reader.read(IOUtils.toInputStream("{\"version\":1000}")); + reader.read(IOUtils.toInputStream("{\"version\":1000}", Charset.defaultCharset())); } @Test public void readEmptyDesignFileShouldReturnDesign() { UgsDesignReader reader = new UgsDesignReader(); - Design design = reader.read(IOUtils.toInputStream("{\"version\":\"1\"}")).get(); - - assertNotNull(design.getSettings()); - assertEquals(20, design.getSettings().getStockThickness(), 0.1); - assertEquals(3, design.getSettings().getToolDiameter(), 0.1); - assertEquals(1.0, design.getSettings().getDepthPerPass(), 0.1); - assertEquals(10, design.getSettings().getSafeHeight(), 0.1); - assertEquals(0.3, design.getSettings().getToolStepOver(), 0.1); - assertEquals(UnitUtils.Units.MM, design.getSettings().getPreferredUnits()); - assertEquals(1000, design.getSettings().getFeedSpeed()); + Design design = reader.read(IOUtils.toInputStream("{\"version\":\"1\"}", Charset.defaultCharset())).get(); assertNotNull(design.getEntities()); assertEquals(0, design.getEntities().size()); @@ -86,7 +85,7 @@ public void readDesignWithRectangle() { String data = convertEntityToString(entity); UgsDesignReader reader = new UgsDesignReader(); - Design design = reader.read(IOUtils.toInputStream(data)).get(); + Design design = reader.read(IOUtils.toInputStream(data, Charset.defaultCharset())).get(); assertEquals(1, design.getEntities().size()); Cuttable readEntity = (Cuttable) design.getEntities().get(0); @@ -106,7 +105,7 @@ public void readDesignWithHiddenObject() { String data = convertEntityToString(entity); UgsDesignReader reader = new UgsDesignReader(); - Design design = reader.read(IOUtils.toInputStream(data)).get(); + Design design = reader.read(IOUtils.toInputStream(data, Charset.defaultCharset())).get(); assertEquals(1, design.getEntities().size()); Cuttable readEntity = (Cuttable) design.getEntities().get(0); @@ -125,7 +124,7 @@ public void readDesignWithEllipse() { String data = convertEntityToString(entity); UgsDesignReader reader = new UgsDesignReader(); - Design design = reader.read(IOUtils.toInputStream(data)).get(); + Design design = reader.read(IOUtils.toInputStream(data, Charset.defaultCharset())).get(); Cuttable readEntity = (Cuttable) design.getEntities().get(0); assertTrue(readEntity instanceof Ellipse); @@ -160,7 +159,7 @@ public void readDesignWithPath() { String data = convertEntityToString(entity); UgsDesignReader reader = new UgsDesignReader(); - Design design = reader.read(IOUtils.toInputStream(data)).get(); + Design design = reader.read(IOUtils.toInputStream(data, Charset.defaultCharset())).get(); Cuttable readEntity = (Cuttable) design.getEntities().get(0); assertTrue(readEntity instanceof Path); @@ -181,7 +180,7 @@ public void readDesignWithGroup() { String data = convertEntityToString(entity); UgsDesignReader reader = new UgsDesignReader(); - Design design = reader.read(IOUtils.toInputStream(data)).get(); + Design design = reader.read(IOUtils.toInputStream(data, Charset.defaultCharset())).get(); Cuttable readEntity = (Cuttable) design.getEntities().get(0); assertTrue(readEntity instanceof Group);