rusefi/java_console/autoupdate/src/com/rusefi/autoupdate/AutoupdateUtil.java

96 lines
3.2 KiB
Java
Raw Normal View History

2020-06-16 21:35:16 -07:00
package com.rusefi.autoupdate;
2020-07-01 20:54:40 -07:00
import com.rusefi.shared.ConnectionAndMeta;
2020-06-16 21:35:16 -07:00
import com.rusefi.ui.util.FrameHelper;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
2020-06-17 19:14:22 -07:00
import java.util.Date;
2020-06-16 21:35:16 -07:00
import java.util.concurrent.atomic.AtomicReference;
public class AutoupdateUtil {
public static final boolean runHeadless = Boolean.getBoolean("run_headless") || GraphicsEnvironment.isHeadless();
2020-06-22 16:52:40 -07:00
// todo: figure out a better way to work with absolute path
private static final String APPICON = "/appicon.png";
2020-06-16 21:35:16 -07:00
2020-06-17 19:14:22 -07:00
public static void downloadAutoupdateFile(String localZipFileName, ConnectionAndMeta connectionAndMeta, String title) throws IOException {
2020-06-16 21:35:16 -07:00
FrameHelper frameHelper = null;
final AtomicReference<JProgressBar> jProgressBarAtomicReference = new AtomicReference<>();
if (!runHeadless) {
frameHelper = new FrameHelper();
JProgressBar jProgressBar = new JProgressBar();
frameHelper.getFrame().setTitle(title);
2020-07-01 20:54:40 -07:00
jProgressBar.setMaximum(ConnectionAndMeta.STEPS);
2020-06-16 21:35:16 -07:00
jProgressBarAtomicReference.set(jProgressBar);
frameHelper.showFrame(jProgressBar, true);
}
2020-07-01 20:54:40 -07:00
ConnectionAndMeta.DownloadProgressListener listener = currentProgress -> {
2020-06-16 21:35:16 -07:00
if (!runHeadless) {
SwingUtilities.invokeLater(() -> jProgressBarAtomicReference.get().setValue(currentProgress));
}
2020-07-01 20:54:40 -07:00
};
2020-06-16 21:35:16 -07:00
2020-07-01 20:54:40 -07:00
ConnectionAndMeta.downloadFile(localZipFileName, connectionAndMeta, listener);
2020-06-16 21:35:16 -07:00
if (!runHeadless) {
frameHelper.getFrame().dispose();
}
}
@NotNull
public static URLClassLoader getClassLoaderByJar(String jar) throws MalformedURLException {
return new URLClassLoader(
new URL[]{new File(jar).toURI().toURL()},
AutoupdateUtil.class.getClassLoader()
);
}
2020-06-16 22:10:35 -07:00
public static void trueLayout(Component component) {
if (component == null)
return;
component.invalidate();
component.validate();
component.repaint();
}
2020-06-17 19:14:22 -07:00
public static boolean hasExistingFile(String zipFileName, long completeFileSize, long lastModified) {
File file = new File(zipFileName);
System.out.println("We have " + file.length() + " " + new Date(file.lastModified()));
return file.length() == completeFileSize && file.lastModified() == lastModified;
}
2020-06-17 20:19:07 -07:00
public static ImageIcon loadIcon(String strPath) {
URL imgURL = AutoupdateUtil.class.getResource(strPath);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
imgURL = AutoupdateUtil.class.getResource("/com/rusefi/" + strPath);
if (imgURL != null) {
return new ImageIcon(imgURL);
}
2020-06-17 20:19:07 -07:00
return null;
}
}
2020-06-22 16:52:40 -07:00
public static void setAppIcon(JFrame frame) {
ImageIcon icon = loadIcon(APPICON);
if (icon != null)
frame.setIconImage(icon.getImage());
}
2020-06-22 21:24:32 -07:00
public static void pack(Window window) {
trueLayout(window);
window.pack();
trueLayout(window);
}
2020-06-16 21:35:16 -07:00
}