View Javadoc

1   /*
2    * @(#) AnimationPanel.java Nov 3, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.gui.animation2D;
11  
12  import java.awt.Color;
13  import java.awt.Dimension;
14  import java.awt.Graphics;
15  import java.awt.Graphics2D;
16  import java.awt.event.MouseListener;
17  import java.awt.geom.Rectangle2D;
18  import java.lang.reflect.Constructor;
19  import java.rmi.RemoteException;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.SortedSet;
23  import java.util.TreeSet;
24  
25  import javax.naming.Binding;
26  import javax.naming.NamingEnumeration;
27  import javax.naming.event.EventContext;
28  import javax.naming.event.NamespaceChangeListener;
29  import javax.naming.event.NamingEvent;
30  import javax.naming.event.NamingExceptionEvent;
31  import javax.vecmath.Point4i;
32  
33  import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
34  import nl.tudelft.simulation.dsol.experiment.Experiment;
35  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
36  import nl.tudelft.simulation.dsol.gui.animation2D.mouse.InputListener;
37  import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
38  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
39  import nl.tudelft.simulation.event.Event;
40  import nl.tudelft.simulation.event.EventInterface;
41  import nl.tudelft.simulation.event.EventListenerInterface;
42  import nl.tudelft.simulation.logger.Logger;
43  import nl.tudelft.simulation.naming.InitialEventContext;
44  
45  /***
46   * The AnimationPanel <br>
47   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
48   * University of Technology </a>, the Netherlands. <br>
49   * See for project information <a
50   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
51   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
52   * License (GPL) </a>, no warranty <br>
53   * 
54   * @version 1.0 18.10.2003 <br>
55   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
56   *         Jacobs </a>
57   */
58  public class AnimationPanel extends GridPanel implements
59  		EventListenerInterface, NamespaceChangeListener
60  {
61  
62  	/***
63  	 * the elements of this panel
64  	 * 
65  	 * @uml.property name="elements"
66  	 */
67  	private SortedSet elements = Collections.synchronizedSortedSet(new TreeSet(
68  			new Renderable2DComparator()));
69  
70  
71  	/*** the application */
72  	private DSOLApplicationInterface application = null;
73  
74  	/*** the eventContext */
75  	private EventContext context = null;
76  
77  	/***
78  	 * the grid must be drawn after all other elements. Therefore we must
79  	 * override the gridPanel.paintGrid.
80  	 * 
81  	 * @uml.property name="showGrid"
82  	 */
83  	private boolean showGrid = true;
84  
85      /*** a line that helps the user to see where he is dragging */
86      private Point4i dragLine = new Point4i();
87  
88      /*** enable drag line */
89      private boolean dragLineEnabled = false;
90  
91  	/***
92  	 * constructs a new AnimationPanel
93  	 * 
94  	 * @param extent the extent of the panel
95  	 * @param size the size of the panel.
96  	 * @param application the application
97  	 */
98  	public AnimationPanel(final Rectangle2D extent, final Dimension size,
99  			final DSOLApplicationInterface application)
100 	{
101 		super(extent, size);
102 		super.showGrid = false;
103 		this.application = application;
104 		String mouseEditorClassName = this.application.getProperties()
105 				.getProperty("dsol-gui.animation.panel.mouseEditor");
106 		InputListener listener = new InputListener(
107 				application, this);
108 		if (mouseEditorClassName != null)
109 		{
110 			try
111 			{
112 				Class editorClass = Class.forName(mouseEditorClassName);
113 				Constructor constructor = editorClass
114 						.getConstructor(new Class[]{
115 								DSOLApplicationInterface.class,
116 								AnimationPanel.class});
117 				this.addMouseListener((MouseListener) constructor
118 						.newInstance(new Object[]{application, this}));
119 
120 			} catch (Exception exception)
121 			{
122 				Logger.warning(this, "<init>", "could not load "
123 						+ mouseEditorClassName);
124 				listener = new InputListener(application, this);
125 			}
126 		} else
127 		{
128 			this.addMouseListener(listener);
129 		}
130 		this.addMouseMotionListener(listener);
131 		this.addMouseWheelListener(listener);
132 		this.addKeyListener(listener);
133 		this.initialize();
134 	}
135 
136 	/***
137 	 * initializes the panel
138 	 */
139 	private void initialize()
140 	{
141 		try
142 		{
143 			this.application.addListener(this,
144 					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
145 			this.notify(new Event(
146 					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
147 					this.application, this.application.getExperiment()));
148 		} catch (RemoteException exception)
149 		{
150 			Logger.warning(this, "initialize", exception);
151 		}
152 	}
153 
154 	/***
155 	 * @see javax.swing.JComponent #paintComponent(java.awt.Graphics)
156 	 */
157 	public void paintComponent(final Graphics g)
158 	{
159 		super.paintComponent(g);
160 		Graphics2D g2 = (Graphics2D) g;
161 		synchronized (this.elements)
162 		{
163 			for (Iterator i = this.elements.iterator(); i.hasNext();)
164 			{
165 				((Renderable2DInterface) i.next()).paint(g2, this.getExtent(),
166 						this.getSize(), this);
167 			}
168 		}
169 		if (this.showGrid)
170 		{
171 			this.drawGrid(g);
172 		}
173 		
174 		// Draw dragline
175         if (this.dragLineEnabled)
176         {
177             g.setColor(Color.BLACK);
178             g.drawLine(this.dragLine.w, this.dragLine.x, this.dragLine.y,
179                     this.dragLine.z);
180             this.dragLineEnabled = false;
181         }
182 	}
183 
184 	/***
185 	 * @see nl.tudelft.simulation.event.EventListenerInterface
186 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
187 	 */
188 	public void notify(final EventInterface event) throws RemoteException
189 	{
190 		if (event.getSource() instanceof AnimatorInterface
191 				&& event.getType().equals(
192 						AnimatorInterface.UPDATE_ANIMATION_EVENT))
193 		{
194 			if (this.getWidth() > 0 || this.getHeight() > 0)
195 			{
196 				this.repaint();
197 			}
198 			return;
199 		}
200 		if (event.getSource() instanceof AnimatorInterface
201 				&& event.getType().equals(
202 						SimulatorInterface.START_REPLICATION_EVENT))
203 		{
204 			this.elements.clear();
205 			AnimatorInterface simulator = (AnimatorInterface) event.getSource();
206 			String contextName = this.application.getExperiment().getRun()
207 					+ "/treatment("
208 					+ simulator.getReplication().getRunControl().getTreatment()
209 							.getNumber() + ")/replication("
210 					+ simulator.getReplication().getNumber() + ")/animation/2D";
211 			try
212 			{
213 				if (this.context != null)
214 				{
215 					this.context.removeNamingListener(this);
216 				}
217 				this.context = (EventContext) new InitialEventContext()
218 						.lookup(contextName);
219 				this.context.addNamingListener("", EventContext.SUBTREE_SCOPE,
220 						this);
221 				NamingEnumeration list = this.context.listBindings("");
222 				while (list.hasMore())
223 				{
224 					Binding binding = (Binding) list.next();
225 					this.objectAdded(new NamingEvent(this.context, -1, binding,
226 							binding, null));
227 				}
228 				this.repaint();
229 			} catch (Exception exception)
230 			{
231 				Logger.warning(this, "notify", exception);
232 			}
233 		}
234 		if (event.getSource() instanceof Experiment
235 				&& event.getType().equals(Experiment.SIMULATOR_CHANGED_EVENT))
236 		{
237 			this.elements.clear();
238 			this.repaint();
239 			if (event.getContent() instanceof AnimatorInterface
240 					&& event.getContent() != null)
241 			{
242 				AnimatorInterface simulator = (AnimatorInterface) event
243 						.getContent();
244 				simulator.addListener(this,
245 						AnimatorInterface.UPDATE_ANIMATION_EVENT);
246 				if (simulator.getReplication() != null)
247 				{
248 					this.notify(new Event(
249 							SimulatorInterface.START_REPLICATION_EVENT,
250 							simulator, simulator));
251 				} else
252 				{
253 					simulator.addListener(this,
254 							SimulatorInterface.START_REPLICATION_EVENT);
255 				}
256 			}
257 		}
258 		if (event.getSource().equals(this.application)
259 				&& event.getType().equals(
260 						DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT))
261 		{
262 			this.elements.clear();
263 			this.repaint();
264 			if (event.getContent() != null)
265 			{
266 				Experiment experiment = (Experiment) event.getContent();
267 				experiment
268 						.addListener(this, Experiment.SIMULATOR_CHANGED_EVENT);
269 				this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
270 						experiment, experiment.getSimulator()));
271 			}
272 			return;
273 		}
274 	}
275 
276 	/***
277 	 * @see javax.naming.event.NamespaceChangeListener
278 	 *      #objectAdded(javax.naming.event.NamingEvent)
279 	 */
280 	public void objectAdded(final NamingEvent namingEvent)
281 	{
282 		Renderable2DInterface element = (Renderable2DInterface) namingEvent
283 				.getNewBinding().getObject();
284 		this.elements.add(element);
285 	}
286 
287 	/***
288 	 * @see javax.naming.event.NamespaceChangeListener
289 	 *      #objectRemoved(javax.naming.event.NamingEvent)
290 	 */
291 	public void objectRemoved(final NamingEvent namingEvent)
292 	{
293 		Renderable2DInterface element = (Renderable2DInterface) namingEvent
294 				.getOldBinding().getObject();
295 		this.elements.remove(element);
296 	}
297 
298 	/***
299 	 * @see javax.naming.event.NamespaceChangeListener
300 	 *      #objectRenamed(javax.naming.event.NamingEvent)
301 	 */
302 	public void objectRenamed(final NamingEvent namingEvent)
303 	{
304 		this.objectRemoved(namingEvent);
305 		this.objectAdded(namingEvent);
306 	}
307 
308 	/***
309 	 * @see javax.naming.event.NamingListener
310 	 *      #namingExceptionThrown(javax.naming.event.NamingExceptionEvent)
311 	 */
312 	public void namingExceptionThrown(final NamingExceptionEvent namingEvent)
313 	{
314 		Logger.warning(this, "namingExceptionThrown", namingEvent
315 				.getException());
316 	}
317 
318 	/***
319 	 * @return Returns the elements.
320 	 * 
321 	 * @uml.property name="elements"
322 	 */
323 	public SortedSet getElements()
324 	{
325 		return this.elements;
326 	}
327 
328 	/***
329 	 * @see nl.tudelft.simulation.dsol.gui.animation2D.GridPanel#isShowGrid()
330 	 * 
331 	 * @uml.property name="showGrid"
332 	 */
333 	public boolean isShowGrid()
334 	{
335 		return this.showGrid;
336 	}
337 
338 	/***
339 	 * @see nl.tudelft.simulation.dsol.gui.animation2D.GridPanel#showGrid(boolean)
340 	 */
341 	public synchronized void showGrid(final boolean bool)
342 	{
343 		this.showGrid = bool;
344 		this.repaint();
345 	}
346 	
347     /***
348      * @return returns the dragLine.
349      */
350     public Point4i getDragLine()
351     {
352         return this.dragLine;
353     }
354 
355     /***
356      * @return returns the dragLineEnabled.
357      */
358     public boolean isDragLineEnabled()
359     {
360         return this.dragLineEnabled;
361     }
362 
363     /***
364      * @param dragLineEnabled
365      *            the dragLineEnabled to set.
366      */
367     public void setDragLineEnabled(final boolean dragLineEnabled)
368     {
369         this.dragLineEnabled = dragLineEnabled;
370     }
371 }