9
9
import io .github .makbn .jlmap .model .*;
10
10
import lombok .AccessLevel ;
11
11
import lombok .experimental .FieldDefaults ;
12
- import lombok .extern .log4j . Log4j2 ;
12
+ import lombok .extern .slf4j . Slf4j ;
13
13
14
14
import java .io .Serializable ;
15
15
import java .util .HashMap ;
16
+ import java .util .Optional ;
16
17
17
18
/**
18
19
* @author Mehdi Akbarian Rastaghi (@makbn)
19
20
*/
20
- @ Log4j2
21
+ @ Slf4j
21
22
@ FieldDefaults (makeFinal = true , level = AccessLevel .PRIVATE )
22
23
public class JLMapCallbackHandler implements Serializable {
23
-
24
- transient JLMapView mapView ;
24
+ private static final String FUNCTION_MOVE = "move" ;
25
+ private static final String FUNCTION_CLICK = "click" ;
26
+ private static final String FUNCTION_ZOOM = "zoom" ;
27
+ private static final String FUNCTION_MOVE_START = "movestart" ;
28
+ private static final String FUNCTION_MOVE_END = "moveend" ;
29
+ transient OnJLMapViewListener listener ;
25
30
transient HashMap <Class <? extends JLObject <?>>, HashMap <Integer , JLObject <?>>> jlObjects ;
26
31
transient Gson gson ;
27
32
HashMap <String , Class <? extends JLObject <?>>[]> classMap ;
28
33
29
- public JLMapCallbackHandler (JLMapView mapView ) {
30
- this .mapView = mapView ;
34
+ public JLMapCallbackHandler (OnJLMapViewListener listener ) {
35
+ this .listener = listener ;
31
36
this .jlObjects = new HashMap <>();
32
37
this .gson = new Gson ();
33
38
this .classMap = new HashMap <>();
34
39
initClassMap ();
35
40
}
41
+
36
42
@ SuppressWarnings ("unchecked" )
37
43
private void initClassMap () {
38
-
39
44
classMap .put ("marker" , new Class []{JLMarker .class });
40
45
classMap .put ("marker_circle" , new Class []{JLCircleMarker .class });
41
46
classMap .put ("polyline" , new Class []{JLPolyline .class , JLMultiPolyline .class });
@@ -50,7 +55,7 @@ private void initClassMap() {
50
55
* @param param4 additional param
51
56
* @param param5 additional param
52
57
*/
53
- @ SuppressWarnings ("unchecked " )
58
+ @ SuppressWarnings ("all " )
54
59
public void functionCalled (String functionName , Object param1 , Object param2 ,
55
60
Object param3 , Object param4 , Object param5 ) {
56
61
log .debug (String .format ("function: %s \t param1: %s \t param2: %s " +
@@ -87,70 +92,69 @@ public void functionCalled(String functionName, Object param1, Object param2,
87
92
return ;
88
93
}
89
94
}
90
- } else if (param1 .equals ("main_map" )
91
- && mapView .getMapListener ().isPresent ()) {
95
+ } else if (param1 .equals ("main_map" ) && getMapListener ().isPresent ()) {
92
96
switch (functionName ) {
93
- case "move" -> mapView . getMapListener ()
97
+ case FUNCTION_MOVE -> getMapListener ()
94
98
.get ()
95
99
.onAction (new MoveEvent (OnJLMapViewListener .Action .MOVE ,
96
100
gson .fromJson (String .valueOf (param4 ), JLLatLng .class ),
97
101
gson .fromJson (String .valueOf (param5 ), JLBounds .class ),
98
102
Integer .parseInt (String .valueOf (param3 ))));
99
- case "movestart" -> mapView . getMapListener ()
103
+ case FUNCTION_MOVE_START -> getMapListener ()
100
104
.get ()
101
105
.onAction (new MoveEvent (OnJLMapViewListener .Action .MOVE_START ,
102
106
gson .fromJson (String .valueOf (param4 ), JLLatLng .class ),
103
107
gson .fromJson (String .valueOf (param5 ), JLBounds .class ),
104
108
Integer .parseInt (String .valueOf (param3 ))));
105
- case "moveend" -> mapView . getMapListener ()
109
+ case FUNCTION_MOVE_END -> getMapListener ()
106
110
.get ()
107
111
.onAction (new MoveEvent (OnJLMapViewListener .Action .MOVE_END ,
108
112
gson .fromJson (String .valueOf (param4 ), JLLatLng .class ),
109
113
gson .fromJson (String .valueOf (param5 ), JLBounds .class ),
110
114
Integer .parseInt (String .valueOf (param3 ))));
111
- case "click" -> mapView . getMapListener ()
115
+ case FUNCTION_CLICK -> getMapListener ()
112
116
.get ()
113
117
.onAction (new ClickEvent (gson .fromJson (String .valueOf (param3 ),
114
118
JLLatLng .class )));
115
119
116
- case "zoom" -> mapView . getMapListener ()
120
+ case FUNCTION_ZOOM -> getMapListener ()
117
121
.get ()
118
122
.onAction (new ZoomEvent (OnJLMapViewListener .Action .ZOOM ,
119
123
Integer .parseInt (String .valueOf (param3 ))));
120
124
default -> log .error (functionName + " not implemented!" );
121
125
}
122
126
}
123
127
} catch (Exception e ) {
124
- log .error (e );
128
+ log .error (e . getMessage (), e );
125
129
}
126
130
}
127
131
128
132
private boolean callListenerOnObject (
129
133
String functionName , JLObject <JLObject <?>> jlObject , Object ... params ) {
130
134
switch (functionName ) {
131
- case "move" -> {
135
+ case FUNCTION_MOVE -> {
132
136
jlObject .getOnActionListener ()
133
137
.move (jlObject , OnJLObjectActionListener .Action .MOVE );
134
138
return true ;
135
139
}
136
- case "movestart" -> {
140
+ case FUNCTION_MOVE_START -> {
137
141
jlObject .getOnActionListener ()
138
142
.move (jlObject , OnJLObjectActionListener .Action .MOVE_START );
139
143
return true ;
140
144
}
141
- case "moveend" -> {
145
+ case FUNCTION_MOVE_END -> {
142
146
//update coordinate of the JLObject
143
- jlObject .update ("moveend" , gson .fromJson (String .valueOf (params [3 ]), JLLatLng .class ));
147
+ jlObject .update (FUNCTION_MOVE_END , gson .fromJson (String .valueOf (params [3 ]), JLLatLng .class ));
144
148
jlObject .getOnActionListener ()
145
149
.move (jlObject , OnJLObjectActionListener .Action .MOVE_END );
146
150
return true ;
147
151
}
148
- case "click" -> {
152
+ case FUNCTION_CLICK -> {
149
153
jlObject .getOnActionListener ()
150
154
.click (jlObject , OnJLObjectActionListener .Action .CLICK );
151
155
return true ;
152
156
}
153
- default -> log .error (functionName + " not implemented!" );
157
+ default -> log .error ("{} not implemented!", functionName );
154
158
}
155
159
return false ;
156
160
}
@@ -171,7 +175,10 @@ public void remove(Class<? extends JLObject<?>> targetClass, int id) {
171
175
if (!jlObjects .containsKey (targetClass ))
172
176
return ;
173
177
JLObject <?> object = jlObjects .get (targetClass ).remove (id );
174
- if (object != null )
175
- log .error (targetClass .getSimpleName () + " id:" + object .getId () + " removed" );
178
+ if (object != null ) log .error ("{} id: {} removed" , targetClass .getSimpleName (), object .getId ());
179
+ }
180
+
181
+ private Optional <OnJLMapViewListener > getMapListener () {
182
+ return Optional .ofNullable (listener );
176
183
}
177
184
}
0 commit comments