package holeg.ui; import holeg.PowerFlowSettings; import ui.controller.SimulationManager; import ui.controller.SingletonControl; import ui.model.Model; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class PowerFlowAnalysisMenu extends JMenu { private JMenuItem settingsMenu; private JMenuItem showFlow; private JMenuItem clearCache; private JMenuItem solve; private JMenuItem clearCacheAndSolve; private JCheckBoxMenuItem disableUpdates; private JCheckBoxMenuItem showResultMessageBox; private JCheckBoxMenuItem showDebugMessageBox; private static PowerFlowAnalysisMenu instance; private SettingsWindow settingsWindow; private FlowTableWindow flowTableWindow; private boolean overwriteDisableUpdatesToForceUpdates = false; public PowerFlowAnalysisMenu(JFrame owner, Model model) { super("Power flow"); settingsMenu = add(new JMenuItem("Settings")); showFlow = add(new JMenuItem("Show flow table")); addSeparator(); clearCache = add(new JMenuItem("Clear cache")); solve = add(new JMenuItem("Solve")); clearCacheAndSolve = add(new JMenuItem("Clear cache and solve")); addSeparator(); disableUpdates = (JCheckBoxMenuItem) add(new JCheckBoxMenuItem("Disable automatic updates")); 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(); }); solve.addActionListener((e) -> { solve(false); }); clearCacheAndSolve.addActionListener((e) -> { solve(true); }); instance = this; } private void clearCache() { SingletonControl.getInstance().getControl().getSimManager().getHolegPowerFlowContext().clearCache(); } private void solve(boolean clearCache) { // This is needed otherwise the calculateStateAndVisualForCurrentTimeStep() function would not do anything // when updates are disabled (same execution path) overwriteDisableUpdatesToForceUpdates = true; if (clearCache) clearCache(); SingletonControl.getInstance().getControl().calculateStateAndVisualForCurrentTimeStep(); overwriteDisableUpdatesToForceUpdates = false; } public PowerFlowSettings getPowerFlowSettings() { return SingletonControl.getInstance().getControl().getSimManager().getHolegPowerFlowContext().settings; } public boolean areUpdatesDisabled() { return !overwriteDisableUpdatesToForceUpdates && disableUpdates.getState(); } public boolean shouldShowResult() { return showResultMessageBox.getState(); } public boolean shouldShowDebug() { return showDebugMessageBox.getState(); } public static PowerFlowAnalysisMenu getInstance() { return instance; } }