public final class BrwsrCtx extends Object implements Executor
net.java.html.json.Model
and other objects
operate in. The context is usually a particular HTML page in a browser.
The context is also associated with the actual HTML technology
in the HTML page - there is likely to be different context for
knockout.js and different one
for angular. Since version 1.1
the content of contexts can be selected by registering
implementations under specific
technology identifiers
and requesting them during
construction
of the
context.Modifier and Type | Field and Description |
---|---|
static BrwsrCtx |
EMPTY
Dummy context without binding to any real browser or technology.
|
Modifier and Type | Method and Description |
---|---|
void |
execute(Runnable exec)
Runs provided code in the context of this
BrwsrCtx . |
static BrwsrCtx |
findDefault(Class<?> requestor)
Seeks for the default context that is associated with the requesting
class.
|
public static final BrwsrCtx EMPTY
public static BrwsrCtx findDefault(Class<?> requestor)
BrwsrCtx.EMPTY
context is returned. One can enter
a context by calling BrwsrCtx.execute(java.lang.Runnable)
.requestor
- the class that makes the requestpublic final void execute(Runnable exec)
Runs provided code in the context of this BrwsrCtx
.
If there is an Executor
registered in the context
it is used to perform the given code. While the code exec
is running the value of BrwsrCtx.findDefault(java.lang.Class)
returns
this
. If the executor supports a single thread execution
policy, it may execute the runnable later (in such case this method
returns immediately). If the call to this method is done on the right
thread, the runnable should be executed synchronously.
Example Using a Timer
public final class Periodicaly extendsTimerTask
{ private finalBrwsrCtx
ctx; private int counter; private Periodicaly(BrwsrCtx
ctx) { // remember the browser context and use it later this.ctx = ctx; this.counter = 0; } @Override
public void run() { // arrives on wrong thread, needs to be re-scheduled ctx.execute(newRunnable
() { @Override
public void run() { codeThatNeedsToBeRunInABrowserEnvironment(); } }); } // called when your page is ready public static void onPageLoad(String
... args) throwsException
{ // the context at the time of page initializationBrwsrCtx
initialCtx =BrwsrCtx
.findDefault(Periodicaly.class); // the task that is associated with context Periodicaly task = new Periodicaly(initialCtx); // creates a new timerTimer
t = newTimer
("Move the box"); // run the task every 100ms t.scheduleAtFixedRate(task, 0, 100); } @JavaScriptBody
(args = { "a", "b" }, body = "return a + b") private static native int plus(int a, int b); void codeThatNeedsToBeRunInABrowserEnvironment() { // invokes JavaScript function in the browser environment counter = plus(counter, 1); } }
Copyright © 2018 NetBeans. All rights reserved.