View Javadoc

1   /*
2    * @(#) ContextFrame.java Oct 26, 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.windows;
11  
12  import java.awt.BorderLayout;
13  import java.awt.Dimension;
14  import java.awt.Graphics;
15  
16  import javax.swing.BorderFactory;
17  import javax.swing.JFrame;
18  import javax.swing.JPanel;
19  import javax.swing.JScrollPane;
20  import javax.swing.JTable;
21  import javax.swing.WindowConstants;
22  import javax.swing.table.DefaultTableModel;
23  import javax.swing.table.TableModel;
24  
25  import nl.tudelft.simulation.dsol.eventlists.TableModelEventList;
26  import nl.tudelft.simulation.dsol.experiment.Experiment;
27  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
28  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
29  import nl.tudelft.simulation.event.Event;
30  import nl.tudelft.simulation.event.EventInterface;
31  import nl.tudelft.simulation.event.EventListenerInterface;
32  import nl.tudelft.simulation.logger.Logger;
33  
34  /***
35   * The EventlistFrame <br>
36   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
37   * University of Technology </a>, the Netherlands. <br>
38   * See for project information <a
39   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
40   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
41   * License (GPL) </a>, no warranty <br>
42   * 
43   * @version 1.0 18.10.2003 <br>
44   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
45   *         Jacobs </a>
46   */
47  public class EventlistFrame extends JFrame implements EventListenerInterface
48  {
49  	/*** the application */
50  	private DSOLApplicationInterface application = null;
51  
52  	/*** the currently active simulator */
53  	private DEVSSimulatorInterface simulator = null;
54  
55  	/*** the table */
56  	private SynchronizedJTable table = new SynchronizedJTable(
57  			new DefaultTableModel(TableModelEventList.HEADER, 0));
58  
59  	/***
60  	 * constructs a new ContextFrame
61  	 * 
62  	 * @param application the application to see
63  	 */
64  	public EventlistFrame(final DSOLApplicationInterface application)
65  	{
66  		super("Eventlist");
67  		this.application = application;
68  		this.initialize();
69  		try
70  		{
71  			this.application.addListener(this,
72  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
73  			this.notify(new Event(
74  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
75  					this.application, this.application.getExperiment()));
76  		} catch (Exception exception)
77  		{
78  			Logger.warning(this, "EventlistFrame", exception);
79  		}
80  
81  		this.pack();
82  		this.setVisible(true);
83  		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
84  	}
85  
86  	/***
87  	 * @see java.awt.Window#dispose()
88  	 */
89  	public void dispose()
90  	{
91  		try
92  		{
93  			if (this.simulator != null
94  					&& this.simulator.getEventList() instanceof TableModelEventList)
95  			{
96  				boolean stopped = false;
97  				if (this.simulator.isRunning())
98  				{
99  					stopped = true;
100 					this.simulator.stop();
101 				}
102 				synchronized (this.simulator)
103 				{
104 					this.simulator.removeListener(this,
105 							DEVSSimulatorInterface.EVENTLIST_CHANGED_EVENT);
106 					this.undoSwapEventList();
107 					if (stopped)
108 					{
109 						this.simulator.start();
110 					}
111 				}
112 			}
113 		} catch (Exception exception)
114 		{
115 			Logger.warning(this, "dispose", exception);
116 		}
117 		super.dispose();
118 	}
119 
120 	/***
121 	 * initializes the contextframe
122 	 */
123 	private void initialize()
124 	{
125 		JPanel panel = new JPanel(new BorderLayout());
126 		panel.setBorder(BorderFactory.createEtchedBorder());
127 		panel.add(new JScrollPane(this.table), BorderLayout.CENTER);
128 		panel.setPreferredSize(new Dimension(700, 300));
129 		this.setContentPane(panel);
130 	}
131 
132 	/***
133 	 * @see nl.tudelft.simulation.event.EventListenerInterface
134 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
135 	 */
136 	public void notify(final EventInterface event)
137 	{
138 		try
139 		{
140 			if (event.getSource().equals(this.application)
141 					&& event.getType().equals(
142 							DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT))
143 			{
144 				if (event.getContent() != null)
145 				{
146 					Experiment experiment = (Experiment) event.getContent();
147 					experiment.addListener(this,
148 							Experiment.SIMULATOR_CHANGED_EVENT);
149 					this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
150 							experiment, experiment.getSimulator()));
151 				}
152 				return;
153 			}
154 			if (event.getSource() instanceof Experiment
155 					&& event.getType().equals(
156 							Experiment.SIMULATOR_CHANGED_EVENT))
157 			{
158 				if (event.getContent() != null
159 						&& event.getContent() instanceof DEVSSimulatorInterface)
160 				{
161 					this.simulator = (DEVSSimulatorInterface) event
162 							.getContent();
163 					this.simulator.addListener(this,
164 							DEVSSimulatorInterface.EVENTLIST_CHANGED_EVENT);
165 					this.notify(new Event(
166 							DEVSSimulatorInterface.EVENTLIST_CHANGED_EVENT,
167 							this.simulator, this.simulator.getEventList()));
168 				}
169 			}
170 			if (event.getSource().equals(this.simulator)
171 					&& event.getType().equals(
172 							DEVSSimulatorInterface.EVENTLIST_CHANGED_EVENT))
173 			{
174 				if (!(this.simulator.getEventList() instanceof TableModelEventList))
175 				{
176 					this.swapEventList();
177 				} else
178 				{
179 					this.table.setModel(((TableModelEventList) this.simulator
180 							.getEventList()).getTableModel());
181 				}
182 			}
183 			return;
184 		} catch (Exception exception)
185 		{
186 			Logger.warning(this, "dispose", exception);
187 		}
188 	}
189 
190 	/***
191 	 * undoes the eventlistSwap
192 	 * 
193 	 * @throws Exception on failure
194 	 */
195 	private void undoSwapEventList() throws Exception
196 	{
197 		if (this.simulator != null)
198 		{
199 			boolean stopped = false;
200 			if (this.simulator.isRunning())
201 			{
202 				stopped = true;
203 				this.simulator.stop();
204 			}
205 			synchronized (this.simulator)
206 			{
207 				if (this.simulator.getEventList() instanceof TableModelEventList)
208 				{
209 					this.simulator
210 							.setEventList(((TableModelEventList) this.simulator
211 									.getEventList()).getOrigin());
212 				}
213 				this.table.setModel(new DefaultTableModel(
214 						TableModelEventList.HEADER, 0));
215 				if (stopped)
216 				{
217 					this.simulator.start();
218 				}
219 			}
220 		}
221 	}
222 
223 	/***
224 	 * swaps the eventList
225 	 * 
226 	 * @throws Exception on exception
227 	 */
228 	private void swapEventList() throws Exception
229 	{
230 		if (this.simulator != null)
231 		{
232 			boolean stopped = false;
233 			if (this.simulator.isRunning())
234 			{
235 				stopped = true;
236 				this.simulator.stop();
237 			}
238 			synchronized (this.simulator)
239 			{
240 				TableModelEventList eventList = null;
241 				synchronized (this.simulator)
242 				{
243 					eventList = new TableModelEventList(this.simulator
244 							.getEventList());
245 					this.simulator.setEventList(eventList);
246 				}
247 				this.table.setModel(eventList.getTableModel());
248 				if (stopped)
249 				{
250 					this.simulator.start();
251 				}
252 			}
253 		}
254 	}
255 
256 	/***
257 	 * Synchronizes the tableModel when updating
258 	 */
259 	private static class SynchronizedJTable extends JTable
260 	{
261 		/***
262 		 * constructs a new SynchronizedJTable
263 		 * 
264 		 * @param dm the tablemodel
265 		 */
266 		public SynchronizedJTable(final TableModel dm)
267 		{
268 			super(dm);
269 		}
270 
271 		/***
272 		 * @see java.awt.Component#paint(java.awt.Graphics)
273 		 */
274 		public void paint(final Graphics g)
275 		{
276 			synchronized (this.getModel())
277 			{
278 				super.paint(g);
279 			}
280 		}
281 
282 		/***
283 		 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
284 		 */
285 		protected void paintComponent(final Graphics g)
286 		{
287 			synchronized (this.getModel())
288 			{
289 				super.paintComponent(g);
290 			}
291 		}
292 	}
293 }