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 javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import javax.swing.Timer;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
public class main {
static class UserProblem extends Concept {
static String _fieldOrder = "name managedBy";
String name;
String managedBy;
}
public static void main(String[] args) throws Exception { swingLater(new Runnable() { public void run() { try { substance();
simpleCRUD(UserProblem.class);
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "substance();\n simpleCRUD(UserProblem.class);"; }});}
static SimpleCRUD simpleCRUD(Class extends Concept> c) {
SimpleCRUD crud = new SimpleCRUD(c);
crud.show();
return crud;
}
static class SimpleCRUD {
Class cc;
JTable table;
SimpleCRUD(Class cc) {
this.cc = cc;}
void show() {
db();
{ swingAndWait(new Runnable() { public void run() { try {
table = showConceptsTable(cc);
addToWindow(table, jRightAlignedLine(
jbutton("Add...", new Runnable() { public void run() { try { newConcept() ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "newConcept()"; }}),
tableDependButton(table, jbutton("Edit", new Runnable() { public void run() { try {
editConcept((A) getConcept(toLong(selectedTableCell(table, 0))))
;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "editConcept((A) getConcept(toLong(selectedTableCell(table, 0))))"; }})),
tableDependButton(table, jbutton("Delete", new Runnable() { public void run() { try {
final long id = toLong(selectedTableCell(table, 0));
withDBLock(new Runnable() { public void run() { try { deleteConcept(id) ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "deleteConcept(id)"; }});
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "final long id = toLong(selectedTableCell(table, 0));\r\n withDBLock(r { deleteConcept(id) });"; }}))));
tablePopupMenuItem(table, "Edit...", new Object() { void get(int row) {
editConcept((A) getConcept(toLong(getTableCell(table, row, 0))))
; }
public String toString() { return "editConcept((A) getConcept(toLong(getTableCell(table, row, 0))))"; }});
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "table = showConceptsTable(cc);\r\n addToWindow(table, jRightAlignedLine(\r\n jbutton(\"Add...\", new Runnable() { public void run() { try { newConcept() ;\n} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return \"newConcept()\"; }}),\r\n tableDependButton(table, jbutton(\"Edit\", new Runnable() { public void run() { try { \r\n editConcept((A) getConcept(toLong(selectedTableCell(table, 0))))\r\n ;\n} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return \"editConcept((A) getConcept(toLong(selectedTableCell(table, 0))))\"; }})),\r\n tableDependButton(table, jbutton(\"Delete\", new Runnable() { public void run() { try { \r\n final long id = toLong(selectedTableCell(table, 0));\r\n withDBLock(new Runnable() { public void run() { try { deleteConcept(id) ;\n} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return \"deleteConcept(id)\"; }});\r\n \n} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return \"final long id = toLong(selectedTableCell(table, 0));\\r\\n withDBLock(r { deleteConcept(id) });\"; }}))));\r\n \r\n tablePopupMenuItem(table, \"Edit...\", new Object { void get(int row) { \r\n editConcept((A) getConcept(toLong(getTableCell(table, row, 0))))\r\n ; }\n public String toString() { return \"editConcept((A) getConcept(toLong(getTableCell(table, row, 0))))\"; }});"; }}); }
}
void newConcept() {
final Map map = makeComponents(null);
Runnable r = new Runnable() { public void run() { try { saveData(cnew(cc), map) ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "saveData(cnew(cc), map)"; }};
showFormTitled("New " + shortClassName(cc), arrayPlus(mapToObjectArray(map), r));
}
void editConcept(final A c) {
if (c == null) return;
final Map map = makeComponents(c);
Runnable r = new Runnable() { public void run() { try { saveData(c, map) ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "saveData(c, map)"; }};
showFormTitled("Edit " + shortClassName(cc) + " #" + c.id, arrayPlus(mapToObjectArray(map), r));
}
Map makeComponents(A c) {
Map map = litorderedmap();
for (String field : conceptFieldsInOrder(cc))
map.put(field, jTextField(structureOrText_crud(getOpt(c, field))));
return map;
}
void saveData(A c, Map components) {
for (String field : keys(components))
cset(c, field, convertToField(getTextTrim((JTextField) components.get(field)), cc, field));
}
}
// A concept should be an object, not just a string.
static int concepts_internStringsLongerThan = 10;
static ThreadLocal concepts_unlisted = new ThreadLocal();
static interface Derefable {
Concept get();
}
static class Concepts {
Map concepts = synchroTreeMap();
HashMap perClassData = new HashMap();
String programID;
long idCounter;
volatile long changes = 1, changesWritten;
volatile java.util.Timer autoSaver;
volatile boolean savingConcepts;
int autoSaveInterval = -1000; // 1 second + wait logic
boolean useGZIP = true;
ReentrantLock lock = new ReentrantLock(true);
ReentrantLock saverLock = new ReentrantLock(true);
long lastSaveTook, lastSaveWas;
float maxAutoSavePercentage = 10;
Concepts() {}
Concepts(String programID) {
this.programID = programID;}
synchronized long internalID() {
do {
++idCounter;
} while (hasConcept(idCounter));
return idCounter;
}
void initProgramID() {
if (programID == null)
programID = getDBProgramID();
}
// Now tries to load from bot first, then go to disk.
Concepts load() {
return load(false);
}
Concepts safeLoad() {
return load(true);
}
Concepts load(boolean allDynamic) {
initProgramID();
if (tryToGrab(allDynamic)) return this;
clearConcepts();
DynamicObject_loading.set(true);
try {
long time = now();
Map _concepts = concepts; // empty map
readLocally2_allDynamic.set(allDynamic);
readLocally2(this, programID, "concepts");
Map __concepts = concepts;
concepts = _concepts;
concepts.putAll(__concepts);
int l = readLocally_stringLength;
int tokrefs = unstructure_tokrefs;
assignConceptsToUs();
done("Loaded " + n(l(concepts), "concepts"), time);
readLocally2(this, programID, "idCounter");
} finally {
DynamicObject_loading.set(null);
}
allChanged();
return this;
}
Concepts loadConcepts() { return load(); }
boolean tryToGrab(boolean allDynamic) {
if (sameSnippetID(programID, getDBProgramID())) return false;
RemoteDB db = new RemoteDB(programID);
try {
if (db.functional()) {
loadGrab(db.fullgrab(), allDynamic);
return true;
}
} finally {
db.close();
}
return false;
}
Concepts load(String grab) {
return loadGrab(grab, false);
}
Concepts loadGrab(String grab, boolean allDynamic) {
clearConcepts();
DynamicObject_loading.set(true);
try {
Map map = (Map)
(allDynamic ? safeUnstructure(grab) : unstructure(grab));
concepts.putAll(map);
assignConceptsToUs();
for (long l : map.keySet())
idCounter = max(idCounter, l);
} finally {
DynamicObject_loading.set(null);
}
allChanged();
return this;
}
void assignConceptsToUs() {
for (Concept c : values(concepts)) {
c._concepts = this;
callOpt_noArgs(c, "_doneLoading2");
}
}
String progID() {
return programID == null ? getDBProgramID() : programID;
}
Concept getConcept(String id) {
return empty(id) ? null : getConcept(parseLong(id));
}
Concept getConcept(long id) {
return (Concept) concepts.get((long) id);
}
Concept getConcept(RC ref) {
return ref == null ? null : getConcept(ref.longID());
}
boolean hasConcept(long id) {
return concepts.containsKey((long) id);
}
void deleteConcept(long id) {
Concept c = getConcept(id);
if (c == null)
print("Concept " + id + " not found");
else
c.delete();
}
void saveConceptsIfDirty() { saveConcepts(); }
void save() { saveConcepts(); }
void saveConcepts() {
initProgramID();
saverLock.lock();
savingConcepts = true;
long start = now(), time;
try {
String s = null;
//synchronized(main.class) {
File f = getProgramFile(programID, useGZIP ? "concepts.structure.gz" : "concepts.structure");
long _changes = changes;
if (_changes == changesWritten) return;
lock.lock();
long fullTime = now();
try {
saveLocally2(this, programID, "idCounter");
if (useGZIP) {
saveGZStructureToFile(f, cloneMap(concepts));
getProgramFile(programID, "concepts.structure").delete();
} else
s = structure(cloneMap(concepts));
} finally {
lock.unlock();
}
changesWritten = _changes; // only update when structure didn't fail
if (!useGZIP) {
time = now()-start;
print("Saving " + toM(l(s)) + "M chars (" /*+ changesWritten + ", "*/ + time + " ms)");
start = now();
saveTextFile(f, javaTokWordWrap(s));
getProgramFile(programID, "concepts.structure.gz").delete();
}
copyFile(f, getProgramFile(programID, "concepts.structure" + (useGZIP ? ".gz" : "") + ".backup" + ymd() + "-" + formatInt(hours(), 2)));
time = now()-start;
print(programID + ": Saved " + toK(f.length()) + " K, " + n(concepts, "concepts") + " (" + time + " ms)");
lastSaveWas = fullTime;
lastSaveTook = now()-fullTime;
} finally {
savingConcepts = false;
saverLock.unlock();
}
}
void _autoSaveConcepts() {
if (autoSaveInterval < 0 && maxAutoSavePercentage != 0) {
long pivotTime = Math.round(lastSaveWas+lastSaveTook*100.0/maxAutoSavePercentage);
if (now() < pivotTime) {
//print("Skipping auto-save (last save took " + lastSaveTook + ")");
return;
}
}
saveConcepts();
}
void clearConcepts() {
concepts.clear();
allChanged();
}
synchronized void allChanged() {
++changes;
}
// auto-save every second if dirty
synchronized void autoSaveConcepts() {
if (autoSaver == null) {
autoSaver = doEvery(abs(autoSaveInterval), new Runnable() { public void run() { try { _autoSaveConcepts() ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "_autoSaveConcepts()"; }});
// print("Installed auto-saver (" + autoSaveInterval + " ms, " + progID() + ")");
}
}
void cleanMeUp() {
if (autoSaver != null) {
autoSaver.cancel();
autoSaver = null;
while (savingConcepts) sleep(10);
saveConceptsIfDirty();
}
}
Map getIDsAndNames() {
Map map = new HashMap();
Map cloned = cloneMap(concepts);
for (long id : keys(cloned))
map.put(id, cloned.get(id).className);
return map;
}
void deleteConcepts(List l) {
for (Object o : l)
if (o instanceof Long)
concepts.remove((Long) o);
else if (o instanceof Concept)
((Concept) o).delete();
else
warn("Can't delete " + getClassName(o));
}
A conceptOfType(Class type) {
return firstOfType(allConcepts(), type);
}
List conceptsOfType(Class type) {
return filterByType(allConcepts(), type);
}
List listConcepts(Class type) {
return conceptsOfType(type);
}
List list(Class type) {
return conceptsOfType(type);
}
List list(String type) {
return conceptsOfType(type);
}
List conceptsOfType(String type) {
return filterByDynamicType(allConcepts(), "main$" + type);
}
boolean hasConceptOfType(Class extends Concept> type) {
return hasType(allConcepts(), type);
}
void persistConcepts() {
loadConcepts();
autoSaveConcepts();
}
// We love synonyms
void conceptPersistence() {
persistConcepts();
}
void persist() { persistConcepts(); }
void persist(int interval) { autoSaveInterval = interval; persist(); }
// Runs r if there is no concept of that type
A ensureHas(Class c, Runnable r) {
A a = conceptOfType(c);
if (a == null) {
r.run();
a = conceptOfType(c);
if (a == null)
throw fail("Concept not made by " + r + ": " + shortClassName(c));
}
return a;
}
// Ensures that every concept of type c1 is ref'd by a concept of
// type c2.
// Type of func: voidfunc(concept)
void ensureHas(Class extends Concept> c1, Class extends Concept> c2, Object func) {
for (Concept a : conceptsOfType(c1)) {
Concept b = findBackRef(a, c2);
if (b == null) {
callF(func, a);
b = findBackRef(a, c2);
if (b == null)
throw fail("Concept not made by " + func + ": " + shortClassName(c2));
}
}
}
// Type of func: voidfunc(concept)
void forEvery(Class extends Concept> type, Object func) {
for (Concept c : conceptsOfType(type))
callF(func, c);
}
int deleteAll(Class extends Concept> type) {
List l = (List) conceptsOfType(type);
for (Concept c : l) c.delete();
return l(l);
}
Collection allConcepts() {
synchronized(concepts) {
return new ArrayList(values(concepts));
}
}
int countConcepts(Class c, Object... params) {
int n = 0;
for (A x : list(c))
if (checkConceptFields(x, params))
++n;
return n;
}
int countConcepts() {
return l(concepts);
}
// inter-process methods
RC xnew(String name, Object... values) {
return new RC(cnew(name, values));
}
void xset(long id, String field, Object value) {
xset(new RC(id), field, value);
}
void xset(RC c, String field, Object value) {
if (value instanceof RC)
value = getConcept((RC) value);
cset(getConcept(c), field, value);
}
Object xget(long id, String field) {
return xget(new RC(id), field);
}
Object xget(RC c, String field) {
return cget(getConcept(c), field);
}
void xdelete(long id) {
xdelete(new RC(id));
}
void xdelete(RC c) {
getConcept(c).delete();
}
void xdelete(List l) {
for (RC c : l)
xdelete(c);
}
List xlist() {
return map("toPassRef", allConcepts());
}
List xlist(String className) {
return map("toPassRef", conceptsOfType(className));
}
String xfullgrab() {
lock.lock();
try {
return structure(cloneMap(concepts));
} finally {
lock.unlock();
}
}
}
static volatile Concepts mainConcepts = new Concepts(); // Where we create new concepts
static class Concept extends DynamicObject {
transient Concepts _concepts; // Where we belong
long id;
//O madeBy;
//double energy;
//bool defunct;
long created;
// used only internally (cnew)
Concept(String className) {
super(className);
_created();
}
Concept() {
if (!isTrue(DynamicObject_loading.get())) {
//className = shortClassName(this); // XXX - necessary?
//print("New concept of type " + className);
_created();
}
}
List[ refs = new ArrayList();
List][ backRefs = new ArrayList();
static boolean loading() {
return isTrue(DynamicObject_loading.get());
}
void _created() {
_concepts = mainConcepts;
id = _concepts.internalID();
created = now();
_concepts.concepts.put((long) id, this);
change();
}
void put(String field, Object value) {
fieldValues.put(field, value);
change();
}
Object get(String field) {
return fieldValues.get(field);
}
class Ref {
A value;
Ref() {
if (!isTrue(DynamicObject_loading.get())) refs.add(this);
}
Ref(A value) {
this.value = value;
refs.add(this);
index();
}
// get owning concept (source)
Concept concept() {
return Concept.this;
}
// get target
A get() {
return value;
}
void set(A a) {
if (a == value) return;
unindex();
value = a;
index();
}
void set(Ref ref) {
set(ref.get());
}
void index() {
if (value != null)
value.backRefs.add(this);
change();
}
void unindex() {
if (value != null)
value.backRefs.remove(this);
}
void change() {
Concept.this.change();
}
}
class RefL extends AbstractList {
List < Ref < A > > l = new ArrayList();
public A set(int i, A o) {
A prev = l.get(i).get();
l.get(i).set(o);
return prev;
}
public void add(int i, A o) {
l.add(i, new Ref(o));
}
public A get(int i) {
return l.get(i).get();
}
public A remove(int i) {
return l.remove(i).get();
}
public int size() {
return l.size();
}
public boolean contains(Object o) {
if (o instanceof Concept)
for (Ref r : l) if (eq(r.get(), o)) return true;
return super.contains(o);
}
}
void delete() {
//name = "[defunct " + name + "]";
//defunct = true;
//energy = 0;
for (Ref r : refs)
r.unindex();
refs.clear();
for (Ref r : cloneList(backRefs))
r.set((Concept) null);
backRefs.clear(); // Should be clear at this point anyway
if (_concepts != null) {
_concepts.concepts.remove((long) id);
change();
_concepts = null;
}
id = 0;
}
BaseXRef export() {
return new BaseXRef(_concepts.progID(), id);
}
// notice system of a change in this object
void change() {
if (_concepts != null) _concepts.allChanged();
}
String _programID() {
return _concepts == null ? getDBProgramID() : _concepts.progID();
}
} // class Concept
// remote reference (for inter-process communication or
// external databases). Formerly "PassRef".
// prepared for string ids if we do them later
static class RC {
Object owner;
String id;
RC() {} // make serialisation happy
RC(long id) { this.id = str(id); }
RC(Concept c) { this(c.id); }
long longID() { return parseLong(id); }
public String toString() {
return id;
}
}
// Reference to a concept in another program
static class BaseXRef {
String programID;
long id;
BaseXRef() {}
BaseXRef(String programID, long id) {
this.id = id;
this.programID = programID;}
public boolean equals(Object o) {
if (!(o instanceof BaseXRef)) return false;
BaseXRef r = (BaseXRef) ( o);
return eq(programID, r.programID) && eq(id, r.id);
}
public int hashCode() {
return programID.hashCode() + (int) id;
}
}
// BaseXRef as a concept
static class XRef extends Concept {
BaseXRef ref;
XRef() {}
XRef(BaseXRef ref) {
this.ref = ref; _doneLoading2(); }
// after we have been added to concepts
void _doneLoading2() {
getIndex().put(ref, this);
}
HashMap getIndex() {
return getXRefIndex(_concepts);
}
}
static synchronized HashMap getXRefIndex(Concepts concepts) {
HashMap cache = (HashMap) concepts.perClassData.get(XRef.class);
if (cache == null)
concepts.perClassData.put(XRef.class, cache = new HashMap());
return cache;
}
// uses mainConcepts
static XRef lookupOrCreateXRef(BaseXRef ref) {
XRef xref = getXRefIndex(mainConcepts).get(ref);
if (xref == null)
xref = new XRef(ref);
return xref;
}
// define standard concept functions to use main concepts
static List list(Class type) {
return mainConcepts.list(type);
}
static List list(Concepts concepts, Class type) {
return concepts.list(type);
}
static List list(String type) {
return mainConcepts.list(type);
}
static List list(Concepts concepts, String type) {
return concepts.list(type);
}
static Concept cnew(String name, Object... values) {
Class extends Concept> cc = findClass(name);
Concept c = cc != null ? nuObject(cc) : new Concept(name);
csetAll(c, values);
return c;
}
static A cnew(Class cc, Object... values) {
A c = nuObject(cc);
csetAll(c, values);
return c;
}
static Object cget(Concept c, String field) {
Object o = getOpt(c, field);
if (o instanceof Concept.Ref) return ((Concept.Ref) o).get();
return o;
}
static void csetAll(Concept c, Object... values) {
cset(c, values);
}
static void cset(Concept c, Object... values) { try {
values = expandParams(c.getClass(), values);
warnIfOddCount(values);
for (int i = 0; i+1 < l(values); i += 2) {
String field = (String) values[i];
Object value = values[i+1];
Field f = setOpt_findField(c.getClass(), field);
//print("cset: " + c.id + " " + field + " " + struct(value) + " " + f);
if (value instanceof RC) value = c._concepts.getConcept((RC) value);
value = deref(value);
if (value instanceof String && l((String) value) >= concepts_internStringsLongerThan) value = ((String) value).intern();
if (f == null) {
// TODO: keep ref if it exists
c.fieldValues.put(field, value instanceof Concept ? c.new Ref((Concept) value) : value);
c.change();
} else if (isSubtypeOf(f.getType(), Concept.Ref.class)) {
((Concept.Ref) f.get(c)).set((Concept) derefRef(value));
c.change();
} else {
Object old = f.get(c);
if (neq(value, old)) {
f.set(c, value);
c.change();
}
}
}
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static int countConcepts(Class c, Object... params) {
return mainConcepts.countConcepts(c, params);
}
static int countConcepts() {
return mainConcepts.countConcepts();
}
static A findBackRef(Concept c, Class type) {
for (Concept.Ref r : c.backRefs)
if (instanceOf(r.concept(), type))
return (A) r.concept();
return null;
}
static List findBackRefs(Collection extends Concept> concepts, Class type) {
IdentityHashMap l = new IdentityHashMap();
for (Concept c : concepts)
for (Concept.Ref r : c.backRefs)
if (instanceOf(r.concept(), type))
l.put((A) r.concept(), true);
return asList(keys(l));
}
// TODO: sort by ID?
static List findBackRefs(Concept c, Class type) {
IdentityHashMap l = new IdentityHashMap();
for (Concept.Ref r : c.backRefs)
if (instanceOf(r.concept(), type))
l.put((A) r.concept(), true);
return asList(keys(l));
}
static List findBackRefs(Class type, Concept c) {
return findBackRefs(c, type);
}
static void cleanMeUp() {
mainConcepts.cleanMeUp();
}
static Concept getConcept(long id) {
return mainConcepts.getConcept(id);
}
static void loadAndAutoSaveConcepts() {
mainConcepts.persist();
}
static void loadAndAutoSaveConcepts(int interval) {
mainConcepts.persist(interval);
}
static void loadConceptsFrom(String progID) {
mainConcepts.programID = progID;
mainConcepts.load();
}
static List conceptsOfType(String type) {
return mainConcepts.conceptsOfType(type);
}
static Collection allConcepts() {
return mainConcepts.allConcepts();
}
static long changeCount() {
return mainConcepts.changes;
}
// make concept instance that is not connected to DB
static A unlisted(Class c, Object... args) {
concepts_unlisted.set(true);
try {
return nuObject(c, args);
} finally {
concepts_unlisted.set(null);
}
}
static List exposedDBMethods = ll("xlist", "xnew", "xset", "xdelete", "xget", "xclass", "xfullgrab");
static RC toPassRef(Concept c) {
return new RC(c);
}
// so we can instantiate the program to run as a bare DB bot
static LinkedHashMap litorderedmap(Object... x) {
LinkedHashMap map = new LinkedHashMap();
litmap_impl(map, x);
return map;
}
static Object getTableCell(JTable tbl, int row, int col) {
if (row >= 0 && row < tbl.getModel().getRowCount())
return tbl.getModel().getValueAt(row, col);
return null;
}
static String structureOrText_crud(Object o) {
if (o == null) return "";
if (o instanceof Long) return str(o); // avoid the "L"
return structureOrText(o);
}
static Set keys(Map map) {
return map.keySet();
}
static Set keys(Object map) {
return keys((Map) map);
}
static void deleteConcept(long id) {
mainConcepts.deleteConcept(id);
}
static Object[] mapToObjectArray(Map map) {
List l = new ArrayList();
for (Object o : keys(map)) {
l.add(o);
l.add(map.get(o));
}
return toObjectArray(l);
}
static Object selectedTableCell(JTable t, int col) {
return getTableCell(t, t.getSelectedRow(), col);
}
static void swingLater(long delay, final Object r) {
javax.swing.Timer timer = new javax.swing.Timer(toInt(delay), actionListener(r));
timer.setRepeats(false);
timer.start();
}
static void swingLater(Object r) {
SwingUtilities.invokeLater(toRunnable(r));
}
//please include function withMargin.
static int showForm_defaultGap = 4;
static JComponent showFormTitled(String title, Object... _parts) {
List] l = new ArrayList();
final Var frame = new Var();
List parts = asList(_parts);
Runnable submit = null;
for (int i = 0; i < l(parts); i++) {
final Object o = parts.get(i), next = get(parts, i+1);
if (o instanceof Runnable)
l.add(ll(null, jbutton("Submit", submit = new Runnable() { public void run() { try {
Object result = call(o);
if (neq(Boolean.FALSE, result))
disposeFrame(frame.get());
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "Object result = call(o);\r\n if (neq(Boolean.FALSE, result))\r\n disposeFrame(frame.get());"; }})));
else if (o instanceof Component || o instanceof String || next instanceof Component) { // smartAdd accepts strings
l.add(ll(o == null ? new JPanel() : o, next));
if (next instanceof JButton && submit == null)
submit = new Runnable() { public void run() { try { ((JButton) next).doClick() ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "((JButton) next).doClick()"; }};
i++;
}
else print("showForm: Unknown element type: " + getClassName(o));
}
if (submit != null) {
List textFields = collectInstances(flattenList(l), JTextField.class);
for (JTextField textField : textFields) onEnter(textField, submit);
}
JPanel panel = hvgrid((List) l, showForm_defaultGap);
frame.set(minFrameWidth(showPackedFrame(title, withMargin(panel)), 400));
return panel;
}
static void withDBLock(Runnable r) {
mainConcepts.lock.lock();
try {
r.run();
} finally {
mainConcepts.lock.unlock();
}
}
static String shortClassName(Object o) {
if (o == null) return null;
Class c = o instanceof Class ? (Class) o : o.getClass();
String name = c.getName();
return shortenClassName(name);
}
// one array plus more elements
static Object[] arrayPlus(Object[] a1, Object... a2) {
return concatArrays(a1, a2);
}
static Object convertToField(Object o, Class c, String field) {
Field f = setOpt_findField(c, field);
if (f == null) return o;
Class t = f.getType();
if (t == Long.class || t == long.class)
return toLong(o);
else if (t == Integer.class || t == int.class)
return toInt(o);
else if (t == String.class)
return o instanceof String ? (String) o : str(o);
return o;
}
static JTextField jTextField() {
return new JTextField();
}
static JTextField jTextField(String text) {
JTextField tf = new JTextField(unnull(text));
tf.selectAll();
return tf;
}
static JTextField jTextField(Object o) {
return jTextField(strOrEmpty(o));
}
static JPanel jRightAlignedLine(Component... components) {
return jrightAlignedLine(components);
}
static JPanel jRightAlignedLine(List components) {
return jrightAlignedLine(components);
}
static long toLong(Object o) {
if (o instanceof Number)
return ((Number) o).longValue();
if (o instanceof String)
return parseLong((String) o);
return 0;
}
static void substance() {
substanceLAF();
}
static void substance(String skinName) {
substanceLAF(skinName);
}
static JButton jbutton(String text, Object action) {
return newButton(text, action);
}
// button without action
static JButton jbutton(String text) {
return newButton(text, null);
}
// optional parameter
static ThreadLocal> showConceptsTable_dropFields = new ThreadLocal();
static JTable showConceptsTable(final Class extends Concept> c) {
List dropFields = getAndClearThreadLocal(showConceptsTable_dropFields);
final List fields = listMinusList(concatLists(ll("id"), conceptFieldsInOrder(c)), dropFields);
final JTable table = sexyTable();
tablePopupMenu(table, new Object() { void get(JPopupMenu menu, final int row) {
addMenuItem(menu, "Delete", new Runnable() { public void run() { try {
final long id = toLong(getTableCell(table, row, 0));
withDBLock(new Runnable() { public void run() { try { deleteConcept(id) ;
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "deleteConcept(id)"; }});
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "final long id = toLong(getTableCell(table, row, 0));\r\n withDBLock(r { deleteConcept(id) });"; }});
}
public String toString() { return "addMenuItem(menu, \"Delete\", r {\r\n final long id = toLong(getTableCell(table, row, 0));\r\n withDBLock(r { deleteConcept(id) });\r\n });"; }});
awtOnConceptChanges(table, new Runnable() { public void run() { try {
//print("Updating concept table");
List data = new ArrayList();
for (final Concept cc : list(c))
data.add(map(new Object() { Object get(String field) { return renderForTable(cget(cc, field)) ; }
public String toString() { return "renderForTable(cget(cc, field))"; }}, fields));
fillTableWithData(table, data, fields);
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } public String toString() { return "//print(\"Updating concept table\");\r\n new List data;\r\n for (final Concept cc : list(c))\r\n data.add(map(new Object { Object get(String field) { return renderForTable(cget(cc, field)) ; }\n public String toString() { return \"renderForTable(cget(cc, field))\"; }}, fields));\r\n fillTableWithData(table, data, fields);"; }}, true);
showFrame(plural(shortClassName(c)), table);
return table;
}
// f takes a concept and makes a map for display
static JTable showConceptsTable(final Class cClass, final Object f) {
final JTable table = showTable();
awtOnConceptChanges(table, 1000, new Runnable() { public void run() { try {
List