diff --git a/java_console/ui/src/com/rusefi/ui/widgets/DetachedSensor.java b/java_console/ui/src/com/rusefi/ui/widgets/DetachedSensor.java new file mode 100644 index 0000000000..3849340d9c --- /dev/null +++ b/java_console/ui/src/com/rusefi/ui/widgets/DetachedSensor.java @@ -0,0 +1,86 @@ +package com.rusefi.ui.widgets; + +import com.irnems.core.Sensor; +import com.rusefi.io.CommandQueue; + +import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import java.awt.*; +import java.awt.event.MouseEvent; +import java.util.Arrays; +import java.util.Collection; + +/** + * (c) Andrey Belomutskiy + * 11/2/14 + */ +public class DetachedSensor { + private static final Collection MOCKABLE = Arrays.asList(Sensor.CLT, Sensor.AFR, Sensor.IAT, Sensor.MAF, + Sensor.TPS); + + /** + * We need to trick the JSlider into displaying float values + */ + private static final int _5_VOLTS_WITH_DECIMAL = 50; + + private Sensor sensor; + private int width; + JPanel content = new JPanel(new BorderLayout()); + JFrame frame; + JPanel mockControlPanel = new JPanel(new BorderLayout()); + + public DetachedSensor(Sensor sensor, int width) { + this.sensor = sensor; + this.width = width; + frame = new JFrame(); + frame.setAlwaysOnTop(true); + onChange(sensor); + } + + public void show(MouseEvent e) { + frame.setVisible(true); + frame.setLocation(e.getXOnScreen(), e.getYOnScreen()); + } + + public void onChange(Sensor sensor) { + this.sensor = sensor; + frame.setTitle(sensor.getName()); + showMockControl(); + } + + public void showMockControl() { + mockControlPanel.removeAll(); + boolean isMockable = MOCKABLE.contains(sensor); + if (isMockable) { + Component mockComponent = createMockVoltageSlider(sensor); + mockControlPanel.add(mockComponent); + } + UpDownImage.trueLayout(content); + int size = width; + int h = isMockable ? (int) (size * 1.5) : size; + frame.setSize(size, h); + } + + + private static Component createMockVoltageSlider(final Sensor sensor) { + /** + */ + final JSlider slider = new JSlider(0, _5_VOLTS_WITH_DECIMAL); + slider.setLabelTable(SensorGauge.SLIDER_LABELS); + slider.setPaintLabels(true); + slider.setPaintTicks(true); + slider.setMajorTickSpacing(10); + slider.setMinorTickSpacing(5); + + slider.addChangeListener(new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + double value = slider.getValue() / 10.0; + CommandQueue.getInstance().write("set_mock_" + sensor.name().toLowerCase() + "_voltage " + value); + } + }); + + return slider; + } +} diff --git a/java_console/ui/src/com/rusefi/ui/widgets/SensorGauge.java b/java_console/ui/src/com/rusefi/ui/widgets/SensorGauge.java index 3bbc9b9346..756cf55498 100644 --- a/java_console/ui/src/com/rusefi/ui/widgets/SensorGauge.java +++ b/java_console/ui/src/com/rusefi/ui/widgets/SensorGauge.java @@ -2,13 +2,10 @@ package com.rusefi.ui.widgets; import com.irnems.core.Sensor; import com.irnems.core.SensorCentral; -import com.rusefi.io.CommandQueue; import eu.hansolo.steelseries.gauges.Radial; import eu.hansolo.steelseries.tools.ColorDef; import javax.swing.*; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -16,8 +13,6 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.text.DecimalFormat; import java.text.Format; -import java.util.Arrays; -import java.util.Collection; import java.util.Hashtable; /** @@ -26,23 +21,24 @@ import java.util.Hashtable; */ public class SensorGauge { - private static final Collection MOCKABLE = Arrays.asList(Sensor.CLT, Sensor.AFR, Sensor.IAT, Sensor.MAF, - Sensor.TPS); - /** - * We need to trick the JSlider into displaying float values - */ - private static final int _5_VOLTS_WITH_DECIMAL = 50; public static Component createGauge(final Sensor sensor) { + return createGauge2(sensor, null); + } + + private static Component createGauge2(Sensor sensor, GaugeChangeListener listener) { JPanel wrapper = new JPanel(new BorderLayout()); - createGaugeBody(sensor, wrapper); - + createGaugeBody(sensor, wrapper, listener); return wrapper; } - private static void createGaugeBody(final Sensor sensor, final JPanel wrapper) { + interface GaugeChangeListener { + void onChange(Sensor sensor); + } + + private static void createGaugeBody(final Sensor sensor, final JPanel wrapper, final GaugeChangeListener listener) { final Radial gauge = createRadial(sensor.getName(), sensor.getUnits(), sensor.getMaxValue(), sensor.getMinValue()); UpDownImage.setTwoLineToolTip(gauge, "Double-click to detach", "Right-click to change"); @@ -60,7 +56,7 @@ public class SensorGauge { @Override public void mouseClicked(MouseEvent e) { if (SwingUtilities.isRightMouseButton(e)) { - showPopupMenu(e, wrapper); + showPopupMenu(e, wrapper, listener); } else if (e.getClickCount() == 2) { handleDoubleClick(e, gauge, sensor); } @@ -72,21 +68,23 @@ public class SensorGauge { UpDownImage.trueLayout(wrapper.getParent()); } - private static void showPopupMenu(MouseEvent e, JPanel wrapper) { + private static void showPopupMenu(MouseEvent e, JPanel wrapper, GaugeChangeListener listener) { JPopupMenu pm = new JPopupMenu(); JMenu gauges = new JMenu("Gauges..."); - fillGaugeItems(gauges, wrapper); + fillGaugeItems(gauges, wrapper, listener); pm.add(gauges); pm.show(e.getComponent(), e.getX(), e.getY()); } - private static void fillGaugeItems(JMenu gauges, final JPanel wrapper) { + private static void fillGaugeItems(JMenu gauges, final JPanel wrapper, final GaugeChangeListener listener) { for (final Sensor s : Sensor.values()) { JMenuItem mi = new JMenuItem(s.getName()); mi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - createGaugeBody(s, wrapper); + createGaugeBody(s, wrapper, listener); + if (listener != null) + listener.onChange(s); } }); gauges.add(mi); @@ -94,26 +92,24 @@ public class SensorGauge { } private static void handleDoubleClick(MouseEvent e, Radial gauge, Sensor sensor) { - int size = gauge.getSize().width; - JFrame n = new JFrame(sensor.getName()); - boolean isMockable = MOCKABLE.contains(sensor); - int h = isMockable ? (int) (size * 1.5) : size; + int width = gauge.getSize().width; + final DetachedSensor ds = new DetachedSensor(sensor, width); - JPanel content = new JPanel(new BorderLayout()); + GaugeChangeListener listener = new GaugeChangeListener() { + @Override + public void onChange(Sensor sensor) { + ds.onChange(sensor); + } + }; + ds.content.add(createGauge2(sensor, listener), BorderLayout.CENTER); + ds.content.add(ds.mockControlPanel, BorderLayout.SOUTH); - content.add(createGauge(sensor), BorderLayout.CENTER); - if (isMockable) - content.add(createMockVoltageSlider(sensor), BorderLayout.SOUTH); - - n.setSize(size, h); - n.setAlwaysOnTop(true); - n.add(content); - n.setVisible(true); - n.setLocation(e.getXOnScreen(), e.getYOnScreen()); + ds.frame.add(ds.content); + ds.show(e); } - private final static Hashtable SLIDER_LABELS = new Hashtable(); + final static Hashtable SLIDER_LABELS = new Hashtable(); static { Format f = new DecimalFormat("0.0"); @@ -125,27 +121,6 @@ public class SensorGauge { } - private static Component createMockVoltageSlider(final Sensor sensor) { - /** - */ - final JSlider slider = new JSlider(0, _5_VOLTS_WITH_DECIMAL); - slider.setLabelTable(SLIDER_LABELS); - slider.setPaintLabels(true); - slider.setPaintTicks(true); - slider.setMajorTickSpacing(10); - slider.setMinorTickSpacing(5); - - slider.addChangeListener(new ChangeListener() { - @Override - public void stateChanged(ChangeEvent e) { - double value = slider.getValue() / 10.0; - CommandQueue.getInstance().write("set_mock_" + sensor.name().toLowerCase() + "_voltage " + value); - } - }); - - return slider; - } - public static Radial createRadial(String title, String units, double maxValue, double minValue) { // final Section[] SECTIONS = // {