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 javax.swing.undo.UndoManager;
import java.awt.datatransfer.StringSelection;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.awt.datatransfer.*;
import java.text.SimpleDateFormat;
import java.nio.charset.Charset;
import java.awt.geom.*;
import java.text.NumberFormat;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.nio.file.Path;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import java.text.*;
import java.util.TimeZone;
class main {
static boolean SimpleCRUD_searcher = true;
static class SimpleCRUD_v2 extends JConceptsTable {
JPanel buttons, panel;
String hID = "ID"; // Column header for concept ID
Set unshownFields; // not shown in table or form
Set excludeFieldsFromEditing;
String modifiedField; // field to hold last-modified timestamp
TableSearcher tableSearcher;
Set multiLineFields; // string fields that should be shown as text areas
Set dontDuplicateFields;
int formFixer = 12; // stupid value to make submit button appear
boolean showBackRefs = false;
int maxRefsToShow = 3;
SimpleCRUD_v2(Class conceptClass) { super(conceptClass); }
SimpleCRUD_v2(Concepts concepts, Class conceptClass) { super(concepts, conceptClass); }
SimpleCRUD_v2 show(String frameTitle) {
make();
showFrame(frameTitle, panel);
return this;
}
SimpleCRUD_v2 show() {
return show(plural(shortClassName(conceptClass)));
}
SimpleCRUD_v2 showMaximized() { show(); maximizeFrame(panel); return this; }
JPanel makePanel() { return make(); }
JPanel make() {
db();
framesBot();
return make_dontStartBots();
}
transient IF1> itemToMap_inner;
Map itemToMap_inner(A a) { return itemToMap_inner != null ? itemToMap_inner.get(a) : itemToMap_inner_base(a); }
final Map itemToMap_inner_fallback(IF1> _f, A a) { return _f != null ? _f.get(a) : itemToMap_inner_base(a); }
Map itemToMap_inner_base(A a) {
return super.itemToMap_base(a);
}
Map itemToMap_base(A a) {
Map map = itemToMap_inner(a);
if (map == null) return null;
return putAll(putAll(specialFieldsForItem(a), map), moreSpecialFieldsForItem(a));
}
// shown on the left (usually)
transient IF1> specialFieldsForItem;
Map specialFieldsForItem(A a) { return specialFieldsForItem != null ? specialFieldsForItem.get(a) : specialFieldsForItem_base(a); }
final Map specialFieldsForItem_fallback(IF1> _f, A a) { return _f != null ? _f.get(a) : specialFieldsForItem_base(a); }
Map specialFieldsForItem_base(A a) {
Map map = litorderedmap(hID, str(a.id));
mapPut(map, "Java Class", javaClassDescForItem(a));
return map;
}
// shown on the right (usually)
transient IF1> moreSpecialFieldsForItem;
Map moreSpecialFieldsForItem(A a) { return moreSpecialFieldsForItem != null ? moreSpecialFieldsForItem.get(a) : moreSpecialFieldsForItem_base(a); }
final Map moreSpecialFieldsForItem_fallback(IF1> _f, A a) { return _f != null ? _f.get(a) : moreSpecialFieldsForItem_base(a); }
Map moreSpecialFieldsForItem_base(A a) {
Map map = litorderedmap();
if (showBackRefs) {
Collection refs = allBackRefs(a);
if (nempty(refs)) {
refs = sortedByConceptID(refs);
int more = l(refs)-maxRefsToShow;
map.put("Referenced by",
joinWithComma(takeFirst(maxRefsToShow, refs))
+ (more > 0 ? ", " + more + " more" : ""));
}
}
return map;
}
String javaClassDescForItem(A a) {
String className = dynShortClassName(a);
if (neq(className, shortClassName(conceptClass))) {
String text = className;
String realClass = shortClassName(a);
if (neq(className, realClass))
text += " as " + realClass;
return text;
}
return null;
}
JPanel make_dontStartBots() {
dropFields = asList(unshownFields);
makeTable();
{ swing(new Runnable() { public void run() { try {
buttons = jRightAlignedLine(
jbutton("Add...", new Runnable() { public void run() { try { newConcept() ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "newConcept()"; }}),
tableDependButton(table, jbutton("Edit", new Runnable() { public void run() { try {
editConcept(selectedConcept())
;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "editConcept(selectedConcept())"; }})),
tableDependButton(table, jbutton("Delete", new Runnable() { public void run() { try {
final List l = selectedConcepts();
withDBLock(concepts, new Runnable() { public void run() { try { for (A c : l) c.delete() ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "for (A c : l) c.delete()"; }});
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final List l = selectedConcepts();\r\n withDBLock(concepts, r { for..."; }})),
tableDependButton(table, jbutton("Duplicate...", new Runnable() { public void run() { try {
duplicateConcept(selectedConcept())
;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "duplicateConcept(selectedConcept())"; }})));
if (SimpleCRUD_searcher) {
tableSearcher = tableWithSearcher2(table, "withMargin" , true);
panel = centerAndSouthWithMargin(tableSearcher.panel, withBottomMargin(buttons));
} else
panel = centerAndSouthWithMargin(table, withBottomMargin(buttons));
Object fEdit = new VF1() { public void get(Integer row) { try {
editConcept(getItem(row))
; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "editConcept(getItem(row))"; }};
tablePopupMenuItem(table, "Edit...", fEdit);
onDoubleClick(table, fEdit);
tablePopupMenuFirst(table, (menu, row) -> {
Concept c = getItem(row);
if (c != null)
addMenuItem(menu, "Delete " + quote(shorten(str(c))), runnableThread(new Runnable() { public void run() { try {
deleteConcept(c);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "deleteConcept(c);"; }}));
});
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "buttons = jRightAlignedLine(\r\n jbutton(\"Add...\", r { newConcept() }),\r..."; }}); } // end of swing
return panel;
}
void newConcept() {
duplicateConcept(null);
}
void duplicateConcept(A oldConcept) {
final A c = unlisted(conceptClass);
ccopyFieldsExcept(oldConcept, c, dontDuplicateFields);
final Map map = makeComponents(c);
Runnable r = new Runnable() { public void run() { try {
concepts.register(c);
saveData(c, map);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "concepts.register(c);\r\n saveData(c, map);"; }};
AutoCloseable __1 = tempSetMCOpt("formLayouter1_fixer2" , formFixer); try {
showFormTitled2("New " + shortClassName(conceptClass), arrayPlus(mapToObjectArray(map), r));
} finally { _close(__1); }}
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 rethrow(__e); } } public String toString() { return "saveData(c, map)"; }};
AutoCloseable __2 = tempSetMCOpt("formLayouter1_fixer2" , formFixer); try {
showFormTitled2("Edit " + shortClassName(conceptClass) + " #" + c.id, arrayPlus(mapToObjectArray(map), r));
} finally { _close(__2); }}
A selectedConcept() {
return (A) concepts.getConcept(toLong(selectedTableCell(table, 0)));
}
A selected() { return selectedConcept(); }
A getItem(int row) {
return (A) concepts.getConcept(toLong(getTableCell(table, row, 0)));
}
int indexOfConcept(final A c) {
if (c == null) return -1;
return swing(new F0() { public Integer get() { try {
int n = tableRowCount(table);
for (int row = 0; row < n; row++)
if (toLong(getTableCell(table, row, 0)) == c.id)
return row;
return -1;
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "int n = tableRowCount(table);\r\n for row to n:\r\n if (toLong(getTab..."; }});
}
List selectedConcepts() {
int[] rows = table.getSelectedRows();
List l = new ArrayList();
for (int row : rows)
l.add(getItem(row));
return l;
}
Map makeComponents(A c) {
Map map = litorderedmap();
makeComponents(c, map);
return map;
}
JComponent fieldComponent(A c, String field) {
Class type = getFieldType(conceptClass, field);
Object value = getOpt(c, field);
//print("Field type: " + field + " => " + type);
if (type == boolean.class)
return jCenteredCheckBox(isTrue(value));
else if (contains(multiLineFields, field) || containsNewLines(optCast(String.class,value)))
return typeWriterTextArea((String) value);
else if (isSubtype(type, Concept.class))
return jcomboboxFromConcepts_str(concepts, type, (Concept) value);
else try {
return autoComboBox(structureOrText_crud(value), new TreeSet(map("structureOrText_crud",collect(list(concepts, conceptClass), field))));
} catch (Throwable e) {
printException(e);
return jTextField(structureOrText_crud(value));
}
}
void saveComponent(A c, String field, JComponent comp) {
comp = unwrap(comp);
Class type = fieldType(c, field);
if (comp instanceof JTextComponent)
cset(c, field, convertToField(trimIf(!(comp instanceof JTextArea), getText((JTextComponent) comp)), conceptClass, field));
else if (comp instanceof JComboBox) {
String text = getTextTrim(((JComboBox) comp));
if (isSubtype(type, Concept.class))
cset(c, field, getConcept(concepts, parseFirstLong(text)));
else
cset(c, field, convertToField(text, conceptClass, field));
} else if (comp instanceof JCheckBox)
cset(c, field, isChecked((JCheckBox) comp));
}
List fields() {
if (excludeFieldsFromEditing != null && modifiedField != null) excludeFieldsFromEditing.add(modifiedField);
return listWithoutSet(filter(conceptFieldsInOrder(conceptClass) , new F1() { public Boolean get(String field) { try { return fieldType(conceptClass, field) != Concept.Ref.class; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "fieldType(conceptClass, field) != Concept.Ref.class"; }}),
joinSets(excludeFieldsFromEditing, unshownFields));
}
void excludeFieldsFromEditing(String... fields) {
excludeFieldsFromEditing = setPlus(excludeFieldsFromEditing, fields);
}
// override the following two methods to customize edit window
void makeComponents(A c, Map map) {
for (String field : fields())
map.put(field, fieldComponent(c, field));
}
void saveData(A c, Map components) {
for (String field : keys(components))
saveComponent(c, field, components.get(field));
if (modifiedField != null) cset(c, modifiedField, now());
}
}
static String structureOrText_crud(Object o) {
if (o == null) return "";
if (o instanceof Long) return str(o); // avoid the "L"
if (o instanceof File) return ((File) o).getAbsolutePath();
return str(o);
}
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 f) {
if (f != null) { swing(new Runnable() { public void run() { try {
if (frameTooSmall(f)) frameStandardSize(f);
if (!f.isVisible()) f.setVisible(true); // XXX
if (f.getState() == Frame.ICONIFIED) f.setState(Frame.NORMAL);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (frameTooSmall(f)) frameStandardSize(f);\r\n if (!f.isVisible()) f.setVis..."; }}); }
return f;
}
// 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 String plural(String s) {
return getPlural(s);
}
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);
}
static A maximizeFrame(A c) {
JFrame f = swing(new F0() { public JFrame get() { try {
JFrame f = getFrame(c);
if (f != null)
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
return f;
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "JFrame f = getFrame(c);\r\n if (f != null)\r\n f.setExtendedState(JFrame...."; }});
// wait until frame is actually maximized so
// we can add components based on correct size
if (f != null && !isAWTThread()) {
Dimension d = maximumWindowBounds().getSize();
long start = sysNow();
while (licensed()) { try {
if (f.getWidth() >= d.getWidth()-100 && f.getHeight() >= d.getHeight()-100) break;
if (sysNow() >= start+100) { warn("maximizeFrame timeout"); break; }
} catch (Throwable __e) { _handleException(__e); } sleep(1); }
}
return c;
}
static void db() {
conceptsAndBot();
}
// use -10000 for 10 seconds plus slowdown logic
static void db(Integer autoSaveInterval) {
conceptsAndBot(autoSaveInterval);
}
static volatile boolean framesBot_has = false;
static Android3 framesBot() {
if (framesBot_has) return null;
framesBot_has = true;
Android3 android = new Android3();
android.greeting = programIDPlusHome() + " Frames.";
android.console = false;
android.responder = new Responder() {
String answer(String s, List history) {
if (match("activate frames", s)) {
swingLater(new Runnable() { public void run() { try { // prevent blocking when called from same program's AWT thread
activateMyFrames();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "// prevent blocking when called from same program's AWT thread\r\n act..."; }});
return "OK, activating " + programName();
}
return null;
}
};
return makeBot(android);
}
static Map putAll(Map a, Map extends A,? extends B> b) {
if (a != null && b != null) a.putAll(b);
return a;
}
static LinkedHashMap litorderedmap(Object... x) {
LinkedHashMap map = new LinkedHashMap();
litmap_impl(map, x);
return map;
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static void mapPut(Map map, A key, B value) {
if (map != null && key != null && value != null) map.put(key, value);
}
static void mapPut(Map map, Pair p) {
if (map != null && p != null) map.put(p.a, p.b);
}
static List allBackRefs(Concept c) {
IdentityHashMap l = new IdentityHashMap();
if (c != null && c.backRefs != null) for (Concept.Ref r : cloneList(c.backRefs))
l.put(r.concept(), true);
return keysList(l);
}
static boolean nempty(Collection c) {
return !empty(c);
}
static boolean nempty(CharSequence s) {
return !empty(s);
}
static boolean nempty(Object[] o) { return !empty(o); }
static boolean nempty(byte[] o) { return !empty(o); }
static boolean nempty(int[] o) { return !empty(o); }
static boolean nempty(Map m) {
return !empty(m);
}
static boolean nempty(Iterator i) {
return i != null && i.hasNext();
}
static boolean nempty(Object o) { return !empty(o); }
static List sortedByConceptID(Collection c) {
return sortedByCalculatedField("conceptID",c);
}
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(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] 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(double[] 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(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
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 == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static int l(MultiSet ms) { return ms == null ? 0 : ms.size(); }
static String joinWithComma(Collection c) {
return join(", ", c);
}
static String joinWithComma(String... c) {
return join(", ", c);
}
static String joinWithComma(Pair p) {
return p == null ? "" : joinWithComma(str(p.a), str(p.b));
}
static List takeFirst(List l, int n) {
return l(l) <= n ? l : newSubListOrSame(l, 0, n);
}
static List takeFirst(int n, List l) {
return takeFirst(l, n);
}
static String takeFirst(int n, String s) { return substring(s, 0, n); }
static String takeFirst(String s, int n) { return substring(s, 0, n); }
static CharSequence takeFirst(int n, CharSequence s) { return subCharSequence(s, 0, n); }
static List takeFirst(int n, Iterable i) {
if (i == null) return null;
List l = new ArrayList();
Iterator it = i.iterator();
for (int _repeat_0 = 0; _repeat_0 < n; _repeat_0++) { if (it.hasNext()) l.add(it.next()); else break; }
return l;
}
static int[] takeFirst(int n, int[] a) {
return takeFirstOfIntArray(n, a);
}
static String dynShortClassName(Object o) {
return shortDynamicClassName(o);
}
static boolean neq(Object a, Object b) {
return !eq(a, b);
}
// 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(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(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 Object swing(Object f) {
return swingAndWait(f);
}
static A swing(F0 f) {
return (A) swingAndWait(f);
}
static A swing(IF0 f) {
return (A) swingAndWait(f);
}
static JPanel jRightAlignedLine(Component... components) {
return jrightAlignedLine(components);
}
static JPanel jRightAlignedLine(List extends Component> components) {
return jrightAlignedLine(components);
}
static JButton jbutton(String text, Object action) {
return newButton(text, action);
}
// button without action
static JButton jbutton(String text) {
return newButton(text, null);
}
/*static JButton jbutton(BufferedImage img, O action) {
ret setButtonImage(img, jbutton("", action));
}*/
static JButton jbutton(Action action) {
return swingNu(JButton.class, action);
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static JButton tableDependButton(JTable tbl, JButton b) {
tableDependButtons(tbl, ll(b));
return b;
}
static JButton tableDependButton(JTable tbl, String text, Object action) {
return tableDependButton(tbl, jbutton(text, action));
}
static A selectedConcept(JTable table, Class cc) {
return (A) getConcept(toLong(selectedTableCell(table, 0)));
}
static Object withDBLock(Object r) {
Lock __0 = db_mainConcepts().lock; lock(__0); try {
return callF(r);
} finally { unlock(__0); } }
static A withDBLock(F0 r) {
return (A) withDBLock((Object) r);
}
static Object withDBLock(Concepts concepts, Object r) {
Lock __1 = concepts.lock; lock(__1); try {
return callF(r);
} finally { unlock(__1); } }
static A withDBLock(Concepts concepts, F0 r) {
return (A) withDBLock(concepts, (Object) r);
}
static A withDBLock(Concept concept, IF0 r) {
return (A) withDBLock(concept._concepts, r);
}
static class TableSearcher {
JTable table;
JTextField tfInput;
JComponent searchPanel, panel;
F2 rowTester;
List rowIndices;
String input() { return gtt(tfInput); }
}
static TableSearcher tableWithSearcher2(final JTable t, Object... __) {
final TableSearcher s = new TableSearcher();
final boolean precise = true;
s.table = t;
s.tfInput = jtextfield();
s.rowTester = new F2() { public Boolean get(String pat, Map row) { try { return anyValueContainsIgnoreCase(row, pat); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "anyValueContainsIgnoreCase(row, pat)"; }};
onUpdate(s.tfInput, new Runnable() {
List lastFiltered, lastOriginal;
public void run() {
String pat = s.input();
List