]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.core/src/org/argeo/app/ux/js/AbstractJsObject.java
Add Apache License permission
[gpl/argeo-suite.git] / org.argeo.app.core / src / org / argeo / app / ux / js / AbstractJsObject.java
1 package org.argeo.app.ux.js;
2
3 import java.util.Optional;
4
5 public abstract class AbstractJsObject {
6 /**
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
9 * new.
10 */
11 private String reference;
12
13 private JsClient jsClient;
14
15 private Object[] jsConstructorArgs;
16
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;
22 // }
23
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();
28 } else {
29 this.jsConstructorArgs = args;
30 }
31 }
32
33 public abstract String getJsPackage();
34
35 public void create(JsClient jsClient, String varName) {
36 if (!isNew())
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() + ";");
42 reference = varName;
43 this.jsClient = jsClient;
44 }
45
46 public void delete() {
47 if (isNew())
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 + ";");
54 }
55
56 public boolean isNew() {
57 return reference == null;
58 }
59
60 public boolean isFunctionReference() {
61 return !isNew() && !getReference().endsWith(")");
62 }
63
64 public String getReference() {
65 return reference;
66 }
67
68 String getJsReference() {
69 return jsClient.getJsVarName(reference);
70 }
71
72 protected String newJs() {
73 StringBuilder sb = new StringBuilder();
74 sb.append("new ");
75 sb.append(getJsClassName());
76 sb.append("(");
77 sb.append(JsClient.toJsArgs(jsConstructorArgs));
78 sb.append(")");
79 return sb.toString();
80 }
81
82 public String getJsClassName() {
83 return getJsPackage() + "." + getClass().getSimpleName();
84 }
85
86 public Object callMethod(String methodName, Object... args) {
87 return jsClient.callMethod(getJsReference(), methodName + "(" + JsClient.toJsArgs(args) + ")");
88 }
89
90 public void executeMethod(String methodName, Object... args) {
91 jsClient.executeMethod(getJsReference(), methodName + "(" + JsClient.toJsArgs(args) + ")");
92 }
93
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);
98 });
99 return methodName.orElseThrow();
100 }
101
102 protected JsClient getJsClient() {
103 return jsClient;
104 }
105
106 protected Object[] getJsConstructorArgs() {
107 return jsConstructorArgs;
108 }
109
110 protected void setJsConstructorArgs(Object[] jsConstructorArgs) {
111 this.jsConstructorArgs = jsConstructorArgs;
112 }
113
114 }