auto-sync

This commit is contained in:
rusEfi 2015-05-03 16:04:48 -04:00
parent c1fa78dba7
commit ad75dfab52
6 changed files with 223 additions and 87 deletions

View File

@ -0,0 +1,116 @@
package com.rusefi;
import com.rusefi.io.CommandQueue;
import com.rusefi.ui.MessagesView;
import com.rusefi.ui.util.UiUtils;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BenchTestPane {
private final JPanel content = new JPanel(new GridLayout(2, 3));
public BenchTestPane() {
content.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
content.add(createFanTest());
content.add(createFuelPumpTest());
content.add(createSparkTest());
content.add(createInjectorTest());
content.add(new JLabel());
content.add(new MessagesView().messagesScroll);
}
private Component createFanTest() {
BenchTestPanel panel = new BenchTestPanel("radiator_fan.jpg", "Radiator Fan") {
@NotNull
protected String getCommand() {
return "fanbench";
}
};
return panel.getContent();
}
private Component createFuelPumpTest() {
BenchTestPanel panel = new BenchTestPanel("fuel_pump.jpg", "Fuel Pump") {
@NotNull
protected String getCommand() {
return "fuelpumpbench";
}
};
return panel.getContent();
}
private Component createSparkTest() {
final JComboBox<Integer> indexes = createIndexCombo();
JLabel label = new JLabel("Spark #", UiUtils.loadIcon("spark.jpg"), SwingConstants.LEFT);
BenchTestPanel panel = new BenchTestPanel(label, indexes) {
@Override
protected String getCommand() {
return "sparkbench2 1000 " + indexes.getSelectedItem() + " 5 333 3";
}
};
return panel.getContent();
}
private Component createInjectorTest() {
final JComboBox<Integer> indexes = createIndexCombo();
JLabel label = new JLabel("Injector #", UiUtils.loadIcon("injector.png"), SwingConstants.LEFT);
BenchTestPanel panel = new BenchTestPanel(label, indexes) {
@Override
protected String getCommand() {
return "fuelbench2 1000 " + indexes.getSelectedItem() + " 5 333 3";
}
};
return panel.getContent();
}
@NotNull
private JComboBox<Integer> createIndexCombo() {
JComboBox<Integer> indexes = new JComboBox<>();
for (int i = 1; i <= 12; i++) {
indexes.addItem(i);
}
return indexes;
}
public JPanel getContent() {
return content;
}
private abstract static class BenchTestPanel {
final JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
final JButton test = new JButton("Test");
public BenchTestPanel(String imageFileName, String label) {
this(new JLabel(label, UiUtils.loadIcon(imageFileName), SwingConstants.LEFT));
}
public BenchTestPanel(String label) {
this(new JLabel(label));
}
public BenchTestPanel(JComponent... components) {
for (JComponent component : components)
panel.add(component);
panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
panel.add(test);
test.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CommandQueue.getInstance().write(getCommand());
}
});
}
protected abstract String getCommand();
public Component getContent() {
return UiUtils.wrap(panel);
}
}
}

View File

