diff --git a/java_console/ui/src/com/irnems/Launcher.java b/java_console/ui/src/com/irnems/Launcher.java index 739c06c073..9850ec7709 100644 --- a/java_console/ui/src/com/irnems/Launcher.java +++ b/java_console/ui/src/com/irnems/Launcher.java @@ -5,6 +5,7 @@ import com.irnems.core.MessagesCentral; import com.rusefi.*; import com.rusefi.io.LinkManager; import com.rusefi.ui.*; +import com.rusefi.ui.storage.PersistentConfiguration; import jssc.SerialPortList; import javax.swing.*; @@ -19,9 +20,11 @@ import javax.swing.*; * @see WavePanel */ public class Launcher extends FrameHelper { - public static final int CONSOLE_VERSION = 20150118; + public static final int CONSOLE_VERSION = 20150125; public static final boolean SHOW_STIMULATOR = true; + public static final String TAB_INDEX = "main_tab"; private final String port; + private final JTabbedPane tabbedPane = new JTabbedPane(); public static int defaultFontSize; @@ -33,7 +36,6 @@ public class Launcher extends FrameHelper { FileLog.MAIN.logLine("Console " + CONSOLE_VERSION); - JTabbedPane tabbedPane = new JTabbedPane(); if (LinkManager.isLogViewerMode(port)) tabbedPane.add("Log Viewer", new LogViewer()); @@ -52,12 +54,14 @@ public class Launcher extends FrameHelper { tabbedPane.add("ECU stimulation", stimulator.getPanel()); } // tabbedPane.addTab("live map adjustment", new Live3DReport().getControl()); - tabbedPane.add("Messages", new MsgPanel(true).getContent()); + tabbedPane.add("Messages", new MessagesPane().getContent()); tabbedPane.add("Wizards", new Wizard().createPane()); - if (!LinkManager.isLogViewerMode(port)) - tabbedPane.setSelectedIndex(2); + if (!LinkManager.isLogViewerMode(port)) { + int selectedIndex = PersistentConfiguration.getInstance().getIntProperty("main_tab", 2); + tabbedPane.setSelectedIndex(selectedIndex); + } showFrame(tabbedPane); } @@ -89,10 +93,14 @@ public class Launcher extends FrameHelper { * looks like reconnectTimer in {@link RpmPanel} keeps AWT alive. Simplest solution would be to 'exit' */ SimulatorHelper.onWindowClosed(); + PersistentConfiguration.getInstance().setProperty("version", CONSOLE_VERSION); + PersistentConfiguration.getInstance().setProperty(TAB_INDEX, tabbedPane.getSelectedIndex()); + PersistentConfiguration.getInstance().save(); System.exit(0); } public static void main(final String[] args) throws Exception { + PersistentConfiguration.getInstance().load(); Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler()); VersionChecker.start(); SwingUtilities.invokeAndWait(new Runnable() { diff --git a/java_console/ui/src/com/rusefi/test/MsgPanelSandbox.java b/java_console/ui/src/com/rusefi/test/MsgPanelSandbox.java index 7bdeac64d6..f7e2cdf6f8 100644 --- a/java_console/ui/src/com/rusefi/test/MsgPanelSandbox.java +++ b/java_console/ui/src/com/rusefi/test/MsgPanelSandbox.java @@ -1,17 +1,15 @@ package com.rusefi.test; import com.rusefi.ui.FrameHelper; -import com.rusefi.ui.MsgPanel; +import com.rusefi.ui.MessagesPane; +import com.rusefi.ui.MessagesPanel; public class MsgPanelSandbox extends FrameHelper { private MsgPanelSandbox() { - showFrame(new MsgPanel(true).getContent()); - + showFrame(new MessagesPane().getContent()); } public static void main(String[] args) { - new MsgPanelSandbox(); - } } \ No newline at end of file diff --git a/java_console/ui/src/com/rusefi/ui/MessagesPane.java b/java_console/ui/src/com/rusefi/ui/MessagesPane.java new file mode 100644 index 0000000000..fcd49f8e02 --- /dev/null +++ b/java_console/ui/src/com/rusefi/ui/MessagesPane.java @@ -0,0 +1,42 @@ +package com.rusefi.ui; + +import com.rusefi.ui.widgets.IdleLabel; + +import javax.swing.*; +import java.awt.*; + +public class MessagesPane { + private final JPanel content = new JPanel(new BorderLayout()) { + @Override + public Dimension getPreferredSize() { + Dimension size = super.getPreferredSize(); + return new Dimension(250, size.height); + } + }; + + public MessagesPane() { + MessagesPanel messagesPanel = new MessagesPanel(); + content.setBorder(BorderFactory.createLineBorder(Color.red)); + + JPanel middlePanel = new JPanel(new BorderLayout()); + middlePanel.add(messagesPanel.getMessagesScroll(), BorderLayout.CENTER); + middlePanel.add(new RecentCommands().getContent(), BorderLayout.EAST); + + content.add(middlePanel, BorderLayout.CENTER); + + messagesPanel.getButtonPanel().add(new RpmControl().getContent()); + content.add(messagesPanel.getButtonPanel(), BorderLayout.NORTH); + + JPanel statsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); + + statsPanel.add(new RpmControl().getContent()); + statsPanel.add(new IdleLabel()); + statsPanel.add(new WarningPanel().getPanel()); + + content.add(statsPanel, BorderLayout.SOUTH); + } + + public JComponent getContent() { + return content; + } +} \ No newline at end of file diff --git a/java_console/ui/src/com/rusefi/ui/MsgPanel.java b/java_console/ui/src/com/rusefi/ui/MessagesPanel.java similarity index 65% rename from java_console/ui/src/com/rusefi/ui/MsgPanel.java rename to java_console/ui/src/com/rusefi/ui/MessagesPanel.java index b7429f003d..706779e758 100644 --- a/java_console/ui/src/com/rusefi/ui/MsgPanel.java +++ b/java_console/ui/src/com/rusefi/ui/MessagesPanel.java @@ -4,7 +4,6 @@ import com.irnems.core.MessagesCentral; import com.rusefi.io.CommandQueue; import com.rusefi.io.serial.PortHolder; import com.rusefi.ui.widgets.AnyCommand; -import com.rusefi.ui.widgets.IdleLabel; import javax.swing.*; import javax.swing.text.*; @@ -23,40 +22,29 @@ import java.util.Date; * * @see AnyCommand */ -public class MsgPanel { +public class MessagesPanel { private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH_mm"); private static final int MAX_SIZE = 50000; - private final JTextPane msg = new JTextPane(); + private final JTextPane messages = new JTextPane(); private boolean isPaused; private final Style bold; private final Style italic; - private final JPanel content = new JPanel(new BorderLayout()) { - @Override - public Dimension getPreferredSize() { - Dimension size = super.getPreferredSize(); - return new Dimension(250, size.height); - } - }; - - public MsgPanel(boolean needsRpmControl) { - content.setBorder(BorderFactory.createLineBorder(Color.green)); - JScrollPane pane = new JScrollPane(msg, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + 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() { JPanel middlePanel = new JPanel(new BorderLayout()); - middlePanel.add(pane, BorderLayout.CENTER); - if (needsRpmControl) - middlePanel.add(new RecentCommands().getContent(), BorderLayout.EAST); + middlePanel.add(messagesScroll, BorderLayout.CENTER); + buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red)); - - StyledDocument d = (StyledDocument) msg.getDocument(); + 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); - content.add(middlePanel, BorderLayout.CENTER); MessagesCentral.getInstance().addListener(new MessagesCentral.MessageListener() { @Override public void onMessage(Class clazz, String message) { @@ -71,7 +59,7 @@ public class MsgPanel { resetButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { - Document d = msg.getDocument(); + Document d = messages.getDocument(); clearMessages(d); } }); @@ -86,25 +74,9 @@ public class MsgPanel { } }); - JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0)); buttonPanel.add(resetButton); buttonPanel.add(pauseButton); buttonPanel.add(new AnyCommand()); - if (needsRpmControl) - buttonPanel.add(new RpmControl().getContent()); - content.add(buttonPanel, BorderLayout.NORTH); - - JPanel statsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); - - statsPanel.add(new RpmControl().getContent()); - statsPanel.add(new IdleLabel()); - statsPanel.add(new WarningPanel().getPanel()); - - content.add(statsPanel, BorderLayout.SOUTH); - } - - public JPanel getContent() { - return content; } private void clearMessages(Document d) { @@ -116,12 +88,12 @@ public class MsgPanel { } private void append(String line, Class clazz) { - Document d = msg.getDocument(); + Document d = messages.getDocument(); if (d.getLength() > MAX_SIZE) clearMessages(d); try { d.insertString(d.getLength(), line + "\r\n", getStyle(clazz)); - msg.select(d.getLength(), d.getLength()); + messages.select(d.getLength(), d.getLength()); } catch (BadLocationException e) { throw new IllegalStateException(e); } @@ -137,4 +109,12 @@ public class MsgPanel { return italic; return null; } + + public JPanel getButtonPanel() { + return buttonPanel; + } + + public JScrollPane getMessagesScroll() { + return messagesScroll; + } } diff --git a/java_console/ui/src/com/rusefi/ui/RecentCommands.java b/java_console/ui/src/com/rusefi/ui/RecentCommands.java index 84c8336227..6de2d6a1a2 100644 --- a/java_console/ui/src/com/rusefi/ui/RecentCommands.java +++ b/java_console/ui/src/com/rusefi/ui/RecentCommands.java @@ -9,11 +9,14 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.Map; +/** + * This UI element displays an array of buttons, each for one of the recently used commands + */ + public class RecentCommands { - private final static int NUMBER_OF_COMMANDS = 12; + private final static int NUMBER_OF_COMMANDS = 20; private final JPanel content = new JPanel(new GridLayout(NUMBER_OF_COMMANDS, 1)); diff --git a/java_console/ui/src/com/rusefi/ui/RpmPanel.java b/java_console/ui/src/com/rusefi/ui/RpmPanel.java index 98f9a4fcaa..f898adf8c1 100644 --- a/java_console/ui/src/com/rusefi/ui/RpmPanel.java +++ b/java_console/ui/src/com/rusefi/ui/RpmPanel.java @@ -42,10 +42,14 @@ public class RpmPanel { startConnectionWatchDog(); + JPanel smallMessagePanel = new JPanel(new BorderLayout()); + MessagesPanel mp = new MessagesPanel(); + smallMessagePanel.add(BorderLayout.NORTH, mp.getButtonPanel()); + smallMessagePanel.add(BorderLayout.CENTER, mp.getMessagesScroll()); + JPanel msgPanel = new JPanel(new BorderLayout()); msgPanel.add(new AnyCommand(), BorderLayout.NORTH); - msgPanel.add(new MsgPanel(false).getContent(), BorderLayout.CENTER); - + msgPanel.add(smallMessagePanel, BorderLayout.CENTER); JComponent rpmPanel = new JPanel(new BorderLayout()); rpmPanel.setBorder(BorderFactory.createLineBorder(Color.white)); diff --git a/java_console/ui/src/com/rusefi/ui/storage/PersistentConfiguration.java b/java_console/ui/src/com/rusefi/ui/storage/PersistentConfiguration.java new file mode 100644 index 0000000000..257758e5ca --- /dev/null +++ b/java_console/ui/src/com/rusefi/ui/storage/PersistentConfiguration.java @@ -0,0 +1,56 @@ +package com.rusefi.ui.storage; + +import com.irnems.FileLog; + +import javax.swing.*; +import java.beans.XMLDecoder; +import java.beans.XMLEncoder; +import java.io.*; +import java.util.HashMap; +import java.util.Map; + +public class PersistentConfiguration { + private static final PersistentConfiguration INSTANCE = new PersistentConfiguration(); + public static final String CONFIG_FILE_NAME = "rusefi_console_properties.xml"; + + private Map config = new HashMap<>(); + + public static PersistentConfiguration getInstance() { + return INSTANCE; + } + + public void load() { + try { + XMLDecoder e = new XMLDecoder(new BufferedInputStream(new FileInputStream(CONFIG_FILE_NAME))); + config = (Map) e.readObject(); + e.close(); + } catch (Throwable e) { + FileLog.rlog("Error reading from " + CONFIG_FILE_NAME); + } + } + + public void save() { + XMLEncoder e = null; + try { + e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(CONFIG_FILE_NAME))); + e.writeObject(config); + e.close(); + System.out.println("Saved to " + CONFIG_FILE_NAME); + } catch (FileNotFoundException e1) { + FileLog.rlog("Error saving " + CONFIG_FILE_NAME); + } + } + + public int getIntProperty(String key, int defaultValue) { + try { + String value = config.get(key); + return Integer.parseInt(value); + } catch (Throwable e) { + return defaultValue; + } + } + + public void setProperty(String key, int value) { + config.put(key, "" + value); + } +} \ No newline at end of file