1 package org.opentrafficsim.draw.network;
2
3 import java.rmi.RemoteException;
4 import java.util.LinkedHashSet;
5 import java.util.Set;
6
7 import javax.naming.NamingException;
8
9 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
10 import org.opentrafficsim.core.network.Network;
11 import org.opentrafficsim.core.network.NetworkUtils;
12
13 import nl.tudelft.simulation.dsol.animation.Locatable;
14 import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
15 import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
16 import nl.tudelft.simulation.naming.context.ContextInterface;
17 import nl.tudelft.simulation.naming.context.util.ContextUtil;
18
19
20
21
22
23
24
25
26
27 public final class NetworkAnimationUtils
28 {
29
30 private NetworkAnimationUtils()
31 {
32
33 }
34
35
36
37
38
39
40 @SuppressWarnings("checkstyle:designforextension")
41 public static void destroy(final Network network, final OtsSimulatorInterface simulator)
42 {
43 Set<Renderable2DInterface<?>> animationObjects = new LinkedHashSet<>();
44 try
45 {
46 ContextInterface context =
47 ContextUtil.lookupOrCreateSubContext(simulator.getReplication().getContext(), "animation/2D");
48 for (Object element : context.values())
49 {
50 Renderable2DInterface<?> animationObject = (Renderable2DInterface<?>) element;
51 animationObjects.add(animationObject);
52 }
53
54 for (Renderable2DInterface<?> ao : animationObjects)
55 {
56 try
57 {
58 ao.destroy(simulator);
59 }
60 catch (Exception e)
61 {
62
63 }
64 }
65 }
66 catch (NamingException | RemoteException exception)
67 {
68 System.err.println("Error when destroying animation objects");
69 }
70
71
72 NetworkUtils.destroy(network);
73 }
74
75
76
77
78
79
80 @SuppressWarnings("checkstyle:designforextension")
81 public static void removeAnimation(final Class<?> clazz, final OtsSimulatorInterface oldSimulator)
82 {
83 if (!(oldSimulator instanceof AnimatorInterface))
84 {
85 return;
86 }
87
88 try
89 {
90 ContextInterface context =
91 ContextUtil.lookupOrCreateSubContext(oldSimulator.getReplication().getContext(), "animation/2D");
92 for (Object element : context.values())
93 {
94 Renderable2DInterface<?> animationObject = (Renderable2DInterface<?>) element;
95 Locatable locatable = animationObject.getSource();
96 if (clazz.isAssignableFrom(locatable.getClass()))
97 {
98 animationObject.destroy(oldSimulator);
99 }
100 }
101 }
102 catch (NamingException | RemoteException exception)
103 {
104 System.err.println("Error when destroying animation objects for class " + clazz.getSimpleName());
105 }
106 }
107
108 }