defunctIDs = acc_defunctProgramIDs(acc);
if (nempty(defunctIDs)) {
S desc = shortClassName(c);
//print("DEFUNCT ACC IN COMPONENT " + field + "! Cleaning. " + defunctIDs + ": " + desc);
set(c, field, acc_current());
}
}
*/
}
static volatile long gc_memoryUsedAfterGC;
static volatile long gc_lastStart;
static volatile long gc_duration;
static Lock gc_lock = lock();
static void gc() {
callOpt(javax(), "cleanWeakMaps");
{
Lock __0 = gc_lock; lock(__0); try {
gc_lastStart = sysNow();
System.gc();
gc_duration = sysNow()-gc_lastStart;
gc_memoryUsedAfterGC = usedMemory();
} finally { unlock(__0); } }
vmBus_send("gcDone");
}
static void clearSnippetTitleCacheIfOnline() {
if (canConnectToTinyBrainDE())
clearSnippetTitleCache();
}
static Map classForNameOpt_cache = synchroHashMap();
static Class classForNameOpt(String name) { try {
if (classForNameOpt_cache.containsKey(name))
return classForNameOpt_cache.get(name);
Class c = null;
try { c = Class.forName(name); } catch (Throwable _e) {}
classForNameOpt_cache.put(name, c);
return c;
} catch (Exception __e) { throw rethrow(__e); } }
static String jlabel_textAsHTML_center(String text) {
return ""
+ replace(htmlencode2(text), "\n", "
")
+ "
";
}
static A onResizeAndNow(A c, Object r) {
if (c != null && r != null) { swing(() -> {
onResize(c, toRunnable(r));
callF(r);
}); }
return c;
}
static A setVerticalAlignment(final int pos, final A a) {
if (a != null) { swing(() -> { a.setVerticalAlignment(pos); }); }
return a;
}
static A setVerticalAlignment(final int pos, final A a) {
if (a != null) { swing(() -> { a.setVerticalAlignment(pos); }); }
return a;
}
static void growInternalFrameLeft(Component c, final int howMuch) {
final JInternalFrame w = getInternalFrame(c);
if (w != null) { swing(() -> {
Rectangle r = w.getBounds();
w.setBounds(r.x-howMuch, r.y, r.width+howMuch, r.height);
}); }
}
static void growInternalFrameSouth(Component c, final int howMuch) {
final JInternalFrame w = getInternalFrame(c);
if (w != null) { swing(() -> {
Rectangle r = w.getBounds();
w.setBounds(r.x, r.y, r.width, r.height+howMuch);
}); }
}
static Set asSet(Object[] array) {
HashSet set = new HashSet();
for (Object o : array)
if (o != null)
set.add(o);
return set;
}
static Set asSet(String[] array) {
TreeSet set = new TreeSet();
for (String o : array)
if (o != null)
set.add(o);
return set;
}
static Set asSet(Iterable l) {
if (l instanceof Set) return (Set) l;
HashSet set = new HashSet();
for (A o : unnull(l))
if (o != null)
set.add(o);
return set;
}
// Note: does not clone the set (keeps multiset alive)
static Set asSet(MultiSet ms) {
return ms == null ? null : ms.asSet();
}
static List tlft(String s) { return toLinesFullTrim(s); }
static List tlft(File f) { return toLinesFullTrim(f); }
static A conceptWhere(Class c, Object... params) {
return findConceptWhere(c, params);
}
static A conceptWhere(Concepts cc, Class c, Object... params) {
return findConceptWhere(cc, c, params);
}
static void stefansOS_installQuickSearchHelper() {
ownResource(tempOnKeyPressed(KeyEvent.VK_DOWN, new Runnable() { public void run() { try { dm_activateFirstQuickSearchModule();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "dm_activateFirstQuickSearchModule();"; }}, dm_topInputField()));
}
static AutoCloseable tempSetMCOpt(final String field, Object value) {
final Object oldValue = getMCOpt(field);
setMCOpt(field, value);
return new AutoCloseable() { public String toString() { return "setMCOpt(field, oldValue);"; } public void close() throws Exception { setMCOpt(field, oldValue); }};
}
static List syncMap(Object f, Map map) {
return syncMap(map, f);
}
// map: func(key, value) -> list element
static List syncMap(Map map, Object f) {
return map(cloneLinkedHashMap(map), f); // TODO: use a temporary list instead
}
static Map syncMap() {
return synchroHashMap();
}
static Map syncMap(Map map) {
return synchronizedMap(map);
}
static boolean getServerTranspiled2_allowLocalFallback = true, getServerTranspiled2_localFallbackVerbose = true;
// to avoid checking server for transpilations too often when booting OS
static Map getServerTranspiled2_tempCache;
static String getServerTranspiled2(String id) {
IResourceLoader rl = vm_getResourceLoader();
if (rl != null)
return rl.getTranspiled(id);
return getServerTranspiled2_noResourceLoader(id);
}
static String getServerTranspiled2_noResourceLoader(String id) {
id = fsIOpt(id);
String transpiled = mapGet(getServerTranspiled2_tempCache, id);
if (transpiled != null) return transpiled;
//if (getServerTranspiled2_tempCache != null) print("CACHE FAIL on " + id);
transpiled = loadCachedTranspilation(id);
String md5 = null;
if (machineIsOffline() || isOfflineMode() || isLocalSnippet(id)) return transpiled;
if (transpiled != null)
md5 = md5(transpiled);
String transpiledSrc;
try {
transpiledSrc = getServerTranspiled(formatSnippetID(id), md5);
} catch (Throwable e) {
if (!getServerTranspiled2_allowLocalFallback) rethrow(e);
printExceptionShort(e);
if (getServerTranspiled2_localFallbackVerbose) print("Fallback to local code");
return transpiled;
}
if (eq(transpiledSrc, "SAME")) {
if (!isTrue(loadPage_silent.get())) printWithMS("SAME");
return mapPut_returnValue(getServerTranspiled2_tempCache, id, transpiled);
}
return mapPut_returnValue(getServerTranspiled2_tempCache, id, transpiledSrc);
}
static AutoCloseable tempSetTL(ThreadLocal tl, A a) {
return tempSetThreadLocal(tl, a);
}
static AutoCloseable tempSetTL(BetterThreadLocal tl, A a) {
return tempSetThreadLocalIfNecessary(tl, a);
}
static void close(AutoCloseable c) {
_close(c);
}
static void disposeInternalFrame(Component c) {
final JInternalFrame f = getInternalFrame(c);
if (f != null) { swing(() -> {
vmBus_send("disposingInternalFrame", f);
f.dispose();
// now illegal... setOpt(f, lastFocusOwner := null); // Help GC
}); }
}
static int iceil(double d) {
return (int) Math.ceil(d);
}
static double elapsedSeconds(long time) {
return toSeconds(sysNow()-time);
}
static double elapsedSeconds(Timestamp ts) {
return ts == null ? Double.NaN : toSeconds(now()-ts.date);
}
static List sortedByComparator(Collection l, Comparator c) {
List l2 = cloneList(l);
sort(l2, c);
return l2;
}
static List sortedByComparator(Comparator c, Collection l) {
return sortedByComparator(l, c);
}
static Comparator combineComparators(final Comparator... l) {
return new Comparator() {
public int compare(A a, A b) {
for (Comparator c : l) {
int i = c.compare(a, b);
if (i != 0) return i;
}
return 0;
}
};
}
static Comparator fieldComparator(final String field) {
return new Comparator() {
public int compare(A a, A b) {
return cmp(getOpt(a, field), getOpt(b, field));
}
};
}
static Comparator descFieldComparator(final String field) {
return new Comparator() {
public int compare(A a, A b) {
return cmp(getOpt(b, field), getOpt(a, field));
}
};
}
static Object pcallF(Object f, Object... args) {
return pcallFunction(f, args);
}
static A pcallF(F0 f) {
try { return f == null ? null : f.get(); } catch (Throwable __e) { pcallFail(__e); } return null;
}
static B pcallF(F1 f, A a) {
try { return f == null ? null : f.get(a); } catch (Throwable __e) { pcallFail(__e); } return null;
}
static void pcallF(VF1 f, A a) {
try {
{ if (f != null) f.get(a); }
} catch (Throwable __e) { pcallFail(__e); }
}
static Object pcallF(Runnable r) {
try { { if (r != null) r.run(); } } catch (Throwable __e) { pcallFail(__e); } return null;
}
static A pcallF(IF0 f) {
try { return f == null ? null : f.get(); } catch (Throwable __e) { pcallFail(__e); } return null;
}
static B pcallF(IF1 f, A a) {
try { return f == null ? null : f.get(a); } catch (Throwable __e) { pcallFail(__e); } return null;
}
static String unnull(String s) {
return s == null ? "" : s;
}
static Collection unnull(Collection l) {
return l == null ? emptyList() : l;
}
static List unnull(List l) { return l == null ? emptyList() : l; }
static int[] unnull(int[] l) { return l == null ? emptyIntArray() : l; }
static char[] unnull(char[] l) { return l == null ? emptyCharArray() : l; }
static double[] unnull(double[] l) { return l == null ? emptyDoubleArray() : l; }
static float[] unnull(float[] l) { return l == null ? emptyFloatArray() : l; }
static Map unnull(Map l) {
return l == null ? emptyMap() : l;
}
static Iterable unnull(Iterable i) {
return i == null ? emptyList() : i;
}
static A[] unnull(A[] a) {
return a == null ? (A[]) emptyObjectArray() : a;
}
static BitSet unnull(BitSet b) {
return b == null ? new BitSet() : b;
}
static Pt unnull(Pt p) {
return p == null ? new Pt() : p;
}
//ifclass Symbol
static Symbol unnull(Symbol s) {
return s == null ? emptySymbol() : s;
}
//endif
static Pair unnull(Pair p) {
return p != null ? p : new Pair(null, null);
}
static int unnull(Integer i) { return i == null ? 0 : i; }
static long unnull(Long l) { return l == null ? 0L : l; }
static double unnull(Double l) { return l == null ? 0.0 : l; }
static List list(Class type) { return list(type, db_mainConcepts()); }
static List list(Class type, Concepts cc) {
return cc.list(type);
}
static List list(Concepts concepts, Class type) {
return concepts.list(type);
}
static List list(String type) {
return db_mainConcepts().list(type);
}
static List list(Concepts concepts, String type) {
return concepts.list(type);
}
static List list(Concepts concepts) {
return asList(concepts.allConcepts());
}
static int getComponentZOrder(final Component c) {
return c == null ? -1 : swing(new F0() { public Integer get() { try {
Container parent = c.getParent();
return parent == null ? -1 : parent.getComponentZOrder(c);
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "Container parent = c.getParent();\r\n ret parent == null ? -1 : parent.getCo..."; }});
}
static void autoConsole() {
setOpt(getJavaX(), "consoleUpdateOff", false);
}
static boolean killProgram(String idToKill) {
return killProgramID(idToKill);
}
static void preCleanUp(Object c) {
if (c instanceof Collection) { for (Object o : ((Collection) c)) preCleanUp(o); return; }
callOpt(c, "licensed_off");
setOpt_raw(c, "ping_anyActions" , true); // so ping notices
setOpt_raw(c, "cleaningUp_flag" , true);
}
static boolean cleanUp_interruptThreads = false; // experimental
static void cleanUp(Object c) {
if (c == null) return;
if (c instanceof AutoCloseable) { close_pcall((AutoCloseable) c); return; }
if (c instanceof java.util.Timer) { ((java.util.Timer) c).cancel(); return; }
if (c instanceof Collection) { cleanUp((Collection) c); return; }
if (c instanceof Map) {
for (Object o : keysList((Map) c)) cleanUp(o);
for (Object o : valuesList((Map) c)) cleanUp(o);
syncClear((Map) c);
return;
}
//if (!(c instanceof Class)) ret;
try {
// revoke license
preCleanUp(c);
// unpause
setOpt_raw(c, "ping_pauseAll", false);
// call custom cleanMeUp() and cleanMeUp_*() functions
innerCleanUp(c);
// Java spec says finalize should only be called by GC,
// but we care to differ.
// Edit: Not anymore (illegal access warnings)
/*if (isTrue(vmMap_get('callFinalize)))
pcallOpt(c, "finalize");*/
// remove all virtual bots (hope this works)
List androids = (List) getOpt(c, "record_list");
for (Object android : unnull(androids))
pcallOpt(android, "dispose"); // heck we'll dispose anything
// sub-cleanup
List classes = (List) (getOpt(c, "hotwire_classes"));
if (classes != null)
for (WeakReference cc : classes) { try {
cleanUp(cc.get());
} catch (Throwable __e) { pcallFail(__e); }}
// interrupt all threads (experimental, they might be doing cleanup?)
if (cleanUp_interruptThreads) {
List threads = registeredThreads(c);
if (nempty(threads)) {
print("cleanUp: Interrupting " + n2(threads, "thread") + ": " + joinWithComma(allToString(threads)));
interruptThreads(threads);
}
}
} catch (Throwable __e) { pcallFail(__e); }
setOpt_raw(c, "cleaningUp_flag" , false);
if (c instanceof Class && ((Class) c).getName().equals("main"))
retireClassLoader(((Class) c).getClassLoader());
}
static void cleanUp(Collection l) {
if (l == null) return;
for (Object c : l)
cleanUp(c);
l.clear();
}
// unclear semantics as to whether return null on null
static ArrayList asList(A[] a) {
return a == null ? new ArrayList() : new ArrayList(Arrays.asList(a));
}
static ArrayList asList(char[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (var i : a) l.add(i);
return l;
}
static ArrayList asList(byte[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (var i : a) l.add(i);
return l;
}
static ArrayList asList(int[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (int i : a) l.add(i);
return l;
}
static ArrayList asList(long[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (long i : a) l.add(i);
return l;
}
static ArrayList asList(float[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (float i : a) l.add(i);
return l;
}
static ArrayList asList(double[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (double i : a) l.add(i);
return l;
}
static ArrayList asList(short[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (short i : a) l.add(i);
return l;
}
static ArrayList asList(Iterator it) {
ArrayList l = new ArrayList();
if (it != null)
while (it.hasNext())
l.add(it.next());
return l;
}
// disambiguation
static ArrayList asList(IterableIterator s) {
return asList((Iterator) s);
}
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 ArrayList asList(Producer p) {
ArrayList l = new ArrayList();
A a;
if (p != null) while ((a = p.next()) != null)
l.add(a);
return l;
}
static ArrayList asList(Enumeration e) {
ArrayList l = new ArrayList();
if (e != null)
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
static ArrayList asList(ReverseChain c) {
return c == null ? emptyList() : c.toList();
}
static List asList(Pair p) {
return p == null ? null : ll(p.a, p.b);
}
static Collection conceptsWhere(Class c, Object... params) {
return findConceptsWhere(c, params);
}
static Collection conceptsWhere(String c, Object... params) {
return findConceptsWhere(c, params);
}
static Collection conceptsWhere(Concepts concepts, Class c, Object... params) {
return findConceptsWhere(concepts, c, params);
}
static boolean hasConcept(Class extends Concept> c, Object... params) { return hasConcept(db_mainConcepts(), c, params); }
static boolean hasConcept(Concepts cc, Class extends Concept> c, Object... params) {
return findConceptWhere(cc, c, params) != null;
}
static Object[] concatArrays(Object[]... arrays) {
int l = 0;
for (Object[] a : arrays) l += l(a);
Object[] x = new Object[l];
int i = 0;
for (Object[] a : arrays) if (a != null) {
System.arraycopy(a, 0, x, i, l(a));
i += l(a);
}
return x;
}
static AutoCloseable tempSet(Object o, final String field, Object value) {
return tempSetField(o, field, value);
}
static boolean contains(Collection c, Object o) {
return c != null && c.contains(o);
}
static boolean contains(Iterable it, Object a) {
if (it != null)
for (Object o : it)
if (eq(a, o))
return true;
return false;
}
static boolean contains(Object[] x, Object o) {
if (x != null)
for (Object a : x)
if (eq(a, o))
return true;
return false;
}
static boolean contains(String s, char c) {
return s != null && s.indexOf(c) >= 0;
}
static boolean contains(String s, String b) {
return s != null && s.indexOf(b) >= 0;
}
static boolean contains(BitSet bs, int i) {
return bs != null && bs.get(i);
}
static boolean contains(Producer p, A a) {
if (p != null && a != null) while (true) {
A x = p.next();
if (x == null) break;
if (eq(x, a)) return true;
}
return false;
}
static boolean contains(Rect r, Pt p) { return rectContains(r, p); }
static RuntimeException failWithInfoBox(String msg) {
return failInfoBox(msg);
}
static void stefansOS_popOutModule(Module m) { try {
final JInternalFrame myFrame = (JInternalFrame) (getPossiblyInternalFrame(m.vis));
if (myFrame == null) return;
if (getParent(myFrame) != main.desktop) return; // popped out already
Rectangle boundsOnScreen = boundsOnScreen(myFrame);
boolean alwaysOnTop = m.hasFlag(m.ALWAYSONTOPWHENPOPPEDOUT) ;
// make a new frame with a new DesktopPane
// (a little trick so we can keep using JInternalFrame)
JDesktopPane desktopPane = jDesktopPane();
autoFixDesktopPane(desktopPane);
showFrame(m.moduleName(), desktopPane);
JFrame frame = getFrame(desktopPane);
// title popup menu items
if (!frameTitlePopupMenuItems(frame,
"Pop Back In", new Runnable() { public void run() { try { stefansOS_popInModule(m) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "stefansOS_popInModule(m)"; }},
jCheckBoxMenuItem("Always On Top", alwaysOnTop, new Runnable() { public void run() { try { toggleAlwaysOnTop(frame) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "toggleAlwaysOnTop(frame)"; }}))) // TODO: don't use toggle, use set instead
// can't add title menu - add button instead
replaceComponent(desktopPane, new F1() { public Component get(JComponent dp) { try {
return withRightAlignedButtons_marginAttachedToCenter(dp,
onChange(jCheckBox("Always On Top", alwaysOnTop), new Runnable() { public void run() { try { toggleAlwaysOnTop(frame) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "toggleAlwaysOnTop(frame)"; }}),
"Pop Back In", new Runnable() { public void run() { try { stefansOS_popInModule(m) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "stefansOS_popInModule(m)"; }}); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "withRightAlignedButtons_marginAttachedToCenter(dp,\r\n onChange(jCheckBo..."; }});
// position, listeners, flags, show
setFrameBounds(desktopPane, boundsOnScreen);
swing(new Runnable() { public void run() { try {
removeFromParent(myFrame);
onResizeAndNow(desktopPane, new Runnable() { public void run() { try {
myFrame.setBounds(0, 0, desktopPane.getWidth(), desktopPane.getHeight())
;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "myFrame.setBounds(0, 0, desktopPane.getWidth(), desktopPane.getHeight())"; }});
desktopPane.add(myFrame);
myFrame.setMaximum(true);
myFrame.setMaximizable(false);
myFrame.setClosable(false);
myFrame.setIconifiable(false);
frame.setAlwaysOnTop(alwaysOnTop);
InternalFrameUI ui = myFrame.getUI();
if (ui instanceof BasicInternalFrameUI)
((BasicInternalFrameUI) ui).setNorthPane(null);
cset(m, "poppedOut" , true);
vmBus_send("modulePoppedOut", m, myFrame);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "removeFromParent(myFrame);\r\n onResizeAndNow(desktopPane, r {\r\n myFram..."; }});
} catch (Exception __e) { throw rethrow(__e); } }
// TODO: test if android complains about this
static boolean isAWTThread() {
if (isAndroid()) return false;
if (isHeadless()) return false;
return isAWTThread_awt();
}
static boolean isAWTThread_awt() {
return SwingUtilities.isEventDispatchThread();
}
static void runInQAndWait(Q q, Runnable r) {
if (r == null) return;
if (isInQ(q)) { callF(r); return; }
final Flag done = new Flag();
final Var error = new Var();
q.add(new Runnable() { public void run() { try {
try {
callF(r);
} catch (Throwable e) {
error.set(e);
} finally {
done.raise();
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "try {\r\n callF(r);\r\n } catch (Throwable e) {\r\n error.set(e);\r\n ..."; }});
done.waitUntilUp();
if (error.has()) throw rethrow(error.get());
}
static F0 toF0(final Object f) {
return f0(f);
}
static A evalInQ(Q q, IF0 f) {
return evalInQ(q, if0ToF0(f));
}
static A evalInQ(Q q, F0 f) {
if (isInQ(q)) return callF(f);
final Var> var = new Var();
q.add(new Runnable() { public void run() { try {
try {
var.set(eitherA(callF(f)));
} catch (Throwable e) {
var.set(eitherB(e));
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "try {\r\n var.set(eitherA(callF(f)));\r\n } catch (Throwable e) {\r\n ..."; }});
return returnOrThrow_either(waitForVarToBeNotNull(var));
}
static boolean stefansOS_moduleIsPoppedOut(Module m) {
final JInternalFrame myFrame = (JInternalFrame) (getPossiblyInternalFrame(m.vis));
if (myFrame == null) return false;
return getParent(myFrame) != main.desktop;
}
static boolean activateFrame(final Component c, Object... __) {
return swing(new F0() { public Boolean get() { try {
Frame f = getAWTFrame(c);
if (f == null) return false;
if (!f.isVisible()) f.setVisible(true);
boolean windowsHack = optPar("windowsHack", __, true);
boolean iconified = f.getState() == Frame.ICONIFIED;
boolean maximize = boolPar("maximize", __);
if (iconified)
f.setState(maximize ? Frame.MAXIMIZED_BOTH : Frame.NORMAL);
// My glorious Windows hack
// See: https://stackoverflow.com/questions/309023/how-to-bring-a-window-to-the-front
if (windowsHack && !iconified && isWindows()) {
boolean fullscreen = f.getExtendedState() == Frame.MAXIMIZED_BOTH;
f.setExtendedState(JFrame.ICONIFIED);
f.setExtendedState(fullscreen ? JFrame.MAXIMIZED_BOTH : JFrame.NORMAL);
}
f.toFront();
return true;
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "Frame f = getAWTFrame(c);\r\n if (f == null) false;\r\n if (!f.isVisible())..."; }});
}
static void activateInternalFrame(final Component c) {
swing(new Runnable() { public void run() { try {
JInternalFrame f = getInternalFrame(c);
if (f == null) return;
if (!f.isVisible()) f.setVisible(true);
if (f.isIcon()) f.setIcon(false);
f.setSelected(true);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JInternalFrame f = getInternalFrame(c);\r\n if (f == null) return;\r\n if (..."; }});
}
static AutoCloseable tempSetThreadLocal(final ThreadLocal tl, A a) {
if (tl == null) return null;
final A prev = setThreadLocal(tl, a);
return new AutoCloseable() { public String toString() { return "tl.set(prev);"; } public void close() throws Exception { tl.set(prev); }};
}
static JInternalFrame showInternalFrame(final JDesktopPane desktop, final String title, final int x, final int y, final int w, final int h) {
return addInternalFrame(desktop, title, x, y, w, h);
}
static JInternalFrame showInternalFrame(final JDesktopPane desktop, final String title, final int x, final int y, final int w, final int h, Component contents) {
return addInternalFrame(desktop, title, x, y, w, h, contents);
}
static JInternalFrame showInternalFrame(final JDesktopPane desktop, final String title, Rect r, Component contents) {
return addInternalFrame(desktop, title, r, contents);
}
static JInternalFrame showInternalFrame(final JDesktopPane desktop, final String title, final Component contents) {
return addInternalFrame(desktop, title, contents);
}
static JInternalFrame showInternalFrame(JDesktopPane desktop, String title) {
return addInternalFrame(desktop, title);
}
static boolean isTrue(Object o) {
if (o instanceof Boolean)
return ((Boolean) o).booleanValue();
if (o == null) return false;
if (o instanceof ThreadLocal) // TODO: remove this
return isTrue(((ThreadLocal) o).get());
throw fail(getClassName(o));
}
static boolean isTrue(Boolean b) {
return b != null && b.booleanValue();
}
static Object callOpt(Object o) {
return callF(o);
}
static Object callOpt(Object o, String method, Object... args) {
return callOpt_withVarargs(o, method, args);
}
static Pt toPt(Point p) {
return p == null ? null : new Pt(p.x, p.y);
}
static Pt toPt(Dimension d) {
return d == null ? null : new Pt(d.width, d.width);
}
static Dimension getPreferredSize(final Component c) {
return c == null ? null : swing(new F0() { public Dimension get() { try { return c.getPreferredSize(); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "return c.getPreferredSize();"; }});
}
static A centerPackInternalFrameWithMinSize(int w, int h, A c) {
return centerPackInternalFrameWithMinSize(new Pt(w, h), c);
}
static A centerPackInternalFrameWithMinSize(Pt minSize, A c) {
JInternalFrame win = getInternalFrame(c);
if (win == null || getParent(win) == null) return c;
{ swing(() -> {
win.pack();
if (minSize != null) minFrameSize(win, minSize.x, minSize.y);
centerInternalFrame(win);
fixInternalFrame(win);
}); }
return c;
}
static A centerPackInternalFrameWithMinSize(A c, Pt minSize) {
return centerPackInternalFrameWithMinSize(minSize, c);
}
static void onInternalFrameIconified(final JInternalFrame w, final Object r) {
if (w != null) { swing(() -> {
w.addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameIconified(InternalFrameEvent e) {
pcallF(r);
}
});
}); }
}
static void onInternalFrameClosing(final JInternalFrame w, final Object r) {
if (w != null) { swing(() -> {
w.addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameClosing(InternalFrameEvent e) {
pcallF(r);
}
});
}); }
}
static void onInternalFrameActivated(final JInternalFrame w, final Object r) {
if (w != null) { swing(() -> {
w.addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameActivated(InternalFrameEvent e) {
pcallF(r);
}
});
}); }
}
static void onInternalFrameDeactivated(final JInternalFrame w, final Object r) {
if (w != null) { swing(() -> {
w.addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameDeactivated(InternalFrameEvent e) {
pcallF(r);
}
});
}); }
}
static A onBoundsChange(A c, final Object r) {
if (c != null && r != null) { swing(() -> {
c.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) { pcallF(r); }
public void componentMoved(ComponentEvent e) { pcallF(r); }
});
}); }
return c;
}
static A onBoundsChange(Object r, A c) {
return onBoundsChange(c, r);
}
static JInternalFrame internalFrameIcon(Component c, final String imageID) {
final JInternalFrame frame = getInternalFrame(c);
if (frame != null) startThread("Loading Icon", new Runnable() { public void run() { try {
final Icon i = imageIcon(or2(imageID, "#1005557"));
swingLater(new Runnable() { public void run() { try { frame.setFrameIcon(i);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "frame.setFrameIcon(i);"; }});
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final Icon i = imageIcon(or2(imageID, \"#1005557\"));\r\n swingLater(new Runna..."; }});
return frame;
}
static JInternalFrame internalFrameIcon(String imageID, Component c) {
return internalFrameIcon(c, imageID);
}
static A nu(Class c, Object... values) {
A a = nuObject(c);
setAll(a, values);
return a;
}
static long now_virtualTime;
static long now() {
return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}
static void removeConcept(long id) {
deleteConcept(id);
}
static void removeConcept(Concept c) {
deleteConcept(c);
}
static void removeConcept(Concept.Ref ref) {
deleteConcept(ref);
}
static void logStructure(File logFile, Object o) {
logQuoted(logFile, structure(o));
}
// quick version - log to file in program directory
static void logStructure(String fileName, Object o) {
logStructure(getProgramFile(fileName), o);
}
static void logStructure(String progID, String fileName, Object o) {
logStructure(getProgramFile(progID, fileName), o);
}
// don't use this anymore, use the other version below
static File deletedModulesLogFile() {
return javaxDataDir("deleted-modules.log");
}
static File deletedModulesLogFile(String moduleSnippetID) {
return javaxDataDir("Deleted Modules/" + dropPrefix("#", moduleSnippetID) + ".txt");
}
static HashMap litmap(Object... x) {
HashMap map = new HashMap();
litmap_impl(map, x);
return map;
}
static void litmap_impl(Map map, Object... x) {
if (x != null) for (int i = 0; i < x.length-1; i += 2)
if (x[i+1] != null)
map.put(x[i], x[i+1]);
}
static String aGlobalID() {
return randomID(globalIDLength());
}
static String aGlobalID(Random random) {
return randomID(random, globalIDLength());
}
static void setInternalFrameContents(final Component c, final Object contents) {
{ swing(() -> {
JInternalFrame frame = getInternalFrame(c);
if (frame == null) return;
frame.getContentPane().removeAll();
frame.getContentPane().setLayout(new BorderLayout());
if (contents != null) frame.getContentPane().add(wrap(contents));
revalidate(frame);
}); }
}
static void lock(Lock lock) { try {
ping();
if (lock == null) return;
try {
vmBus_send("locking", lock, "thread" , currentThread());
lock.lockInterruptibly();
vmBus_send("locked", lock, "thread" , currentThread());
} catch (InterruptedException e) {
Object reason = vm_threadInterruptionReasonsMap().get(currentThread());
print("Locking interrupted! Reason: " + strOr(reason, "Unknown"));
printStackTrace(e);
rethrow(e);
}
// NO call to ping here! Make sure lock is always released.
} catch (Exception __e) { throw rethrow(__e); } }
static void lock(Lock lock, String msg) {
print("Locking: " + msg);
lock(lock);
}
static void lock(Lock lock, String msg, long timeout) {
print("Locking: " + msg);
lockOrFail(lock, timeout);
}
static ReentrantLock lock() {
return fairLock();
}
static Rect toRect(Rectangle r) {
return r == null ? null : new Rect(r);
}
static Rect toRect(RectangularShape r) {
return r == null ? null : toRect(r.getBounds());
}
static Rect toRect(Rect r) { return r; }
static Rectangle getBounds(final Component c) {
return c == null ? null : swing(new F0() { public Rectangle get() { try { return c.getBounds(); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "return c.getBounds();"; }});
}
static String javaxSourceOfMyClass1(String name) {
return joinOrNull(/*findInnerClassOfMain*/findTopLevelClass(javaTok(mySource()), name));
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return c == null ? "null" : new String(c);
}
static String str(char[] c, int offset, int count) {
return new String(c, offset, count);
}
static PersistableThrowable persistableThrowable(Throwable e) {
return e == null ? null : new PersistableThrowable(e);
}
static Map humanizeFormLabel_replacements = litmap("id" , "ID", "md5" , "MD5");
static String humanizeFormLabel(String s) {
if (!isIdentifier(s)) return s;
return firstToUpper(
joinWithSpace(replaceElementsUsingMap(splitCamelCase(s), humanizeFormLabel_replacements)).replace("I D", "ID")
);
}
static boolean anyCommonBits(int a, int b) {
return (a & b) != 0;
}
static JLabel jCenteredMultiLineLabel(String text) {
return jcenteredlabel(jlabel_textAsHTML_center(text));
}
static A findConcept(Class c, Object... params) {
return findConceptWhere(c, params);
}
static A findConcept(Concepts concepts, Class c, Object... params) {
return findConceptWhere(concepts, c, params);
}
static A firstThat(Iterable l, IF1 pred) {
for (A a : unnullForIteration(l))
if (pred.get(a))
return a;
return null;
}
static A firstThat(A[] l, IF1 pred) {
for (A a : unnullForIteration(l))
if (pred.get(a))
return a;
return null;
}
static A firstThat(IF1 pred, Iterable l) {
return firstThat(l, pred);
}
static A firstThat(IF1 pred, A[] l) {
return firstThat(l, pred);
}
static boolean endsWith(String a, String b) {
return a != null && a.endsWith(b);
}
static boolean endsWith(String a, char c) {
return nempty(a) && lastChar(a) == c;
}
static boolean endsWith(String a, String b, Matches m) {
if (!endsWith(a, b)) return false;
m.m = new String[] {dropLast(l(b), a)};
return true;
}
static String className(Object o) {
return getClassName(o);
}
static JComponent dm_getVisualization(Object module) {
return (JComponent) callOpt(dm_getStem(module), "vis");
}
static JComponent dm_getVisualization() {
return dm_getVisualization(dm_current_mandatory_generic());
}
static Pt centerOfRect(Rect r) {
return r == null ? null : new Pt(r.x+r.w/2, r.y+r.h/2);
}
static double rectPointDistance(Rect r, Pt p) {
return rectDistance(r, rect(p.x, p.y, 0, 0));
}
static List filterByExactType(Class c, Iterable l) {
List l2 = new ArrayList();
for (Object o : unnull(l))
if (isExactType(o, c))
l2.add((A) o);
return l2;
}
static List objectsWhere(Collection c, Object... data) {
return filterByFields(c, data);
}
static A or(A a, A b) {
return a != null ? a : b;
}
static boolean isConceptRegistered(Concepts concepts, Concept c) {
return c != null && c._concepts == concepts;
}
static Concept getConcept(long id) {
return db_mainConcepts().getConcept(id);
}
static Concept getConcept(Concepts concepts, long id) {
return concepts.getConcept(id);
}
static A getConcept(Class cc, long id) {
return getConcept(db_mainConcepts(), cc, id);
}
static A getConcept(Concepts concepts, Class cc, long id) {
Concept c = concepts.getConcept(id);
if (c == null) return null;
if (!isInstance(cc, c))
throw fail("Can't convert concept: " + getClassName(c) + " -> " + getClassName(cc) + " (" + id + ")");
return (A) c;
}
static long parseLong(String s) {
if (empty(s)) return 0;
return Long.parseLong(dropSuffix("L", s));
}
static long parseLong(Object s) {
return Long.parseLong((String) s);
}
static String struct(Object o) {
return structure(o);
}
static String struct(Object o, structure_Data data) {
return structure(o, data);
}
static String localDateWithMilliseconds(Date time) {
return localDateWithMilliseconds(time.getTime());
}
static String localDateWithMilliseconds(long time) {
SimpleDateFormat format = simpleDateFormat_local("yyyy/MM/dd HH:mm:ss''SSSS");
return format.format(time);
}
static String localDateWithMilliseconds() {
return localDateWithMilliseconds(now());
}
static File javaxBackupDir_dir; // can be set to work on different base dir
static File javaxBackupDir() {
return javaxBackupDir_dir != null ? javaxBackupDir_dir : new File(userHome(), "JavaX-Backup");
}
static File javaxBackupDir(String sub) {
return newFile(javaxBackupDir(), sub);
}
static String fsI(String id) {
return formatSnippetID(id);
}
static String fsI(long id) {
return formatSnippetID(id);
}
static void deleteConcepts(Collection conceptsOrIDs) {
db_mainConcepts().deleteConcepts(asList(conceptsOrIDs));
}
static List deleteConcepts(Class c, Object... params) { return deleteConcepts(db_mainConcepts(), c, params); }
static List deleteConcepts(Concepts cc, Class c, Object... params) {
List l = asList(findConceptsWhere(cc, c, params));
deleteConcepts(l);
return l;
}
static List deleteConcepts(Class c, IF1 pred) { return deleteConcepts(db_mainConcepts(), c, pred); }
static List deleteConcepts(Concepts cc, Class c, IF1 pred) {
var l = filter(list(cc, c), pred);
deleteConcepts(l);
return l;
}
static List deleteConcepts(Concepts cc) {
return deleteConcepts(cc, Concept.class);
}
static String dropFirstLine(String text) {
if (text == null) return null;
int i = text.indexOf('\n');
return i >= 0 ? text.substring(i+1) : "";
}
static AutoCloseable castForTemp(Object o) {
return (AutoCloseable) o;
}
static Object vmGeneralMap_get(Object key) {
return vm_generalMap_get(key);
}
static void internalFrameTitlePopupMenuItem(JInternalFrame f, final String name, final Object action) {
internalFrameTitlePopupMenu(f, new VF1() { public void get(JPopupMenu menu) { try {
addMenuItem(menu, name, action)
; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "addMenuItem(menu, name, action)"; }});
}
static void dm_openSnippetInEditor(String snippetID) {
// Look for existing editor
for (Object mod : dm_modulesWithSnippetID("#1016118"))
if (sameSnippetID(optCastToString(dm_get(mod, "editing")), snippetID))
{ dm_showModule(mod); return; }
// Make new editor
dm_call(dm_showNewModule("#1016118/SFEditor"), "_loadSnippet", snippetID);
}
static class transpileOnServerWithErrorWindow_DaClass {
String snippetID;
JFastLogView_noWrap transpilationErrorBox;
Runnable onSuccess;
Object onError; // voidfunc(S) or Runnable
void go(boolean medium) {
infoBox("Transpiling " + snippetID);
final Pair p = transpileOnBestServer(snippetID, medium ? "medium" : "quick");
swingLater(new Runnable() { public void run() { try {
if (p.a) {
infoBox("Transpiled OK!");
hideWindow(transpilationErrorBox);
transpilationErrorBox = null;
callFInNewThread(onSuccess);
} else {
infoBox("Transpilation error. " + p.b);
boolean first = transpilationErrorBox == null;
transpilationErrorBox = maximizeFrame(scrollAllTheWayDown(showText_fast_noWrap(transpilationErrorBox, "Transpilation Error", p.b)));
if (first) {
setFrameIcon("#1101268", transpilationErrorBox);
addButtonsToWindow(transpilationErrorBox,
jbuttonWithDisable("Medium transpile", new Runnable() { public void run() { try { go(true) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "go(true)"; }})
);
}
callFInNewThread(onError, p.b);
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (p.a) {\r\n infoBox(\"Transpiled OK!\");\r\n hideWindow(transpilat..."; }});
}
}
static void transpileOnServerWithErrorWindow(String snippetID, boolean medium, final Runnable onSuccess, Object... __) {
nu(transpileOnServerWithErrorWindow_DaClass.class, paramsPlus(__, "snippetID", snippetID, "onSuccess", onSuccess)).go(medium);
}
// menuMaker = voidfunc(JPopupMenu)
static void internalFrameTitlePopupMenu(final JInternalFrame f, final Object... params) {
{ swing(() -> {
Object menuMaker;
if (l(params) == 1 && !(params[0] instanceof JMenu))
menuMaker = params[0];
else
menuMaker = new VF1