TS plugin

This commit is contained in:
rusefi 2020-06-17 01:14:45 -04:00
parent 1da3395fd3
commit 5727723dcc
5 changed files with 86 additions and 18 deletions

View File

@ -15,8 +15,11 @@ import javax.swing.*;
import javax.xml.bind.JAXBException;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.TreeMap;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
/**
* TsPlugin launcher creates an instance of this class via reflection.
@ -132,4 +135,24 @@ public class PluginEntry implements TsPluginBody {
return null;
}
}
/**
* this method is invoked by refection
*
* @see TsPluginBody#GET_VERSION
*/
@SuppressWarnings("unused")
public static String getVersion() {
try {
InputStream stream = PluginEntry.class.getResourceAsStream("/META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(stream);
Attributes attributes = manifest.getMainAttributes();
String result = attributes.getValue("Built-Date");
return result == null ? "Unknown version" : result;
} catch (IOException e) {
return "Unknown version";
}
}
}

View File

@ -0,0 +1 @@
*.jar

View File

@ -3,6 +3,8 @@ package com.rusefi.ts_plugin;
import javax.swing.*;
public interface TsPluginBody {
String GET_VERSION = "getVersion";
JComponent getContent();
void close();

View File

@ -10,12 +10,11 @@ import javax.swing.*;
* by the way TS installs stuff into %user%\.efianalytics\TunerStudio\plugins folder
*/
public class TsPluginLauncher implements ApplicationPlugin {
static final String VERSION = "alpha2020";
public static final String VERSION = "alpha2020";
private final JPanel content = new JPanel(new VerticalFlowLayout());
public TsPluginLauncher() {
content.add(new JLabel("" + VERSION));
content.add(new Updater().getContent());
}

View File

@ -1,33 +1,70 @@
package com.rusefi.ts_plugin;
import com.rusefi.autoupdate.AutoupdateUtil;
import org.putgemin.VerticalFlowLayout;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import static com.rusefi.ts_plugin.TsPluginLauncher.VERSION;
public class Updater {
private static final String PLUGIN_BODY_JAR = "rusefi_plugin_body.jar";
private static final String TITLE = "rusEFI plugin installer " + TsPluginLauncher.VERSION;
private static final String TITLE = "rusEFI plugin installer " + VERSION;
private final JPanel content = new JPanel();
private final JPanel content = new JPanel(new VerticalFlowLayout());
public Updater() {
JButton download = new JButton("Download");
content.add(new JLabel("" + VERSION));
String version = null;
if (new File(PLUGIN_BODY_JAR).exists()) {
version = getVersion();
}
JButton download = new JButton("Download latest");
if (version != null) {
JButton run = new JButton("Run " + version);
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);
}
download.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
new Thread(() -> startDownload(download)).start();
}
});
content.add(download);
}
private String getVersion() {
try {
Class clazz = getPluginClass();
Method method = clazz.getMethod(TsPluginBody.GET_VERSION);
return (String) method.invoke(null);
} catch (NoSuchMethodException | MalformedURLException | ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
return null;
}
}
private void startDownload(JButton download) {
SwingUtilities.invokeLater(new Runnable() {
@Override
@ -42,27 +79,33 @@ public class Updater {
AutoupdateUtil.downloadAutoupdateFile(PLUGIN_BODY_JAR, connectionAndMeta.getHttpConnection(), connectionAndMeta.getCompleteFileSize(),
TITLE);
URLClassLoader jarClassLoader = AutoupdateUtil.getClassLoaderByJar(PLUGIN_BODY_JAR);
Class clazz = Class.forName("com.rusefi.ts_plugin.PluginEntry", true, jarClassLoader);
TsPluginBody instance = (TsPluginBody) clazz.newInstance();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
replaceWith(instance);
}
});
startPlugin();
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
download.setEnabled(true);
}
}
private void startPlugin() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
Class clazz = getPluginClass();
TsPluginBody instance = (TsPluginBody) clazz.newInstance();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
replaceWith(instance);
}
});
}
private static Class getPluginClass() throws MalformedURLException, ClassNotFoundException {
URLClassLoader jarClassLoader = AutoupdateUtil.getClassLoaderByJar(PLUGIN_BODY_JAR);
return Class.forName("com.rusefi.ts_plugin.PluginEntry", true, jarClassLoader);
}
private void replaceWith(TsPluginBody instance) {
content.removeAll();
content.add(instance.getContent());
AutoupdateUtil.trueLayout(content.getParent());
}
public JPanel getContent() {