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.Transferable;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import java.awt.datatransfer.StringSelection;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.text.*;
class main {
static class Thing extends Concept {
static String _fieldOrder = "thing category";
String thing, category;
}
static boolean _moduleClass_Things;
static class Things extends DynCRUD < Thing > {
Things() { super(Thing.class); }
}
/*meta-postProcess {
todo
}*/
abstract static class DynCRUD extends DynModule {
transient Class conceptClass;
transient SimpleCRUD crud;
DynCRUD() {}
DynCRUD(Class conceptClass) {
this.conceptClass = conceptClass;}
void start() { super.start();
db(); // So we can do stuff in overridden start methods
crud = makeCRUD(); // so we can customize early
}
JComponent visualize() {
makeConceptsTable_idWidth = 0;
showConceptsTable_afterUpdate.set(new VF1() { public void get(JTable t) { try {
int n = tableColumnCount(t);
for (int i = 0; i < n; i++)
setColumnName(t, i, i == 0 ? "" : humanizeFormLabel(getColumnName(t, i)));
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "int n = tableColumnCount(t);\r\n for i to n:\r\n setColumnName(t, i, ..."; }});
return withMargin(crud.make());
}
SimpleCRUD makeCRUD() {
return new SimpleCRUD(db_mainConcepts(), conceptClass);
}
JTable table() { return crud == null ? null : crud.table; }
A selected() { return crud == null ? null : crud.selectedConcept(); }
void addButton(JComponent button) {
if (crud != null) addComponent(crud.buttons, button);
}
void addButton(String name, Object action) { addButton(jbutton(name, action)); }
void makeSortable() { // broken?
addRowSorter(table());
rowSorter_setComparatorForAllColumns(table(), alphaNumComparator());
}
String searchTerm() {
return crud == null || crud.tableSearcher == null ? null : gtt(crud.tableSearcher.tfInput);
}
void addCountToName() {
onConceptChangeAndNow(new Runnable() { public void run() { try { setModuleName(programTitle() + " (" + conceptCount() + ")") ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "setModuleName(programTitle() + \" (\" + conceptCount() + \")\")"; }});
}
Concepts conceptsObject() {
return crud.concepts;
}
// overwrite for compact modules to work
List list(Class c) {
return conceptsObject().list(c);
}
// API
List concepts() { AutoCloseable __2 = enter(); try {
return list(conceptClass);
} finally { _close(__2); }}
int conceptCount() { AutoCloseable __3 = enter(); try { return countConcepts(conceptClass); } finally { _close(__3); }}
void deleteAll() { AutoCloseable __4 = enter(); try {
deleteConcepts(conceptClass);
} finally { _close(__4); }}
void addDialog() { AutoCloseable __5 = enter(); try { crud.newConcept(); } finally { _close(__5); }}
}
static abstract class VF1 {
abstract void get(A a);
}static boolean SimpleCRUD_searcher = true;
static class SimpleCRUD {
Class cc;
JTable table;
JPanel buttons, panel;
Object renderer; // optional, func(A) -> Map
String hID = "ID";
Set excludeFieldsFromEditing;
String modifiedField; // field to hold last-modified timestamp
TableSearcher tableSearcher;
Set multiLineFields; // string fields that should be shown as text areas
Concepts concepts;
SimpleCRUD(Class cc) { this(db_mainConcepts(), cc); }
SimpleCRUD(Concepts concepts, Class cc) {
this.cc = cc;
this.concepts = concepts;}
SimpleCRUD show(String frameTitle) {
make();
showFrame(frameTitle, panel);
return this;
}
SimpleCRUD show() {
return show(plural(shortClassName(cc)));
}
SimpleCRUD showMaximized() { show(); maximizeFrame(panel); return this; }
JPanel makePanel() { return make(); }
JPanel make() {
db();
framesBot();
// next line not in swing part to allow passing arguments
// from outside like showConceptsTable_afterUpdate
table = makeConceptsTable(cc, wrapRenderer(renderer));
{ 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(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(r { for (A c : l)..."; }})));
if (SimpleCRUD_searcher) {
tableSearcher = tableWithSearcher2(table, "withMargin" , true);
panel = centerAndSouthWithMargin(tableSearcher.panel, buttons);
} else
panel = centerAndSouthWithMargin(table, buttons);
Object fEdit = new VF1() { public void get(Integer row) { try {
editConcept((A) getConcept(toLong(getTableCell(table, row, 0))))
; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "editConcept((A) getConcept(toLong(getTableCell(table, row, 0))))"; }};
tablePopupMenuItem(table, "Edit...", fEdit);
onDoubleClick(table, fEdit);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "buttons = jRightAlignedLine(\r\n jbutton(\"Add...\", r { newConcept() }),\r..."; }}); }
return panel;
}
Object wrapRenderer(final Object renderer) {
return renderer == null ? null : new F1() { Object get(A a) { try {
return putAll(litorderedmap(hID, str(a.id)), (Map) callF(renderer, a));
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "putAll(litorderedmap(hID, str(a.id)), (Map) callF(renderer, a))"; }};
}
void newConcept() {
final A c = unlisted(cc);
final Map map = makeComponents(c);
Runnable r = new Runnable() { public void run() { try {
register(c);
saveData(c, map);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "register(c);\r\n saveData(c, map);"; }};
AutoCloseable __9 = tempSetMCOpt("formLayouter1_fixer2" , 12); try {
showFormTitled2("New " + shortClassName(cc), arrayPlus(mapToObjectArray(map), r));
} finally { _close(__9); }}
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 __10 = tempSetMCOpt("formLayouter1_fixer2" , 12); try {
showFormTitled2("Edit " + shortClassName(cc) + " #" + c.id, arrayPlus(mapToObjectArray(map), r));
} finally { _close(__10); }}
A selectedConcept() {
return (A) getConcept(toLong(selectedTableCell(table, 0)));
}
List selectedConcepts() {
int[] rows = table.getSelectedRows();
List l = new ArrayList();
for (int row : rows)
l.add((A) getConcept(toLong(getTableCell(table, row, 0))));
return l;
}
Map makeComponents(A c) {
Map map = litorderedmap();
makeComponents(c, map);
return map;
}
JComponent fieldComponent(A c, String field) {
Class type = getFieldType(cc, field);
Object value = getOpt(c, field);
//print("Field type: " + field + " => " + type);
if (type == boolean.class)
return jCenteredCheckBox(isTrue(value));
else if (contains(multiLineFields, field))
return typeWriterTextArea((String) value);
else try {
return autoComboBox(structureOrText_crud(value), new TreeSet(map("structureOrText_crud",collect(list(cc), field))));
} catch (Throwable e) {
printException(e);
return jTextField(structureOrText_crud(value));
}
}
void saveComponent(A c, String field, JComponent comp) {
comp = unwrap(comp);
if (comp instanceof JTextComponent)
cset(c, field, convertToField(trimIf(!(comp instanceof JTextArea), getText((JTextComponent) comp)), cc, field));
else if (comp instanceof JComboBox)
cset(c, field, convertToField(getTextTrim((JComboBox) comp), cc, 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(cc) , new F1() { Boolean get(String field) { try { return fieldType(cc, field) != Concept.Ref.class; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "fieldType(cc, field) != Concept.Ref.class"; }}), excludeFieldsFromEditing);
}
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 abstract class DynModule {
String name, toolTip;
PersistableThrowable _error;
Map> mechLists;
Map _persistenceInfo;
transient Object _host;
transient Map timers = newWeakHashMap(); // all kinds of resources actually; value is closer helper
transient Set _resources = synchroHashSet();
transient Lock lock; // set by stem
transient boolean persistOnChangedField = true;
transient int changeCount;
transient Object changeCountSync = new Object();
transient List onChange;
transient boolean verboseTimers;
transient ReliableSingleThread rstUpdate;
transient Set componentFieldsToKeep;
transient Map transientGeneralMap = synchroHashMap();
transient Q q; // module command queue
DynModule() {
dm_initErrorHandling();
setMainDesktopPane((JDesktopPane) getCreatorOpt("desktop"));
}
boolean isVisible() { return isTrue(getOpt(_host, "visible")); }
String moduleName() { return name; }
void setModuleName(String name) {
String oldName = this.name;
if (!eq(name, oldName)) {
this.name = name;
possiblyInternalFrameTitle(vis(), name);
vmBus_send("moduleNameChange", this, oldName, name);
}
}
void setModuleToolTip(String toolTip) {
this.toolTip = toolTip;
}
JComponent vis() {
return (JComponent) getOpt(_host, "vis");
}
A ownResource(A a) {
if (a != null)
_resources.add(a);
return a;
}
A ownTimer(A timer) {
if (timer instanceof AutoCloseable) ownResource((AutoCloseable) timer);
ownTimer(timer, "cancelTimerOrInterruptThread");
return timer;
}
void ownTimer(Object timer, Object closerHelper) {
timers.put(timer, closerHelper);
}
void singleTimer(java.util.Timer timer) {
stopAllTimers();
ownTimer(timer);
}
void stopAllTimers() {
for (AutoCloseable resource : getAndClearList(_resources)) {
if (verboseTimers)
print("Releasing resource: " + resource);
try { resource.close(); } catch (Throwable __e) { _handleException(__e); }
}
{ final Map