123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- package eu.tankernn.grid.frame;
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- import java.text.DecimalFormat;
- import java.util.Arrays;
- import javax.swing.JCheckBoxMenuItem;
- import javax.swing.JComboBox;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.JSpinner;
- import javax.swing.SpinnerNumberModel;
- import javax.swing.border.TitledBorder;
- import javax.swing.event.ChangeEvent;
- import eu.tankernn.grid.FanSpeedProfile;
- import eu.tankernn.grid.GridControl;
- import eu.tankernn.grid.model.ComputerModel;
- public class GridControlPanel extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private ComputerModel model;
- private JMenuBar menuBar = new JMenuBar();
- private JMenu fileMenu = new JMenu("File"), settingsMenu = new JMenu("Settings"),
- profileMenu = new JMenu("Profiles");
- private JMenuItem saveSettings = new JMenuItem("Save settings..."), exit = new JMenuItem("Exit"),
- sensorConf = new JMenuItem("Configure sensors..."), startMinimized = new JCheckBoxMenuItem("Start minimized"), addProfile = new JMenuItem("Add profile...");
- private FanPanel[] fanPanels;
- private JPanel serialPanel = new JPanel(), gridPanel = new JPanel(), infoPanel = new JPanel();
- private JSpinner minSpeed = new JSpinner(new SpinnerNumberModel(30, 0, 100, 5)),
- pollingSpeed = new JSpinner(new SpinnerNumberModel(500, 100, 2000, 100));
- private JComboBox<String> portMap = new JComboBox<>();
- private JLabel CPULabel = new JLabel("CPU:");
- private JLabel CPULoadLabel = new JLabel("CPU load");
- private JLabel GPULabel = new JLabel("GPU:");
- private JLabel PowerLabel = new JLabel("Power");
- private void setMinRPM(ChangeEvent event) {
- getModel().setMinSpeed((int) minSpeed.getValue());
- }
- private void setPort(ItemEvent event) {
- model.setGrid((String) portMap.getSelectedItem());
- }
- public GridControlPanel(GridControl control, ComputerModel model) {
- setModel(model);
- this.setLayout(new BorderLayout());
- menuBar.add(fileMenu);
- fileMenu.add(exit);
- exit.addActionListener(a -> control.exit());
- fileMenu.add(saveSettings);
- saveSettings.addActionListener(e -> control.saveSettings());
- menuBar.add(settingsMenu);
- settingsMenu.add(sensorConf);
- sensorConf.addActionListener(e -> new SensorConfig(model.getSensor()));
- settingsMenu.add(startMinimized);
- startMinimized.addActionListener(e -> control.setStartMinimized(startMinimized.isEnabled()));
- menuBar.add(profileMenu);
- profileMenu.add(addProfile);
- addProfile.addActionListener(e -> {
- FanSpeedProfile p = new ProfileEditor().editProfile(null);
- if (p != null) {
- model.addProfile(p);
- Arrays.stream(fanPanels).forEach(f -> f.addProfile(p));
- }
- });
- this.setJMenuBar(this.menuBar);
- serialPanel.setLayout(new FlowLayout());
- serialPanel.setBorder(new TitledBorder("Serial settings"));
- serialPanel.add(labelledComponent("COM port: ", portMap));
- serialPanel.add(labelledComponent("Polling speed: ", pollingSpeed));
- this.add(serialPanel, BorderLayout.NORTH);
- fanPanels = model.getGrid().fanStream().map(f -> new FanPanel(f, model.getProfiles())).toArray(FanPanel[]::new);
- gridPanel.setLayout(new GridLayout(3, 2));
- for (FanPanel p : fanPanels)
- gridPanel.add(p);
- this.add(gridPanel, BorderLayout.CENTER);
- minSpeed.setValue(model.getMinSpeed());
- minSpeed.addChangeListener(this::setMinRPM);
- pollingSpeed.setValue(control.getPollingSpeed());
- pollingSpeed.addChangeListener(e -> {
- control.setPollingSpeed((int) pollingSpeed.getValue());
- });
- infoPanel.setBorder(new TitledBorder("System info"));
- infoPanel.setLayout(new GridLayout(3, 2));
- infoPanel.add(CPULabel);
- infoPanel.add(GPULabel);
- infoPanel.add(CPULoadLabel);
- infoPanel.add(PowerLabel);
- infoPanel.add(labelledComponent("Minimum speed (%): ", minSpeed));
- this.add(infoPanel, BorderLayout.SOUTH);
- portMap.addItemListener(new ItemListener() {
- @Override
- public void itemStateChanged(ItemEvent e) {
- setPort(e);
- }
- });
- this.setTitle("JavaGridControl");
- }
- static JPanel labelledComponent(String labelText, JComponent component) {
- JPanel panel = new JPanel(new FlowLayout());
- panel.add(new JLabel(labelText));
- panel.add(component);
- return panel;
- }
- /**
- * This method sets the model for this controller. After the model is set
- * certain UI elements are updated Finally a pollAndCompute Thread is
- * started
- *
- * @param model
- * the model to set
- */
- private void setModel(ComputerModel model) {
- this.model = model;
- portMap.removeAllItems();
- for (String key : model.getPortMap().keySet()) {
- portMap.addItem(key);
- }
- setPort(null);
- }
- /**
- * This method updates the values of some UI elements and binds properties
- * to others
- *
- */
- public void updateProperties() {
- DecimalFormat df = new DecimalFormat("#.##");
- // \u00B0 = "Degree sign"
- CPULabel.setText("CPU: " + df.format(getModel().getSensor().getCPUTemp()) + " \u00B0C");
- PowerLabel.setText("Total power: " + df.format(getModel().getGrid().getTotalWattage()) + " W");
- CPULoadLabel.setText("CPU load: " + df.format(getModel().getSensor().getCpuLoad()) + " %");
- GPULabel.setText("GPU: " + df.format(getModel().getSensor().getGPUTemp()) + " \u00B0C");
- for (FanPanel p : fanPanels)
- p.update();
- }
- public ComputerModel getModel() {
- return model;
- }
- }
|