auto-sync

This commit is contained in:
rusEfi 2015-08-22 01:03:04 -04:00
parent ce5d013689
commit 97dac29b7c
6 changed files with 184 additions and 41 deletions

View File

@ -1,16 +1,84 @@
package com.rusefi.ui;
import com.rusefi.core.Sensor;
import com.rusefi.ui.storage.Node;
import com.rusefi.ui.widgets.JPanelWithListener;
import com.rusefi.ui.widgets.SensorGauge;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* (c) Andrey Belomutskiy
* 8/21/2015.
*/
public class GaugesGridElement {
public static Component createGauge(Sensor sensor, SensorGauge.GaugeChangeListener gaugeChangeListener) {
return SensorGauge.createGauge(sensor, gaugeChangeListener);
private static final String GAUGE_TYPE = "gauge";
private static final String IS_LIVE_GRAPH = "type";
private final JPanelWithListener wrapper = new JPanelWithListener(new BorderLayout());
private final Node config;
private GaugesGridElement(Node config) {
this.config = config;
}
private Component createLiveBarElement(Sensor defaultSensor) {
wrapper.setLayout(new GridLayout(2, 1));
wrapper.add(new SensorLiveGraph());
wrapper.add(new SensorLiveGraph());
return wrapper;
}
private Component createGauge(final Sensor sensor) {
SensorGauge.GaugeChangeListener gaugeChangeListener = new SensorGauge.GaugeChangeListener() {
@Override
public void onSensorChange(Sensor sensor) {
config.setProperty(GAUGE_TYPE, sensor.name());
}
};
JMenuItem switchToLiveGraph = new JMenuItem("Live Graph");
switchToLiveGraph.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
wrapper.removeAllChildrenAndListeners();
config.setBoolProperty(IS_LIVE_GRAPH, true);
createLiveBarElement(sensor);
}
});
wrapper.setLayout(new BorderLayout());
SensorGauge.createGaugeBody(sensor, wrapper, gaugeChangeListener, switchToLiveGraph);
return wrapper;
}
public static Component read(final Node config, Sensor defaultSensor) {
if (config.getBoolProperty(IS_LIVE_GRAPH)) {
return new GaugesGridElement(config).createLiveBarElement(defaultSensor);
}
String gaugeName = config.getProperty(GAUGE_TYPE, defaultSensor.name());
Sensor sensor = lookup(gaugeName, defaultSensor);
return new GaugesGridElement(config).createGauge(sensor);
}
private static Sensor lookup(String gaugeName, Sensor defaultValue) {
Sensor sensor;
try {
sensor = Sensor.valueOf(Sensor.class, gaugeName);
} catch (IllegalArgumentException e) {
sensor = defaultValue;
}
return sensor;
}
}

View File

@ -47,9 +47,9 @@ public class GaugesPanel {
};
private static final String GAUGES_ROWS = "gauges_rows";
private static final String GAUGES_COLUMNS = "gauges_cols";
public static final String SHOW_MESSAGES = "show_messages";
public static final String SHOW_RPM = "show_rpm";
public static final String SPLIT_LOCATION = "SPLIT_LOCATION";
private static final String SHOW_MESSAGES = "show_messages";
private static final String SHOW_RPM = "show_rpm";
private static final String SPLIT_LOCATION = "SPLIT_LOCATION";
static {
if (DEFAULT_LAYOUT.length != SizeSelectorPanel.WIDTH * SizeSelectorPanel.HEIGHT)
@ -209,35 +209,14 @@ public class GaugesPanel {
gauges.removeAll();
for (int i = 0; i < rows * columns; i++) {
String gaugeName = config.getProperty(getKey(i), DEFAULT_LAYOUT[i].name());
Sensor sensor = lookup(i, gaugeName);
final int currentGaugeIndex = i;
gauges.add(GaugesGridElement.createGauge(sensor, new SensorGauge.GaugeChangeListener() {
@Override
public void onSensorChange(Sensor sensor) {
config.setProperty(getKey(currentGaugeIndex), sensor.name());
}
}));
Component element = GaugesGridElement.read(config.getChild("element_" + i), DEFAULT_LAYOUT[i]);
gauges.add(element);
}
saveConfig(rows, columns);
}
private Sensor lookup(int i, String gaugeName) {
Sensor sensor;
try {
sensor = Sensor.valueOf(Sensor.class, gaugeName);
} catch (IllegalArgumentException e) {
sensor = DEFAULT_LAYOUT[i];
}
return sensor;
}
@NotNull
private String getKey(int i) {
return "gauge_" + i;
}
private void saveConfig(int rows, int columns) {
config.setProperty(GAUGES_ROWS, rows);
config.setProperty(GAUGES_COLUMNS, columns);

View File

@ -0,0 +1,80 @@
package com.rusefi.ui;
import com.rusefi.ui.util.UiUtils;
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.Random;
/**
* (c) Andrey Belomutskiy
* 8/22/2015.
*/
public class SensorLiveGraph extends JPanel {
private static final int COUNT = 30;
private final LinkedList<Double> values = new LinkedList<>();
public SensorLiveGraph() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Random r = new Random();
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
addValue(r.nextInt(1221));
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
UiUtils.trueRepaint(SensorLiveGraph.this);
}
});
}
}
});
thread.setDaemon(true);
thread.start();
setBorder(BorderFactory.createLineBorder(Color.red));
}
private synchronized void addValue(double value) {
if (values.size() > COUNT)
values.removeFirst();
values.add(value);
}
@Override
public synchronized void paint(Graphics g) {
super.paint(g);
Dimension d = getSize();
double minValue = Double.MAX_VALUE;
double maxValue = Double.MIN_VALUE;
for (double value : values) {
minValue = Math.min(minValue, value);
maxValue = Math.max(maxValue, value);
}
int index = 0;
for (double value : values) {
int x = d.width * index / values.size();
int y = (int) (d.height - (value - minValue) * d.height / (maxValue - minValue));
g.drawOval(x, y, 3, 3);
index++;
}
}
//
// @Override
// public Dimension getPreferredSize() {
// return new Dimension(300, 300);
// }
}

