!include once #1027578 // Named
asclass DynVoiceMultiCRUD extends DynModule {
srecord Table(S tabName, S name, Class extends Concept> conceptClass, O[] scope) {
Table(S tabName, S name, Class extends Concept> conceptClass) {
this(tabName, name, conceptClass, null);
}
}
transient Concepts cc;
transient L
tables;
transient LPair swingCRUDs; // pair(tab name, crud)
transient L voiceCRUDs;
// current selections for persistence. concept name to concept id
Map selections = syncCIMap();
start {
startDB();
tables = makeTables();
startCRUDs();
}
void startDB { db(); cc = db_mainConcepts(); }
// override me
L makeTables() { ret ll(); }
void startCRUDs {
startSwingCRUDs();
makeVoiceCRUDs();
}
// idempotent
void makeVoiceCRUDs {
voiceCRUDs = map(tables, t -> {
NameBasedVoiceCRUD crud = new(cc, t.conceptClass, t.name, t.scope);
restoreSelection(crud);
crud.onSelect(value -> saveSelection(crud));
ret crud;
});
}
void remakeVoiceCRUDs {
tables = makeTables();
makeVoiceCRUDs();
}
void startSwingCRUDs {
swingCRUDs = map(tables, t -> pair(t.tabName, CRUD(cc, t.conceptClass)));
}
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(mapPairsB(swingCRUDs, crud -> crud.visualize())));
if (nempty(swingCRUDs))
addComponent(firstSwingCRUDButtons(), 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(cc, 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();
}
JPanel firstSwingCRUDButtons() {
if (empty(swingCRUDs)) null;
ret first(swingCRUDs).b.crud.buttons;
}
}