View Javadoc

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