diff --git a/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/TsPluginLauncher.java b/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/TsPluginLauncher.java index 7e63f1a8c3..eb4beb2bf1 100644 --- a/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/TsPluginLauncher.java +++ b/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/TsPluginLauncher.java @@ -10,13 +10,13 @@ import javax.swing.*; * by the way TS installs stuff into %user%\.efianalytics\TunerStudio\plugins folder */ public class TsPluginLauncher implements ApplicationPlugin { - static final String VERSION = "alpha2_2020"; + static final String VERSION = "alpha3_2020"; private static final String HELP_URL = "https://github.com/rusefi/rusefi/wiki/TS-Plugin"; private final JPanel content = new JPanel(new VerticalFlowLayout()); public TsPluginLauncher() { - content.add(new Updater().getContent()); + System.out.println("TsPluginLauncher " + this); } @Override @@ -61,6 +61,14 @@ public class TsPluginLauncher implements ApplicationPlugin { @Override public JComponent getPluginPanel() { + synchronized (this) { + // only create content if TS is actually planning to display this plugin instance + if (content.getComponents().length == 0) { + System.out.println("Create Updater " + this); + Updater updater = new Updater(); + content.add(updater.getContent()); + } + } return content; } diff --git a/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/Updater.java b/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/Updater.java index 81a386a95b..3fdb229e5d 100644 --- a/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/Updater.java +++ b/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/Updater.java @@ -3,6 +3,7 @@ package com.rusefi.ts_plugin; import com.rusefi.autoupdate.AutoupdateUtil; import com.rusefi.shared.ConnectionAndMeta; import com.rusefi.shared.FileUtil; +import org.jetbrains.annotations.Nullable; import org.putgemin.VerticalFlowLayout; import javax.swing.*; @@ -28,6 +29,7 @@ public class Updater { private static final ImageIcon LOGO = AutoupdateUtil.loadIcon("/rusefi_online_color_300.png"); private final JLabel countDownLabel = new JLabel(); private final AtomicInteger autoStartCounter = new AtomicInteger(4); + private TsPluginBody instance; private final Timer timer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -35,9 +37,12 @@ public class Updater { timer.stop(); try { if (shouldAutoStart) { + shouldAutoStart = false; + System.out.println("Auto-starting startPlugin"); startPlugin(); } } catch (IllegalAccessException | MalformedURLException | ClassNotFoundException | InstantiationException ex) { + ex.printStackTrace(); JOptionPane.showMessageDialog(content, "Error " + ex); } } else { @@ -46,7 +51,7 @@ public class Updater { } }); - private boolean shouldAutoStart = true; + private volatile boolean shouldAutoStart = true; public Updater() { content.add(new JLabel("" + VERSION)); @@ -60,25 +65,7 @@ public class Updater { } JButton download = new JButton("Update plugin"); - if (version != null) { - JButton run = new JButton("Run Version " + version); - run.setBackground(new Color(0x90EE90)); - run.addActionListener(new AbstractAction() { - @Override - public void actionPerformed(ActionEvent e) { - try { - startPlugin(); - } catch (IllegalAccessException | MalformedURLException | ClassNotFoundException | InstantiationException ex) { - run.setText(e.toString()); - } - } - }); - - content.add(run); - - content.add(countDownLabel); - timer.start(); - } + JButton run = createRunThisVersionButton(version); new Thread(new Runnable() { @Override @@ -107,6 +94,8 @@ public class Updater { download.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { + if (run != null) + run.setEnabled(false); cancelAutoStart(); new Thread(() -> startDownload(download)).start(); } @@ -115,6 +104,31 @@ public class Updater { content.add(download); } + @Nullable + private JButton createRunThisVersionButton(String version) { + if (version == null) + return null; + JButton run = new JButton("Run Version " + version); + run.setBackground(new Color(0x90EE90)); + run.addActionListener(new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + try { + cancelAutoStart(); + System.out.println("run startPlugin"); + startPlugin(); + } catch (IllegalAccessException | MalformedURLException | ClassNotFoundException | InstantiationException ex) { + run.setText(e.toString()); + } + } + }); + content.add(run); + + content.add(countDownLabel); + timer.start(); + return run; + } + private void cancelAutoStart() { timer.stop(); shouldAutoStart = false; @@ -131,6 +145,7 @@ public class Updater { } private void startDownload(JButton download) { + System.out.println("startDownload"); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { @@ -143,7 +158,7 @@ public class Updater { AutoupdateUtil.downloadAutoupdateFile(LOCAL_JAR_FILE_NAME, connectionAndMeta, TITLE); - + System.out.println("Downloaded, now startPlugin"); startPlugin(); } catch (Exception e) { @@ -153,8 +168,15 @@ public class Updater { } private void startPlugin() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException { + System.out.println("Starting plugin " + this); Class clazz = getPluginClass(); - TsPluginBody instance = (TsPluginBody) clazz.newInstance(); + synchronized (this) { + if (instance != null) { + System.out.println("Not starting second instance"); + return; // avoid having two instances running + } + instance = (TsPluginBody) clazz.newInstance(); + } SwingUtilities.invokeLater(new Runnable() { @Override public void run() {