PowerFlowAnalysisMenu.java 3.3 KB

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