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
32 public class AnimationPanel extends VisualizationPanel
33 {
34
35 private static final long serialVersionUID = 1L;
36
37
38 private SimulatorInterface<?> simulator;
39
40
41
42
43
44
45
46
47 public AnimationPanel(final Bounds2d homeExtent, final SimulatorInterface<?> simulator)
48 throws RemoteException, DsolException
49 {
50 super(homeExtent, simulator);
51 Throw.when(!(simulator instanceof AnimatorInterface), DsolException.class,
52 "Simulator must implement the AnimatorInterface");
53 this.simulator = simulator;
54 simulator.addListener(this, Replication.START_REPLICATION_EVENT);
55 }
56
57
58 @Override
59 public void notify(final Event event) throws RemoteException
60 {
61 super.notify(event);
62
63 if (event.getType().equals(Replication.START_REPLICATION_EVENT))
64 {
65 synchronized (this.elementList)
66 {
67 this.elements.clear();
68 try
69 {
70 if (this.context != null)
71 {
72 this.context.removeListener(this, ContextInterface.OBJECT_ADDED_EVENT);
73 this.context.removeListener(this, ContextInterface.OBJECT_REMOVED_EVENT);
74 }
75 this.context =
76 ContextUtil.lookupOrCreateSubContext(this.simulator.getReplication().getContext(), "animation/2D");
77 subscribeToContext();
78 }
79 catch (Exception exception)
80 {
81 this.simulator.getLogger().always().warn(exception, "notify");
82 }
83 }
84 }
85 }
86
87 }