PowerFlowAnalysisMenu.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package holeg.ui;
  2. import holeg.HolegGateway;
  3. import holeg.HolegPowerFlow;
  4. import holeg.HolegPowerFlowContext;
  5. import holeg.PowerFlowSettings;
  6. import holeg.test_sensitivity.TestSensitivityProgram;
  7. import ui.controller.SimulationManager;
  8. import ui.controller.SingletonControl;
  9. import ui.model.Model;
  10. import javax.swing.*;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.util.Set;
  14. public class PowerFlowAnalysisMenu extends JMenu {
  15. private JMenuItem settingsMenu;
  16. private JMenuItem showFlow;
  17. private JMenuItem clearCache;
  18. private JMenuItem solve;
  19. private JMenuItem clearCacheAndSolve;
  20. private JCheckBoxMenuItem disableUpdates;
  21. private JCheckBoxMenuItem showResultMessageBox;
  22. private JCheckBoxMenuItem showDebugMessageBox;
  23. private JCheckBoxMenuItem showVoltages;
  24. private static PowerFlowAnalysisMenu instance;
  25. private SettingsWindow settingsWindow;
  26. private FlowTableWindow flowTableWindow;
  27. private boolean overwriteDisableUpdatesToForceUpdates = false;
  28. public PowerFlowAnalysisMenu(JFrame owner, Model model) {
  29. super("Power flow");
  30. settingsMenu = add(new JMenuItem("Settings"));
  31. showFlow = add(new JMenuItem("Show flow table"));
  32. addSeparator();
  33. clearCache = add(new JMenuItem("Clear cache"));
  34. solve = add(new JMenuItem("Solve"));
  35. clearCacheAndSolve = add(new JMenuItem("Clear cache and solve"));
  36. addSeparator();
  37. disableUpdates = (JCheckBoxMenuItem) add(new JCheckBoxMenuItem("Disable automatic updates"));
  38. showResultMessageBox = (JCheckBoxMenuItem) add(new JCheckBoxMenuItem("Show result message"));
  39. showDebugMessageBox = (JCheckBoxMenuItem) add(new JCheckBoxMenuItem("Show debug message"));
  40. showVoltages = (JCheckBoxMenuItem) add(new JCheckBoxMenuItem("Show voltagess"));
  41. showVoltages.setState(true);
  42. settingsWindow = new SettingsWindow(owner, getPowerFlowSettings());
  43. flowTableWindow = new FlowTableWindow(owner);
  44. settingsMenu.addActionListener((e) -> {
  45. settingsWindow.setVisible(true);
  46. });
  47. showFlow.addActionListener((e) -> {
  48. flowTableWindow.setVisible(true);
  49. flowTableWindow.update();
  50. });
  51. clearCache.addActionListener((e) -> {
  52. clearCache();
  53. });
  54. solve.addActionListener((e) -> {
  55. solve(false);
  56. });
  57. clearCacheAndSolve.addActionListener((e) -> {
  58. solve(true);
  59. });
  60. instance = this;
  61. //TestSensitivityProgram.test();
  62. }
  63. private void clearCache() {
  64. for (HolegPowerFlowContext context : HolegGateway.getALlContext())
  65. context.clearCache();
  66. }
  67. private void solve(boolean clearCache) {
  68. // This is needed otherwise the calculateStateAndVisualForCurrentTimeStep() function would not do anything
  69. // when updates are disabled (same execution path)
  70. overwriteDisableUpdatesToForceUpdates = true;
  71. if (clearCache)
  72. clearCache();
  73. SingletonControl.getInstance().getControl().calculateStateAndVisualForCurrentTimeStep();
  74. overwriteDisableUpdatesToForceUpdates = false;
  75. }
  76. public PowerFlowSettings getPowerFlowSettings() {
  77. return SingletonControl.getInstance().getControl().getSimManager().getHolegPowerFlowSettings();
  78. }
  79. public boolean areUpdatesDisabled() {
  80. return !overwriteDisableUpdatesToForceUpdates && disableUpdates.getState();
  81. }
  82. public boolean shouldShowResult() {
  83. return showResultMessageBox.getState();
  84. }
  85. public boolean shouldShowDebug() {
  86. return showDebugMessageBox.getState();
  87. }
  88. public boolean shouldShowVoltages() { return showVoltages.getState(); }
  89. public static PowerFlowAnalysisMenu getInstance() {
  90. return instance;
  91. }
  92. }