Skip to content

Commit

Permalink
disable update configuration button if library updates are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-republic committed Aug 11, 2024
1 parent 4b15b88 commit c794b8b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 26 deletions.
Binary file modified configurator/current/configurator.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.ServiceLoader;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
Expand All @@ -45,19 +45,20 @@ public class BMSDialog extends JDialog {
private final JTextField delayAfterNoBytesField = new JTextField("200");
private final NumberInputVerifier numberInputVerifier = new NumberInputVerifier();
private final JTextField idField = new JTextField();
final JButton okButton = new JButton("Ok");
private final JButton okButton = new JButton("Ok");
private final ActionListener disableUpdateConfiguration;

/**
* Constructor.
*
* @param owner the owning frame
* @param configurator the owning frame
*/
public BMSDialog(final JFrame owner) {
super(owner, "BMS configuration...", true);

public BMSDialog(final Configurator configurator) {
super(configurator, "BMS configuration...", true);
disableUpdateConfiguration = (action) -> configurator.disableUpdateConfiguration();
descriptors = createBMSDescriptors();

setLocation(owner.getBounds().width / 2 - 175, owner.getBounds().height / 2 - 60);
setLocation(configurator.getBounds().width / 2 - 175, configurator.getBounds().height / 2 - 60);
setSize(new Dimension(350, 359));
setResizable(false);
getContentPane().setLayout(new BorderLayout(0, 0));
Expand Down Expand Up @@ -252,6 +253,18 @@ public void setBMSConfig(final BMSConfig config) {
portLocatorField.setText(config.getPortLocator());
baudRateField.setText("" + config.getBaudRate());
delayAfterNoBytesField.setText("" + config.getDelayAfterNoBytes());

bmses.addActionListener(disableUpdateConfiguration);
}


@Override
public void setVisible(final boolean b) {
super.setVisible(b);

if (!b) {
bmses.removeActionListener(disableUpdateConfiguration);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
Expand All @@ -46,7 +45,7 @@ public class BMSPanel extends JPanel {
private final JButton editBMSButton;
private final NumberInputVerifier numberInputVerifier = new NumberInputVerifier();

public BMSPanel(final JFrame frame) {
public BMSPanel(final Configurator configurator) {
setBorder(new EmptyBorder(10, 10, 10, 10));
final GridBagLayout gbl_bmsPanel = new GridBagLayout();
gbl_bmsPanel.columnWidths = new int[] { 100, 300, 70, 70 };
Expand Down Expand Up @@ -79,7 +78,6 @@ public BMSPanel(final JFrame frame) {

@Override
public void mouseClicked(final MouseEvent evt) {
final JList<MenuItem<BMSConfig>> list = (JList<MenuItem<BMSConfig>>) evt.getSource();
if (evt.getClickCount() == 2) {
Stream.of(editBMSButton.getActionListeners()).forEach(listener -> listener.actionPerformed(new ActionEvent(editBMSButton, ActionEvent.ACTION_PERFORMED, "edit")));
}
Expand All @@ -89,13 +87,19 @@ public void mouseClicked(final MouseEvent evt) {

final JButton addBMSButton = new JButton("Add");
addBMSButton.addActionListener(e -> {
final BMSDialog dlg = new BMSDialog(frame);
final BMSDialog dlg = new BMSDialog(configurator);
dlg.setVisible(true);
final BMSConfig config = dlg.getBMSConfig();

if (config != null) {
config.setBmsId(bmsListModel.getSize());
bmsListModel.addElement(new MenuItem<>(createBMSDisplayName(config), config));

// see if the same BMS type is already present
if (!isBMSTypePresent(config)) {
// if not present, need to do a clean install
configurator.disableUpdateConfiguration();
}
}
});
final GridBagConstraints gbc_addBMSButton = new GridBagConstraints();
Expand All @@ -107,7 +111,14 @@ public void mouseClicked(final MouseEvent evt) {
final JButton removeBMSButton = new JButton("Remove");
removeBMSButton.addActionListener(e -> {
if (bmsList.getSelectedIndex() != -1) {
bmsListModel.remove(bmsList.getSelectedIndex());
final MenuItem<BMSConfig> item = bmsListModel.remove(bmsList.getSelectedIndex());

// see if the same BMS type is still present
if (!isBMSTypePresent(item.getValue())) {
// if not present, need to do a clean install
configurator.disableUpdateConfiguration();
}

}
});
final GridBagConstraints gbc_removeBMSButton = new GridBagConstraints();
Expand All @@ -127,7 +138,7 @@ public void mouseClicked(final MouseEvent evt) {
final MenuItem<BMSConfig> item = bmsList.getSelectedValue();

if (item != null) {
final BMSDialog dlg = new BMSDialog(frame);
final BMSDialog dlg = new BMSDialog(configurator);
dlg.setBMSConfig(item.getValue());
dlg.setVisible(true);
item.setDisplayName(createBMSDisplayName(item.getValue()));
Expand Down Expand Up @@ -175,6 +186,23 @@ public void mouseClicked(final MouseEvent evt) {
}


private boolean isBMSTypePresent(final BMSConfig config) {
// go through the the existing BMS configs and see if the same BMS type is already
// present
boolean bmsTypePresent = false;

for (int i = 0; i < bmsListModel.size(); i++) {
final MenuItem<BMSConfig> item = bmsListModel.get(i);
if (item.getValue().getDescriptor().getName().equals(config.getDescriptor().getName())) {
bmsTypePresent = true;
break;
}
}

return bmsTypePresent;
}


private String createBMSDisplayName(final BMSConfig config) {
return config.getDescriptor().getName() + "(ID: " + config.getBmsId() + ") on " + config.getPortLocator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
public class Configurator extends JFrame {
private static final long serialVersionUID = 1L;
private final GeneralPanel generalPanel;
private final JButton updateConfigButton;
private final BMSPanel bmsPanel;
private final InverterPanel inverterPanel;
private final ServicesPanel servicesPanel;
Expand Down Expand Up @@ -116,13 +117,13 @@ public Configurator() {
bmsPanel = new BMSPanel(this);
tabbedPane.addTab("BMS", bmsPanel);

inverterPanel = new InverterPanel();
inverterPanel = new InverterPanel(this);
tabbedPane.addTab("Inverter", null, inverterPanel, null);

final JScrollPane servicesScrollPane = new JScrollPane();
tabbedPane.addTab("Services", null, servicesScrollPane, null);

servicesPanel = new ServicesPanel();
servicesPanel = new ServicesPanel(this);
servicesScrollPane.setViewportView(servicesPanel);

final JPanel buttonPanel = new JPanel();
Expand All @@ -133,12 +134,13 @@ public Configurator() {
gbl_buttonPanel.rowWeights = new double[] { 0.0, 0.0 };
buttonPanel.setLayout(gbl_buttonPanel);

final JButton updateConfigButton = new JButton("Update Configuration");
updateConfigButton = new JButton("Update Configuration");
final GridBagConstraints gbc_updateConfigButton = new GridBagConstraints();
gbc_updateConfigButton.insets = new Insets(0, 0, 0, 5);
gbc_updateConfigButton.gridx = 1;
gbc_updateConfigButton.gridy = 1;
buttonPanel.add(updateConfigButton, gbc_updateConfigButton);
disableUpdateConfiguration();
updateConfigButton.addActionListener(e -> {
try {
updateConfiguration();
Expand Down Expand Up @@ -373,7 +375,10 @@ private void buildApplication(final JTextArea out) throws Throwable {

// add the webserver
Files.deleteIfExists(installDirectory.resolve("lib/webserver-0.0.1-SNAPSHOT.jar"));
Files.copy(srcDirectory.resolve("webserver/target/webserver-0.0.1-SNAPSHOT.jar"), installDirectory.resolve("lib/webserver-0.0.1-SNAPSHOT.jar"));

if (servicesPanel.isWebserverEnabled()) {
Files.copy(srcDirectory.resolve("webserver/target/webserver-0.0.1-SNAPSHOT.jar"), installDirectory.resolve("lib/webserver-0.0.1-SNAPSHOT.jar"));
}

// clean up source directory and zip
Files.deleteIfExists(srcZip);
Expand Down Expand Up @@ -698,10 +703,19 @@ void loadConfiguration(final Path path) {
servicesPanel.setConfiguration(config);

JOptionPane.showMessageDialog(Configurator.this, "Successfully loaded the configuration!", "Information", JOptionPane.INFORMATION_MESSAGE);
updateConfigButton.setEnabled(true);
} catch (final Exception e) {
JOptionPane.showMessageDialog(Configurator.this, "Failed to load the configuration!\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}


/**
* Disables the Update Configuration button.
*/
public void disableUpdateConfiguration() {
updateConfigButton.setEnabled(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class InverterPanel extends JPanel {
private final JTextField inverterSendIntervalField;
private final NumberInputVerifier numberInputVerifier = new NumberInputVerifier();

public InverterPanel() {
public InverterPanel(final Configurator configurator) {
setBorder(new EmptyBorder(10, 10, 10, 10));
final GridBagLayout gbl_inverterPanel = new GridBagLayout();
gbl_inverterPanel.columnWidths = new int[] { 100, 250, 70, 70 };
Expand Down Expand Up @@ -112,8 +112,11 @@ public InverterPanel() {
gbc_inverterPushInvervalField.gridy = 3;
add(inverterSendIntervalField, gbc_inverterPushInvervalField);

inverterField.addActionListener(e -> inverterPortLocatorField.requestFocus());
inverterField.addActionListener(e -> inverterBaudRateField.setText("" + inverterField.getModel().getElementAt(inverterField.getSelectedIndex()).getValue().getDefaultBaudRate()));
inverterField.addActionListener(e -> {
inverterPortLocatorField.requestFocus();
inverterBaudRateField.setText("" + inverterField.getModel().getElementAt(inverterField.getSelectedIndex()).getValue().getDefaultBaudRate());
configurator.disableUpdateConfiguration();
});
inverterPortLocatorField.addActionListener(e -> inverterBaudRateField.requestFocus());
inverterBaudRateField.addActionListener(e -> inverterSendIntervalField.requestFocus());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class MQTTServicePanel extends JPanel {
private final JTextField mqttBrokerTopicField;
private boolean overrideBrokerEnabled = false;

public MQTTServicePanel() {
public MQTTServicePanel(final Configurator configurator) {

final GridBagLayout gbl_mqttPanel = new GridBagLayout();
gbl_mqttPanel.columnWidths = new int[] { 80, 0 };
Expand Down Expand Up @@ -182,9 +182,15 @@ public MQTTServicePanel() {
gbc_mqttBrokerTopicField.gridy = 7;
add(mqttBrokerTopicField, gbc_mqttBrokerTopicField);

activateMQTTBrokerCheckBox.addActionListener(t -> mqttBrokerSelectionChanged());
activateMQTTBrokerCheckBox.addActionListener(t -> {
mqttBrokerSelectionChanged();
configurator.disableUpdateConfiguration();
});

activateMQTTProducerCheckBox.addActionListener(t -> mqttProducerSelectionchanged());
activateMQTTProducerCheckBox.addActionListener(t -> {
mqttProducerSelectionchanged();
configurator.disableUpdateConfiguration();
});
}


Expand All @@ -195,6 +201,12 @@ void enableMQTTBroker(final boolean isEnabled) {
}


void enableMQTTProducer(final boolean isEnabled) {
activateMQTTProducerCheckBox.setSelected(isEnabled);
mqttProducerSelectionchanged();
}


public boolean isMQTTBrokerEnabled() {
return activateMQTTBrokerCheckBox.isSelected();
}
Expand Down Expand Up @@ -305,6 +317,8 @@ void setConfiguration(final Properties config) {
mqttBrokerTopicField.setText(config.getProperty("mqtt.broker.topic"));
activateMQTTBrokerCheckBox.setSelected(true);
mqttBrokerSelectionChanged();
} else {
enableMQTTBroker(false);
}

if (config.containsKey("mqtt.producer.enabled")) {
Expand All @@ -314,6 +328,8 @@ void setConfiguration(final Properties config) {
mqttProducerPasswordField.setText(config.getProperty("mqtt.producer.password"));
activateMQTTProducerCheckBox.setSelected(true);
mqttProducerSelectionchanged();
} else {
enableMQTTProducer(false);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ServicesPanel extends JPanel {
private final JCheckBox webserverCheckBox;
private final WebserverServicePanel webserverPanel;

public ServicesPanel() {
public ServicesPanel(final Configurator configurator) {
final GridBagLayout gbl_servicesPanel = new GridBagLayout();
gbl_servicesPanel.columnWidths = new int[] { 100, 100, 100, 100, 0 };
gbl_servicesPanel.rowHeights = new int[] { 30, 30, 30, 0, 30, 0, 30 };
Expand Down Expand Up @@ -62,9 +62,12 @@ public ServicesPanel() {
add(emailPanel, gbc_emailPanel);

enableComponent(emailPanel, false);
emailCheckBox.addActionListener(t -> enableComponent(emailPanel, emailCheckBox.isSelected()));
emailCheckBox.addActionListener(t -> {
enableComponent(emailPanel, emailCheckBox.isSelected());
configurator.disableUpdateConfiguration();
});

mqttPanel = new MQTTServicePanel();
mqttPanel = new MQTTServicePanel(configurator);
final GridBagConstraints gbc_mqttPanel = new GridBagConstraints();
gbc_mqttPanel.fill = GridBagConstraints.BOTH;
gbc_mqttPanel.gridwidth = 4;
Expand Down Expand Up @@ -95,6 +98,7 @@ public ServicesPanel() {
webserverCheckBox.addActionListener(t -> {
mqttPanel.enableMQTTBroker(webserverCheckBox.isSelected());
enableComponent(webserverPanel, webserverCheckBox.isSelected());
configurator.disableUpdateConfiguration();
});

}
Expand Down

0 comments on commit c794b8b

Please sign in to comment.