GridControlPanel.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package eu.tankernn.grid.frame;
  2. import java.awt.BorderLayout;
  3. import java.awt.FlowLayout;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ItemEvent;
  6. import java.awt.event.ItemListener;
  7. import java.text.DecimalFormat;
  8. import java.util.Arrays;
  9. import javax.swing.JCheckBoxMenuItem;
  10. import javax.swing.JComboBox;
  11. import javax.swing.JComponent;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JMenu;
  15. import javax.swing.JMenuBar;
  16. import javax.swing.JMenuItem;
  17. import javax.swing.JPanel;
  18. import javax.swing.JSpinner;
  19. import javax.swing.SpinnerNumberModel;
  20. import javax.swing.border.TitledBorder;
  21. import javax.swing.event.ChangeEvent;
  22. import eu.tankernn.grid.FanSpeedProfile;
  23. import eu.tankernn.grid.GridControl;
  24. import eu.tankernn.grid.model.ComputerModel;
  25. public class GridControlPanel extends JFrame {
  26. /**
  27. *
  28. */
  29. private static final long serialVersionUID = 1L;
  30. private ComputerModel model;
  31. private JMenuBar menuBar = new JMenuBar();
  32. private JMenu fileMenu = new JMenu("File"), settingsMenu = new JMenu("Settings"),
  33. profileMenu = new JMenu("Profiles");
  34. private JMenuItem saveSettings = new JMenuItem("Save settings..."), exit = new JMenuItem("Exit"),
  35. sensorConf = new JMenuItem("Configure sensors..."), startMinimized = new JCheckBoxMenuItem("Start minimized"), addProfile = new JMenuItem("Add profile...");
  36. private FanPanel[] fanPanels;
  37. private JPanel serialPanel = new JPanel(), gridPanel = new JPanel(), infoPanel = new JPanel();
  38. private JSpinner minSpeed = new JSpinner(new SpinnerNumberModel(30, 0, 100, 5)),
  39. pollingSpeed = new JSpinner(new SpinnerNumberModel(500, 100, 2000, 100));
  40. private JComboBox<String> portMap = new JComboBox<>();
  41. private JLabel CPULabel = new JLabel("CPU:");
  42. private JLabel CPULoadLabel = new JLabel("CPU load");
  43. private JLabel GPULabel = new JLabel("GPU:");
  44. private JLabel PowerLabel = new JLabel("Power");
  45. private void setMinRPM(ChangeEvent event) {
  46. getModel().setMinSpeed((int) minSpeed.getValue());
  47. }
  48. private void setPort(ItemEvent event) {
  49. model.setGrid((String) portMap.getSelectedItem());
  50. }
  51. public GridControlPanel(GridControl control, ComputerModel model) {
  52. setModel(model);
  53. this.setLayout(new BorderLayout());
  54. menuBar.add(fileMenu);
  55. fileMenu.add(exit);
  56. exit.addActionListener(a -> control.exit());
  57. fileMenu.add(saveSettings);
  58. saveSettings.addActionListener(e -> control.saveSettings());
  59. menuBar.add(settingsMenu);
  60. settingsMenu.add(sensorConf);
  61. sensorConf.addActionListener(e -> new SensorConfig(model.getSensor()));
  62. settingsMenu.add(startMinimized);
  63. startMinimized.addActionListener(e -> control.setStartMinimized(startMinimized.isEnabled()));
  64. menuBar.add(profileMenu);
  65. profileMenu.add(addProfile);
  66. addProfile.addActionListener(e -> {
  67. FanSpeedProfile p = new ProfileEditor().editProfile(null);
  68. if (p != null) {
  69. model.addProfile(p);
  70. Arrays.stream(fanPanels).forEach(f -> f.addProfile(p));
  71. }
  72. });
  73. this.setJMenuBar(this.menuBar);
  74. serialPanel.setLayout(new FlowLayout());
  75. serialPanel.setBorder(new TitledBorder("Serial settings"));
  76. serialPanel.add(labelledComponent("COM port: ", portMap));
  77. serialPanel.add(labelledComponent("Polling speed: ", pollingSpeed));
  78. this.add(serialPanel, BorderLayout.NORTH);
  79. fanPanels = model.getGrid().fanStream().map(f -> new FanPanel(f, model.getProfiles())).toArray(FanPanel[]::new);
  80. gridPanel.setLayout(new GridLayout(3, 2));
  81. for (FanPanel p : fanPanels)
  82. gridPanel.add(p);
  83. this.add(gridPanel, BorderLayout.CENTER);
  84. minSpeed.setValue(model.getMinSpeed());
  85. minSpeed.addChangeListener(this::setMinRPM);
  86. pollingSpeed.setValue(control.getPollingSpeed());
  87. pollingSpeed.addChangeListener(e -> {
  88. control.setPollingSpeed((int) pollingSpeed.getValue());
  89. });
  90. infoPanel.setBorder(new TitledBorder("System info"));
  91. infoPanel.setLayout(new GridLayout(3, 2));
  92. infoPanel.add(CPULabel);
  93. infoPanel.add(GPULabel);
  94. infoPanel.add(CPULoadLabel);
  95. infoPanel.add(PowerLabel);
  96. infoPanel.add(labelledComponent("Minimum speed (%): ", minSpeed));
  97. this.add(infoPanel, BorderLayout.SOUTH);
  98. portMap.addItemListener(new ItemListener() {
  99. @Override
  100. public void itemStateChanged(ItemEvent e) {
  101. setPort(e);
  102. }
  103. });
  104. this.setTitle("JavaGridControl");
  105. }
  106. static JPanel labelledComponent(String labelText, JComponent component) {
  107. JPanel panel = new JPanel(new FlowLayout());
  108. panel.add(new JLabel(labelText));
  109. panel.add(component);
  110. return panel;
  111. }
  112. /**
  113. * This method sets the model for this controller. After the model is set
  114. * certain UI elements are updated Finally a pollAndCompute Thread is
  115. * started
  116. *
  117. * @param model
  118. * the model to set
  119. */
  120. private void setModel(ComputerModel model) {
  121. this.model = model;
  122. portMap.removeAllItems();
  123. for (String key : model.getPortMap().keySet()) {
  124. portMap.addItem(key);
  125. }
  126. setPort(null);
  127. }
  128. /**
  129. * This method updates the values of some UI elements and binds properties
  130. * to others
  131. *
  132. */
  133. public void updateProperties() {
  134. DecimalFormat df = new DecimalFormat("#.##");
  135. // \u00B0 = "Degree sign"
  136. CPULabel.setText("CPU: " + df.format(getModel().getSensor().getCPUTemp()) + " \u00B0C");
  137. PowerLabel.setText("Total power: " + df.format(getModel().getGrid().getTotalWattage()) + " W");
  138. CPULoadLabel.setText("CPU load: " + df.format(getModel().getSensor().getCpuLoad()) + " %");
  139. GPULabel.setText("GPU: " + df.format(getModel().getSensor().getGPUTemp()) + " \u00B0C");
  140. for (FanPanel p : fanPanels)
  141. p.update();
  142. }
  143. public ComputerModel getModel() {
  144. return model;
  145. }
  146. }