]>
git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.core/src/org/argeo/app/ux/js/AbstractJsObject.java
1 package org
.argeo
.app
.ux
.js
;
3 import java
.util
.Optional
;
5 public abstract class AbstractJsObject
{
7 * JavaScript expression returning a reference to the object. It can be either a
8 * variable or a function call. If it is not set the object is assumed to be a
11 private String reference
;
13 private JsClient jsClient
;
15 private Object
[] jsConstructorArgs
;
17 // public AbstractJsObject(JsClient jsClient, String reference) {
18 // Objects.requireNonNull(jsClient);
19 // Objects.requireNonNull(reference, "JS reference cannot be null");
20 // this.jsClient = jsClient;
21 // this.reference = reference;
24 public AbstractJsObject(Object
... args
) {
25 if (args
.length
== 2 && args
[0] instanceof JsClient jsClient
) {
26 this.jsClient
= jsClient
;
27 this.reference
= args
[1].toString();
29 this.jsConstructorArgs
= args
;
33 public abstract String
getJsPackage();
35 public void create(JsClient jsClient
, String varName
) {
37 throw new IllegalStateException("JS object " + getJsClassName() + " is not new");
38 if (isFunctionReference())
39 throw new IllegalStateException(
40 "JS object " + getJsClassName() + " cannot be created since it is a function reference");
41 jsClient
.execute(jsClient
.getJsVarName(varName
) + " = " + newJs() + ";");
43 this.jsClient
= jsClient
;
46 public void delete() {
48 throw new IllegalStateException(
49 "JS object " + getJsClassName() + " cannot be deleted since it is anonymous");
50 if (isFunctionReference())
51 throw new IllegalStateException(
52 "JS object " + getJsClassName() + " cannot be deleted since it is a function reference");
53 jsClient
.execute(reference
+ " = undefined; delete " + reference
+ ";");
56 public boolean isNew() {
57 return reference
== null;
60 public boolean isFunctionReference() {
61 return !isNew() && !getReference().endsWith(")");
64 public String
getReference() {
68 protected String
getJsReference() {
69 return jsClient
.getJsVarName(reference
);
72 protected String
newJs() {
73 StringBuilder sb
= new StringBuilder();
75 sb
.append(getJsClassName());
77 sb
.append(JsClient
.toJsArgs(jsConstructorArgs
));
82 public String
getJsClassName() {
83 return getJsPackage() + "." + getClass().getSimpleName();
86 public Object
callMethod(String methodName
, Object
... args
) {
87 return jsClient
.callMethod(getJsReference(), methodName
+ "(" + JsClient
.toJsArgs(args
) + ")");
90 public void executeMethod(String methodName
, Object
... args
) {
91 jsClient
.executeMethod(getJsReference(), methodName
+ "(" + JsClient
.toJsArgs(args
) + ")");
94 protected String
getMethodName() {
95 StackWalker walker
= StackWalker
.getInstance();
96 Optional
<String
> methodName
= walker
.walk(frames
-> {
97 return frames
.skip(1).findFirst().map(StackWalker
.StackFrame
::getMethodName
);
99 return methodName
.orElseThrow();
102 protected JsClient
getJsClient() {
106 protected Object
[] getJsConstructorArgs() {
107 return jsConstructorArgs
;
110 protected void setJsConstructorArgs(Object
[] jsConstructorArgs
) {
111 this.jsConstructorArgs
= jsConstructorArgs
;