diff --git a/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java b/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java index 3e991c5de0..20f8ca9ae3 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java @@ -22,7 +22,6 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.universalgcodesender.model.BackendAPI; import com.willwinder.universalgcodesender.model.Position; import com.willwinder.universalgcodesender.services.KeyboardService; -import com.willwinder.universalgcodesender.utils.GUIHelpers; import net.miginfocom.swing.MigLayout; import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.StringUtils; @@ -32,13 +31,9 @@ This file is part of Universal Gcode Sender (UGS). import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.KeyStroke; -import java.awt.AWTException; -import java.awt.Robot; -import java.awt.event.InputEvent; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Future; -import java.util.concurrent.atomic.AtomicReference; + import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -58,7 +53,7 @@ public class MacroHelper { private static final Pattern WORK_X = Pattern.compile("\\{work_x}"); private static final Pattern WORK_Y = Pattern.compile("\\{work_y}"); private static final Pattern WORK_Z = Pattern.compile("\\{work_z}"); - private static final Pattern PROMPT_REGEX = Pattern.compile("\\{prompt\\|([^}]+)}"); + private static final Pattern PROMPT_REGEX = Pattern.compile("\\{prompt\\|([^}|]+)\\|?([^}]+)?}"); private static final Pattern KEYPRESS_REGEX = Pattern.compile("\\{keypress\\|([^}]+)}"); /** @@ -107,6 +102,8 @@ public static void executeCustomGcode(final String str, BackendAPI backend) thro * {work_y} - The work Y location * {work_z} - The work Z location * {prompt|name} - Prompt the user for a value named 'name'. + * {prompt|name|default} - Prompt the user for a value named 'name' with the default value 'default'. + * {name} - Reuse the value of a previous prompt e.g. X{prompt|name} Y{name}. * {keypress|keys} - Dispatch keyboard press events on the host system. Keys are defined using AWT format, see {@link KeyStroke#getKeyStroke(String)} * * @param str @@ -157,18 +154,19 @@ private static String parseKeyPress(String command) { private static String parsePrompts(String command) { // Prompt for additional substitutions Matcher m = PROMPT_REGEX.matcher(command); - List prompts = new ArrayList<>(); + List prompts = new ArrayList<>(); + while (m.find()) { - prompts.add(m.group(1)); + prompts.add(new Prompt(m.group(1), m.groupCount() > 1 ? m.group(2) : null)); } if (prompts.size() > 0) { List fields = new ArrayList<>(); JPanel myPanel = new JPanel(); myPanel.setLayout(new MigLayout("wrap 2, width 200")); - for (String s : prompts) { - JTextField field = new JTextField(); - myPanel.add(new JLabel(s + ":")); + for (Prompt s : prompts) { + JTextField field = new JTextField(s.defaultValue); + myPanel.add(new JLabel(s.prompt + ":")); myPanel.add(field, "growx, pushx"); fields.add(field); } @@ -180,8 +178,8 @@ private static String parsePrompts(String command) { if (result == JOptionPane.OK_OPTION) { for (int i = 0; i < prompts.size(); i++) { - command = command.replace("{prompt|" + prompts.get(i) + "}", fields.get(i).getText()); - command = command.replace("{" + prompts.get(i) + "}", fields.get(i).getText()); // for reusing values + command = command.replace(prompts.get(i).toPlaceholder(), fields.get(i).getText()); + command = command.replace(prompts.get(i).toValuePlaceholder(), fields.get(i).getText()); // for reusing values } } else { command = ""; diff --git a/ugs-core/src/com/willwinder/universalgcodesender/Prompt.java b/ugs-core/src/com/willwinder/universalgcodesender/Prompt.java new file mode 100644 index 0000000000..1bf0d0f716 --- /dev/null +++ b/ugs-core/src/com/willwinder/universalgcodesender/Prompt.java @@ -0,0 +1,19 @@ +package com.willwinder.universalgcodesender; + +public class Prompt { + public String prompt; + public String defaultValue; + + public Prompt(String prompt, String defaultValue) { + this.prompt = prompt; + this.defaultValue = defaultValue; + } + + public String toPlaceholder() { + return "{prompt|"+this.prompt+(this.defaultValue == null ? "" : "|"+this.defaultValue)+"}"; + } + + public String toValuePlaceholder() { + return "{"+this.prompt+"}"; + } +} diff --git a/ugs-core/src/resources/MessagesBundle_en_US.properties b/ugs-core/src/resources/MessagesBundle_en_US.properties index 75924e014a..5e0ece2d84 100644 --- a/ugs-core/src/resources/MessagesBundle_en_US.properties +++ b/ugs-core/src/resources/MessagesBundle_en_US.properties @@ -116,7 +116,7 @@ mainWindow.swing.softResetMachineControl = Soft Reset mainWindow.swing.stepSizeLabel = XY Step size\: mainWindow.swing.visualizeButton = Visualize mainWindow.swing.workPositionLabel = Work Position\: -mainWindow.swing.macroInstructions = Each box can contain a series of GCode commands seperated by ';'.
To execute the command, click the button to the left of the text.

Interactive substitutions can be made with\:
  • {machine_x} - The machine X location
  • {machine_y} - The machine Y location
  • {machine_z} - The machine Z location
  • {work_x} - The work X location
  • {work_y} - The work Y location
  • {work_z} - The work Z location
  • {prompt|name} - Prompt the user for a value named 'name'.
  • +mainWindow.swing.macroInstructions = Each box can contain a series of GCode commands seperated by ';'.
    To execute the command, click the button to the left of the text.

    Interactive substitutions can be made with\:
    • {machine_x} - The machine X location
    • {machine_y} - The machine Y location
    • {machine_z} - The machine Z location
    • {work_x} - The work X location
    • {work_y} - The work Y location
    • {work_z} - The work Z location
    • {prompt|name} - Prompt the user for a value named 'name'. The value of a prompt
      can be re-used with a {name} placeholder e.g. X{prompt|pos} Y{pos} would resolve to X0 Y0.
    • {prompt|name|default} - Same as a normal prompt, except the third value will be included
      in the popup input by default. e.g. {prompt|pos|0}
    • mainWindow.swing.inchRadioButton = Inches mainWindow.swing.mmRadioButton = Millimeters gcodeTable.command = Command