import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.function.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import java.awt.geom.*;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.text.SimpleDateFormat;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import static x30_pkg.x30_util.DynamicObject;
class main {
static JLabel jImage_scaledToHeight(String imageID, int height) { return jImage_scaledToHeight(imageID, height, null); }
static JLabel jImage_scaledToHeight(String imageID, int height, String toolTip) {
return setToolTip(toolTip, jImage(resizeImageToHeight_bilinear(loadImage2(imageID), height)));
}
static JLabel jImage_scaledToHeight(int height, String imageID) { return jImage_scaledToHeight(height, imageID, null); }
static JLabel jImage_scaledToHeight(int height, String imageID, String toolTip) {
return jImage_scaledToHeight(imageID, height, toolTip);
}
static A setToolTip(A c, Object toolTip) {
return setToolTipText(c, toolTip);
}
static A setToolTip(Object toolTip, A c) {
return setToolTipText(c, toolTip);
}
static void setToolTip(TrayIcon trayIcon, String toolTip) {
setTrayIconToolTip(trayIcon, toolTip);
}
static JLabel jImage(Image img) {
return jimage(img);
}
static JLabel jImage(String imageID) {
return jimage(imageID);
}
static BufferedImage resizeImageToHeight_bilinear(BufferedImage img, int newH) {
return scaleImage_bilinear(doubleRatio(newH, img.getHeight()), img);
}
static BufferedImage loadImage2(String snippetIDOrURL) {
return loadBufferedImage(snippetIDOrURL);
}
static BufferedImage loadImage2(File file) {
return loadBufferedImage(file);
}
static A setToolTipText(final A c, final Object toolTip) {
if (c == null) return null;
{ swing(() -> {
String s = nullIfEmpty(str(toolTip));
if (neq(s, c.getToolTipText()))
c.setToolTipText(s);
}); }
return c;
}
static A setToolTipText(Object toolTip, A c) {
return setToolTipText(c, toolTip);
}
static void setTrayIconToolTip(TrayIcon trayIcon, String toolTip) {
if (trayIcon != null) trayIcon.setToolTip(toolTip);
}
static JLabel jimage(Image img) {
return jImageLabel(img);
}
static JLabel jimage(String imageID) {
return jImageLabel(imageID);
}
static BufferedImage scaleImage_bilinear(BufferedImage before, double scaleX) { return scaleImage_bilinear(before, scaleX, scaleX); }
static BufferedImage scaleImage_bilinear(BufferedImage before, double scaleX, double scaleY) {
return scaleImageWithOp(before, scaleX, scaleY, AffineTransformOp.TYPE_BILINEAR);
}
static BufferedImage scaleImage_bilinear(double scale, BufferedImage before) {
return scaleImage_bilinear(before, scale);
}
static double doubleRatio(double x, double y) {
return y == 0 ? 0 : x/y;
}
static boolean loadBufferedImage_useImageCache = true;
static BufferedImage loadBufferedImage(String snippetIDOrURLOrFile) { try {
ping();
if (snippetIDOrURLOrFile == null) return null;
if (isURL(snippetIDOrURLOrFile))
return imageIO_readURL(snippetIDOrURLOrFile);
if (isSnippetID(snippetIDOrURLOrFile)) {
String snippetID = "" + parseSnippetID(snippetIDOrURLOrFile);
IResourceLoader rl = vm_getResourceLoader();
if (rl != null)
return loadBufferedImage(rl.loadLibrary(snippetID));
File dir = imageSnippetsCacheDir();
if (loadBufferedImage_useImageCache) {
dir.mkdirs();
File file = new File(dir, snippetID + ".png");
if (file.exists() && file.length() != 0)
try {
return ImageIO.read(file);
} catch (Throwable e) {
e.printStackTrace();
// fall back to loading from sourceforge
}
}
String imageURL = snippetImageURL_http(snippetID);
print("Loading image: " + imageURL);
BufferedImage image = imageIO_readURL(imageURL);
if (loadBufferedImage_useImageCache) {
File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis());
ImageIO.write(image, "png", tempFile);
tempFile.renameTo(new File(dir, snippetID + ".png"));
//Log.info("Cached image.");
}
//Log.info("Loaded image.");
return image;
} else
return loadBufferedImage(new File(snippetIDOrURLOrFile));
} catch (Exception __e) { throw rethrow(__e); } }
static BufferedImage loadBufferedImage(File file) {
return loadBufferedImageFile(file);
}
static Object swing(Object f) {
return swingAndWait(f);
}
static void swing(Runnable f) {
swingAndWait(f);
}
static A swing(F0 f) {
return (A) swingAndWait(f);
}
static A swing(IF0 f) {
return (A) swingAndWait(f);
}
static String nullIfEmpty(String s) {
return isEmpty(s) ? null : s;
}
static Map nullIfEmpty(Map map) {
return isEmpty(map) ? null : map;
}
static List nullIfEmpty(List l) {
return isEmpty(l) ? null : l;
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static boolean neq(Object a, Object b) {
return !eq(a, b);
}
static JLabel jImageLabel(Image img) {
return swingNu(JLabel.class, imageIcon(img));
}
static JLabel jImageLabel(javax.swing.Icon icon) {
return swingNu(JLabel.class, icon);
}
static JLabel jImageLabel(String imageID) {
return jImageLabel(imageIcon(imageID));
}
static JLabel jImageLabel(String text, String imageID) {
final JLabel l = swingNu(JLabel.class, text, imageIcon(imageID), JLabel.CENTER);
{ swing(() -> {
l.setVerticalTextPosition(SwingConstants.BOTTOM);
l.setHorizontalTextPosition(SwingConstants.CENTER);
}); }
return l;
}
static BufferedImage scaleImageWithOp(BufferedImage before, double scaleX, int affineTransformOpType) { return scaleImageWithOp(before, scaleX, scaleX, affineTransformOpType); }
static BufferedImage scaleImageWithOp(BufferedImage before, double scaleX, double scaleY, int affineTransformOpType) {
if (scaleX == 1 && scaleY == 1) return before;
int w = before.getWidth();
int h = before.getHeight();
int neww = max(1, iround(w*scaleX)), newh = max(1, iround(h*scaleY));
BufferedImage after = new BufferedImage(neww, newh, BufferedImage.TYPE_INT_ARGB);
AffineTransform at = new AffineTransform();
at.scale(scaleX, scaleY);
AffineTransformOp scaleOp = new AffineTransformOp(at, affineTransformOpType);
return scaleOp.filter(before, after);
}
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll = false;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions = false;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// always returns true
static boolean ping() {
//ifdef useNewPing
newPing();
//endifdef
if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static boolean isURL(String s) {
return startsWithOneOf(s, "http://", "https://", "file:");
}
static BufferedImage imageIO_readURL(String url) { try {
return ImageIO.read(new URL(url));
} catch (Exception __e) { throw rethrow(__e); } }
public static boolean isSnippetID(String s) {
try {
parseSnippetID(s);
return true;
} catch (RuntimeException e) {
return false;
}
}
public static long parseSnippetID(String snippetID) {
long id = Long.parseLong(shortenSnippetID(snippetID));
if (id == 0) throw fail("0 is not a snippet ID");
return id;
}
static IResourceLoader vm_getResourceLoader() {
return proxy(IResourceLoader.class, vm_generalMap_get("_officialResourceLoader"));
}
static File imageSnippetsCacheDir() {
return javaxCachesDir("Image-Snippets");
}
static String snippetImageURL_http(String snippetID) {
return snippetImageURL_http(snippetID, "png");
}
static String snippetImageURL_http(String snippetID, String contentType) {
return replacePrefix("https://", "http://", snippetImageURL(snippetID, contentType)).replace(":8443", ":8080");
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal