!include once #1027578 // Named
asclass DynVoiceMultiCRUD extends DynModule {
srecord Table(S tabName, S name, Class extends Concept> conceptClass) {}
transient L
tables;
transient L swingCRUDs;
transient L voiceCRUDs;
// current selections for persistence. concept name to concept id
Map selections = syncCIMap();
start {
db();
tables = makeTables();
startCRUDs();
}
// override me
L makeTables() { ret ll(); }
void startCRUDs {
swingCRUDs = map(tables, t -> CRUD(t.conceptClass));
voiceCRUDs = map(tables, t -> {
NameBasedVoiceCRUD crud = new(t.conceptClass, t.name);
restoreSelection(crud);
crud.onSelect(value -> saveSelection(crud));
ret crud;
});
}
int idx(Class extends Concept> c) { ret indexOfPred(tables, t -> t.conceptClass == c); }
!include #1027563 // Undo Manager
S answer(S s) {
ret dm_q(() -> {
print("answering: " + s);
try answer undoActions(s);
try answer moreActions(s);
for (NameBasedVoiceCRUD crud : nonNulls(voiceCRUDs)) {
crud.addUndo = (IVF1) lambda1 addUndo;
try answer crud.answer(s);
}
null;
});
}
// override me
S moreActions(S s) { null; }
visual {
JComponent c = jtabs(pairsToParams(mapPairsToList(zipTwoListsToPairs(tables, swingCRUDs), (t, crud) ->
pair(t.tabName, crud.visualize()))));
if (nempty(swingCRUDs))
addComponent(first(swingCRUDs).crud.buttons, jbutton("Talk to raw backend", rThread talkToMe));
ret c;
}
void talkToMe enter { dm_showConversationPopupForModule(); }
// e.g. for concept ScenarioCondition > Named { Scenario scenario; }
// c2 is the dependent concept (ScenarioCondition)
void addDependency(Class extends Concept> c2, S field) {
Class extends Concept> c1 = fieldType(c2, field);
int i1 = idx(c1), i2 = idx(c2);
NameBasedVoiceCRUD originalCRUD2 = voiceCRUDs.get(i2);
S conceptName = originalCRUD2.concept;
NameBasedVoiceCRUD crud = voiceCRUDs.get(i1);
//printVars("addDependency", +c1, +c2, +conceptName, +field, +i1, +i2);
IVF1 r = value -> {
printVars("addDependency", +i2, +field, +value);
voiceCRUDs.set(i2, value == null ? null : NameBasedVoiceCRUD(c2, conceptName, field, value));
};
crud.onSelect(r);
r.get(crud.selected);
}
void restoreSelection(NameBasedVoiceCRUD crud) {
Long id = selections.get(crud.concept);
Concept c = getConcept_allowNull(id);
//printStackTrace("restoreSelection " + crud.concept + " " + id + " " + c);
crud.select(c);
}
void saveSelection(NameBasedVoiceCRUD crud) {
Long id = conceptIDOrNull(crud.selected);
//printStackTrace("saveSelection: " + crud.concept + " " + id);
if (mapPutOrRemove_trueIfChanged(selections, crud.concept, id)) change();
}
}