diff --git a/ugs-core/src/com/willwinder/universalgcodesender/uielements/UGSSettingsDialog.java b/ugs-core/src/com/willwinder/universalgcodesender/uielements/UGSSettingsDialog.java index 2858da29c..eecc30a37 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/uielements/UGSSettingsDialog.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/uielements/UGSSettingsDialog.java @@ -66,7 +66,7 @@ public UGSSettingsDialog(String settingsTitle, AbstractUGSSettings panel, Frame initComponents(); setLocationRelativeTo(parent); - setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); saveChanges = false; } diff --git a/ugs-core/src/com/willwinder/universalgcodesender/uielements/panels/ConnectionSettingsPanel.java b/ugs-core/src/com/willwinder/universalgcodesender/uielements/panels/ConnectionSettingsPanel.java index b716112dc..7db5575bf 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/uielements/panels/ConnectionSettingsPanel.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/uielements/panels/ConnectionSettingsPanel.java @@ -57,6 +57,7 @@ public class ConnectionSettingsPanel extends AbstractUGSSettings { Localization.getString("sender.nightly-warning")); private final Checkbox autoStartPendant = new Checkbox( Localization.getString("sender.autostartpendant")); + private final Textfield pendantPort = new Textfield(Localization.getString("settings.pendantPort")); private final JComboBox languageCombo = new JComboBox<>(AvailableLanguages.getAvailableLanguages().toArray(new Language[0])); private final JComboBox connectionDriver = new JComboBox<>(ConnectionDriver.getPrettyNames()); private final JTextField workspaceDirectory = new JTextField(); @@ -92,6 +93,7 @@ public void save() { //settings.setAutoConnectEnabled(autoConnect.getValue()); settings.setShowNightlyWarning(showNightlyWarning.getValue()); settings.setAutoStartPendant(autoStartPendant.getValue()); + settings.setPendantPort(Integer.parseInt(pendantPort.getValue().toString())); settings.setLanguage(((Language) languageCombo.getSelectedItem()).getLanguageCode()); settings.setConnectionDriver(ConnectionDriver.prettyNameToEnum(connectionDriver.getSelectedItem().toString())); settings.setWorkspaceDirectory(workspaceDirectory.getText()); @@ -134,6 +136,9 @@ protected void updateComponentsInternal(Settings s) { autoStartPendant.setSelected(s.isAutoStartPendant()); add(autoStartPendant, "spanx, wrap"); + pendantPort.setValue(String.valueOf(s.getPendantPort())); + add(pendantPort, "spanx, grow"); + for (int i = 0; i < languageCombo.getItemCount(); i++) { Language l = languageCombo.getItemAt(i); if (l.getLanguageCode().equals(s.getLanguage())) { @@ -151,8 +156,8 @@ protected void updateComponentsInternal(Settings s) { workspaceDirectory.setText(settings.getWorkspaceDirectory()); JPanel panel = new JPanel(); - panel.setLayout(new MigLayout("insets 0", "fill")); - panel.add(workspaceDirectory, "gapright 0"); + panel.setLayout(new MigLayout("insets 0, fill")); + panel.add(workspaceDirectory, "grow"); panel.add(workspaceDirectoryBrowseButton); workspaceDirectoryBrowseButton.setAction(createBrowseDirectoryAction()); diff --git a/ugs-core/src/com/willwinder/universalgcodesender/utils/Settings.java b/ugs-core/src/com/willwinder/universalgcodesender/utils/Settings.java index 95177b16f..263ac518d 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/utils/Settings.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/utils/Settings.java @@ -77,6 +77,7 @@ public class Settings { private boolean showNightlyWarning = true; private boolean showSerialPortWarning = true; private boolean autoStartPendant = false; + private int pendantPort = 8080; private boolean autoConnect = false; private boolean autoReconnect = false; @@ -523,6 +524,15 @@ public void setSafetyHeight(double safetyHeight) { this.safetyHeight = safetyHeight; } + public int getPendantPort() { + return pendantPort; + } + + public void setPendantPort(int pendantPort) { + this.pendantPort = pendantPort; + changed(); + } + public static class AutoLevelSettings { // Setting window public double autoLevelProbeZeroHeight = 0; diff --git a/ugs-core/src/resources/MessagesBundle_en_US.properties b/ugs-core/src/resources/MessagesBundle_en_US.properties index fd24a0fa8..27d88f986 100644 --- a/ugs-core/src/resources/MessagesBundle_en_US.properties +++ b/ugs-core/src/resources/MessagesBundle_en_US.properties @@ -340,6 +340,7 @@ settings.connectionDriver = Connection driver settings.workspaceDirectory = Workspace directory settings.help.disableAxis = Each disabled axis is removed from the UI when possible. settings.disableAxis = Disable axis: %s +settings.pendantPort = Pendant web port ArcExpander = Arc Expander CommandLengthProcessor = Command Length Processor CommandSplitter = Command Splitter diff --git a/ugs-pendant/src/main/java/com/willwinder/universalgcodesender/pendantui/PendantUI.java b/ugs-pendant/src/main/java/com/willwinder/universalgcodesender/pendantui/PendantUI.java index d34770d0f..66bbd8963 100644 --- a/ugs-pendant/src/main/java/com/willwinder/universalgcodesender/pendantui/PendantUI.java +++ b/ugs-pendant/src/main/java/com/willwinder/universalgcodesender/pendantui/PendantUI.java @@ -18,7 +18,10 @@ This file is part of Universal Gcode Sender (UGS). */ package com.willwinder.universalgcodesender.pendantui; +import com.willwinder.universalgcodesender.listeners.UGSEventListener; import com.willwinder.universalgcodesender.model.BackendAPI; +import com.willwinder.universalgcodesender.model.UGSEvent; +import com.willwinder.universalgcodesender.model.events.SettingChangedEvent; import net.glxn.qrgen.QRCode; import net.glxn.qrgen.image.ImageType; import org.eclipse.jetty.server.Handler; @@ -47,15 +50,16 @@ This file is part of Universal Gcode Sender (UGS). * * @author bobj */ -public class PendantUI { - private BackendAPI mainWindow; +public class PendantUI implements UGSEventListener { + private BackendAPI backendAPI; private Server server = null; private int port = 8080; private static final Logger LOG = Logger.getLogger(PendantUI.class.getSimpleName()); - public PendantUI(BackendAPI mainWindow) { - this.mainWindow = mainWindow; - BackendAPIFactory.getInstance().register(mainWindow); + public PendantUI(BackendAPI backendAPI) { + this.backendAPI = backendAPI; + backendAPI.addUGSEventListener(this); + BackendAPIFactory.getInstance().register(backendAPI); } public Resource getBaseResource(String directory) { @@ -74,6 +78,7 @@ public Resource getBaseResource(String directory) { * @return the url for the pendant interface */ public List start() { + int port = backendAPI.getSettings().getPendantPort(); server = new Server(port); ResourceHandler staticResourceHandler = new ResourceHandler(); @@ -149,19 +154,25 @@ public void stop() { } } - public int getPort() { - return port; + public boolean isStarted() { + return server != null && server.isStarted(); } - public void setPort(int port) { - this.port = port; + public BackendAPI getBackendAPI() { + return backendAPI; } - public BackendAPI getMainWindow() { - return mainWindow; + public void setBackendAPI(BackendAPI backendAPI) { + this.backendAPI = backendAPI; } - public void setMainWindow(BackendAPI mainWindow) { - this.mainWindow = mainWindow; + @Override + public void UGSEvent(UGSEvent evt) { + if (evt instanceof SettingChangedEvent) { + if (backendAPI.getSettings().getPendantPort() != port && isStarted()) { + stop(); + start(); + } + } } } diff --git a/ugs-pendant/src/test/java/com/willwinder/universalgcodesender/pendantui/PendantUITest.java b/ugs-pendant/src/test/java/com/willwinder/universalgcodesender/pendantui/PendantUITest.java index d22958f6b..884d97635 100644 --- a/ugs-pendant/src/test/java/com/willwinder/universalgcodesender/pendantui/PendantUITest.java +++ b/ugs-pendant/src/test/java/com/willwinder/universalgcodesender/pendantui/PendantUITest.java @@ -22,7 +22,7 @@ public void setup(){ } @Test public void testPendantUI() { - assertSame(mockBackend, pendantUI.getMainWindow()); + assertSame(mockBackend, pendantUI.getBackendAPI()); } @Test @@ -32,16 +32,4 @@ public void testGetUrl() { assertTrue(test.startsWith("http://")); assertTrue(test.contains("8080")); } - - @Test - public void testGetPort() { - pendantUI.setPort(999); - assertEquals(999, pendantUI.getPort()); - } - - @Test - public void testSetPort() { - pendantUI.setPort(999); - assertEquals(999, pendantUI.getPort()); - } }