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

188
LINES

< > BotCompany Repo | #1015803 // Cubes Frame [v4, with module persistence, OK]

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

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

!7

sS background = #1009931;
static JDesktopPane desktop;
static JTextField tfInput;
//static L<Module> modules;
static ReliableSingleThread rst = new(f updateModules);

p-noconsole {
  autoRestart(5);
  db();

  desktop = jTiledBackgroundDesktopPane(background);
  autoFixDesktopPane(desktop);
  cleanExitOnFrameClose(showMaximizedFrame(desktop));
    
  final JInternalFrame f = showCenteredInternalFrame(desktop, "Hello", 700, 150, setFontSize(20, tfInput = jcenteredtextfield("Find me some stuff")));
  packInternalFrameVertically(f);
  centerInternalFrame(f);
  swing {
 f.setLocation(f.getX(), 40);
 }

  cset(uniq(ModuleClasses), on := true, visible := true);
  for (Module m : onModules())
    startModule(m);

  addConceptIndex(simpleConceptIndex(rTrigger(rst)));
}

svoid updateModules {
  for (Module m : onModules())
    pcall { m.update(); }
}

svoid cleanMeUp {
  print('cleanMeUp);
  for (Module m) if (m.vis != null) pcall {
    print(m);
    m.unvisualize();
  }
  print("done cleanMeUp");
}

static L<Module> onModules() {
  ret conceptsWhere(Module, on := true);
}

sbool hasModuleWithFields(Class c, O... params) {
  ret hasConcept(Module, c, concatArrays(new O[] {on := true}, params));
}

svoid startModule(Module m) {
  //addIfNotThere(modules, m);
  lock m.lock;
  if (m.started) ret;
  m.started = true;
  m.start();
  if (m.visible) showModule(m);
}

svoid showModule(final Module m) {
  startModule(m);
  lock m.lock;
  cset(m, visible := true);
  pcall-messagebox {
    if (m.vis == null)
      m.vis = m.visualize();
  }
  if (m.vis != null) swing {
    Rect r = m.frameRect;
    if (r == null) 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));
    onInternalFrameClosing(showInternalFrame(desktop, frameTitle, r.x, r.y, r.w, r.h, m.vis), r {
      cset(m, visible := false);
      m.unvisualize();
    });
  }
}

concept Module {
  transient Component vis;
  transient bool started;
  transient Lock lock = lock();
  
  bool on = true, visible;
  Rect frameRect;
  
  JComponent visualize() { ret jCenteredMultiLineLabel(sfu(this)); }
  void unvisualize() { unvisualize1(); unvisualize2(); }
  void start() {}
  void unvisualize1() {
    print("unvisualize1");
    JInternalFrame f = getInternalFrame(vis);
    if (f != null)
      cset(this, frameRect := toRect(getBounds(f)));
    vis = null;
    print("done unvisualize1");
  }
  void unvisualize2() {}
  void update() {}

  void delete() {  
    unvisualize();
    cleanUp(this);
    super.delete();
  }
}

sclass InterestingStringModule extends Module {
  S theString;
}

sclass InputToInterestingString extends Module {
  transient java.util.Timer timer;
  
  void start {
    timer = doEvery(1000, r {
      S s = getTextTrim(tfInput);
      if (!hasModuleWithFields(InterestingStringModule, theString := s))
        startModule(nu(InterestingStringModule, theString := s));
    });
  }

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

sclass ModuleCount extends Module {
  transient JLabel label;

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

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

sclass ModuleList extends Module {
  transient JList list;

  void update() {
    if (list != null)
      fillListWithStrings(list, calc());
  }

  JComponent visualize() {
    list = jlist(calc());
    listPopupMenuItem(list, "Delete", voidfunc(fS s) {
      thread-messagebox {
        Module m = getConcept(Module, parseFirstLong(s));
        removeConcept(m);
      }
    });
    ret list;
  }

  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));
    listPopupMenuItem(list, "Instantiate", voidfunc(fS s) {
      thread-messagebox {
        showModule((Module) nu(classForName("main$" + s)));
      }
    });
    ret list;
  }
}

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: #1015803
Snippet name: Cubes Frame [v4, with module persistence, OK]
Eternal ID of this version: #1015803/33
Text MD5: b892446d27e85bd981e9e246e39190eb
Transpilation MD5: 8d91fb60b05ca51a19bf68c450eb4488
Author: stefan
Category:
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-27 23:53:46
Source code size: 4565 bytes / 188 lines
Pitched / IR pitched: No / No
Views / Downloads: 588 / 2125
Version history: 32 change(s)
Referenced in: [show references]