PowerFlowAnalysisMenu.java 3.4 KB

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