!7 sS background = #1009931; static JDesktopPane desktop; static ReliableSingleThread rst = new(f updateModules); static volatile long updateCycles; p-noconsole { autoRestart(); db(); desktop = jDesktopPaneWithSkyPicture(background, Color.black); autoFixDesktopPane(desktop); cleanExitOnFrameClose(frameIcon(#1101272, showMaximizedFrame(desktop))); titlePopupMenuItem(desktop, "New Session", rThreadPcallMessageBox(r deleteAllModules)); initAfterDBLoad(); } svoid initAfterDBLoad { initialModules(); UpdateCycles uc = conceptWhere(UpdateCycles); if (uc != null) updateCycles = uc.value; for (Module m : onModules()) startModule(m); addConceptIndex(simpleConceptIndex(rst)); rst.trigger(); } svoid initialModules { if (!hasConcept(ModuleClasses)) showModule(uniq(ModuleClasses)); } svoid updateModules { ++updateCycles; for (Module m : onModules()) pcall { m.update(); } } svoid cleanMeUp { for (Module m) if (m.vis != null) pcall { m.unvisualize(); } for (Module m) cleanUp(m); } static L onModules() { ret conceptsWhere(Module, on := true); } sbool hasModuleWithFields(Class c, O... params) { ret hasConcept(c, concatArrays(new O[] {on := true}, params)); } svoid startModule(Module m) { //addIfNotThere(modules, m); lock m.lock; if (m.started) ret; m.started = true; print("Starting module " + m); m.start(); if (m.visible) showModule(m); } svoid showModule(final Module m) { if (m == null) ret; startModule(m); lock m.lock; if (m.vis != null) ret; cset(m, visible := true); visualizeModule(m); if (m.vis != null) swing { Rect r = m.frameRect; if (r == null) r = randomRect(desktop.getWidth(), desktop.getHeight(), 10, 150, 100); if (r == null) r = Rect(10, 10, 200, 100); print("Showing frame at " + r); S frameTitle = humanizeFormLabel(shortClassName(m)); final JInternalFrame f = showInternalFrame(desktop, frameTitle, r.x, r.y, r.w, r.h, m.vis); internalFrameTitlePopupMenuItem(f, "Module source", r-thread { pcall-messagebox { S src = m.sourceCode(); if (src != null) showText(internalFrameTitle(f) + " [Source]", src); else infoBox("No source code found"); }}); onInternalFrameIconified(f, r { hideModule(m) }); onInternalFrameClosing(f, r { deleteModule(m) /* really? */ }); m.enhanceFrame(f); } } svoid deleteModule(Module m) { removeConcept(m); } svoid visualizeModule(Module m) { pcall-messagebox { if (m.vis == null) m.vis = m.visualize(); if (m.vis == null) m.vis = defaultVisualize(m); } } svoid hideModule(final Module m) { if (m == null) ret; lock m.lock; cset(m, visible := false); pcall-messagebox { m.unvisualize(); } } abstract concept Module { transient Component vis; transient bool started; transient Lock lock = lock(); bool on = true, visible; Rect frameRect; JComponent visualize() { null; } void unvisualize() { unvisualize1(); unvisualize2(); } void enhanceFrame(JInternalFrame frame) {} void start() {} void unvisualize1() { disposeInternalFrame(getInternalFrame(vis)); unvisualize1b(); } void unvisualize1b() { grabFrameRect(); vis = null; } void unvisualize2() {} void update() {} void grabFrameRect() { JInternalFrame f = getInternalFrame(vis); if (f != null) cset(this, frameRect := toRect(getBounds(f))); } void cleanMeUp_started() { started = false; } void delete() { unvisualize(); cleanUp(this); super.delete(); } S sourceCode() { ret javaxSourceOfMyClass1(shortClassName(this)); } void triggerUpdate { rst.trigger(); } } sclass InterestingString extends Module { S theString; transient SimpleLiveValue lv = new(S); void update { lv.set(theString); } JComponent visualize() { ret centerLabel(jLiveValueLabel(lv)); } } sclass UpdateCycles extends Module { long value; transient SimpleLiveValue lv = new(S); void update { value = updateCycles; lv.set(str(value)); } JComponent visualize() { ret onLeftClick(centerLabel(makeBold(setFontSize(40, jLiveValueLabel(lv)))), rst); } void enhanceFrame(JInternalFrame f) { internalFrameTitlePopupMenuItem(f, "Update", rst); } } sclass InputToInterestingString extends Module { transient java.util.Timer timer; void start { timer = doEvery(1000, r { Hello h = findModule(Hello); if (h == null) ret; S s = h.tf != null ? getTextTrim(h.tf) : trim(h.text); showModule(csetAndReturn(uniq(InterestingString), theString := s)); }); } void cleanMeUp() { cancelTimer(timer); } } sclass ModuleCount extends Module { transient JLabel label; void update { if (label != null) setText(label, lstr(onModules())); } JComponent visualize() { ret label = setFontSize(30, jcenteredlabel(lstr(onModules()))); } void unvisualize2() { label = null; } } sclass ModuleList extends Module { transient JList list; void update() { if (list != null) fillListWithStrings(list, calc()); } Module getModule(S item) { ret getConcept(Module, parseFirstLong(item)); } JComponent visualize() { list = jlist(calc()); ret listPopupMenuItemsThreaded(list, "Delete", voidfunc(fS s) { removeConcept(getModule(s)) }, "Show", voidfunc(fS s) { showModule(getModule(s)) }, "Hide", voidfunc(fS s) { hideModule(getModule(s)) }); } L calc() { ret map(func(Module m) -> S { renderConcept(m) /* contains id as first int */ }, onModules()); } void unvisualize2() { list = null; } } sclass ModuleClasses extends Module { transient L> classes; void start() { classes = myNonAbstractClassesImplementing(Module); } JComponent visualize() { JList list = jlist(map shortClassName(classes)); VF1 instantiate = voidfunc(fS s) { showModule((Module) nu(classForName("main$" + s))); }; listPopupMenuItemsThreaded(list, "Instantiate", instantiate); ret onDoubleClickThreaded(list, instantiate); } } sclass DynModule extends Module { S moduleID; transient O o; *() {} *(S *moduleID) {} JComponent visualize() { ret (JComponent) callOpt(o, 'visualize); } void enhanceFrame(final JInternalFrame f) { internalFrameTitlePopupMenuItem(f, "Reload", rThread(r reload)); thread { internalFrameTitle(f, snippetTitle(moduleID)); } } void start() { if (moduleID != null) o = hotwireDependent(moduleID); setOpt(o, host := this); callMain(o); } void unvisualize2() { callOpt(o, 'unvisualize2); } void update() { callOpt(o, 'update); } void cleanMeUp() { cleanUp(o); o = null; } void reload() { JInternalFrame frame = getInternalFrame(vis); unvisualize1b(); unvisualize2(); cleanUp(this); // also sets started to false if (frame != null) setInternalFrameContents(frame, jcenteredlabel("Reloading...")); visible = false; startModule(this); if (frame != null) { cset(this, visible := true); visualizeModule(this); print("New content: " + vis); setInternalFrameContents(frame, vis); } rst.trigger(); } S sourceCode() { ret loadSnippet(moduleID); } } static JComponent defaultVisualize(Module m) { ret jCenteredMultiLineLabel(renderConcept(m)); } sclass DynamicModuleMaker extends Module { JComponent visualize() { ret jfullcenter(jbutton("Add dynamic module...", r { selectSnippetID(voidfunc(S snippetID) { showModule(new DynModule(snippetID)); }); })); } } static A findModule(Class c) { ret findConcept(c, on := true); } sS getInterestingString { InterestingString m = findModule(InterestingString); ret m == null ? null : m.theString; } sclass Hello extends Module { S text = "class main {}"; transient JTextField tf; JComponent visualize() { ret tf = setFontSize(20, jcenteredtextfield(text)); } void unvisualize2() { if (tf != null) cset(this, text := getText(tf)); tf = null; } void enhanceFrame(JInternalFrame f) { packInternalFrameVertically(700, f); centerInternalFrame(f); f.setLocation(f.getX(), 40); } } sS modulesSessionGrab() { grabFrameRects(); ret struct(ll(programID(), localDateWithMilliseconds())) + "\n" + mainConcepts.xfullgrab(); } svoid autoSaveModulesSession() { infoBox("Auto-saving session."); S grab; logQuoted(javaxBackupDir(fsI(programID()) + "/auto-saved-sessions.txt"), grab = modulesSessionGrab()); infoBox("Auto-save done (" + l(grab) + " chars)"); } svoid deleteAllModules() { autoSaveModulesSession(); deleteConcepts(Module); initialModules; } svoid restoreModulesSession(S text) { autoSaveModulesSession(); infoBox("Releasing session"); cleanMeUp(); cleanUp(mainConcepts); mainConcepts = null; //sleepSeconds(1); infoBox("Loading session"); mainConcepts = new Concepts().load(dropFirstLine(text)); initAfterDBLoad(); infoBox("Session restore done"); } svoid grabFrameRects { for (Module m : onModules()) m.grabFrameRect(); } !include once #1015842 // SavedSessiosn