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 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 javax.imageio.*;
import java.math.*;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import org.pushingpixels.substance.api.*;
import org.pushingpixels.substance.api.skin.*;
import javax.swing.Timer;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
public class main {
static int imageWidth = 200;
static RGBImage originalImage;
static ImageSurface imageSurface;
static JLabel isTitle;
static RGB probeRandomPixel(RGBImage image) {
int x = (int) (random()*(image.getWidth()-1));
int y = (int) (random()*(image.getHeight()-1));
return image.getPixel(x, y);
}
static float probeRandomPixel(BWImage image) {
int x = (int) (random()*(image.getWidth()-1));
int y = (int) (random()*(image.getHeight()-1));
return image.getPixel(x, y);
}
static RGB randomColor() {
return new RGB(random(), random(), random());
}
static float randomBrightness() {
return (float) random();
}
static RGBImage resizeToWidth(RGBImage image, int w) {
return resize(image, w, (int) ((image.getHeight()*(double) w)/image.getWidth()));
}
public static RGBImage resize(RGBImage image, int w, int h) {
if (w == image.getWidth() && h == image.getHeight()) return image;
int[] pixels = new int[w*h];
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
pixels[y*w+x] = image.getInt(x*image.getWidth()/w, y*image.getHeight()/h);
return new RGBImage(w, h, pixels);
}
static Random _random = new Random();
static double random() {
return _random.nextInt(100001)/100000.0;
}
static int random(int max) {
return _random.nextInt(max);
}
static double random(double min, double max) {
return min+random()*(max-min);
}
static double normalize(double d) {
return Math.max(0, Math.min(1, d));
}
static int round(double d) {
return (int) Math.round(d);
}
static double mix(double a, double b, double bishness) {
return a+(b-a)*bishness;
}
public static double pixelDiff(RGB a, RGB b) {
return (Math.abs(a.r-b.r) + Math.abs(a.g-b.g) + Math.abs(a.b-b.b))/3;
}
public static double diff(RGBImage image, RGBImage image2) {
int w = image.getWidth(), h = image.getHeight();
double sum = 0;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
sum += pixelDiff(image.getRGB(x, y), image2.getRGB(x, y));
return sum/(w*h);
}
static double diff(BWImage image, BWImage image2) {
int w = image.getWidth(), h = image.getHeight();
double sum = 0;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
sum += Math.abs(image.getPixel(x, y) - image2.getPixel(x, y));
return sum/(w*h);
}
public static void copy(RGBImage src, int srcX, int srcY, RGBImage dst, int dstX, int dstY, int w, int h) {
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
dst.setPixel(dstX+x, dstY+y, src.getPixel(srcX+x, srcY+y));
}
public static void copy(BWImage src, int srcX, int srcY, BWImage dst, int dstX, int dstY, int w, int h) {
w = min(w, dst.getWidth()-dstX);
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
dst.setByte(dstX+x, dstY+y, src.getByte(srcX+x, srcY+y));
}
static float varyChannel(float x) {
return Math.max(0f, Math.min(1f, (float) (x+random(-0.1, 0.1))));
}
static double vary01(double x) {
return Math.max(0, Math.min(1, x+random(-0.1, 0.1)));
}
static RGB varyColor(RGB rgb) {
int s = random(3);
if (s == 0)
return new RGB(varyChannel(rgb.r), rgb.g, rgb.b);
else if (s == 1)
return new RGB(rgb.r, varyChannel(rgb.g), rgb.b);
else
return new RGB(rgb.r, rgb.g, varyChannel(rgb.b));
}
static abstract class Base {
abstract Base copy();
abstract void vary();
}
static abstract class Overlay extends Base {
abstract Overlay copy();
abstract void renderOn(RGBImage image);
}
static abstract class Params extends Base {
RGBImage originalImage;
abstract Params copy();
abstract RGBImage render();
void baseClone(Params p) {
p.originalImage = originalImage;
}
RGBImage rendered;
RGBImage getImage() {
if (rendered == null)
rendered = render();
return rendered;
}
double score = 2.0;
double getScore() {
if (score == 2.0)
score = calcScore();
return score;
}
// when object has changed
double getFreshScore() {
resetScore();
return getScore();
}
void resetScore() {
score = 2.0;
rendered = null;
}
double calcScore() {
return diff(originalImage, getImage());
}
boolean isBetterThan(Params p) {
return getScore() < p.getScore();
}
}
static class Coin extends Overlay {
double cx, cy, radius;
RGB color;
Coin copy() {
Coin p = new Coin();
p.cx = cx;
p.cy = cy;
p.radius = radius;
p.color = color;
return p;
}
public String toString() {
return "Coin " + cx + " " + cy + " " + radius + " " + color;
}
void renderOn(RGBImage image) {
coin(image, this);
}
void vary() {
int s = random(4);
if (s == 0)
cx = vary01(cx);
else if (s == 1)
cy = vary01(cy);
else if (s == 2)
radius = vary01(radius);
else
color = varyColor(color);
}
}
static class Solid extends Overlay {
RGB col;
Solid copy() {
Solid p = new Solid();
p.col = col;
return p;
}
public String toString() {
return "Solid " + col;
}
void renderOn(RGBImage image) {
int w = image.getWidth(), h = image.getHeight();
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
image.setPixel(x, y, col);
}
void vary() {
col = varyColor(col);
}
}
interface Reproducer {
public Params reproduce(RGBImage original);
}
interface OverlayReproducer {
public Overlay reproduce(RGBImage original);
}
static class RandomCoin implements OverlayReproducer {
int n = -1;
public Overlay reproduce(RGBImage original) {
++n;
Coin p = new Coin();
p.cx = random();
p.cy = random();
p.radius = random()*0.2;
if (n % 2 == 0)
p.color = randomColor();
else
p.color = probeRandomPixel(original);
return p;
}
}
static class RandomSolid implements OverlayReproducer {
int n = -1;
public Overlay reproduce(RGBImage original) {
++n;
Solid p = new Solid();
if (n % 2 == 0) {
p.col = randomColor();
} else {
p.col = probeRandomPixel(original);
}
return p;
}
}
static class WhiteSolid implements OverlayReproducer {
public Overlay reproduce(RGBImage original) {
Solid p = new Solid();
p.col = new RGB(Color.white);
return p;
}
}
static class VaryBest implements Reproducer {
Reproducer base;
Params best;
VaryBest(Reproducer base) {
this.base = base;
}
public Params reproduce(RGBImage original) {
Params p = base.reproduce(original);
if (best == null || p.isBetterThan(best))
best = p;
Params variation = best.copy();
variation.vary();
//System.out.println("Best: " + best.getScore() + ", variation: " + variation.getScore());
if (variation.isBetterThan(best)) {
//System.out.println("Using variation, diff=" + (best.getScore()-variation.getScore()));
best = variation;
}
return best;
}
void reset() {
best = null;
}
}
static class Layered extends Params {
Layers myMaker;
Overlay[] list;
Layered(Layers myMaker, Overlay[] list) {
this.myMaker = myMaker;
this.list = list;
}
Layered copy() {
Layered p = new Layered(myMaker, new Overlay[list.length]);
baseClone(p);
for (int i = 0; i < list.length; i++)
p.list[i] = list[i].copy();
return p;
}
public String toString() {
StringBuilder buf = new StringBuilder("layered{");
for (int i = 0; i < list.length; i++) {
if (i != 0)
buf.append(", ");
buf.append(list[i]);
}
return buf + "}";
}
RGBImage render() {
RGBImage image = new RGBImage(originalImage.getWidth(), originalImage.getHeight(), Color.white);
for (int i = 0; i < list.length; i++)
list[i].renderOn(image);
return image;
}
void vary() {
int i = random(list.length);
if (random(2) == 0) {
//System.out.println("Varying layer " + i);
list[i].vary();
} else {
//double score = getScore();
OverlayReproducer maker = myMaker.list[i];
list[i] = maker.reproduce(originalImage);
//System.out.println("Exchanging layer " + i + " from " + maker + ", improvement: " + (score-getFreshScore()));
}
resetScore();
}
}
static class Layers implements Reproducer {
OverlayReproducer[] list;
long ntry = -1;
int stepTime = 0/*500*/;
VaryBest varyBest = new VaryBest(this);
Layers(OverlayReproducer... list) {
this.list = list;
}
public Params reproduce(RGBImage original) {
++ntry;
int n = this.list.length;
if (stepTime != 0) {
// build up image gradually to optimize lower layers first
n = (int) Math.min(n, ntry/stepTime+1);
varyBest.reset();
}
Overlay[] list = new Overlay[n];
for (int i = 0; i < n; i++)
list[i] = this.list[i].reproduce(original);
Layered result = new Layered(this, list);
result.originalImage = original;
return result;
}
}
// main reproduce function
static Reproducer reproducer =
/*new VaryBest*/(new Layers(
/*new RandomSolid()*/new WhiteSolid(),
new RandomCoin(),
new RandomCoin()
)).varyBest;
static Params reproduce(RGBImage original) {
int w = original.getWidth(), h = original.getHeight();
return reproducer.reproduce(original);
}
public static void main(final String[] args) throws Exception { substance();
String imageID = "#1000332"; // Picture of two coins
if (args.length != 0) imageID = args[0];
final String _imageID = imageID;
originalImage = loadImage(_imageID);
originalImage = resizeToWidth(originalImage, imageWidth);
{ swingAndWait(new Runnable() { public void run() { try {
imageSurface = new ImageSurface();
isTitle = jCenteredLabel("Reproduction");
showPackedFrame(vgrid(
withCenteredTitle("Original Image", new ImageSurface(originalImage)),
withTitle(isTitle, imageSurface)));
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "imageSurface = new ImageSurface;\n isTitle = jCenteredLabel(\"Reproduction\");\n \n showPackedFrame(vgrid(\n withCenteredTitle(\"Original Image\", new ImageSurface(originalImage)),\n withTitle(isTitle, imageSurface)));"; }}); }
reproduceOpenEnd(originalImage, imageSurface);
}
static void reproduceOpenEnd(RGBImage original, ImageSurface imageSurface) {
Params best = null;
long lastPrint = 0, lastN = 0;
for (long ntry = 1; ; ntry++) {
long now = System.currentTimeMillis();
if (now >= lastPrint+1000) {
long tps = (ntry-lastN)*1000/(now-lastPrint);
lastPrint = now;
lastN = ntry;
String s = "Try " + ntry + " (" + tps + "/s)";
if (best == null)
System.out.println(s);
else {
System.out.println("Best: " + best);
System.out.println(s + ", score: " + formatDouble(best.getScore()*100, 2) + "%, structure size: " + structureSize(best, Params.class));
}
}
Params p = reproduce(original);
if (best == null || p != null && p.getScore() < best.getScore()) {
//System.out.println("New best! " + p.getScore());
best = p;
imageSurface.setImage(p.getImage());
setText(isTitle, print("Reproduction (" + formatDouble(100*(1-p.getScore()), 1) + "%)"));
}
if (p != null && p.getScore() == 0.0)
break;
}
}
static void coin(RGBImage img, Coin p) {
int w = img.getWidth(), h = img.getHeight();
double ratio = ((double) w)/h;
for (int yy = 0; yy < h; yy++) {
double y = ((double) yy)/(h-1);
double ybla = Math.abs(y-p.cy)/p.radius;
if (ybla <= 1) {
double xbla = Math.sqrt(1-ybla*ybla)/ratio;
double l = p.cx-xbla*p.radius, r = p.cx+xbla*p.radius;
int ll = (int) (l*w), rr = (int) (r*w);
ll = Math.max(0, ll);
rr = Math.min(w-1, rr);
for (int xx = ll; xx <= rr; xx++)
img.setPixel(xx, yy, p.color);
}
}
}
static JFrame showPackedFrame(String title, Component contents) {
return packFrame(showFrame(title, contents));
}
static JFrame showPackedFrame(Component contents) {
return packFrame(showFrame(contents));
}
public static String formatDouble(double d, int digits) {
String format = "0.";
for (int i = 0; i < digits; i++) format += "#";
return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH)).format(d);
}
static JLabel jCenteredLabel(String text) {
return jcenteredLabel(text);
}
static JLabel jCenteredLabel() {
return jcenteredLabel();
}
public static int structureSize(Object o, Class baseClass) {
return structureSize(o, baseClass, new IdentityHashMap());
}
static int structureSize(Object o, Class baseClass,
IdentityHashMap seen) {
if (o == null || seen.containsKey(o)) return 0;
seen.put(o, Boolean.TRUE);
int size = 1;
Class c = o.getClass();
if (c.isArray()) {
int n = Array.getLength(o);
for (int i = 0; i < n; i++)
size += structureSize(Array.get(o, i), baseClass, seen);
} else
while (c != Object.class && c != baseClass) {
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
if ((field.getModifiers() & Modifier.STATIC) != 0)
continue;
if (field.getType().isPrimitive())
++size;
else {
Object value = null;
try {
value = field.get(o);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
size += structureSize(value, baseClass, seen);
}
}
c = c.getSuperclass();
}
return size;
}
static RGBImage loadImage(String snippetIDOrURL) {
return new RGBImage(loadBufferedImage(snippetIDOrURL));
}
static JComponent withCenteredTitle(String title, Component c) {
return withTitle(jCenteredLabel(title), c);
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile StringBuffer 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 int print_maxLineLength = 0; // 0 = unset
static boolean print_silent; // total mute if set
static volatile ThreadLocal print_byThread; // special handling by thread
static void print() {
print("");
}
// slightly overblown signature to return original object...
static A print(A o) {
ping();
if (print_silent) return o;
String s = String.valueOf(o) + "\n";
print_noNewLine(s);
return o;
}
static void print_noNewLine(String s) {
if (print_byThread != null) {
Object f = print_byThread.get();
if (f != null)
if (isFalse(callF(f, s))) return;
}
print_raw(s);
}
static void print_raw(String s) {
s = fixNewLines(s);
// TODO if (print_maxLineLength != 0)
StringBuffer loc = local_log;
StringBuffer buf = print_log;
int loc_max = print_log_max;
if (buf != loc && buf != null) {
print_append(buf, s, print_log_max);
loc_max = local_log_max;
}
if (loc != null)
print_append(loc, s, loc_max);
System.out.print(s);
}
static void print(long l) {
print(String.valueOf(l));
}
static void print(char c) {
print(String.valueOf(c));
}
static void print_append(StringBuffer buf, String s, int max) {
synchronized(buf) {
buf.append(s);
max /= 2;
if (buf.length() > max) try {
int newLength = max/2;
int ofs = buf.length()-newLength;
String newString = buf.substring(ofs);
buf.setLength(0);
buf.append("[...] ").append(newString);
} catch (Exception e) {
buf.setLength(0);
}
}
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static float min(float a, float b) { return Math.min(a, b); }
static float min(float a, float b, float c) { return min(min(a, b), c); }
static double min(double a, double b) {
return Math.min(a, b);
}
static double min(double[] c) {
double x = Double.MAX_VALUE;
for (double d : c) x = Math.min(x, d);
return x;
}
static float min(float[] c) {
float x = Float.MAX_VALUE;
for (float d : c) x = Math.min(x, d);
return x;
}
static byte min(byte[] c) {
byte x = 127;
for (byte d : c) if (d < x) x = d;
return x;
}
static JPanel vgrid(List parts) {
return vgrid(asArray(parts));
}
static JPanel vgrid(Object... parts) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(parts.length, 1));
smartAdd(panel, parts);
return panel;
}
static JComponent withTitle(String title, Component c) {
return withTitle(withTitle_titlePanel(title), c);
}
static JComponent withTitle_titlePanel(String title) {
return new JLabel(title);
}
static JPanel withTitle(JComponent titleComponent, Component c) {
titleComponent.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, UIManager.getColor("Button.borderColor")));
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(Color.WHITE);
panel.add(BorderLayout.NORTH, titleComponent);
panel.add(BorderLayout.CENTER, wrap(c));
return panel;
}
static void setText(JTextComponent c, Object text) {
setText((Object) c, text);
}
static void setText(JLabel c, Object text) {
setText((Object) c, text);
}
static void setText(JButton c, Object text) {
setText((Object) c, text);
}
static void setText(final Object c, Object text) {
if (c == null) return;
final String s = str(text);
swingAndWait(new Runnable() { public void run() { try { call(c, "setText", s) ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "call(c, \"setText\", s)"; }});
}
static String fixNewLines(String s) {
return s.replace("\r\n", "\n").replace("\r", "\n");
}
static Object[] asArray(List l) {
return toObjectArray(l);
}
static A[] asArray(Class type, List l) {
return (A[]) l.toArray((Object[]) Array.newInstance(type, l.size()));
}
static volatile boolean ping_pauseAll;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions;
static Map ping_actions = synchroMap(new WeakHashMap());
// returns true if it did anything
static boolean ping() { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) {
Object action;
synchronized(mc()) {
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 (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Object callF(Object f, Object... args) {
return callFunction(f, args);
}
static JLabel jcenteredLabel(String text) {
JLabel l = jLabel(text);
l.setHorizontalAlignment(JLabel.CENTER);
return l;
}
static JLabel jcenteredLabel() {
return jcenteredLabel(" ");
}
static JFrame showFrame() {
return makeFrame();
}
static JFrame showFrame(Object content) {
return makeFrame(content);
}
static JFrame showFrame(String title) {
return makeFrame(title);
}
static JFrame showFrame(String title, Object content) {
return makeFrame(title, content);
}
static JFrame showFrame(final JFrame frame) {
if (frame != null) { swingAndWait(new Runnable() { public void run() { try {
if (frameTooSmall(frame)) frameStandardSize(frame);
frame.setVisible(true);
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "if (frameTooSmall(frame)) frameStandardSize(frame);\r\n frame.setVisible(true);"; }}); }
return frame;
}
// make or update frame
static JFrame showFrame(String title, Object content, JFrame frame) {
if (frame == null)
return showFrame(title, content);
else {
frame.setTitle(title);
setFrameContents(frame, content);
return frame;
}
}
static void swingAndWait(Runnable r) { try {
if (isAWTThread())
r.run();
else
EventQueue.invokeAndWait(r);
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Object swingAndWait(final Object f) {
if (isAWTThread())
return callF(f);
else {
final Var result = new Var();
swingAndWait(new Runnable() { public void run() { try {
result.set(callF(f));
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "result.set(callF(f));"; }});
return result.get();
}
}
static int packFrame_minw = 150, packFrame_minh = 50;
static JFrame packFrame(Component c) {
JFrame frame = getFrame(c);
if (frame == null) return null;
frame.pack();
int maxW = getScreenWidth()-50, maxH = getScreenHeight()-50;
frame.setSize(
min(maxW, max(frame.getWidth(), packFrame_minw)),
min(maxH, max(frame.getHeight(), packFrame_minh)));
return frame;
}
static JPanel smartAdd(JPanel panel, List parts) {
for (Object o : parts) {
Component c;
if (o == null)
c = new JPanel();
else if (o instanceof String)
c = jlabel((String) o);
else
c = wrap(o);
panel.add(c);
}
return panel;
}
static JPanel smartAdd(JPanel panel, Object... parts) {
return smartAdd(panel, asList(parts));
}
static boolean isFalse(Object o) {
return eq(false, o);
}
static String str(Object o) {
return String.valueOf(o);
}
static String str(char[] c) {
return new String(c);
}
static Object call(Object o) {
return callFunction(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
try {
if (o instanceof Class) {
Method m = call_findStaticMethod((Class) o, method, args, false);
m.setAccessible(true);
return m.invoke(null, args);
} else {
Method m = call_findMethod(o, method, args, false);
m.setAccessible(true);
return m.invoke(o, args);
}
} catch (Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
Class _c = c;
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (!m.getName().equals(method)) {
if (debug) System.out.println("Method name mismatch: " + method);
continue;
}
if ((m.getModifiers() & Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug))
continue;
return m;
}
c = c.getSuperclass();
}
throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + _c.getName());
}
static Method call_findMethod(Object o, String method, Object[] args, boolean debug) {
Class c = o.getClass();
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (m.getName().equals(method) && call_checkArgs(m, args, debug))
return m;
}
c = c.getSuperclass();
}
throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName());
}
private static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
Class>[] types = m.getParameterTypes();
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
static boolean loadBufferedImage_useImageCache = true;
static BufferedImage loadBufferedImage(String snippetIDOrURL) { try {
if (snippetIDOrURL == null) return null;
if (isURL(snippetIDOrURL))
return ImageIO.read(new URL(snippetIDOrURL));
if (!isSnippetID(snippetIDOrURL)) throw fail("Not a URL or snippet ID: " + snippetIDOrURL);
String snippetID = "" + parseSnippetID(snippetIDOrURL);
try {
File dir = getCacheProgramDir("Image-Snippets");
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(snippetID);
System.err.println("Loading image: " + imageURL);
BufferedImage image = ImageIO.read(new URL(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;
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static BufferedImage loadBufferedImage(File file) { try {
return file.isFile() ? ImageIO.read(file) : null;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
// c = JComponent or something implementing swing()
static JComponent wrap(Object swingable) {
JComponent c = (JComponent) ( swingable instanceof JComponent ? swingable : callOpt(swingable, "swing"));
if (c instanceof JTable || c instanceof JList
|| c instanceof JTextArea || c instanceof JEditorPane
|| c instanceof JTextPane)
return new JScrollPane(c);
return c;
}
static boolean frameTooSmall(JFrame frame) {
return frame.getWidth() < 100 || frame.getHeight() < 50;
}
static JFrame makeFrame() {
return makeFrame((Component) null);
}
static JFrame makeFrame(Object content) {
return makeFrame(programTitle(), content);
}
static JFrame makeFrame(String title) {
return makeFrame(title, null);
}
static JFrame makeFrame(String title, Object content) {
return makeFrame(title, content, true);
}
static JFrame makeFrame(final String title, final Object content, final boolean showIt) {
return (JFrame) swing(new Object() { Object get() { try {
if (getFrame(content) != null)
return setFrameTitle((Component) content, title);
final JFrame frame = new JFrame(title);
if (content != null)
frame.getContentPane().add(wrap(content));
frame.setBounds(300, 100, 500, 400);
if (showIt)
frame.setVisible(true);
//callOpt(content, "requestFocus");
//exitOnFrameClose(frame);
// standard right-click behavior on titles
if (isSubstanceLAF())
titlePopupMenu(frame,
new Object() { void get(JPopupMenu menu) { try {
boolean alwaysOnTop = frame.isAlwaysOnTop();
menu.add(jmenuItem("Restart Program", "restart"));
menu.add(jmenuItem("Show Console", "showConsole"));
menu.add(jCheckBoxMenuItem("Always On Top", alwaysOnTop, new Runnable() { public void run() { try {
toggleAlwaysOnTop(frame) ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "toggleAlwaysOnTop(frame)"; }}));
menu.add(jMenuItem("Shoot Window", new Runnable() { public void run() { try { showImage("Window Shot", shootWindow(frame)) ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "showImage(\"Window Shot\", shootWindow(frame))"; }}));
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
public String toString() { return "bool alwaysOnTop = frame.isAlwaysOnTop();\r\n menu.add(jmenuItem(\"Restart Program\", f restart));\r\n menu.add(jmenuItem(\"Show Console\", f showConsole));\r\n menu.add(jCheckBoxMenuItem(\"Always On Top\", alwaysOnTop, r {\r\n toggleAlwaysOnTop(frame) }));\r\n menu.add(jMenuItem(\"Shoot Window\", r { showImage(\"Window Shot\", shootWindow(frame)) }));"; }});
return frame;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
public String toString() { return "if (getFrame(content) != null)\r\n ret setFrameTitle((Component) content, title);\r\n final JFrame frame = new JFrame(title);\r\n if (content != null)\r\n frame.getContentPane().add(wrap(content));\r\n frame.setBounds(300, 100, 500, 400);\r\n if (showIt)\r\n frame.setVisible(true);\r\n //callOpt(content, \"requestFocus\");\r\n //exitOnFrameClose(frame);\r\n \r\n // standard right-click behavior on titles\r\n if (isSubstanceLAF())\r\n titlePopupMenu(frame,\r\n new O { void get(JPopupMenu menu) { try {\n \r\n bool alwaysOnTop = frame.isAlwaysOnTop();\r\n menu.add(jmenuItem(\"Restart Program\", f restart));\r\n menu.add(jmenuItem(\"Show Console\", f showConsole));\r\n menu.add(jCheckBoxMenuItem(\"Always On Top\", alwaysOnTop, r {\r\n toggleAlwaysOnTop(frame) }));\r\n menu.add(jMenuItem(\"Shoot Window\", r { showImage(\"Window Shot\", shootWindow(frame)) }));\r\n \n} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}\n public S toString() { ret \"bool alwaysOnTop = frame.isAlwaysOnTop();\\r\\n menu.add(jmenuItem(\\\"Restart Program\\\", f restart));\\r\\n menu.add(jmenuItem(\\\"Show Console\\\", f showConsole));\\r\\n menu.add(jCheckBoxMenuItem(\\\"Always On Top\\\", alwaysOnTop, r {\\r\\n toggleAlwaysOnTop(frame) }));\\r\\n menu.add(jMenuItem(\\\"Shoot Window\\\", r { showImage(\\\"Window Shot\\\", shootWindow(frame)) }));\"; }});\r\n ret frame;"; }});
}
static Object callFunction(Object f, Object... args) {
if (f == null) return null;
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
} else if (f instanceof String)
return call(mc(), (String) f, args);
else
return call(f, "get", args);
//else throw fail("Can't call a " + getClassName(f));
}
static ArrayList asList(A[] a) {
return new ArrayList(Arrays.asList(a));
}
static ArrayList asList(int[] a) {
ArrayList l = new ArrayList();
for (int i : a) l.add(i);
return l;
}
static ArrayList asList(Iterable s) {
if (s instanceof ArrayList) return (ArrayList) s;
ArrayList l = new ArrayList();
if (s != null)
for (A a : s)
l.add(a);
return l;
}
static JLabel jLabel(String text) {
return jlabel(text);
}
static JLabel jLabel() {
return jlabel();
}
static Object mc() {
return getMainClass();
}
static boolean isURL(String s) {
return s.startsWith("http://") || s.startsWith("https://");
}
static void setFrameContents(JFrame frame, Object contents) {
frame.getContentPane().removeAll();
frame.getContentPane().add(wrap(contents));
revalidate(frame);
}
static boolean isAWTThread() {
if (isAndroid()) return false;
if (isHeadless()) return false;
return isTrue(callOpt(getClass("javax.swing.SwingUtilities"), "isEventDispatchThread"));
}
// extended over Class.isInstance() to handle primitive types
static boolean isInstanceX(Class type, Object arg) {
if (type == boolean.class) return arg instanceof Boolean;
if (type == int.class) return arg instanceof Integer;
if (type == long.class) return arg instanceof Long;
if (type == float.class) return arg instanceof Float;
if (type == short.class) return arg instanceof Short;
if (type == char.class) return arg instanceof Character;
if (type == byte.class) return arg instanceof Byte;
if (type == double.class) return arg instanceof Double;
return type.isInstance(arg);
}
static Thread currentThread() {
return Thread.currentThread();
}
static String snippetImageURL(String snippetID) {
long id = parseSnippetID(snippetID);
String url;
if (id == 1000010 || id == 1000012)
url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + id;
else
url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id
+ "&contentType=image/png";
return url;
}
static boolean eq(Object a, Object b) {
if (a == null) return b == null;
if (a.equals(b)) return true;
if (a instanceof BigInteger) {
if (b instanceof Integer) return a.equals(BigInteger.valueOf((Integer) b));
if (b instanceof Long) return a.equals(BigInteger.valueOf((Long) b));
}
return false;
}
static RuntimeException fail() {
throw new RuntimeException("fail");
}
static RuntimeException fail(Object msg) {
throw new RuntimeException(String.valueOf(msg));
}
static RuntimeException fail(String msg) {
throw new RuntimeException(unnull(msg));
}
static RuntimeException fail(String msg, Throwable innerException) {
throw new RuntimeException(msg, innerException);
}
// disabled for now to shorten some programs
/*static RuntimeException fail(S msg, O... args) {
throw new RuntimeException(format(msg, args));
}*/
static Map synchroMap() {
return synchroHashMap();
}
static Map synchroMap(Map map) {
return Collections.synchronizedMap(map);
}
static Object callOpt(Object o) {
if (o == null) return null;
return callF(o);
}
static Object callOpt(Object o, String method, Object... args) {
try {
if (o == null) return null;
if (o instanceof Class) {
Method m = callOpt_findStaticMethod((Class) o, method, args, false);
if (m == null) return null;
m.setAccessible(true);
return m.invoke(null, args);
} else {
Method m = callOpt_findMethod(o, method, args, false);
if (m == null) return null;
m.setAccessible(true);
return m.invoke(o, args);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Method callOpt_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
Class _c = c;
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (!m.getName().equals(method)) {
if (debug) System.out.println("Method name mismatch: " + method);
continue;
}
if ((m.getModifiers() & Modifier.STATIC) == 0 || !callOpt_checkArgs(m, args, debug))
continue;
return m;
}
c = c.getSuperclass();
}
return null;
}
static Method callOpt_findMethod(Object o, String method, Object[] args, boolean debug) {
Class c = o.getClass();
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (m.getName().equals(method) && callOpt_checkArgs(m, args, debug))
return m;
}
c = c.getSuperclass();
}
return null;
}
private static boolean callOpt_checkArgs(Method m, Object[] args, boolean debug) {
Class>[] types = m.getParameterTypes();
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
static JFrame getFrame(Object o) {
if (!(o instanceof Component)) return null;
Component c = (Component) o;
while (c != null) {
if (c instanceof JFrame) return (JFrame) c;
c = c.getParent();
}
return null;
}
static int getScreenWidth() {
return getScreenSize().width;
}
static JLabel jlabel(String text) {
final JLabel l = new JLabel(text) {
@Override
public void setText(String text) {
super.setText(text);
setToolTipText(text);
}
};
componentPopupMenu(l, new Object() { void get(JPopupMenu menu) { try {
addMenuItem(menu, "Copy text to clipboard", new Runnable() { public void run() { try {
copyTextToClipboard(l.getText());
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "copyTextToClipboard(l.getText());"; }});
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
public String toString() { return "addMenuItem(menu, \"Copy text to clipboard\", r {\r\n copyTextToClipboard(l.getText());\r\n });"; }});
return l;
}
static JLabel jlabel() {
return jlabel(" ");
}
static File getCacheProgramDir() {
return getCacheProgramDir(getProgramID());
}
static File getCacheProgramDir(String snippetID) {
return new File(userHome(), "JavaX-Caches/" + formatSnippetIDOpt(snippetID));
}
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 void frameStandardSize(JFrame frame) {
frame.setBounds(300, 100, 500, 400);
}
static Object[] toObjectArray(Collection c) {
List l = asList(c);
return l.toArray(new Object[l.size()]);
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int max(int a, int b, int c) {
return max(max(a, b), c);
}
static long max(int a, long b) {
return Math.max((long) a, b);
}
static long max(long a, long b) {
return Math.max(a, b);
}
static double max(int a, double b) {
return Math.max((double) a, b);
}
static float max(float a, float b) {
return Math.max(a, b);
}
static int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
return x;
}
static double max(double[] c) {
if (c.length == 0) return Double.MIN_VALUE;
double x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static float max(float[] c) {
if (c.length == 0) return Float.MAX_VALUE;
float x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static byte max(byte[] c) {
byte x = -128;
for (byte d : c) if (d > x) x = d;
return x;
}
static int getScreenHeight() {
return getScreenSize().height;
}
static Class> getClass(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
return null;
}
}
static Class getClass(Object o) {
return o instanceof Class ? (Class) o : o.getClass();
}
static Class getClass(Object realm, String name) { try {
try {
return getClass(realm).getClassLoader().loadClass(classNameToVM(name));
} catch (ClassNotFoundException e) {
return null;
}
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Boolean isHeadless_cache;
static boolean isHeadless() {
if (isHeadless_cache != null) return isHeadless_cache;
if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true;
// Also check if AWT actually works.
// If DISPLAY variable is set but no X server up, this will notice.
try {
callOpt(getClass("javax.swing.SwingUtilities"), "isEventDispatchThread");
return isHeadless_cache = false;
} catch (Throwable e) { return isHeadless_cache = true; }
}
static A setToolTipText(A c, Object toolTip) {
if (c == null) return null;
c.setToolTipText(str(toolTip));
return c;
}
static String showImage_defaultIcon = "#1004230"; // "#1004227";
static ImageSurface showImage(String snippetIDOrURL, String title) {
return showImage(loadImage(snippetIDOrURL), title);
}
static ImageSurface showImage(String title, BufferedImage img) {
return showImage(img, title);
}
static ImageSurface showImage(final BufferedImage img, final String title) {
return (ImageSurface) swing(new Object() { Object get() { try {
ImageSurface is = showImage(img);
getFrame(is).setTitle(title);
return is;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
public String toString() { return "ImageSurface is = showImage(img);\r\n getFrame(is).setTitle(title);\r\n return is;"; }});
}
static ImageSurface showImage(final BufferedImage img) {
return (ImageSurface) swing(new Object() { Object get() { try {
ImageSurface is = new ImageSurface(img);
JFrame frame = showPackedFrame(new JScrollPane(is));
moveToTopRightCorner(frame);
frameIcon(frame, showImage_defaultIcon);
return is;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
public String toString() { return "ImageSurface is = new ImageSurface(img);\r\n JFrame frame = showPackedFrame(new JScrollPane(is));\r\n moveToTopRightCorner(frame);\r\n frameIcon(frame, showImage_defaultIcon);\r\n return is;"; }});
}
static ImageSurface showImage(String title, RGBImage img) {
return showImage(title, img.getBufferedImage());
}
static ImageSurface showImage(RGBImage img) {
return showImage(img.getBufferedImage());
}
static ImageSurface showImage(RGBImage img, String title) {
ImageSurface is = showImage(img.getBufferedImage());
getFrame(is).setTitle(title);
return is;
}
static ImageSurface showImage(String imageID) {
return showImage(loadImage(imageID));
}
static ImageSurface showImage(ImageSurface surface, RGBImage img) {
return showImage(img, surface);
}
static ImageSurface showImage(RGBImage img, ImageSurface surface) {
if (surface == null)
return showImage(img);
else {
surface.setImage(img);
return surface;
}
}
static ImageSurface showImage(ImageSurface surface, BufferedImage img) {
return showImage(img, surface);
}
static ImageSurface showImage(ImageSurface surface, BufferedImage img, String title) {
return setFrameTitle(showImage(img, surface), title);
}
static ImageSurface showImage(ImageSurface surface, String title, RGBImage img) {
return showImage(surface, img, title);
}
static ImageSurface showImage(ImageSurface surface, RGBImage img, String title) {
return setFrameTitle(showImage(img, surface), title);
}
static ImageSurface showImage(BufferedImage img, ImageSurface surface) {
if (surface == null)
return showImage(img);
else {
surface.setImage(img);
return surface;
}
}
static JCheckBoxMenuItem jCheckBoxMenuItem(String text, boolean checked, final Object r) {
JCheckBoxMenuItem mi = new JCheckBoxMenuItem(text, checked);
mi.addActionListener(actionListener(r));
return mi;
}
static String shortenSnippetID(String snippetID) {
if (snippetID.startsWith("#"))
snippetID = snippetID.substring(1);
String httpBlaBla = "http://tinybrain.de/";
if (snippetID.startsWith(httpBlaBla))
snippetID = snippetID.substring(httpBlaBla.length());
return "" + parseLong(snippetID);
}
static boolean isSubstanceLAF() {
return substanceLookAndFeelEnabled();
}
static JMenuItem jmenuItem(String text, final Object r) {
JMenuItem mi = new JMenuItem(text);
mi.addActionListener(actionListener(r));
return mi;
}
static JMenuItem jMenuItem(String text, Object r) {
return jmenuItem(text, r);
}
static void revalidate(Component c) {
if (c == null || !c.isShowing()) return;
// magic combo to actually relayout and repaint
c.revalidate();
c.repaint();
}
static void addMenuItem(JPopupMenu menu, String text, Object action) {
menu.add(jmenuItem(text, action));
}
static void addMenuItem(JPopupMenu menu, JMenuItem menuItem) {
menu.add(menuItem);
}
static String programID;
static String getProgramID() {
return nempty(programID) ? formatSnippetIDOpt(programID) : "?";
}
// TODO: ask JavaX instead
static String getProgramID(Class c) {
String id = (String) getOpt(c, "programID");
if (nempty(id))
return formatSnippetID(id);
return "?";
}
static String getProgramID(Object o) {
return getProgramID(getMainClass(o));
}
static A setFrameTitle(A c, String title) {
JFrame f = getFrame(c);
if (f == null)
showFrame(title, c);
else
f.setTitle(title);
return c;
}
static A setFrameTitle(String title, A c) {
return setFrameTitle(c, title);
}
// magically find a field called "frame" in main class :-)
static JFrame setFrameTitle(String title) {
Object f = getOpt(mc(), "frame");
if (f instanceof JFrame)
return setFrameTitle((JFrame) f, title);
return null;
}
static String unnull(String s) {
return s == null ? "" : s;
}
static List unnull(List l) {
return l == null ? emptyList() : l;
}
static Iterable unnull(Iterable i) {
return i == null ? emptyList() : i;
}
static Object[] unnull(Object[] a) {
return a == null ? new Object[0] : a;
}
static BitSet unnull(BitSet b) {
return b == null ? new BitSet() : b;
}
static BufferedImage shootWindow(Window w) {
w.toFront();
sleep(50);
return shootScreen2(w.getBounds());
}
static void toggleAlwaysOnTop(JFrame frame) {
frame.setAlwaysOnTop(!frame.isAlwaysOnTop());
}
static String _userHome;
static String userHome() {
if (_userHome == null) {
if (isAndroid())
_userHome = "/storage/sdcard0/";
else
_userHome = System.getProperty("user.home");
//System.out.println("userHome: " + _userHome);
}
return _userHome;
}
static File userHome(String path) {
return new File(userDir(), path);
}
static class componentPopupMenu_Maker {
List menuMakers = new ArrayList();
}
static Map componentPopupMenu_map = new WeakHashMap();
static ThreadLocal componentPopupMenu_mouseEvent = new ThreadLocal();
// menuMaker = voidfunc(JPopupMenu)
static void componentPopupMenu(final JComponent component, final Object menuMaker) {
swingNowOrLater(new Runnable() { public void run() { try {
componentPopupMenu_Maker maker = componentPopupMenu_map.get(component);
if (maker == null) {
componentPopupMenu_map.put(component, maker = new componentPopupMenu_Maker());
final componentPopupMenu_Maker _maker = maker;
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) { displayMenu(e); }
public void mouseReleased(MouseEvent e) { displayMenu(e); }
void displayMenu(MouseEvent e) {
if (e.isPopupTrigger()) {
JPopupMenu menu = new JPopupMenu();
int emptyCount = menu.getComponentCount();
componentPopupMenu_mouseEvent.set(e);
for (Object menuMaker : _maker.menuMakers)
pcallF(menuMaker, menu);
// show menu if any items in it
if (menu.getComponentCount() != emptyCount)
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
}
maker.menuMakers.add(menuMaker);
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "componentPopupMenu_Maker maker = componentPopupMenu_map.get(component);\n if (maker == null) {\n componentPopupMenu_map.put(component, maker = new componentPopupMenu_Maker);\n final componentPopupMenu_Maker _maker = maker;\n component.addMouseListener(new MouseAdapter {\n public void mousePressed(MouseEvent e) { displayMenu(e); }\n public void mouseReleased(MouseEvent e) { displayMenu(e); }\n \n void displayMenu(MouseEvent e) {\n if (e.isPopupTrigger()) {\n new JPopupMenu menu;\n int emptyCount = menu.getComponentCount();\n \n componentPopupMenu_mouseEvent.set(e);\n for (Object menuMaker : _maker.menuMakers)\n pcallF(menuMaker, menu);\n \n // show menu if any items in it\n if (menu.getComponentCount() != emptyCount)\n menu.show(e.getComponent(), e.getX(), e.getY());\n }\n }\n });\n }\n maker.menuMakers.add(menuMaker);"; }});
}
// menuMaker = voidfunc(JPopupMenu)
static void titlePopupMenu(final Component c, final Object menuMaker) {
swingNowOrLater(new Runnable() { public void run() { try {
if (!isSubstanceLAF())
print("Can't add title right click!");
else {
JComponent titleBar = getTitlePaneComponent(getFrame(c));
componentPopupMenu(titleBar, menuMaker);
}
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "if (!isSubstanceLAF())\n print(\"Can't add title right click!\");\n else {\n JComponent titleBar = getTitlePaneComponent(getFrame(c));\n componentPopupMenu(titleBar, menuMaker);\n }"; }});
}
static Dimension getScreenSize() {
return Toolkit.getDefaultToolkit().getScreenSize();
}
static String formatSnippetIDOpt(String s) {
return isSnippetID(s) ? formatSnippetID(s) : s;
}
static Class getMainClass() {
return main.class;
}
static Class getMainClass(Object o) { try {
return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main");
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static String programTitle() {
return getProgramName();
}
// hmm, this shouldn't call functions really. That was just
// for coroutines.
static boolean isTrue(Object o) {
if (o instanceof Boolean)
return ((Boolean) o).booleanValue();
if (o == null) return false;
return ((Boolean) callF(o)).booleanValue();
}
static boolean isTrue(Object pred, Object arg) {
return booleanValue(callF(pred, arg));
}
static Object swing(Object f) {
return swingAndWait(f);
}
static Map synchroHashMap() {
return Collections.synchronizedMap(new HashMap());
}
static String copyTextToClipboard(String text) {
StringSelection selection = new StringSelection(text);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
return text;
}
static int isAndroid_flag;
static boolean isAndroid() {
if (isAndroid_flag == 0)
isAndroid_flag = System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0 ? 1 : -1;
return isAndroid_flag > 0;
}
static boolean substanceLookAndFeelEnabled() {
return startsWith(getLookAndFeel(), "org.pushingpixels.");
}
static JComponent getTitlePaneComponent(Window window) {
if (!substanceLookAndFeelEnabled()) return null;
JRootPane rootPane = null;
if (window instanceof JFrame) {
JFrame f = (JFrame) window;
rootPane = f.getRootPane();
}
if (window instanceof JDialog) {
JDialog d = (JDialog) window;
rootPane = d.getRootPane();
}
if (rootPane != null) {
Object /*SubstanceRootPaneUI*/ ui = rootPane.getUI();
return (JComponent) call(ui, "getTitlePane");
}
return null;
}
static void sleep(long ms) {
ping();
// allow spin locks
if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread");
try {
Thread.sleep(ms);
} catch (Exception e) { throw new RuntimeException(e); }
}
static void sleep() { try {
print("Sleeping.");
synchronized(main.class) { main.class.wait(); }
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static ActionListener actionListener(final Object runnable) {
return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { pcallF(runnable); }};
}
static boolean shootScreen_useScrot; // Use program "scrot" to make screenshots (on Linux)
static BufferedImage shootScreen2() {
return shootScreen2(screenRectangle());
}
static BufferedImage shootScreen2(Rectangle area) { try {
if (eq(area, screenRectangle())) {
if (shootScreen_useScrot && !isOnPATH("scrot")) shootScreen2_fallback();
if (shootScreen_useScrot) {
File f = prepareProgramFile(randomID(12) + ".png");
try {
String cmd = "scrot " + bashQuote(f);
String out = backtick(cmd);
if (f.exists())
return loadPNG(f);
shootScreen2_fallback();
} catch (Throwable e) {
print(e);
shootScreen2_fallback();
} finally {
f.delete();
}
}
}
return new Robot().createScreenCapture(area);
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static BufferedImage shootScreen2(Rect area) {
return shootScreen2(area.getRectangle());
}
static BufferedImage shootScreen2(int x, int y, int w, int h) {
return shootScreen2(new Rectangle(x, y, w, h));
}
// internal
static void shootScreen2_fallback() {
if (shootScreen_useScrot) {
print("Scrot failed. Reverting to internal screenshots.");
shootScreen_useScrot = false;
}
}
static long parseLong(String s) {
if (s == null) return 0;
return Long.parseLong(dropSuffix("L", s));
}
static long parseLong(Object s) {
return Long.parseLong((String) s);
}
static Object pcallF(Object f, Object... args) {
return pcallFunction(f, args);
}
static String formatSnippetID(String id) {
return "#" + parseSnippetID(id);
}
static String formatSnippetID(long id) {
return "#" + id;
}
static String classNameToVM(String name) {
return name.replace(".", "$");
}
static boolean booleanValue(Object o) {
return eq(true, o);
}
static List emptyList() {
return new ArrayList();
//ret Collections.emptyList();
}
static boolean nempty(Collection c) {
return !isEmpty(c);
}
static boolean nempty(CharSequence s) {
return !isEmpty(s);
}
static boolean nempty(Object[] o) {
return !isEmpty(o);
}
static boolean nempty(Map m) {
return !isEmpty(m);
}
static boolean nempty(Iterator i) {
return i != null && i.hasNext();
}
static Object getOpt(Object o, String field) {
return getOpt_cached(o, field);
}
static Object getOpt_raw(Object o, String field) {
try {
Field f = getOpt_findField(o.getClass(), field);
if (f == null) return null;
f.setAccessible(true);
return f.get(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Object getOpt(Class c, String field) {
try {
if (c == null) return null;
Field f = getOpt_findStaticField(c, field);
if (f == null) return null;
f.setAccessible(true);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field getOpt_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static Field getOpt_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static String getProgramName_cache;
static synchronized String getProgramName() {
if (getProgramName_cache == null)
getProgramName_cache = getSnippetTitleOpt(programID());
return getProgramName_cache;
}
static File userDir() {
return new File(userHome());
}
static File userDir(String path) {
return new File(userHome(), path);
}
static int moveToTopRightCorner_inset = 20;
static void moveToTopRightCorner(Component c) {
Window w = getWindow(c);
if (w != null)
w.setLocation(getScreenSize().width-w.getWidth()-moveToTopRightCorner_inset, moveToTopRightCorner_inset);
}
static JFrame frameIcon(Component c, String imageID) {
return setFrameIconLater(c, imageID);
}
/** possibly improvable */
static String bashQuote(String text) {
if (text == null) return null;
return "\"" + text
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r") + "\"";
}
static String bashQuote(File f) {
return bashQuote(f.getAbsolutePath());
}
static String getSnippetTitleOpt(String s) {
return isSnippetID(s) ? getSnippetTitle(s) : s;
}
static BufferedImage loadPNG(File file) {
return loadBufferedImage(file);
}
static Object pcallFunction(Object f, Object... args) {
try { /* pcall 1*/ return callFunction(f, args); /* pcall 2 */ } catch (Throwable __e) { printStackTrace(__e); }
return null;
}
static int backtick_exitValue;
static boolean backtick_verbose;
static ThreadLocal backtick_scriptFile = new ThreadLocal();
static String backtick(String cmd) { try {
File outFile = File.createTempFile("_backtick", "");
backtickToFile(cmd, outFile);
String result = loadTextFile(outFile.getPath(), "");
if (backtick_verbose)
print("[[\n" + result + "]]");
return result;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Process backtickToFile(String cmd, File outFile) { try {
try {
Process process = backtickToFile_noWait(cmd, outFile);
process.waitFor();
backtick_exitValue = process.exitValue();
if (backtick_verbose)
System.out.println("Process return code: " + backtick_exitValue);
return process;
} finally {
deleteFile(backtick_scriptFile.get());
backtick_scriptFile.set(null);
}
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Process backtickToFile_noWait(String cmd, File outFile) { try {
File scriptFile = File.createTempFile("_backtick", isWindows() ? ".bat" : "");
backtick_scriptFile.set(scriptFile);
if (backtick_verbose)
print("backtick: scriptFile " + f2s(scriptFile));
String command = cmd + " >" + bashQuote(outFile.getPath()) + " 2>&1";
//Log.info("[Backtick] " + command);
if (backtick_verbose)
print("backtick: command " + command);
saveTextFile(scriptFile.getPath(), command);
String[] command2;
if (isWindows())
command2 = new String[] { scriptFile.getPath() };
else
command2 = new String[] { "/bin/bash", scriptFile.getPath() };
if (backtick_verbose)
print("backtick: command2 " + structure(command2));
return Runtime.getRuntime().exec(command2);
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static File prepareProgramFile(String name) {
return mkdirsForFile(getProgramFile(name));
}
static File prepareProgramFile(String progID, String name) {
return mkdirsForFile(getProgramFile(progID, name));
}
static boolean isOnPATH(String cmd) {
String path = System.getenv("PATH");
List dirs = splitAt(path, File.pathSeparator);
String c = isWindows() ? cmd + ".exe" : cmd;
for (String dir : dirs)
if (new File(dir, c).isFile())
return true;
return false;
}
static String getLookAndFeel() {
return getClassName(UIManager.getLookAndFeel());
}
static Rectangle screenRectangle() {
return new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
}
static Window getWindow(Object o) {
if (!(o instanceof Component)) return null;
Component c = (Component) o;
while (c != null) {
if (c instanceof Window) return (Window) c;
c = c.getParent();
}
return null;
}
static JFrame setFrameIconLater(Component c, final String imageID) {
final JFrame frame = getFrame(c);
if (frame != null)
{ /*nt*/ Thread _t_0 = new Thread("Loading Icon") {
public void run() { /* in run */ try { /* pcall 1*/ /* in thread */
final Image i = imageIcon(or2(imageID, "#1005557")).getImage();
swingLater(new Runnable() { public void run() { try {
frame.setIconImage(i);
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "frame.setIconImage(i);"; }});
/* in thread */ /* pcall 2 */ } catch (Throwable __e) { printStackTrace(__e); } /* in run */ }
};
_t_0.start(); }
return frame;
}
static final WeakHashMap> getOpt_cache = new WeakHashMap();
static final HashMap getOpt_special = new HashMap(); // just a marker
static {
getOpt_cache.put(Class.class, getOpt_special);
getOpt_cache.put(String.class, getOpt_special);
}
static Object getOpt_cached(Object o, String field) { try {
if (o == null) return null;
Class c = o.getClass();
HashMap map;
synchronized(getOpt_cache) {
map = getOpt_cache.get(c);
if (map == null)
map = getOpt_makeCache(c);
}
if (map == getOpt_special) {
if (o instanceof Class)
return getOpt((Class) o, field);
if (o instanceof String)
return getOpt(getBot((String) o), field);
if (o instanceof Map)
return ((Map) o).get(field);
}
Field f = map.get(field);
if (f != null) return f.get(o);
if (o instanceof DynamicObject)
return ((DynamicObject) o).fieldValues.get(field);
return null;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
// used internally - we are in synchronized block
static HashMap getOpt_makeCache(Class c) {
HashMap map;
if (isSubtypeOf(c, Map.class))
map = getOpt_special;
else {
map = new HashMap();
Class _c = c;
do {
for (Field f : _c.getDeclaredFields()) {
f.setAccessible(true);
String name = f.getName();
if (!map.containsKey(name))
map.put(name, f);
}
_c = _c.getSuperclass();
} while (_c != null);
}
getOpt_cache.put(c, map);
return map;
}
static String programID() {
return getProgramID();
}
static String dropSuffix(String suffix, String s) {
return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static boolean isEmpty(Collection c) {
return c == null || c.isEmpty();
}
static boolean isEmpty(CharSequence s) {
return s == null || s.length() == 0;
}
static boolean isEmpty(Object[] a) {
return a == null || a.length == 0;
}
static boolean isEmpty(Map map) {
return map == null || map.isEmpty();
}
static int randomID_defaultLength = 12;
static String randomID(int length) {
return makeRandomID(length);
}
static String randomID() {
return randomID(randomID_defaultLength);
}
static boolean startsWith(String a, String b) {
return a != null && a.startsWith(b);
}
static boolean startsWith(List a, List b) {
if (a == null || l(b) > l(a)) return false;
for (int i = 0; i < l(b); i++)
if (neq(a.get(i), b.get(i)))
return false;
return true;
}
public static boolean isWindows() {
return System.getProperty("os.name").contains("Windows");
}
static Object getBot(String botID) {
return callOpt(getMainBot(), "getBot", botID);
}
static boolean isSubtypeOf(Class a, Class b) {
return b.isAssignableFrom(a); // << always hated that method, let's replace it!
}
// TODO: returns empty first, but not empty last
static List splitAt(String s, String splitter) {
List parts = new ArrayList();
int i = 0;
if (s != null)
while (i < l(s)) {
int j = indexOf(s, splitter, i);
if (j < 0) j = l(s);
parts.add(substring(s, i, j));
i = j+l(splitter);
}
return parts;
}
static String f2s(File f) {
return f == null ? null : f.getAbsolutePath();
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); } static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o instanceof String ? l((String) o)
: l((Collection) o); // incomplete
}
static int l(MultiSet ms) { return ms == null ? 0 : ms.size(); }
static String getSnippetTitle(String id) { try {
if (!isSnippetID(id)) return "?";
return trim(loadPageSilently(new URL("http://tinybrain.de:8080/tb-int/getfield.php?id=" + parseSnippetID(id) + "&field=title" + standardCredentials())));
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static boolean neq(Object a, Object b) {
return !eq(a, b);
}
static String or2(String a, String b) {
return nempty(a) ? a : b;
}
/** writes safely (to temp file, then rename) */
public static void saveTextFile(String fileName, String contents) throws IOException {
File file = new File(fileName);
File parentFile = file.getParentFile();
if (parentFile != null)
parentFile.mkdirs();
String tempFileName = fileName + "_temp";
File tempFile = new File(tempFileName);
if (contents != null) {
if (tempFile.exists()) try {
String saveName = tempFileName + ".saved." + now();
copyFile(tempFile, new File(saveName));
} catch (Throwable e) { printStackTrace(e); }
FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.print(contents);
printWriter.close();
}
if (file.exists() && !file.delete())
throw new IOException("Can't delete " + fileName);
if (contents != null)
if (!tempFile.renameTo(file))
throw new IOException("Can't rename " + tempFile + " to " + file);
}
public static void saveTextFile(File fileName, String contents) {
try {
saveTextFile(fileName.getPath(), contents);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static String getClassName(Object o) {
return o == null ? "null" : o.getClass().getName();
}
public static File mkdirsForFile(File file) {
File dir = file.getParentFile();
if (dir != null) // is null if file is in current dir
dir.mkdirs();
return file;
}
static ImageIcon imageIcon(String imageID) { try {
return new ImageIcon(loadBinarySnippet(imageID).toURI().toURL());
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static ImageIcon imageIcon(BufferedImage img) {
return new ImageIcon(img);
}
static ImageIcon imageIcon(RGBImage img) {
return imageIcon(img.getBufferedImage());
}
static boolean deleteFile(File file) {
return file != null && file.delete();
}
static File getProgramFile(String progID, String fileName) {
if (new File(fileName).isAbsolute())
return new File(fileName);
return new File(getProgramDir(progID), fileName);
}
static File getProgramFile(String fileName) {
return getProgramFile(getProgramID(), fileName);
}
public static String loadTextFile(String fileName) {
return loadTextFile(fileName, null);
}
public static String loadTextFile(String fileName, String defaultContents) {
try {
if (!new File(fileName).exists())
return defaultContents;
FileInputStream fileInputStream = new FileInputStream(fileName);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
return loadTextFile(inputStreamReader);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String loadTextFile(File fileName) {
return loadTextFile(fileName, null);
}
public static String loadTextFile(File fileName, String defaultContents) {
return loadTextFile(fileName.getPath(), defaultContents);
}
public static String loadTextFile(Reader reader) throws IOException {
StringBuilder builder = new StringBuilder();
try {
char[] buffer = new char[1024];
int n;
while (-1 != (n = reader.read(buffer)))
builder.append(buffer, 0, n);
} finally {
reader.close();
}
return builder.toString();
}
static String makeRandomID(int length) {
Random random = new Random();
char[] id = new char[length];
for (int i = 0; i < id.length; i++)
id[i] = (char) ((int) 'a' + random.nextInt(26));
return new String(id);
}
static boolean structure_showTiming, structure_checkTokenCount;
static String structure(Object o) {
structure_Data d = new structure_Data();
StringWriter sw = new StringWriter();
d.out = new PrintWriter(sw);
structure_go(o, d);
String s = str(sw);
if (structure_checkTokenCount) {
print("token count=" + d.n);
assertEquals("token count", l(javaTokC(s)), d.n);
}
return s;
}
static void structure_go(Object o, structure_Data d) {
structure_1(o, d);
while (nempty(d.stack))
popLast(d.stack).run();
}
static void structureToPrintWriter(Object o, PrintWriter out) {
structure_Data d = new structure_Data();
d.out = out;
structure_go(o, d);
}
// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
static int structure_shareStringsLongerThan = 20;
static class structure_Data {
PrintWriter out;
int stringSizeLimit;
IdentityHashMap