]> git.argeo.org Git - gpl/argeo-suite.git/blob - app/ol/AbstractOlObject.java
Prepare next development cycle
[gpl/argeo-suite.git] / app / ol / AbstractOlObject.java
1 package org.argeo.app.ol;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Objects;
6
7 import org.argeo.app.ux.js.AbstractJsObject;
8
9 public abstract class AbstractOlObject extends AbstractJsObject {
10 public final static String JS_PACKAGE = "argeo.tp.ol";
11
12 public AbstractOlObject(Object... args) {
13 super(args.length > 0 ? args : new Object[] { new HashMap<String, Object>() });
14 }
15
16 // public AbstractOlObject(Map<String, Object> options) {
17 // super(new Object[] { options });
18 // }
19
20 public String getJsPackage() {
21 return JS_PACKAGE;
22 }
23
24 @SuppressWarnings("unchecked")
25 protected Map<String, Object> getNewOptions() {
26 if (!isNew())
27 throw new IllegalStateException("Object " + getJsClassName() + " is not new");
28 Object[] args = getJsConstructorArgs();
29 if (args.length != 1 || !(args[0] instanceof Map))
30 throw new IllegalStateException("Object " + getJsClassName() + " has no available options");
31 return (Map<String, Object>) args[0];
32 }
33
34 protected void doSetValue(String methodName, String newOption, Object value) {
35 if (isNew()) {
36 Objects.requireNonNull(newOption, "Value cannot be set as an option for " + getJsClassName() + ", use "
37 + methodName + "() after the object has been created");
38 getNewOptions().put(newOption, value);
39 } else {
40 Objects.requireNonNull(methodName, "Value cannot be set via a method for " + getJsClassName() + ", use "
41 + newOption + " before the object is created");
42 executeMethod(methodName, value);
43 }
44 }
45
46 public void set(String key, Object value) {
47 set(key, value, false);
48 }
49
50 public void set(String key, Object value, boolean silent) {
51 if (isNew()) {
52 getNewOptions().put(key, value);
53 } else {
54 executeMethod(getMethodName(), new Object[] { key, value, silent });
55 }
56 }
57
58 public Object get(String key) {
59 if (isNew()) {
60 return getNewOptions().get(key);
61 } else {
62 // TDO deal with reference if we are trying to get an object
63 return callMethod(getMethodName(), key);
64 }
65
66 }
67
68 public static String getJsClassName(Class<?> clss) {
69 if (AbstractOlObject.class.isAssignableFrom(clss)) {
70 // NB: would failed for renamed classes
71 return JS_PACKAGE + "." + clss.getSimpleName();
72 }
73 throw new IllegalArgumentException(clss + " is not an OpenLayers object");
74 }
75
76 }