TS plugin
This commit is contained in:
parent
1da3395fd3
commit
5727723dcc
|
@ -15,8 +15,11 @@ import javax.swing.*;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
import java.util.jar.Attributes;
|
||||||
|
import java.util.jar.Manifest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TsPlugin launcher creates an instance of this class via reflection.
|
* TsPlugin launcher creates an instance of this class via reflection.
|
||||||
|
@ -132,4 +135,24 @@ public class PluginEntry implements TsPluginBody {
|
||||||
return null;
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
*.jar
|
|
@ -3,6 +3,8 @@ package com.rusefi.ts_plugin;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
public interface TsPluginBody {
|
public interface TsPluginBody {
|
||||||
|
String GET_VERSION = "getVersion";
|
||||||
|
|
||||||
JComponent getContent();
|
JComponent getContent();
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
|
|
@ -10,12 +10,11 @@ import javax.swing.*;
|
||||||
* by the way TS installs stuff into %user%\.efianalytics\TunerStudio\plugins folder
|
* by the way TS installs stuff into %user%\.efianalytics\TunerStudio\plugins folder
|
||||||
*/
|
*/
|
||||||
public class TsPluginLauncher implements ApplicationPlugin {
|
public class TsPluginLauncher implements ApplicationPlugin {
|
||||||
|
static final String VERSION = "alpha2020";
|
||||||
|
|
||||||
public static final String VERSION = "alpha2020";
|
|
||||||
private final JPanel content = new JPanel(new VerticalFlowLayout());
|
private final JPanel content = new JPanel(new VerticalFlowLayout());
|
||||||
|
|
||||||
public TsPluginLauncher() {
|
public TsPluginLauncher() {
|
||||||
content.add(new JLabel("" + VERSION));
|
|
||||||
content.add(new Updater().getContent());
|
content.add(new Updater().getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,33 +1,70 @@
|
||||||
package com.rusefi.ts_plugin;
|
package com.rusefi.ts_plugin;
|
||||||
|
|
||||||
import com.rusefi.autoupdate.AutoupdateUtil;
|
import com.rusefi.autoupdate.AutoupdateUtil;
|
||||||
|
import org.putgemin.VerticalFlowLayout;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
|
||||||
|
import static com.rusefi.ts_plugin.TsPluginLauncher.VERSION;
|
||||||
|
|
||||||
public class Updater {
|
public class Updater {
|
||||||
private static final String PLUGIN_BODY_JAR = "rusefi_plugin_body.jar";
|
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() {
|
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() {
|
download.addActionListener(new AbstractAction() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
new Thread(() -> startDownload(download)).start();
|
new Thread(() -> startDownload(download)).start();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
content.add(download);
|
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) {
|
private void startDownload(JButton download) {
|
||||||
SwingUtilities.invokeLater(new Runnable() {
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -42,9 +79,15 @@ public class Updater {
|
||||||
AutoupdateUtil.downloadAutoupdateFile(PLUGIN_BODY_JAR, connectionAndMeta.getHttpConnection(), connectionAndMeta.getCompleteFileSize(),
|
AutoupdateUtil.downloadAutoupdateFile(PLUGIN_BODY_JAR, connectionAndMeta.getHttpConnection(), connectionAndMeta.getCompleteFileSize(),
|
||||||
TITLE);
|
TITLE);
|
||||||
|
|
||||||
URLClassLoader jarClassLoader = AutoupdateUtil.getClassLoaderByJar(PLUGIN_BODY_JAR);
|
startPlugin();
|
||||||
|
|
||||||
Class clazz = Class.forName("com.rusefi.ts_plugin.PluginEntry", true, jarClassLoader);
|
} 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();
|
TsPluginBody instance = (TsPluginBody) clazz.newInstance();
|
||||||
SwingUtilities.invokeLater(new Runnable() {
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -52,17 +95,17 @@ public class Updater {
|
||||||
replaceWith(instance);
|
replaceWith(instance);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
|
||||||
download.setEnabled(true);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
private void replaceWith(TsPluginBody instance) {
|
||||||
content.removeAll();
|
content.removeAll();
|
||||||
content.add(instance.getContent());
|
content.add(instance.getContent());
|
||||||
|
AutoupdateUtil.trueLayout(content.getParent());
|
||||||
}
|
}
|
||||||
|
|
||||||
public JPanel getContent() {
|
public JPanel getContent() {
|
||||||
|
|
Loading…
Reference in New Issue