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

403
LINES

< > BotCompany Repo | #1015871 // Stefan's OS v2 [OK, with new dyn modules]

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

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

1  
!7
2  
3  
sS background = #1009931;
4  
static JDesktopPane desktop;
5  
static ReliableSingleThread rst = new(f updateModules);
6  
static volatile long updateCycles;
7  
static int systemErrorsToKeep = 10;
8  
static L systemErrors = synchroList(new CircularArrayList);
9  
10  
p-subst {
11  
  _handleException_addHandler(voidfunc(Throwable e) {
12  
    addToListWithMaxSize(systemErrors, e, systemErrorsToKeep);
13  
    infoBox("Error: " + exceptionToStringShort(e));
14  
    LastErrors lastErrors = first(staticModulesOfType(LastErrors));
15  
    if (lastErrors != null) lastErrors.addError(e);
16  
  });
17  
  
18  
  autoRestart();
19  
  db();
20  
21  
  desktop = jDesktopPaneWithSkyPicture_autoUnload(background, Color.black);
22  
  autoFixDesktopPane(desktop);
23  
  cleanExitOnFrameClose(frameIcon(#1101272, showMaximizedFrame(desktop)));
24  
  titlePopupMenu_top(desktop, voidfunc(JPopupMenu menu) {
25  
    addMenuItems(menu,
26  
      "Update One Cycle", rst,
27  
      "New Session", rThreadPcallMessageBox(r deleteAllModules),
28  
    );
29  
  });
30  
    
31  
  initAfterDBLoad();
32  
}
33  
34  
svoid initAfterDBLoad {
35  
  initialModules();
36  
  
37  
  UpdateCycles uc = conceptWhere(UpdateCycles);
38  
  if (uc != null) updateCycles = uc.value;
39  
  
40  
  for (Module m : onModules()) startModule(m);
41  
  
42  
  addConceptIndex(simpleConceptIndex(rst));
43  
  rst.trigger();
44  
}
45  
46  
svoid initialModules {
47  
  if (!hasConcept(ModuleClasses))
48  
    showModule(uniq(ModuleClasses));
49  
}
50  
51  
svoid triggerUpdate { rst.trigger(); }
52  
53  
svoid updateModules {
54  
  ++updateCycles;
55  
  for (Module m : onModules())
56  
    pcall { m.update(); }
57  
}
58  
59  
svoid cleanMeUp {
60  
  for (Module m) if (m.vis != null) pcall {
61  
    m.unvisualize();
62  
  }
63  
  
64  
  for (Module m) cleanUp(m);
65  
}
66  
67  
static L<Module> onModules() { ret conceptsWhere(Module, on := true); }
68  
69  
sbool hasModuleWithFields(Class<? extends Module> c, O... params) {
70  
  ret hasConcept(c, concatArrays(new O[] {on := true}, params));
71  
}
72  
73  
svoid startModule(Module m) { pcall {
74  
  //addIfNotThere(modules, m);
75  
  lock m.lock;
76  
  if (m.started) ret;
77  
  m.started = true;
78  
  print("Starting module " + m);
79  
  try {
80  
    m.start();
81  
  } catch e {
82  
    m.setError(e);
83  
    _handleException(e);
84  
  }
85  
  rst.trigger();
86  
  if (m.visible) showModule(m);
87  
}}
88  
89  
svoid showModule(final Module m) {
90  
  if (m == null) ret;
91  
  startModule(m);
92  
  lock m.lock;
93  
  if (m.vis != null) {
94  
    activateInternalFrame(m.vis);
95  
    ret;
96  
  }
97  
  cset(m, visible := true);
98  
  visualizeModule(m);
99  
  if (m.vis != null) swing {
100  
    Rect r = m.frameRect;
101  
    if (r == null) r = randomRect(desktop.getWidth(), desktop.getHeight(), 10, 150, 100);
102  
    if (r == null) r = Rect(10, 10, 200, 100);
103  
    print("Showing frame at " + r);
104  
    S frameTitle = humanizeFormLabel(shortClassName(m));
105  
    final JInternalFrame f = showInternalFrame(desktop, frameTitle, r.x, r.y, r.w, r.h, m.vis);
106  
    
107  
    internalFrameTitlePopupMenuItem(f, "Module source", r-thread { pcall {
108  
      S src = m.sourceCode();
109  
      if (src != null) showText(internalFrameTitle(f) + " [Source]", src); else infoBox("No source code found");
110  
    }});
111  
    onInternalFrameIconified(f, r { hideModule(m) });
112  
    onInternalFrameClosing(f, r { deleteModule(m) /* really? */ });
113  
    internalFrameIcon(f, m.iconID);
114  
    m.enhanceFrame(f);
115  
  }
116  
}
117  
118  
svoid showModules(L<? extends Module> l) {
119  
  for (Module m : unnull(l)) showModule(m);
120  
}
121  
122  
svoid deleteModule(Module m) { removeConcept(m); triggerUpdate(); }
123  
124  
svoid visualizeModule(Module m) {
125  
  pcall {
126  
    if (m.vis == null) m.vis = m.visualize();
127  
    if (m.vis == null) m.vis = defaultVisualize(m);
128  
  }
129  
}
130  
131  
svoid hideModule(final Module m) {
132  
  if (m == null) ret;
133  
  lock m.lock;
134  
  cset(m, visible := false);
135  
  pcall { m.unvisualize(); }
136  
}
137  
138  
svoid revisualizeModule(Module m) {
139  
  pcall {
140  
    if (m == null) ret;
141  
    lock m.lock;
142  
    JInternalFrame frame = getInternalFrame(m.vis);
143  
    m.unvisualize1b();
144  
    m.unvisualize2();
145  
    visualizeModule(m);
146  
    setInternalFrameContents(frame, m.vis);
147  
  }
148  
}
149  
150  
abstract concept Module {
151  
  transient Component vis;
152  
  transient bool started;
153  
  transient Lock lock = lock();
154  
155  
  bool on = true, visible;
156  
  Rect frameRect;
157  
  S iconID;
158  
  PersistableThrowable error;
159  
  
160  
  JComponent visualize() { null; }
161  
  void unvisualize() { unvisualize1(); unvisualize2(); }
162  
  void enhanceFrame(JInternalFrame frame) {}
163  
  void start() {}
164  
  void unvisualize1() {
165  
    disposeInternalFrame(getInternalFrame(vis));
166  
    unvisualize1b();
167  
  }
168  
  
169  
  void unvisualize1b() {
170  
    grabFrameRect();
171  
    vis = null;
172  
  }
173  
  void unvisualize2() {}
174  
  void update() {}
175  
  
176  
  void grabFrameRect() {
177  
    JInternalFrame f = getInternalFrame(vis);
178  
    if (f != null)
179  
      cset(this, frameRect := toRect(getBounds(f)));
180  
  }
181  
  
182  
  void cleanMeUp_started() { started = false; }
183  
184  
  void delete() {
185  
    unvisualize();
186  
    cleanUp(this);
187  
    super.delete();
188  
  }
189  
190  
  S sourceCode() {  
191  
    ret javaxSourceOfMyClass1(shortClassName(this));
192  
  }
193  
  
194  
  // for all modules
195  
  void triggerUpdate { rst.trigger(); }
196  
  
197  
  void setModuleIcon(S iconID) {
198  
    if (eq(iconID, this.iconID)) ret;
199  
    this.iconID = iconID;
200  
    internalFrameIcon(vis, iconID);
201  
  }
202  
  
203  
  O resolve() { ret this; }
204  
  O getError() { ret error; }
205  
  S moduleID() { ret str(id); }
206  
  
207  
  void setError(Throwable e) {
208  
    cset(this, error := persistableThrowable(e));
209  
  }
210  
} // END CONCEPT MODULE
211  
212  
static JComponent defaultVisualize(Module m) {
213  
  ret jCenteredMultiLineLabel(renderConcept(m)); 
214  
}
215  
216  
static <A extends Module> A findModule(Class<A> c) {
217  
  ret findConcept(c, on := true);
218  
}
219  
220  
// returns module ID
221  
static S findDynModuleOfType(S type) {
222  
  DynamicModule m = findConcept(DynamicModule, on := true, _className := "main$" + type);
223  
  ret m == null ? null : m.moduleID();
224  
}
225  
226  
static <A extends Module> L<A> staticModulesOfType(Class<A> type) {
227  
  ret conceptsWhere(type, on := true);
228  
}
229  
230  
static L listModules() {
231  
  ret map unwrapDynamicModule(onModules());
232  
}
233  
234  
static O unwrapDynamicModule(Module m) {
235  
  ret m instanceof DynamicModule ? or(m/DynamicModule.o, m) : m;
236  
}
237  
238  
sbool moduleStillThere(O o) {
239  
  Module m = o instanceof Module ? o/Module : (Module) get(o, '_host);
240  
  ret isConceptRegistered(mainConcepts, m);
241  
}
242  
243  
static O getDynModuleByID(S moduleID) {
244  
  if (moduleID == null) null;
245  
  DynamicModule m = cast getConcept(parseLong(moduleID));
246  
  ret m == null ? null : m.o;
247  
}
248  
  
249  
sS getInterestingString {
250  
  InterestingString m = findModule(InterestingString);
251  
  ret m == null ? null : m.theString;
252  
}
253  
254  
sS modulesSessionGrab() {
255  
  grabFrameRects();
256  
  ret struct(ll(programID(), localDateWithMilliseconds())) + "\n"
257  
    + mainConcepts.xfullgrab();
258  
}
259  
260  
svoid autoSaveModulesSession() {
261  
  infoBox("Auto-saving session.");
262  
  S grab;
263  
  logQuoted(javaxBackupDir(fsI(programID()) + "/auto-saved-sessions.txt"), grab = modulesSessionGrab());
264  
  infoBox("Auto-save done (" + l(grab) + " chars)");
265  
}
266  
267  
svoid deleteAllModules() {
268  
  autoSaveModulesSession();
269  
  deleteConcepts(Module);
270  
  initialModules;
271  
}
272  
273  
svoid restoreModulesSession(S text) {
274  
  autoSaveModulesSession();
275  
  infoBox("Releasing session");
276  
  cleanMeUp();
277  
  cleanUp(mainConcepts);
278  
  mainConcepts = null;
279  
  //sleepSeconds(1);
280  
  infoBox("Loading session");
281  
  mainConcepts = new Concepts().load(dropFirstLine(text));
282  
  initAfterDBLoad();
283  
  infoBox("Session restore done");
284  
}
285  
286  
svoid grabFrameRects {
287  
  for (Module m : onModules()) m.grabFrameRect();
288  
}
289  
290  
sclass DynamicModule extends Module {
291  
  S moduleID, _className; // "className" is taken by DynamicObject; _className == null for old-style dyn module
292  
  S oStruct; // serialized dynamic object
293  
  
294  
  transient Class c;
295  
  transient O o;
296  
297  
  *() {}
298  
  *(S *moduleID, S *_className) {}
299  
  *(S *moduleID, S *_className, Class *c) {}
300  
  
301  
  AutoCloseable enter() {
302  
    ret castForTemp(callOpt(o, 'enter));
303  
  }
304  
305  
  JComponent visualize() {
306  
    temp enter();
307  
    ret (JComponent) callOpt(o, 'visualize);
308  
  }
309  
  
310  
  void enhanceFrame(final JInternalFrame f) {
311  
    internalFrameTitlePopupMenuItem(f, "Reload", rThread(r reload));
312  
    S title = internalFrameTitle(f);
313  
    {
314  
      temp enter();
315  
      pcallOpt(o, 'enhanceFrame, f);
316  
    }
317  
    if (eq(internalFrameTitle(f), title)) // not changed by dynamic module
318  
      thread { internalFrameTitle(f, dropSuffixICTrimOneOf(snippetTitle(moduleID), "[Dyn Module]", "[Dyn Module, OK]")); }
319  
  }
320  
  
321  
  void start() {
322  
    if (moduleID == null) ret;
323  
    if (c == null) c = hotwireDependent(moduleID);
324  
    if (oStruct != null) pcall {
325  
      o = unstructureInRealm(oStruct, c);
326  
    }
327  
    if (o == null)
328  
      if (_className == null)
329  
        o = c;
330  
      else
331  
        o = nu(_getClass(c, _className));
332  
    setOpt(o, _host := this);
333  
    if (o instanceof Class)
334  
      callMain(o);
335  
    else
336  
      callOpt(o, 'start);
337  
  }
338  
  
339  
  void unvisualize2() { callOpt(o, 'unvisualize2); }
340  
  
341  
  void update() { callOpt(o, 'update); }
342  
  
343  
  void cleanMeUp() {
344  
    oStruct = null; pcall { if (o != null && !o instanceof Class) oStruct = struct(o); }
345  
    cleanUp(o);
346  
    if (!o instanceof Class)
347  
      cleanUp(_getClass(o, "main"));
348  
    o = null;
349  
    c = null;
350  
  }
351  
  
352  
  void reload() {
353  
    JInternalFrame frame = getInternalFrame(vis);
354  
    unvisualize1b();
355  
    unvisualize2();
356  
    cleanUp(this); // also sets started to false
357  
    if (frame != null)
358  
      setInternalFrameContents(frame, jcenteredlabel("Reloading..."));
359  
    visible = false;
360  
    startModule(this);
361  
    if (frame != null) {
362  
      cset(this, visible := true);
363  
      visualizeModule(this);
364  
      print("New content: " + vis);
365  
      setInternalFrameContents(frame, vis);
366  
    }
367  
    rst.trigger();
368  
  }
369  
  
370  
  S sourceCode() {  
371  
    ret loadSnippet(moduleID);
372  
  }
373  
  
374  
  O resolve() { ret or(o, this); }
375  
  O getError() {
376  
    if (o != null) {
377  
      O error = callOpt(o, 'getError);
378  
      if (error != null) ret error;
379  
    }
380  
    ret super.getError();
381  
  }
382  
  
383  
  toString {
384  
    ret "DynModule " + moduleID + "/" + className;
385  
  }
386  
  
387  
  void setError(Throwable e) {
388  
    if (o != null && isTrue(callOpt(o, 'setError, e))) ret;
389  
    super.setError(e);
390  
  }
391  
392  
}
393  
394  
static L resolvedModules() {
395  
  new L l;
396  
  for (Module m : onModules())
397  
    l.add(m.resolve());
398  
  ret l;
399  
}
400  
401  
!include once #1015842 // SavedSessions
402  
!include once #1015885 // Standard Modules
403  
!include once #1015959 // More Standard Modules

Author comment

Began life as a copy of #1015808

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1015871
Snippet name: Stefan's OS v2 [OK, with new dyn modules]
Eternal ID of this version: #1015871/59
Text MD5: f9a85d9782a1602e98ad5b789e14a90e
Transpilation MD5: 2a63a8542c5b5d24b2d1501af1511efc
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-06-02 19:23:33
Source code size: 10391 bytes / 403 lines
Pitched / IR pitched: No / No
Views / Downloads: 715 / 3840
Version history: 58 change(s)
Referenced in: [show references]