rusefi/java_console/ui/src/main/java/com/rusefi/ui/GaugesGridElement.java

91 lines
3.2 KiB
Java
Raw Normal View History

2015-08-21 21:01:32 -07:00
package com.rusefi.ui;
import com.rusefi.core.Sensor;
import com.rusefi.core.preferences.storage.Node;
2015-08-21 22:03:04 -07:00
import com.rusefi.ui.widgets.JPanelWithListener;
2015-08-21 21:01:32 -07:00
import com.rusefi.ui.widgets.SensorGauge;
2015-08-21 22:03:04 -07:00
import javax.swing.*;
2015-08-21 21:01:32 -07:00
import java.awt.*;
2015-08-21 22:03:04 -07:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2015-08-21 21:01:32 -07:00
/**
2015-08-31 10:02:28 -07:00
* This panel contains either a {@link SensorGauge} or {@link SensorLiveGraph}
*
2020-06-09 17:08:16 -07:00
* Andrey Belomutskiy, (c) 2013-2020
2015-08-21 21:01:32 -07:00
* 8/21/2015.
*/
public class GaugesGridElement {
2015-08-21 22:03:04 -07:00
private static final String GAUGE_TYPE = "gauge";
private static final String IS_LIVE_GRAPH = "type";
private final JPanelWithListener wrapper = new JPanelWithListener(new BorderLayout());
2020-06-25 18:40:36 -07:00
private final UIContext uiContext;
2015-08-21 22:03:04 -07:00
private final Node config;
2020-06-25 18:40:36 -07:00
private GaugesGridElement(UIContext uiContext, Node config) {
this.uiContext = uiContext;
2015-08-21 22:03:04 -07:00
this.config = config;
}
2020-06-25 18:40:36 -07:00
public static JComponent create(UIContext uiContext, Sensor sensor) {
return new GaugesGridElement(uiContext, new Node()).createGauge(sensor);
2019-04-23 16:49:01 -07:00
}
2019-04-23 17:49:06 -07:00
private JComponent createLiveBarElement(final Sensor defaultSensor) {
2015-08-21 22:03:04 -07:00
wrapper.setLayout(new GridLayout(2, 1));
2015-08-23 13:01:34 -07:00
JMenuItem switchToGauge = new JMenuItem("Switch to Gauge Mode");
switchToGauge.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
wrapper.removeAllChildrenAndListeners();
config.setBoolProperty(IS_LIVE_GRAPH, false);
createGauge(defaultSensor);
}
});
2015-08-21 22:03:04 -07:00
2015-08-23 13:01:34 -07:00
wrapper.add(new SensorLiveGraph(config.getChild("top"), defaultSensor, switchToGauge));
2022-04-15 13:37:31 -07:00
wrapper.add(new SensorLiveGraph(config.getChild("bottom"), Sensor.RPMValue, switchToGauge));
2015-08-21 22:03:04 -07:00
return wrapper;
}
2019-04-23 17:49:06 -07:00
private JComponent createGauge(final Sensor sensor) {
2015-08-21 22:03:04 -07:00
SensorGauge.GaugeChangeListener gaugeChangeListener = new SensorGauge.GaugeChangeListener() {
@Override
public void onSensorChange(Sensor sensor) {
config.setProperty(GAUGE_TYPE, sensor.name());
}
};
2015-08-23 13:01:34 -07:00
JMenuItem switchToLiveGraph = new JMenuItem("Switch to Live Graph");
2015-08-21 22:03:04 -07:00
switchToLiveGraph.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
wrapper.removeAllChildrenAndListeners();
config.setBoolProperty(IS_LIVE_GRAPH, true);
2019-04-23 16:49:01 -07:00
createLiveBarElement(sensor);
2015-08-21 22:03:04 -07:00
}
});
wrapper.setLayout(new BorderLayout());
2020-06-25 18:40:36 -07:00
SensorGauge.createGaugeBody(uiContext, sensor, wrapper, gaugeChangeListener, switchToLiveGraph);
2015-08-21 22:03:04 -07:00
return wrapper;
}
2020-06-25 18:40:36 -07:00
public static JComponent read(UIContext uiContext, final Node config, Sensor defaultSensor) {
2015-08-21 22:03:04 -07:00
if (config.getBoolProperty(IS_LIVE_GRAPH)) {
2020-06-25 18:40:36 -07:00
return new GaugesGridElement(uiContext, config).createLiveBarElement(defaultSensor);
2015-08-21 22:03:04 -07:00
}
String gaugeName = config.getProperty(GAUGE_TYPE, defaultSensor.name());
2015-08-23 13:01:34 -07:00
Sensor sensor = Sensor.lookup(gaugeName, defaultSensor);
2020-06-25 18:40:36 -07:00
return new GaugesGridElement(uiContext, config).createGauge(sensor);
2015-08-21 22:03:04 -07:00
}
2015-08-21 21:01:32 -07:00
}