Search in sources :

Example 1 with SystemStateBean

use of com.willwinder.universalgcodesender.pendantui.SystemStateBean in project Universal-G-Code-Sender by winder.

the class MacroHelper method substituteValues.

protected static String substituteValues(String str, BackendAPI backend) {
    SystemStateBean bean = new SystemStateBean();
    backend.updateSystemState(bean);
    // Early exit if there is nothing to match.
    if (!str.contains("{")) {
        return str;
    }
    // Do simple substitutions
    String command = str;
    command = MACHINE_X.matcher(command).replaceAll(bean.getMachineX());
    command = MACHINE_Y.matcher(command).replaceAll(bean.getMachineY());
    command = MACHINE_Z.matcher(command).replaceAll(bean.getMachineZ());
    command = WORK_X.matcher(command).replaceAll(bean.getWorkX());
    command = WORK_Y.matcher(command).replaceAll(bean.getWorkY());
    command = WORK_Z.matcher(command).replaceAll(bean.getWorkZ());
    // Prompt for additional substitutions
    Matcher m = PROMPT_REGEX.matcher(command);
    List<String> prompts = new ArrayList<>();
    while (m.find()) {
        prompts.add(m.group(1));
    }
    if (prompts.size() > 0) {
        List<JTextField> 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 + ":"));
            myPanel.add(field, "growx, pushx");
            fields.add(field);
        }
        int result = JOptionPane.showConfirmDialog(null, myPanel, Localization.getString("macro.substitution"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            for (int i = 0; i < prompts.size(); i++) {
                command = command.replace("{prompt|" + prompts.get(i) + "}", fields.get(i).getText());
            }
        } else {
            command = "";
        }
    }
    return command;
}
Also used : JPanel(javax.swing.JPanel) Matcher(java.util.regex.Matcher) MigLayout(net.miginfocom.swing.MigLayout) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel) SystemStateBean(com.willwinder.universalgcodesender.pendantui.SystemStateBean) JTextField(javax.swing.JTextField)

Example 2 with SystemStateBean

use of com.willwinder.universalgcodesender.pendantui.SystemStateBean in project Universal-G-Code-Sender by winder.

the class MacroHelperTest method testSubstitutePrompt.

@Test
// This test creates a modal dialog.
@Ignore
public void testSubstitutePrompt() {
    System.out.println("substituteValuesPrompt");
    BackendAPI backend = EasyMock.mock(BackendAPI.class);
    EasyMock.reset(backend);
    final Capture<SystemStateBean> capture = EasyMock.newCapture();
    backend.updateSystemState(EasyMock.capture(capture));
    EasyMock.expect(EasyMock.expectLastCall());
    EasyMock.replay(backend);
    String result = MacroHelper.substituteValues("{prompt|value 1} {prompt|value 2} {prompt|value 3}", backend);
    System.out.println(result);
}
Also used : BackendAPI(com.willwinder.universalgcodesender.model.BackendAPI) SystemStateBean(com.willwinder.universalgcodesender.pendantui.SystemStateBean) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with SystemStateBean

use of com.willwinder.universalgcodesender.pendantui.SystemStateBean in project Universal-G-Code-Sender by winder.

the class MacroHelperTest method testSubstituteValues.

/**
 * Test of substituteValues method, of class MacroHelper.
 */
@Test
public void testSubstituteValues() {
    System.out.println("substituteValues");
    BackendAPI backend = EasyMock.mock(BackendAPI.class);
    EasyMock.reset(backend);
    final Capture<SystemStateBean> capture = EasyMock.newCapture();
    backend.updateSystemState(EasyMock.capture(capture));
    EasyMock.expect(EasyMock.expectLastCall()).andAnswer(() -> {
        capture.getValue().setMachineX("1");
        capture.getValue().setMachineY("2");
        capture.getValue().setMachineZ("3");
        capture.getValue().setWorkX("4");
        capture.getValue().setWorkY("5");
        capture.getValue().setWorkZ("6");
        return null;
    });
    EasyMock.replay(backend);
    String result = MacroHelper.substituteValues("{machine_x} {machine_y} {machine_z} {work_x} {work_y} {work_z}", backend);
    assertEquals("1 2 3 4 5 6", result);
}
Also used : BackendAPI(com.willwinder.universalgcodesender.model.BackendAPI) SystemStateBean(com.willwinder.universalgcodesender.pendantui.SystemStateBean) Test(org.junit.Test)

Aggregations

SystemStateBean (com.willwinder.universalgcodesender.pendantui.SystemStateBean)3 BackendAPI (com.willwinder.universalgcodesender.model.BackendAPI)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JTextField (javax.swing.JTextField)1 MigLayout (net.miginfocom.swing.MigLayout)1 Ignore (org.junit.Ignore)1