Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

379
LINES

< > BotCompany Repo | #1015808 // Stefan's OS v1 [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (15599L/115K).

!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<Module> onModules() { ret conceptsWhere(Module, on := true); }

sbool hasModuleWithFields(Class<? extends Module> 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<S> lv = new(S);
  void update { lv.set(theString); }

  JComponent visualize() {
    ret centerLabel(jLiveValueLabel(lv));
  }
}

sclass UpdateCycles extends Module {
  long value;
  transient SimpleLiveValue<S> 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<S> 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<Class<? extends Module>> classes;

  void start() {
    classes = myNonAbstractClassesImplementing(Module);
  }

  JComponent visualize() {
    JList list = jlist(map shortClassName(classes));
    VF1<S> 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 extends Module> A findModule(Class<A> 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

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015808
Snippet name: Stefan's OS v1 [OK]
Eternal ID of this version: #1015808/94
Text MD5: dca5c06e2f4a2ab9b5fa4254c18f2d45
Transpilation MD5: 881132d65f76e6f9ef0245a9b6b057f0
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-29 00:52:27
Source code size: 9602 bytes / 379 lines
Pitched / IR pitched: No / No
Views / Downloads: 588 / 1687
Version history: 93 change(s)
Referenced in: [show references]