1 package nl.tudelft.simulation.dsol.swing.animation.d2;
2
3 import java.rmi.RemoteException;
4
5 import org.djutils.draw.bounds.Bounds2d;
6 import org.djutils.event.Event;
7 import org.djutils.exceptions.Throw;
8
9 import nl.tudelft.simulation.dsol.experiment.Replication;
10 import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
11 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
12 import nl.tudelft.simulation.language.DsolException;
13 import nl.tudelft.simulation.naming.context.ContextInterface;
14 import nl.tudelft.simulation.naming.context.util.ContextUtil;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class AnimationPanel extends VisualizationPanel
32 {
33
34 private static final long serialVersionUID = 1L;
35
36
37 private SimulatorInterface<?> simulator;
38
39
40
41
42
43
44
45
46 public AnimationPanel(final Bounds2d homeExtent, final SimulatorInterface<?> simulator)
47 throws RemoteException, DsolException
48 {
49 super(homeExtent, simulator);
50 Throw.when(!(simulator instanceof AnimatorInterface), DsolException.class,
51 "Simulator must implement the AnimatorInterface");
52 this.simulator = simulator;
53 simulator.addListener(this, Replication.START_REPLICATION_EVENT);
54 }
55
56 @Override
57 public void notify(final Event event) throws RemoteException
58 {
59 super.notify(event);
60
61 if (event.getType().equals(Replication.START_REPLICATION_EVENT))
62 {
63 synchronized (this.elementList)
64 {
65 this.elements.clear();
66 try
67 {
68 if (this.context != null)
69 {
70 this.context.removeListener(this, ContextInterface.OBJECT_ADDED_EVENT);
71 this.context.removeListener(this, ContextInterface.OBJECT_REMOVED_EVENT);
72 }
73 this.context =
74 ContextUtil.lookupOrCreateSubContext(this.simulator.getReplication().getContext(), "animation/2D");
75 subscribeToContext();
76 }
77 catch (Exception exception)
78 {
79 this.simulator.getLogger().always().warn(exception, "notify");
80 }
81 }
82 }
83 }
84
85 }