View File

@ -40,6 +40,10 @@ public class Node {
}
}
public boolean getBoolProperty(String key) {
return getBoolProperty(key, false);
}
public boolean getBoolProperty(String key, boolean defaultValue) {
String value = (String) config.get(key);
if (value == null)
@ -59,6 +63,10 @@ public class Node {
config.put(key, "" + value);
}
public void setBoolProperty(String key, boolean b) {
setProperty(key, "" + b);
}
public String getProperty(String key, String defaultValue) {
String value = (String) config.get(key);
return value == null ? defaultValue : value;

View File

@ -30,8 +30,13 @@ public class JPanelWithListener extends JPanel {
}
public synchronized void removeAllMouseListeners() {
for(MouseListener l : listeners)
for (MouseListener l : listeners)
super.removeMouseListener(l);
listeners.clear();
}
public void removeAllChildrenAndListeners() {
removeAll();
removeAllMouseListeners();
}
}

View File

@ -22,10 +22,10 @@ public class SensorGauge {
private static final String HINT_LINE_1 = "Double-click to detach";
private static final String HINT_LINE_2 = "Right-click to change";
public static Component createGauge(Sensor sensor, GaugeChangeListener listener) {
public static Component createGauge(Sensor sensor, GaugeChangeListener listener, JMenuItem extraMenuItem) {
JPanelWithListener wrapper = new JPanelWithListener(new BorderLayout());
createGaugeBody(sensor, wrapper, listener);
createGaugeBody(sensor, wrapper, listener, extraMenuItem);
return wrapper;
}
@ -46,7 +46,8 @@ public class SensorGauge {
void onSensorChange(Sensor sensor);
}
private static void createGaugeBody(final Sensor sensor, final JPanelWithListener wrapper, final GaugeChangeListener listener) {
public static void createGaugeBody(final Sensor sensor, final JPanelWithListener wrapper, final GaugeChangeListener listener,
final JMenuItem extraMenuItem) {
final Radial gauge = createRadial(sensor.getName(), sensor.getUnits(), sensor.getMaxValue(), sensor.getMinValue());
UiUtils.setTwoLineToolTip(gauge, HINT_LINE_1, HINT_LINE_2);
@ -66,28 +67,30 @@ public class SensorGauge {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
showPopupMenu(e, wrapper, listener);
showPopupMenu(e, wrapper, listener, extraMenuItem);
} else if (e.getClickCount() == 2) {
handleDoubleClick(e, gauge, sensor);
}
}
};
gauge.addMouseListener(mouseListener);
wrapper.removeAllMouseListeners();
wrapper.removeAllChildrenAndListeners();
wrapper.addMouseListener(mouseListener);
wrapper.removeAll();
wrapper.add(gauge, BorderLayout.CENTER);
UiUtils.trueRepaint(wrapper.getParent());
UiUtils.trueLayout(wrapper.getParent());
}
private static void showPopupMenu(MouseEvent e, JPanelWithListener wrapper, GaugeChangeListener listener) {
private static void showPopupMenu(MouseEvent e, JPanelWithListener wrapper, GaugeChangeListener listener,
JMenuItem extraMenuItem) {
JPopupMenu pm = new JPopupMenu();
fillGaugeMenuItems(pm, wrapper, listener);
fillGaugeMenuItems(pm, wrapper, listener, extraMenuItem);
if (extraMenuItem != null)
pm.add(extraMenuItem);
pm.show(e.getComponent(), e.getX(), e.getY());
}
private static void fillGaugeMenuItems(JPopupMenu popupMenu, final JPanelWithListener wrapper, final GaugeChangeListener listener) {
private static void fillGaugeMenuItems(JPopupMenu popupMenu, final JPanelWithListener wrapper, final GaugeChangeListener listener, final JMenuItem extraMenuItem) {
for (final SensorCategory sc : SensorCategory.values()) {
JMenuItem cmi = new JMenu(sc.getName());
popupMenu.add(cmi);
@ -97,7 +100,7 @@ public class SensorGauge {
mi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createGaugeBody(s, wrapper, listener);
createGaugeBody(s, wrapper, listener, extraMenuItem);
listener.onSensorChange(s);
}
});
@ -116,7 +119,7 @@ public class SensorGauge {
ds.onChange(sensor);
}
};
ds.content.add(createGauge(sensor, listener), BorderLayout.CENTER);
ds.content.add(createGauge(sensor, listener, null), BorderLayout.CENTER);
ds.content.add(ds.mockControlPanel, BorderLayout.SOUTH);
ds.frame.add(ds.content);