99import io .github .makbn .jlmap .model .*;
1010import lombok .AccessLevel ;
1111import lombok .experimental .FieldDefaults ;
12- import lombok .extern .log4j . Log4j2 ;
12+ import lombok .extern .slf4j . Slf4j ;
1313
1414import java .io .Serializable ;
1515import java .util .HashMap ;
16+ import java .util .Optional ;
1617
1718/**
1819 * @author Mehdi Akbarian Rastaghi (@makbn)
1920 */
20- @ Log4j2
21+ @ Slf4j
2122@ FieldDefaults (makeFinal = true , level = AccessLevel .PRIVATE )
2223public 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 ;
2530 transient HashMap <Class <? extends JLObject <?>>, HashMap <Integer , JLObject <?>>> jlObjects ;
2631 transient Gson gson ;
2732 HashMap <String , Class <? extends JLObject <?>>[]> classMap ;
2833
29- public JLMapCallbackHandler (JLMapView mapView ) {
30- this .mapView = mapView ;
34+ public JLMapCallbackHandler (OnJLMapViewListener listener ) {
35+ this .listener = listener ;
3136 this .jlObjects = new HashMap <>();
3237 this .gson = new Gson ();
3338 this .classMap = new HashMap <>();
3439 initClassMap ();
3540 }
41+
3642 @ SuppressWarnings ("unchecked" )
3743 private void initClassMap () {
38-
3944 classMap .put ("marker" , new Class []{JLMarker .class });
4045 classMap .put ("marker_circle" , new Class []{JLCircleMarker .class });
4146 classMap .put ("polyline" , new Class []{JLPolyline .class , JLMultiPolyline .class });
@@ -50,7 +55,7 @@ private void initClassMap() {
5055 * @param param4 additional param
5156 * @param param5 additional param
5257 */
53- @ SuppressWarnings ("unchecked " )
58+ @ SuppressWarnings ("all " )
5459 public void functionCalled (String functionName , Object param1 , Object param2 ,
5560 Object param3 , Object param4 , Object param5 ) {
5661 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,
8792 return ;
8893 }
8994 }
90- } else if (param1 .equals ("main_map" )
91- && mapView .getMapListener ().isPresent ()) {
95+ } else if (param1 .equals ("main_map" ) && getMapListener ().isPresent ()) {
9296 switch (functionName ) {
93- case "move" -> mapView . getMapListener ()
97+ case FUNCTION_MOVE -> getMapListener ()
9498 .get ()
9599 .onAction (new MoveEvent (OnJLMapViewListener .Action .MOVE ,
96100 gson .fromJson (String .valueOf (param4 ), JLLatLng .class ),
97101 gson .fromJson (String .valueOf (param5 ), JLBounds .class ),
98102 Integer .parseInt (String .valueOf (param3 ))));
99- case "movestart" -> mapView . getMapListener ()
103+ case FUNCTION_MOVE_START -> getMapListener ()
100104 .get ()
101105 .onAction (new MoveEvent (OnJLMapViewListener .Action .MOVE_START ,
102106 gson .fromJson (String .valueOf (param4 ), JLLatLng .class ),
103107 gson .fromJson (String .valueOf (param5 ), JLBounds .class ),
104108 Integer .parseInt (String .valueOf (param3 ))));
105- case "moveend" -> mapView . getMapListener ()
109+ case FUNCTION_MOVE_END -> getMapListener ()
106110 .get ()
107111 .onAction (new MoveEvent (OnJLMapViewListener .Action .MOVE_END ,
108112 gson .fromJson (String .valueOf (param4 ), JLLatLng .class ),
109113 gson .fromJson (String .valueOf (param5 ), JLBounds .class ),
110114 Integer .parseInt (String .valueOf (param3 ))));
111- case "click" -> mapView . getMapListener ()
115+ case FUNCTION_CLICK -> getMapListener ()
112116 .get ()
113117 .onAction (new ClickEvent (gson .fromJson (String .valueOf (param3 ),
114118 JLLatLng .class )));
115119
116- case "zoom" -> mapView . getMapListener ()
120+ case FUNCTION_ZOOM -> getMapListener ()
117121 .get ()
118122 .onAction (new ZoomEvent (OnJLMapViewListener .Action .ZOOM ,
119123 Integer .parseInt (String .valueOf (param3 ))));
120124 default -> log .error (functionName + " not implemented!" );
121125 }
122126 }
123127 } catch (Exception e ) {
124- log .error (e );
128+ log .error (e . getMessage (), e );
125129 }
126130 }
127131
128132 private boolean callListenerOnObject (
129133 String functionName , JLObject <JLObject <?>> jlObject , Object ... params ) {
130134 switch (functionName ) {
131- case "move" -> {
135+ case FUNCTION_MOVE -> {
132136 jlObject .getOnActionListener ()
133137 .move (jlObject , OnJLObjectActionListener .Action .MOVE );
134138 return true ;
135139 }
136- case "movestart" -> {
140+ case FUNCTION_MOVE_START -> {
137141 jlObject .getOnActionListener ()
138142 .move (jlObject , OnJLObjectActionListener .Action .MOVE_START );
139143 return true ;
140144 }
141- case "moveend" -> {
145+ case FUNCTION_MOVE_END -> {
142146 //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 ));
144148 jlObject .getOnActionListener ()
145149 .move (jlObject , OnJLObjectActionListener .Action .MOVE_END );
146150 return true ;
147151 }
148- case "click" -> {
152+ case FUNCTION_CLICK -> {
149153 jlObject .getOnActionListener ()
150154 .click (jlObject , OnJLObjectActionListener .Action .CLICK );
151155 return true ;
152156 }
153- default -> log .error (functionName + " not implemented!" );
157+ default -> log .error ("{} not implemented!", functionName );
154158 }
155159 return false ;
156160 }
@@ -171,7 +175,10 @@ public void remove(Class<? extends JLObject<?>> targetClass, int id) {
171175 if (!jlObjects .containsKey (targetClass ))
172176 return ;
173177 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 );
176183 }
177184}
0 commit comments