@ -31,7 +31,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
* @see com.rusefi.StartupFrame
*/
public class Launcher {
public static final int CONSOLE_VERSION = 20150502;
public static final int CONSOLE_VERSION = 20150503;
public static final boolean SHOW_STIMULATOR = false;
private static final String TAB_INDEX = "main_tab";
protected static final String PORT_KEY = "port";
@ -99,7 +99,7 @@ public class Launcher {
// tabbedPane.add("Wizards", new Wizard().createPane());
tabbedPane.add("Settings", new SettingsTab().createPane());
tabbedPane.add("Bench Test", new BenchTestPane().getContent());
if (!LinkManager.isLogViewerMode(port)) {
int selectedIndex = getConfig().getRoot().getIntProperty(TAB_INDEX, 2);

View File

@ -18,7 +18,6 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -56,7 +55,7 @@ public class StartupFrame {
}
public static void setAppIcon(JFrame frame) {
ImageIcon icon = loadIcon(APPICON);
ImageIcon icon = UiUtils.loadIcon(APPICON);
if (icon != null)
frame.setIconImage(icon.getImage());
}
@ -103,7 +102,7 @@ public class StartupFrame {
JPanel rightPanel = new JPanel(new VerticalFlowLayout());
ImageIcon logoIcon = loadIcon(LOGO);
ImageIcon logoIcon = UiUtils.loadIcon(LOGO);
if (logoIcon != null) {
JLabel logo = new JLabel(logoIcon);
logo.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
@ -159,11 +158,4 @@ public class StartupFrame {
return combo;
}
public static ImageIcon loadIcon(String strPath) {
URL imgURL = StartupFrame.class.getResource(strPath);
if (imgURL != null)
return new ImageIcon(imgURL);
else
return null;
}
}

View File

@ -1,19 +1,13 @@
package com.rusefi.ui;
import com.rusefi.core.MessagesCentral;
import com.rusefi.io.CommandQueue;
import com.rusefi.io.serial.PortHolder;
import com.rusefi.ui.storage.Node;
import com.rusefi.ui.util.UiUtils;
import com.rusefi.ui.widgets.AnyCommand;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.rusefi.ui.util.LocalizedMessages.CLEAR;
import static com.rusefi.ui.util.LocalizedMessages.PAUSE;
@ -28,54 +22,25 @@ import static com.rusefi.ui.util.LocalizedMessages.PAUSE;
* @see AnyCommand
*/
public class MessagesPanel {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH_mm");
private static final int MAX_SIZE = 50000;
private static final String FONT_SIZE = "font_size";
private static final String FONT_NAME = "font_name";
private final AnyCommand anyCommand;
private final JTextPane messages = new JTextPane() {
@Override
public void setVisible(boolean aFlag) {
super.setVisible(aFlag);
// todo: get focus on startup somehow
// anyCommand.getText().requestFocus();
}
};
private boolean isPaused;
private final Style bold;
private final Style italic;
private final MessagesView messagesView = new MessagesView();
private final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
private final JScrollPane messagesScroll = new JScrollPane(messages, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public MessagesPanel(Node config, boolean listenToCommands) {
JPanel middlePanel = new JPanel(new BorderLayout());
middlePanel.add(messagesScroll, BorderLayout.CENTER);
middlePanel.add(messagesView.messagesScroll, BorderLayout.CENTER);
// buttonPanel.setBorder(BorderFactory.createLineBorder(Color.cyan));
StyledDocument d = (StyledDocument) messages.getDocument();
bold = d.addStyle("StyleName", null);
bold.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
italic = d.addStyle("StyleName", null);
italic.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);
MessagesCentral.getInstance().addListener(new MessagesCentral.MessageListener() {
@Override
public void onMessage(Class clazz, String message) {
final String date = DATE_FORMAT.format(new Date());
if (!isPaused)
append(date + ": " + clazz.getSimpleName() + ": " + message, clazz);
}
});
JButton resetButton = new JButton(CLEAR.getMessage());
resetButton.setMnemonic('c');
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
Document d = messages.getDocument();
clearMessages(d);
messagesView.clear();
}
});
@ -84,8 +49,8 @@ public class MessagesPanel {
pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isPaused = !isPaused;
UiUtils.setPauseButtonText(pauseButton, isPaused);
messagesView.isPaused = !messagesView.isPaused;
UiUtils.setPauseButtonText(pauseButton, messagesView.isPaused);
}
});
@ -95,51 +60,20 @@ public class MessagesPanel {
buttonPanel.add(anyCommand.getContent());
}
private void clearMessages(Document d) {
try {
d.remove(0, d.getLength());
} catch (BadLocationException e) {
throw new IllegalStateException(e);
}
}
private void append(String line, Class clazz) {
Document d = messages.getDocument();
if (d.getLength() > MAX_SIZE)
clearMessages(d);
try {
d.insertString(d.getLength(), line + "\r\n", getStyle(clazz));
messages.select(d.getLength(), d.getLength());
} catch (BadLocationException e) {
throw new IllegalStateException(e);
}
}
private AttributeSet getStyle(Class clazz) {
/**
* this is ugly as hell, but that's so much better then nothing...
*/
if (clazz == CommandQueue.class)
return bold;
if (clazz == PortHolder.class)
return italic;
return null;
}
public JPanel getButtonPanel() {
return buttonPanel;
}
public JScrollPane getMessagesScroll() {
return messagesScroll;
return messagesView.messagesScroll;
}
public Font getFont() {
return messages.getFont();
return messagesView.messages.getFont();
}
public void setFont(Font font, Node config) {
messages.setFont(font);
messagesView.messages.setFont(font);
config.setProperty(FONT_SIZE, font.getSize());
config.setProperty(FONT_NAME, font.getName());
}

View File

@ -0,0 +1,83 @@
package com.rusefi.ui;
import com.rusefi.core.MessagesCentral;
import com.rusefi.io.CommandQueue;
import com.rusefi.io.serial.PortHolder;
import javax.swing.*;
import javax.swing.text.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MessagesView {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH_mm");
private static final int MAX_SIZE = 50000;
private final Style bold;
private final Style italic;
protected boolean isPaused;
protected final JTextPane messages = new JTextPane() {
@Override
public void setVisible(boolean aFlag) {
super.setVisible(aFlag);
// todo: get focus on startup somehow
// anyCommand.getText().requestFocus();
}
};
public final JScrollPane messagesScroll = new JScrollPane(messages, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public MessagesView() {
StyledDocument d = (StyledDocument) messages.getDocument();
bold = d.addStyle("StyleName", null);
bold.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
italic = d.addStyle("StyleName", null);
italic.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);
MessagesCentral.getInstance().addListener(new MessagesCentral.MessageListener() {
@Override
public void onMessage(Class clazz, String message) {
final String date = DATE_FORMAT.format(new Date());
if (!isPaused)
append(date + ": " + clazz.getSimpleName() + ": " + message, clazz);
}
});
}
private void append(String line, Class clazz) {
Document d = messages.getDocument();
if (d.getLength() > MAX_SIZE)
clearMessages(d);
try {
d.insertString(d.getLength(), line + "\r\n", getStyle(clazz));
messages.select(d.getLength(), d.getLength());
} catch (BadLocationException e) {
throw new IllegalStateException(e);
}
}
private AttributeSet getStyle(Class clazz) {
/**
* this is ugly as hell, but that's so much better then nothing...
*/
if (clazz == CommandQueue.class)
return bold;
if (clazz == PortHolder.class)
return italic;
return null;
}
private void clearMessages(Document d) {
try {
d.remove(0, d.getLength());
} catch (BadLocationException e) {
throw new IllegalStateException(e);
}
}
public void clear() {
Document d = messages.getDocument();
clearMessages(d);
}
}

View File

@ -1,5 +1,7 @@
package com.rusefi.ui.util;
import com.rusefi.StartupFrame;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
@ -8,6 +10,7 @@ import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import static com.rusefi.ui.util.LocalizedMessages.PAUSE;
import static com.rusefi.ui.util.LocalizedMessages.RESUME;
@ -103,4 +106,12 @@ public class UiUtils {
imageButton.setMnemonic('s');
return imageButton;
}
public static ImageIcon loadIcon(String strPath) {
URL imgURL = StartupFrame.class.getResource(strPath);
if (imgURL != null)
return new ImageIcon(imgURL);
else
return null;
}
}