Explorar el Código

Implement settings window

Henrik Kunzelmann hace 3 años
padre
commit
8eed807b93

+ 6 - 2
src/holeg/HolegGateway.java

@@ -97,8 +97,12 @@ public class HolegGateway {
         boolean solve = !PowerFlowAnalysisMenu.getInstance().areUpdatesDisabled();
         Grid grid = convert(minimumNetwork, iteration, flexManager);
 
+        // Create settings when null
+        if (context != null && context.settings == null)
+            context.settings = PowerFlowSettings.getDefault();
+
         // Check if the grid is equal to one already solved
-        if (context != null) {
+        if (context != null && context.settings.onlyUpdateGridWhenChanged) {
             for (Grid lastSolved : context.lastSolvedGrids) {
                 if (GridComparator.isEqual(lastSolved, grid)) {
                     decorateNetwork(minimumNetwork, iteration, flexManager, network, lastSolved);
@@ -138,7 +142,7 @@ public class HolegGateway {
             // Starting solving when requested
             if (solve) {
                 HolegPowerFlow powerFlow = new HolegPowerFlow();
-                result = powerFlow.solve(grid, PowerFlowSettings.getDefault());
+                result = powerFlow.solve(grid, context.settings);
             }
 
             // Decorate network

+ 12 - 2
src/holeg/ui/FlowTableWindow.java

@@ -1,4 +1,14 @@
 package holeg.ui;
 
-public class FlowTableWindow {
-}
+import javax.swing.*;
+import java.awt.*;
+
+public class FlowTableWindow extends JDialog {
+    public FlowTableWindow(Frame owner) {
+        super(owner);
+
+        setTitle("HOLEG: Power flow table");
+        setSize(500, 600);
+        add(new Label("Work in progress"));
+    }
+}

+ 20 - 1
src/holeg/ui/PowerFlowAnalysisMenu.java

@@ -1,5 +1,6 @@
 package holeg.ui;
 
+import holeg.PowerFlowSettings;
 import ui.controller.SimulationManager;
 import ui.controller.SingletonControl;
 import ui.model.Model;
@@ -20,9 +21,12 @@ public class PowerFlowAnalysisMenu extends JMenu {
 
     private static PowerFlowAnalysisMenu instance;
 
+    private SettingsWindow settingsWindow;
+    private FlowTableWindow flowTableWindow;
+
     private boolean overwriteDisableUpdatesToForceUpdates = false;
 
-    public PowerFlowAnalysisMenu(Model model) {
+    public PowerFlowAnalysisMenu(JFrame owner, Model model) {
         super("Power flow");
 
         settingsMenu = add(new JMenuItem("Settings"));
@@ -36,6 +40,17 @@ public class PowerFlowAnalysisMenu extends JMenu {
         showResultMessageBox = (JCheckBoxMenuItem) add(new JCheckBoxMenuItem("Show result message"));
         showDebugMessageBox = (JCheckBoxMenuItem) add(new JCheckBoxMenuItem("Show debug message"));
 
+        settingsWindow = new SettingsWindow(owner, getPowerFlowSettings());
+        flowTableWindow = new FlowTableWindow(owner);
+
+        settingsMenu.addActionListener((e) -> {
+            settingsWindow.setVisible(true);
+        });
+
+        showFlow.addActionListener((e) -> {
+            flowTableWindow.setVisible(true);
+        });
+
         clearCache.addActionListener((e) -> {
             clearCache();
         });
@@ -65,6 +80,10 @@ public class PowerFlowAnalysisMenu extends JMenu {
         overwriteDisableUpdatesToForceUpdates = false;
     }
 
+    public PowerFlowSettings getPowerFlowSettings() {
+        return SingletonControl.getInstance().getControl().getSimManager().getHolegPowerFlowContext().settings;
+    }
+
     public boolean areUpdatesDisabled() {
         return !overwriteDisableUpdatesToForceUpdates && disableUpdates.getState();
     }

+ 135 - 1
src/holeg/ui/SettingsWindow.java

@@ -1,4 +1,138 @@
 package holeg.ui;
 
-public class SettingsWindow {
+import holeg.PowerFlowSettings;
+import holeg.SlackNodePlacementStrategy;
+
+import javax.swing.*;
+import javax.swing.event.ChangeListener;
+import java.awt.*;
+import java.util.function.Consumer;
+
+public class SettingsWindow extends JDialog {
+    public SettingsWindow(Frame owner, PowerFlowSettings settings) {
+        super(owner);
+
+        setTitle("HOLEG: Power flow settings");
+        setSize(400, 600);
+
+        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
+
+        createCheckbox("Only update grid when changed", settings.onlyUpdateGridWhenChanged, (e) -> {
+            settings.onlyUpdateGridWhenChanged = e;
+        });
+        createCheckbox("Wait for solver job", settings.waitForSolverJob, (e) -> {
+            settings.waitForSolverJob = e;
+        });
+        createCheckbox("Skip grids with no producers", settings.skipGridsWithNoProducers, (e) -> {
+            settings.skipGridsWithNoProducers = e;
+        });
+        createCheckbox("Replace node with slack node", settings.replaceNodeWithSlackNode, (e) -> {
+            settings.replaceNodeWithSlackNode = e;
+        });
+
+        createComboBox("Slack node placement strategy", SlackNodePlacementStrategy.values(), settings.slackNodePlacementStrategy, (e) -> {
+            settings.slackNodePlacementStrategy = e;
+        });
+
+        createNumberTextBox("Max iterations", settings.solverSettings.maxIterations, (e) -> {
+            settings.solverSettings.maxIterations = e;
+        });
+
+        createNumberTextBox("Jacobian recalculation interval", settings.solverSettings.jacobianRecalculationInterval, (e) -> {
+            settings.solverSettings.jacobianRecalculationInterval = e;
+        });
+
+        createNumberTextBox("Min voltage until invalid", settings.minVoltageUntilInvalid, (e) -> {
+            settings.minVoltageUntilInvalid = e;
+        });
+    }
+
+    private void createCheckbox(String label, boolean value, Consumer<Boolean> listener) {
+        JCheckBox checkbox = new JCheckBox();
+        checkbox.setText(label);
+        checkbox.setSelected(value);
+        checkbox.setAlignmentX(Component.LEFT_ALIGNMENT);
+        checkbox.addActionListener((e) -> {
+            if (listener != null)
+                listener.accept(((JCheckBox) e.getSource()).isSelected());
+        });
+        add(checkbox);
+    }
+
+    private <T> void createComboBox(String label, T[] choices, T value, Consumer<T> listener) {
+        Container container = new Container();
+        container.setLayout(new BoxLayout(container, BoxLayout.LINE_AXIS));
+
+        // Add label
+        container.add(new JLabel(label));
+
+        // Create combo box
+        JComboBox<T> comboBox = new JComboBox<T>(choices);
+        comboBox.setSelectedItem(value);
+        comboBox.setMaximumSize(comboBox.getPreferredSize());
+        comboBox.setAlignmentX(Component.LEFT_ALIGNMENT);
+        comboBox.addActionListener((e) -> {
+            if (listener != null)
+                listener.accept((T) ((JComboBox<T>) e.getSource()).getSelectedItem());
+        });
+        container.add(comboBox);
+        add(container);
+    }
+
+    private JTextField createTextField(String value) {
+        JTextField textField = new JTextField();
+        textField.setColumns(8);
+        textField.setText(value);
+        textField.setMaximumSize(textField.getPreferredSize());
+        textField.setAlignmentX(Component.LEFT_ALIGNMENT);
+        return textField;
+    }
+
+    private void createNumberTextBox(String label, Integer value, Consumer<Integer> listener) {
+        Container container = new Container();
+        container.setLayout(new BoxLayout(container, BoxLayout.LINE_AXIS));
+
+        // Add label
+        container.add(new JLabel(label));
+
+        // Create text field
+        JTextField textField = createTextField(Integer.toString(value));
+        textField.addActionListener((e) -> {
+            if (listener != null) {
+                String text = ((JTextField) e.getSource()).getText();
+                try {
+                    listener.accept(Integer.parseInt(text));
+                }
+                catch(NumberFormatException ignored) {
+
+                }
+            }
+        });
+        container.add(textField);
+        add(container);
+    }
+
+    private void createNumberTextBox(String label, Double value, Consumer<Double> listener) {
+        Container container = new Container();
+        container.setLayout(new BoxLayout(container, BoxLayout.LINE_AXIS));
+
+        // Add label
+        container.add(new JLabel(label));
+
+        // Create text field
+        JTextField textField = createTextField(Double.toString(value));
+        textField.addActionListener((e) -> {
+            if (listener != null) {
+                String text = ((JTextField) e.getSource()).getText();
+                try {
+                    listener.accept(Double.parseDouble(text));
+                }
+                catch(NumberFormatException ignored) {
+
+                }
+            }
+        });
+        container.add(textField);
+        add(container);
+    }
 }

+ 1 - 1
src/ui/view/GUI.java

@@ -1011,7 +1011,7 @@ public class GUI implements CategoryListener {
 		mnHelp.add(mntmAboutUs);
 
 		// Power flow
-		powerFlowMenu = new PowerFlowAnalysisMenu(model);
+		powerFlowMenu = new PowerFlowAnalysisMenu(holegJFrame, model);
 		menuBar.add(powerFlowMenu);
 
 		tabbedPaneOriginal.addChangeListener(changeEvent -> {