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

190
LINES

< > BotCompany Repo | #1015885 // Standard Modules [Include]

JavaX fragment (include)

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 jSection("INTERNAL UPDATES", onLeftClick(centerLabel(makeBold(setFontSize(40, jLiveValueLabel(lv)))), rstUpdateModules));
  }
  
  void enhanceFrame(JInternalFrame f) {
    internalFrameTitlePopupMenuItem(f, "Update", rstUpdateModules);
  }
}

sclass InputToInterestingString extends Module {
  transient java.util.Timer timer;
  transient S lastHello, lastTop;
  
  void start {
    timer = doEvery(1000, r {
      Hello h = findModule(Hello);
      S hello = h == null ? null : h.tf != null ? getText(h.tf) : h.text;
      S top = getText(tfTopInput);
      S value = null;
      if (neq(hello, lastHello))
        value = hello;
      else if (neq(top, lastTop))
        value = top;
      if (value == null)
        value = or2(top, hello);
      lastHello = hello;
      lastTop = top;
      
      startModule(csetAndReturn(uniq(InterestingString), theString := value));
    });
  }

  void cleanMeUp() { cancelTimer(timer); }
}

sclass ModuleCount extends Module {
  transient JLabel label;

  void update {
    if (label != null) setText(label, lstr(onModules()));
  }

  JComponent visualize() {
    ret jSection("MODULES", label = setFontSize(30, jcenteredlabel(lstr(onModules()))));
  }
  
  void unvisualize2() { label = null; }  
}

sclass ModuleList extends StaticModule_CalculatedList {
  transient AutoCloseable listener;
  
  *() {
    listener = vmBus_onMessages(vmBus_modulesListChangeMessages(), rThread {
      //print("Updating module list");
      update();
    });
  }
  
  void cleanMeUp_ml {
    closeAutoCloseable(listener);
  }
  
  Module getModule(S item) {
    ret getConcept(Module, parseFirstLong(item));
  }
  
  S moduleName() { if (eq(getClass(), ModuleList)) ret "Loaded Modules"; ret super.moduleName(); }

  JComponent visualize() {
    super.visualize();
    onDoubleClickOrEnter(list, voidfunc(fS s) { showModule(getModule(s)) });
    listPopupMenuItemsThreaded(list,
      "Delete", voidfunc(fS s) { removeConcept(getModule(s)) }, 
      "Show", voidfunc(fS s) { showModule(getModule(s)) },
      "Hide", voidfunc(fS s) { hideModule(getModule(s)) });
    listPopupMenu(list, voidfunc(JPopupMenu menu, S s) { 
      final Module m = getModule(s);
      if (m cast DynamicModule)
        addMenuItem(menu, "Reload", rThread { m.reload() });
    });
    ret list;
  }

  L<S> calc() {
    ret map(func(final Module m) -> S {
      //renderConcept(m) /* contains id as first int */
      "[" + m.id + "] " + or2(pcallF(func -> S { m.moduleName() }), "?")
    }, modulesToList());
  }
  
  L<Module> modulesToList() { ret onModules(); }
}

sclass InvisibleModules extends ModuleList {
  L<Module> modulesToList() {
    ret sortedByMethodIC('moduleName, filterByField(onModules(), visible := false));
  }
  
  visualize { ret jSection("INVISIBLE MODULES", super.visualize()); }
}

sclass ModuleClasses extends Module {
  transient L<Class<? extends Module>> classes;

  void start() {
    classes = filter(func(Class<? extends Module> c) -> bool { !isTrue(callOpt(c, 'needsParameters)) },
      myNonAbstractClassesImplementing(Module));
  }

  JComponent visualize() {
    JList list = jlist(map shortClassName(classes));
    VF1<S> instantiate = voidfunc(fS s) {
      showModule((Module) nu(classForName("main$" + s)));
    };
    VF1<S> defaultAction = voidfunc(fS s) {
      makeOrShowStaticModuleOfType(s);
    };
    listPopupMenuItemsThreaded(list, "Instantiate", instantiate);
    listPopupMenu(list, voidfunc(JPopupMenu menu, S item) {
      Class<? extends Module> c = classForName("main$" + item);
      final L<? extends Module> l = staticModulesOfType(c);
      if (nempty(l))
        addMenuItem(menu, l(l) == 1 ? "Show instance" : "Show all " + l(l) + " instances", r-thread { showModules(l) });
    });
    ret onDoubleClickThreaded(list, defaultAction);
  }
}

sclass DynamicModuleMaker extends Module {
  L uSearchData() {
    ret ll(pair("Add dynamic module", r doIt));
  }
  
  void doIt {
    stefansOS_addDynamicModuleDialog();
  }

  JComponent visualize() {
    ret jfullcenter(jbutton("Add dynamic module...", r doIt);
  }
}

sclass Hello extends Module {
  S text = "class main {}";
  
  transient JTextField tf;
  
  void start {
    makeModule(InputToInterestingString);
  }
  
  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);
  }
  
  void setText(S text) {
    temp enter();
    cset(this, +text);
    main.setText(tf, text);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 20 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jcllbfdqhrgy, lpdgvwnxivlt, mqqgnosmbjvj, omdjrrnzbjjv, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, snaazhdonpnp, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1015885
Snippet name: Standard Modules [Include]
Eternal ID of this version: #1015885/57
Text MD5: 925638b2f6a041f6237bc88dccee3698
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-11-10 20:06:32
Source code size: 5283 bytes / 190 lines
Pitched / IR pitched: No / No
Views / Downloads: 473 / 3055
Version history: 56 change(s)
Referenced in: #1015871 - Stefan's OS v2 [OK, with new dyn modules]
#1016478 - Stefan's OS v6
#1022729 - Stefan's OS v6 - copy for transpiler test
#1024932 - Stefan's OS v7 [LIVE]
#1031320 - Stefan's OS v7 [backup before allowing dynamic main classes]