View Javadoc
1   package org.opentrafficsim.core.animation.network;
2   
3   import java.rmi.RemoteException;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.opentrafficsim.core.animation.Drawable;
8   import org.opentrafficsim.core.animation.DrawingInfo;
9   import org.opentrafficsim.core.network.Network;
10  
11  import nl.tudelft.simulation.event.EventInterface;
12  import nl.tudelft.simulation.event.EventListenerInterface;
13  import nl.tudelft.simulation.event.EventProducer;
14  
15  /**
16   * NetworkAnimation stores the relations between drawable objects and their drawing info. <br>
17   * <br>
18   * Copyright (c) 2003-2019 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
19   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
20   * source code and binary code of this software is proprietary information of Delft University of Technology.
21   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
22   */
23  public class NetworkAnimation extends EventProducer implements EventListenerInterface
24  {
25      /** */
26      private static final long serialVersionUID = 1L;
27  
28      /** the network to which the animation info is connected. */
29      private final Network network;
30  
31      /** drawing info: base information per class. */
32      private Map<Class<? extends Drawable>, DrawingInfo> classDrawingInfoMap = new HashMap<>();
33  
34      /** drawing info: base information per instance. */
35      private Map<Drawable, DrawingInfo> baseDrawingInfoMap = new HashMap<>();
36  
37      /** drawing info: dynamic information per instance. */
38      private Map<Drawable, DrawingInfo> dynamicDrawingInfoMap = new HashMap<>();
39  
40      /**
41       * Construct this NetworkAnimation object with a connection to the Network.
42       * @param network Network; the network to which the animation info is connected
43       * @throws RemoteException in case of remote events and network error
44       */
45      public NetworkAnimation(Network network) throws RemoteException
46      {
47          super();
48          this.network = network;
49          this.network.addListener(this, Network.ANIMATION_GTU_ADD_EVENT);
50          this.network.addListener(this, Network.ANIMATION_GTU_REMOVE_EVENT);
51          this.network.addListener(this, Network.ANIMATION_NODE_ADD_EVENT);
52          this.network.addListener(this, Network.ANIMATION_NODE_REMOVE_EVENT);
53          this.network.addListener(this, Network.ANIMATION_LINK_ADD_EVENT);
54          this.network.addListener(this, Network.ANIMATION_LINK_REMOVE_EVENT);
55          this.network.addListener(this, Network.ANIMATION_OBJECT_ADD_EVENT);
56          this.network.addListener(this, Network.ANIMATION_OBJECT_REMOVE_EVENT);
57          this.network.addListener(this, Network.ANIMATION_INVISIBLE_OBJECT_ADD_EVENT);
58          this.network.addListener(this, Network.ANIMATION_INVISIBLE_OBJECT_REMOVE_EVENT);
59          this.network.addListener(this, Network.ANIMATION_ROUTE_ADD_EVENT);
60          this.network.addListener(this, Network.ANIMATION_ROUTE_REMOVE_EVENT);
61          this.network.addListener(this, Network.ANIMATION_GTU_ADD_EVENT);
62          this.network.addListener(this, Network.ANIMATION_GTU_REMOVE_EVENT);
63      }
64  
65      /**
66       * Add the drawing info for a class. Here it can e.g., be specified that all lanes are filled with a light gray color and
67       * drawn with a dark gray stroke. The class drawing info <b>can</b> be cached.
68       * @param drawableClass Class&lt;? extends Drawable&gt;; the class to set the drawing info for
69       * @param drawingInfo DrawingInfo; the default drawing info for the class
70       */
71      public void addDrawingInfoClass(final Class<? extends Drawable> drawableClass, final DrawingInfo drawingInfo)
72      {
73          this.classDrawingInfoMap.put(drawableClass, drawingInfo);
74      }
75  
76      /**
77       * Add the drawing info for an instance. This overrides the drawing info for the class. An example is that a bus lane can be
78       * drawn using a black color to make it different from the standard lanes. The base drawing info <b>can</b> be cached.
79       * @param drawable Drawable; the object to set the drawing info for
80       * @param drawingInfo DrawingInfo; the default drawing info for the drawable
81       */
82      public void addDrawingInfoBase(final Drawable drawable, final DrawingInfo drawingInfo)
83      {
84          this.baseDrawingInfoMap.put(drawable, drawingInfo);
85      }
86  
87      /**
88       * Add the dynamic drawing information for an instance. This overrides the drawing info for the object and the class, and
89       * should <b>not</b> be cached. An example is that a lane on a highway that turns red when it is forbidden for traffic to
90       * use the lane.
91       * @param drawable Drawable; the object to set the drawing info for
92       * @param drawingInfo DrawingInfo; the dynamic drawing info for the drawable
93       */
94      public void addDrawingInfoDynamic(final Drawable drawable, final DrawingInfo drawingInfo)
95      {
96          this.dynamicDrawingInfoMap.put(drawable, drawingInfo);
97      }
98  
99      /**
100      * Get the drawing information for a drawable instance. It first checks the dynamic info, then the base info, and then the
101      * class info.
102      * @param drawable Drawable; the object to get the drawing info for
103      * @return DrawingInfo; the drawing info for the instance, or null if no Drawing info could be found
104      */
105     public DrawingInfo getDrawingInfo(final Drawable drawable)
106     {
107         DrawingInfo drawingInfo = this.dynamicDrawingInfoMap.get(drawable);
108         if (drawingInfo != null)
109         {
110             return drawingInfo;
111         }
112         return getDrawingInfoBase(drawable);
113     }
114 
115     /**
116      * Get the static drawing information for a drawable instance. It first checks the base info, and then the class info.
117      * @param drawable Drawable; the object to get the drawing info for
118      * @return DrawingInfo; the drawing info for the instance, or null if no Drawing info could be found
119      */
120     public DrawingInfo getDrawingInfoBase(final Drawable drawable)
121     {
122         DrawingInfo drawingInfo = this.baseDrawingInfoMap.get(drawable);
123         if (drawingInfo != null)
124         {
125             return drawingInfo;
126         }
127         return getDrawingInfoClass(drawable.getClass());
128     }
129 
130     /**
131      * Get the static class-based drawing information for a drawable instance.
132      * @param drawableClass Class&lt;? extends Drawable&gt;; the class to get the drawing info for
133      * @return DrawingInfo; the drawing info for the class, or null if no Drawing info could be found
134      */
135     public DrawingInfo getDrawingInfoClass(final Class<? extends Drawable> drawableClass)
136     {
137         DrawingInfo drawingInfo = this.classDrawingInfoMap.get(drawableClass);
138         if (drawingInfo != null)
139         {
140             return drawingInfo;
141         }
142         return null;
143     }
144 
145     /**
146      * @return the network backed by this NetworkAnimation object
147      */
148     public final Network getNetwork()
149     {
150         return this.network;
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public void notify(final EventInterface event) throws RemoteException
156     {
157         if (event.getType().equals(Network.ANIMATION_NODE_ADD_EVENT))
158         {
159             //
160         }
161     }
162 
163 }