X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=swt%2Forg.argeo.app.swt%2Fsrc%2Forg%2Fargeo%2Fapp%2Fswt%2Fjs%2FSwtBrowserJsPart.java;h=41cc3afe2ddbf587b51f3695d7c5528033839dd2;hb=720e78163faf96ccc966d155a516487af52ccec2;hp=6282b1e30f1fbac8959cb2730e3c82bbc54cbc78;hpb=59da7271e876ca8a429beb86b67e7350eef1e1ca;p=gpl%2Fargeo-suite.git diff --git a/swt/org.argeo.app.swt/src/org/argeo/app/swt/js/SwtBrowserJsPart.java b/swt/org.argeo.app.swt/src/org/argeo/app/swt/js/SwtBrowserJsPart.java index 6282b1e..41cc3af 100644 --- a/swt/org.argeo.app.swt/src/org/argeo/app/swt/js/SwtBrowserJsPart.java +++ b/swt/org.argeo.app.swt/src/org/argeo/app/swt/js/SwtBrowserJsPart.java @@ -6,7 +6,6 @@ import java.util.Locale; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.function.Function; -import java.util.function.Supplier; import org.argeo.api.cms.CmsLog; import org.argeo.app.ux.js.JsClient; @@ -37,7 +36,7 @@ public class SwtBrowserJsPart implements JsClient { * Tasks that were requested before the context was ready. Typically * configuration methods on the part while the user interfaces is being build. */ - private List> preReadyToDos = new ArrayList<>(); + private List preReadyToDos = new ArrayList<>(); public SwtBrowserJsPart(Composite parent, int style, String url) { this.browser = new Browser(parent, 0); @@ -55,10 +54,8 @@ public class SwtBrowserJsPart implements JsClient { init(); loadExtensions(); // execute todos in order - for (Supplier toDo : preReadyToDos) { - boolean success = toDo.get(); - if (!success) - throw new IllegalStateException("Post-initalisation JavaScript execution failed"); + for (PreReadyToDo toDo : preReadyToDos) { + toDo.run(); } preReadyToDos.clear(); readyStage.complete(true); @@ -87,22 +84,18 @@ public class SwtBrowserJsPart implements JsClient { protected void init() { } - /** To be overridden with calls to {@link #loadExtension(String)}. */ + /** + * To be overridden with calls to {@link #loadExtension(String)}. + */ protected void loadExtensions() { } protected void loadExtension(String url) { -// String js = """ -// var script = document.createElement("script"); -// script.src = '%s'; -// document.head.appendChild(script); -// """; -// browser.evaluate(String.format(Locale.ROOT, js, url)); browser.evaluate(String.format(Locale.ROOT, "import('%s')", url)); } - protected CompletionStage getReadyStage() { + public CompletionStage getReadyStage() { return readyStage.minimalCompletionStage(); } @@ -114,7 +107,7 @@ public class SwtBrowserJsPart implements JsClient { public Object evaluate(String js, Object... args) { assert browser.getDisplay().equals(Display.findDisplay(Thread.currentThread())) : "Not the proper UI thread."; if (!readyStage.isDone()) - throw new IllegalStateException("Methods returning a result can only be called after UI initilaisation."); + throw new IllegalStateException("Methods returning a result can only be called after UI initialisation."); // wait for the context to be ready // boolean ready = readyStage.join(); // if (!ready) @@ -125,15 +118,13 @@ public class SwtBrowserJsPart implements JsClient { @Override public void execute(String js, Object... args) { + String jsToExecute = String.format(Locale.ROOT, js, args); if (readyStage.isDone()) { - boolean success = browser.execute(String.format(Locale.ROOT, js, args)); + boolean success = browser.execute(jsToExecute); if (!success) throw new RuntimeException("JavaScript execution failed."); } else { - Supplier toDo = () -> { - boolean success = browser.execute(String.format(Locale.ROOT, js, args)); - return success; - }; + PreReadyToDo toDo = new PreReadyToDo(jsToExecute); preReadyToDos.add(toDo); } } @@ -167,6 +158,21 @@ public class SwtBrowserJsPart implements JsClient { return GLOBAL_THIS_ + name; } + class PreReadyToDo implements Runnable { + private String js; + + public PreReadyToDo(String js) { + this.js = js; + } + + @Override + public void run() { + boolean success = browser.execute(js); + if (!success) + throw new IllegalStateException("Pre-ready JavaScript failed: " + js); + } + } + /* * ACCESSORS */